求大佬救救孩子 为啥我写的c语言 学生管理系统 只能运行一次。第二次打开 几个功能就跑不了了 是存放数据出错了吗
#include<stdio.h>#include<stdlib.h>
#include<conio.h>
typedef struct _Stduent
{
int num; //学号
char name[20]; //名字
int score; //成绩
}Student;
typedef struct _Node
{
Student student;
struct _Node* next;
}Node;
void project();
void inputstudent(Node* head);
void outputstudent(Node* head);
void countstudent(Node* head);
void findstduent(Node* head);
void savestudent(Node* head);
void loadstudent(Node* head);
void changestudent(Node* head);
void deletestudent(Node* head);
void paixu(Node* head);
int main()
{
//创建头节点;
Node* head = (Node*)malloc(sizeof(Node));
head->next = NULL;
loadstudent(head);
while(1){
project();
char c = _getch();
switch (c)
{
case '1': //录入
inputstudent(head);
break;
case '2': //打印
outputstudent(head);
break;
case '3': //统计
countstudent(head);
break;
case '4': //查找
findstduent(head);
break;
case '5': //修改
changestudent(head);
break;
case '6': //删除
deletestudent(head);
break;
case '7'://排序
paixu(head);
break;
case '8'://退出
system("cls");
printf("BYE BYE");
exit(0);
break;
default:
printf("请重新输入:");
system("pause");//暂停程序;
system("cls");//清空控制台;
break;
}
}
return 0;
}
void project() {
printf("*********************************\n");
printf("*\t学生成绩管理系统\t*\n");
printf("*********************************\n");
printf("*\t请选择功能列表\t\t*\n");
printf("*********************************\n");
printf("*\t1.录入学生信息\t\t*\n");
printf("*\t2.打印学生信息\t\t*\n");
printf("*\t3.统计学生人数\t\t*\n");
printf("*\t4.查找学生信息\t\t*\n");
printf("*\t5.修改学生信息\t\t*\n");
printf("*\t6.删除学生信息\t\t*\n");
printf("*\t7.按成绩排序\t\t*\n");
printf("*\t8.退出系统\t\t*\n");
printf("*********************************\n");
}
void inputstudent(Node *head)
{
Node* fresh=(Node*)malloc(sizeof(Node));
fresh->next = NULL;
printf("请输入学生的学号,姓名,成绩:");
scanf("%d%s%d", &fresh->student.num,fresh->student.name,&fresh->student.score);
Node* move = head;
while (move->next != NULL) {
move = move->next;
}//链到最后的节点
//将学生插入到尾部;
move->next = fresh;
savestudent(head);
system("pause");//暂停程序;
system("cls");//清空控制台;
}
void outputstudent(Node *head) {
Node* move = head->next;
while (move != NULL) {
printf("学号:%d 姓名:%s 成绩:%d\n", move->student.num, move->student.name, move->student.score);
move = move->next;
}
system("pause");//暂停程序;
system("cls");//清空控制台;
}
void countstudent(Node* head) {
int count = 0;
Node* move = head->next;
while (move != NULL) {
count++;
move = move->next;
}
printf("学生的总人数为%d\n", count);
system("pause");//暂停程序;
system("cls");//清空控制台;
}
void findstduent(Node* head) {
int stunumber=0;
printf("请输入要查找的学生学号");
scanf("%d", &stunumber);
Node* move = head->next;
while (move != NULL) {
if (stunumber == move->student.num)
{
printf("学号:%d姓名:%s成绩:%d\n", move->student.num, move->student.name, move->student.score);
system("pause");//暂停程序;
system("cls");//清空控制台;
return;
}
move = move->next;
}
printf("未找到学生信息\n");
system("pause");//暂停程序;
system("cls");//清空控制台;
}
void savestudent(Node* head) {
FILE* file=fopen(".\\student.info","w"); // ".\\"表示当前目录 相对路径
Node* move = head->next;
while (move != NULL) {
if (fwrite(&move->student, sizeof(Student), 1, file) != 1) {
printf("写入失败\n");
return;
}
move = move->next;
}
fclose(file);
}
void loadstudent(Node* head) {
FILE* file=fopen(".\\student.info", "r");
if (!file) {
printf("没有学生信息,跳过读取\n");
return;
}
Node* fresh = (Node*)malloc(sizeof(Node));
fresh->next = NULL;
Node* move = head;
while(fread(&fresh->student, sizeof(Student), 1, file) == 1) {
move->next = fresh;
move = fresh;
fresh = (Node*)malloc(sizeof(Node));
fresh->next == NULL;
}
free(fresh);
fclose(file);
printf("读取成功\n");
}
void changestudent(Node* head) {
printf("请输入要修改的学生学号");
int number;
scanf("%d", &number);
Node* move = head->next;
while (move != NULL) {
if (move->student.num == number) {
printf("请输入修改的学生姓名,成绩\n");
scanf("%s%d", move->student.name, &move->student.score);
savestudent(head);
printf("修改成功");
system("pause");//暂停程序;
system("cls");//清空控制台;
return;
}
move = move->next;
}
printf("未找到学生信息");
system("pause");//暂停程序;
system("cls");//清空控制台;
}
void deletestudent(Node* head) {
printf("请输入要删除的学生学号:");
int number = 0;
scanf("%d", &number);
Node* move = head;
while (move->next!= NULL) {
if (move->next->student.num == number) {
Node* tmp = move->next;
move->next = move->next->next; //链这个指针的后一个;
free(tmp);//释放删除的节点;
tmp = NULL;
savestudent(head);
printf("删除成功");
system("pause");//暂停程序;
system("cls");//清空控制台;
return;
}
move = move->next;
}
printf("未找到学生");
system("pause");//暂停程序;
system("cls");//清空控制台;
}
void paixu(Node* head) {
Node* turn = head->next;
Node* move = head->next;
Node* save=NULL;
for (turn;turn->next != NULL;turn = turn->next) {
for (move;move->next != save;move = move->next) {
if (move->student.score < move->next->student.score) {
Student tmp = move->student;
move->student = move->next->student;
move->next->student = tmp;
}
}
save=move;
}
outputstudent(head);
system("pause");//暂停程序;
system("cls");//清空控制台;
}