#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#pragma warning(disable:4996)
struct student
{
char name[20];
int num;
int score[5];
int sum;
}students;
void input(struct student students[3])
{
int i;
for (i = 0; i < 3; i++)
{
scanf("%s %d %d %d %d %d %d\n",students[i].name,
&students[i].num,&students[i].score[0],
&students[i].score[1],&students[i].score[2],&students[i].score[3],&students[i].score[4]);
}
}
void Printscore(struct student students[3])
{
int i;
for (i = 0; i < 3; i++)
{
printf("学生姓名:%s 学号:%d 成绩:%d\n",students[i].name,students[i].num,students[i].score[0]);
}
}
int max(int a, int b)
{
return (a > b) ? a : b;
}
int main()
{
struct student students[3],*p=students;
int i,j, maxScore;
void input(struct student students[3]);
void Printscore(struct student students[3]);
printf("输入学生姓名,学号,成绩:\n");
input(students);
Printscore(students);
int maxScoreIndex = 0;
for (i = 1; i < 3; i++)
{
if (students[i].sum > students[maxScoreIndex].sum)
{
maxScoreIndex = i;
}
}
printf("学生姓名:%s 学号:%d 总分最高:%d\n", students[maxScoreIndex].name, students[maxScoreIndex].num, students[maxScoreIndex].sum);
return 0;
}

这个程序中,我注意到以下一些可能的问题:
students
这个结构体并没有定义sum
成员变量的计算方法,在程序中直接使用了这个成员变量,可能会导致错误。- 在main函数中,你定义了一个指针
p
,它指向students
数组的首地址,但在后续的代码中并没有使用到这个指针。 max
函数的实现没有在main
函数中被调用,且其参数为两个整数,但在函数定义中你使用了五个参数。Printscore
函数打印了每个学生的成绩,但只打印了一个学生的姓名和学号。- 在
main
函数中,你在for循环中遍历了数组,但在if语句中比较了students[i].sum
和students[maxScoreIndex].sum
,而此时maxScoreIndex
并没有被赋予任何值,可能会导致错误。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#pragma warning(disable:4996)
struct student
{
char name[20];
int num;
int score[5];
int sum; // add a method to calculate sum or use a function to calculate it in main function.
}students;
void input(struct student students[3])
{
int i;
for (i = 0; i < 3; i++)
{
scanf("%s %d %d %d %d %d %d\n",students[i].name,
&students[i].num,&students[i].score[0],
&students[i].score[1],&students[i].score[2],&students[i].score[3],&students[i].score[4]);
// you can calculate the sum here or in main function.
students[i].sum = students[i].score[0] + students[i].score[1] + students[i].score[2] +
students[i].score[3] + students[i].score[4];
}
}
void Printscore(struct student students[3])
{
int i;
for (i = 0; i < 3; i++)
{
printf("学生姓名:%s 学号:%d 成绩:%d\n",students[i].name,students[i].num,students[i].score[0]);
printf("学生姓名:%s 学号:%d 总分:%d\n",students[i].name,students[i].num,students[i].sum); // print the total score here.
}
}
int max(int a, int b)
{ // fix the parameters of this function and call it in main function.
return (a > b) ? a : b;
}
int main() // main function.
{
struct student students[3],*p=students; // use the pointer here.
int i,j, maxScore;
void input(struct student students[3]);
void Printscore(struct student students[3]); // call these functions before you use the data.
printf("输入学生姓名,学号,成绩:\n");
input(students);
Printscore(students);
// calculate the max score here.
int maxScoreIndex = 0;
for (i = 1; i < 3; i++)
{
if (students[i].sum > students[maxScoreIndex].sum)
{
maxScoreIndex = i;
}
}
printf("学生姓名:%s 学号:%d 总分最高:%d\n", students[maxScoreIndex].name, students[maxScoreIndex].num, students[maxScore