大神帮忙
这个是一个统计学生成绩的程序,基本可以运行。但是我存放各科成绩用的数组,显示的时候只可以显示第一个成绩,我该用什么办法才可以显示所有的成绩呢比如说我输入成绩为:82 60 98 78 63就显示第一个82其他都不显示。代码如下:
#include <stdio.h>
struct Student
{
char name;
char number;
char score;
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;
} char score; 改成 char score;
输入:82,60,98,78,63 struct Student
{
char name;
char number;
char score; /*需要加大此数组的定义*/
struct Student *next;
};
另输入字符串应该为:82;60;98;78;63 或 82,60,98,78,63
注意,不能含空格,输入的字符串中若有空格为结束符。 weizhongyang 发表于 2017-10-11 17:02
struct Student
{
char name;
若要是我就像输入空格,该怎么办呢,可以不用数组的话。 neil123 发表于 2017-10-22 16:24
若要是我就像输入空格,该怎么办呢,可以不用数组的话。
可以用getchar()函数,接受空格的输入
页:
[1]