请教一道C语言编程题

2025-01-07 07:42:05
推荐回答(1个)
回答1:

# include "stdio.h"
# include "stdlib.h"

typedef struct Student
{
int Studentid;
int Chinese;
int mathematics;
struct Student * Next;
int Totalscore;
int average;

}* pStudent;

pStudent initialization(void);
void print(pStudent pH);

int len;
int main(void)
{
pStudent pH = initialization();
print(pH);
return 0;
}

pStudent initialization(void)
{

pStudent pH = (pStudent)malloc(sizeof(Student));
if(NULL == pH)
{
printf("动态内存分配失败,程序结束\n");
exit(-1);
}
pStudent pT;
pT = pH;
pT->Next = NULL;
printf("初始化进行中\n");
printf("请问一共有几个学生:\n");
scanf("%d", &len);

for(int x = 0; x < len; x++)
{
pStudent pNew = (pStudent)malloc(sizeof(Student));
printf("\n正在编辑第%d个学生的信息\n", x+1);
printf("\n学生%d的学号为:\n", x+1);
scanf("%d", &pNew->Studentid);
printf("\n学生%d的语文成绩为:", x+1);
scanf("%d", &pNew->Chinese);
printf("\n学生%d的数学成绩为:", x+1);
scanf("%d", &pNew->mathematics);
pNew->Totalscore = pNew->Chinese + pNew->mathematics;
pNew->average = pNew->Totalscore / 2;
pT->Next = pNew;
pNew->Next = NULL;
pT = pNew;
}
return pH;
}

void print(pStudent pH)
{
pStudent pT = pH;
int i = 1;
while(NULL != pT->Next)
{
pT = pT->Next;
printf("\n正在输出第%d个学生的信息", i);
printf("\n学生%d的学号是%d", i, pT->Studentid);
printf("\n学生%d的语文成绩是%d", i,pT->Chinese);
printf("\n学生%d的数学成绩是%d", i,pT->mathematics);
printf("\n学生%d的总分是%d", i, pT->Totalscore);
printf("\n学生%d的平均分是%d", i,pT->average);
i++;
printf("\n");
}
printf("\n所有学生信息输出完毕\n");
return;
}
/*
这里只有两门课程的成绩,语文,数学,其他都OK了
*/