#include<stdio.h>
#include<stdlib.h>
void inputrecord();
void totalandaverage(int n, int t);
struct student
{
long int id;
char name[100];
int score[10];
int paiming;
};
typedef struct student STU;
STU* stu = NULL;
int n = 0;
int a = 0;
int main()
{
printf("1.Input record\n");
printf("2.Calculate total and average score of every course\n");
printf("3.Calculate total and average score of every student\n");
printf("4.Sort in descending order by total score of every student\n");
printf("5.Sort in ascending order by total score of every student\n");
printf("6.Sort in ascending order by number\n");
printf("7.Sort in dictionary order by name\n");
printf("8.Search by number\n");
printf("9.Search by name\n");
printf("10.Statistic analysis for every course\n");
printf("11.List record\n");
printf("0.sign out\n");
printf("choose function 0-11\n");
STU* stu = NULL;
stu = (STU*)malloc(n * sizeof(STU));
int i;
scanf_s("%d", &i);
while (i < 0 || i>11)
{
printf("please input again");
}
switch (i)
{
case 0:
break;
case 1:
inputrecord();
break;
case 2:
void totalandaverage(n, a);
}
return 0;
}
void inputrecord()
{
printf("请输入学生的个数");
int n;
scanf_s("%d", &n);
STU* stu= NULL;
stu = (STU*)malloc(n * sizeof(STU));
printf("请输入学科的个数");
scanf_s("%d", &a);
printf("请依次输入%d个学生的学号,姓名和各科考试成绩", n);
int i = 0;
int t = 0;
for (i = 0; i < n; i++)
{
scanf_s("%ld", &stu[i].id);
getchar();
gets(stu[i].name);
for (t = 0; t < a; t++)
{
scanf_s("%d", &stu[i].score[t]);
}
}
}
void totalandaverage(int n,int t)
{
int i;
int y;
for (y=0;y<a;y++)
{
int sum = 0;
for (i = 0; i < n; i++)
{
sum = sum + stu[i].score[t];
}
printf("第%d科总分是%d", y + 1, sum);
}
}
我看了一下你的代码,功能二的函数应该写的没有问题,问题应该出在你的主函数里,你看,你在输入功能序号的时候没有用到循环
当你在这个17行输入一个数字表示你的功能之后,之后并有没有继续输入功能序号的语句,所以,当你这个1输入后switch语句会判断这个 1 ,判断完成之后执行了你的input函数的功能,但是没有后续了,你没有用循环让他判断下一个功能,所以建议你写一个循环包含功能输入语句和switch语句并以0作为跳出条件,这才是你后续功能能够展现的前提条件,
还有一个问题是
这个也要放到我上面说的循环里去,不然你想想我输入一个12然后程序就结束了,这弄了半天啥也没干,这就没有意义了
最后
这里是你声明的全局变量,当然,全局变量很方便,现在我们不会用到太多的变量,这个全局变量在我们写程序的时候会很方便,让我们少走很多弯路,不失为一个好方法,但是我还是不建议你用全局变量做这件事,假设,你整个程序写好了,我现在让你再加一个功能,而这个功能不得不修改掉你的全局变量,你怎么办呢,你要是修改全局变量的话整个程序里面的变量都要改,不改的话新功能有加不上去,就会陷入一个比较尴尬的境地,当然这只是一个很小的情况,只有你在做一个特别大的程序时候可能才会遇到这样的问题,如果你还在初学,你可以使用来方便你程序的编写,如果你还打算进阶,我建议你在想想如果不用全局变量是否还能解决现在的问题来提高你的代码能力
以上仅代表个人观点,如果还有疑问,欢迎叨扰!