|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是一个统计学生成绩的程序,基本可以运行。但是我存放各科成绩用的数组,显示的时候只可以显示第一个成绩,我该用什么办法才可以显示所有的成绩呢比如说我输入成绩为:82 60 98 78 63就显示第一个82其他都不显示。
代码如下:
#include <stdio.h>
struct Student
{
char name[10];
char number[20];
char score[5];
struct Student *next;
};
void getInput(struct Student *student);
void getInput(struct Student *student)
{
printf("\n请输入学生姓名:");
scanf("%s", student->name);
printf("请输入学号:");
scanf("%s", student->number);
printf("请输入各科成绩:");
scanf("%s", student->score);
}
void addScore(struct Student **head);
void addScore(struct Student **head)
{
struct Student *student;
struct Student *temp;
student = (struct Student *)malloc(sizeof(struct Student));
if(student == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
getInput(student);
if(*head != NULL)
{
temp = *head;
*head = student;
student->next = temp;
}
else
{
*head = student;
student->next = NULL;
}
}
void printScore(struct Student *head);
void printScore(struct Student *head)
{
struct Student *student;
student = head;
printf("\n学号:%s\n", student->number);
printf("学生姓名:%s\n", student->name);
printf("各科成绩:%s\n", student->score);
}
void displayScore(struct Student *head);
void displayScore(struct Student *head)
{
struct Student *current;
current = head;
while(current != NULL)
{
printScore(current);
current = current->next;
}
}
void releaseScore(struct Student **head);
void releaseScore(struct Student **head)
{
struct Student *temp;
while(*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
int main(void)
{
struct Student *head = NULL;
struct Student *student;
int code;
printf("|欢迎使用学生成绩登记程序|\n");
printf("|---- 1.登记学生成绩 ----|\n");
printf("|---- 2.显示学生成绩 ----|\n");
printf("|---- 3.退出登记程序 ----|\n");
while(1)
{
printf("\n请输入指令码:");
scanf("%d", &code);
switch(code)
{
case 1:addScore(&head);
break;
case 2:displayScore(head);
break;
case 3:goto END;
}
}
END:releaseScore(&head);
printf("谢谢使用,再见!\n");
return 0;
} |
|