#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int score;
char* name;
};
struct student *aa(int cnt)
{
int i;
struct student* p = (struct student*)malloc(cnt * sizeof(struct student));
for (i = 0; i < cnt; i++) {
printf("请输入名字\n");
p->name = (char*)malloc(128);
scanf("%s", p->name);
printf("请输入分数\n");
scanf("%d", &p->score);
p++;
}
return p - cnt;
}
void oo(struct student* p, int cnt)
{
int i;
for (i = 0; i < cnt; i++) {
printf("名字:%s,分数:%d\n", p->name, p->score);
p++;
}
}
struct student *findmax(struct student* p, int cnt)
{
int i;
struct student* max;
max = p;
for (i = 0; i < cnt; i++) {
if (max->score < p->score) {
max = p;
}
p++;
}
return max;
}
struct student *findmin(struct student* p, int cnt)
{
int i;
struct student* min;
min = p;
for (i = 0; i < cnt; i++) {
if (min->score > p->score) {
min = p;
}
p++;
}
return min;
}
float wer(struct student* p, int cnt)
{
int i;
int total = 0;
for (i = 0; i < cnt; i++) {
total += p->score;
p++;
}
return (float)total / cnt;
}
int findname(struct student* p, int cnt, struct student* name)
{
int i;
for (i = 0; i < cnt; i++) {
if (strcmp(name, p->name)) {
return 1;
}
p++;
}
return -1;
}
int main()
{
int cnt = 0;
struct student* max = NULL;
struct student* min = NULL;
printf("请输入总人数\n");
scanf("%d", &cnt);
struct student* p = aa(cnt);
oo(p, cnt);
max = findmax(p, cnt);
min = findmin(p, cnt);
printf("最高分:%d,名字%s\n", max->score, max->name);
printf("最低分:%d,名字%s\n", min->score, min->name);
printf("平均分%f\n", wer(p, cnt));
if (findname(p, cnt, "chen") == -1) {
printf("没有此人\n");
}
else {
printf("找到了\n");
}
return 0;
}
结构体学生成绩
最新推荐文章于 2022-07-01 10:35:17 发布