|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#define FILENAME "student.dat"
|
|
|
typedef enum{
|
|
|
MAN, WOMAN} SEX;
|
|
|
typedef struct tagStudent
|
|
|
{
|
|
|
int id;
|
|
|
char name[20];
|
|
|
SEX sex;
|
|
|
int age;
|
|
|
char major[20];
|
|
|
int math,english,computer;
|
|
|
struct tagStudent *next;
|
|
|
}STUDENT, *PSTUDENT;
|
|
|
STUDENT g_head;
|
|
|
void Show_menu();
|
|
|
int Get_menunumber();
|
|
|
PSTUDENT Create_student();
|
|
|
int Add_student(PSTUDENT pstu);
|
|
|
PSTUDENT GetPrevAddr(int id);
|
|
|
void Show_all();
|
|
|
int idShow_student();
|
|
|
int Show_studentcount();
|
|
|
void Modity_student(int num);
|
|
|
int Question(const char *pstr);
|
|
|
int GetInputNum();
|
|
|
void Del_student(int num);
|
|
|
void Del_all();
|
|
|
void Save_tofile();
|
|
|
void Load_fromfile();
|
|
|
int main()
|
|
|
{
|
|
|
int running = 1;
|
|
|
while(running)
|
|
|
{
|
|
|
switch(Get_menunumber())
|
|
|
{
|
|
|
case 0:running = 0;break;
|
|
|
case 1:Add_student(Create_student());break;
|
|
|
case 2:Del_student(GetInputNum());break;
|
|
|
case 3:idShow_student();break;
|
|
|
case 4:Modity_student(GetInputNum());break;
|
|
|
case 5:Del_all();break;
|
|
|
case 6:Show_all();break;
|
|
|
case 7:Show_studentcount();break;
|
|
|
case 8:Load_fromfile();break;
|
|
|
case 9:Save_tofile();break;
|
|
|
}
|
|
|
system("pause");
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
void Show_menu()
|
|
|
{
|
|
|
system("cls");
|
|
|
printf("\n\n------------------------------学生管理系统--------------------------------\n\n");
|
|
|
printf("\n\t1.添加学生信息 2.删除某个学生信息 3.显示某个学生信息\n");
|
|
|
printf("\t4.修改学生信息 5.删除所有学生信息 6.显示所有学生信息\n");
|
|
|
printf("\t7.显示信息数量 8.读取文件学生信息 9.保存学生信息至文件\n");
|
|
|
printf("\t0.退出系统\n");
|
|
|
printf("\n-------------------------------------------------------------------------\n");
|
|
|
}
|
|
|
int Get_menunumber()
|
|
|
{
|
|
|
int num;
|
|
|
Show_menu();
|
|
|
printf("请选择菜单(0 ~ 9):");
|
|
|
while(1 != scanf("%d", &num) || num < 0 || num > 9)
|
|
|
{
|
|
|
Show_menu();
|
|
|
printf("选择菜单错误,请重新选择(0 ~ 9):");
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
return num;
|
|
|
}
|
|
|
|
|
|
PSTUDENT Create_student()
|
|
|
{
|
|
|
int sex;
|
|
|
PSTUDENT pstu = (PSTUDENT)malloc(sizeof(STUDENT));
|
|
|
|
|
|
if(!pstu)
|
|
|
{
|
|
|
printf("申请内存空间失败!\n");
|
|
|
return NULL;
|
|
|
}
|
|
|
printf("请输入学生的编号(整型):");
|
|
|
while(1 != scanf("%d", &pstu->id) || GetPrevAddr(pstu->id))
|
|
|
{
|
|
|
printf("学生编号输入错误或已经存在,请重新输入学生的编号(整型):");
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
printf("请输入学生的姓名(小于20字符):");
|
|
|
scanf("%20s", pstu->name);
|
|
|
printf("请选择学生的性别(1.男 2.女):");
|
|
|
while(1 != scanf("%d", &sex) || sex < 1 || sex > 2)
|
|
|
{
|
|
|
printf("性别选择错误,请重新选择学生的性别(1.男 2.女):");
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
if(1 == sex)
|
|
|
pstu->sex = MAN;
|
|
|
else
|
|
|
pstu->sex = WOMAN;
|
|
|
printf("请输入学生的年龄(10 ~ 40):");
|
|
|
while(1 != scanf("%d", &pstu->age) || pstu->age < 10 || pstu->age > 40)
|
|
|
{
|
|
|
printf("年龄输入错误!请重新输入学生的年龄(10 ~ 40):");
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
printf("请输入学生的专业(小于20字符):");
|
|
|
scanf("%20s", pstu->major);
|
|
|
printf("请输入学生的数学成绩:");
|
|
|
scanf("%d", &pstu->math);
|
|
|
printf("请输入学生的英语成绩:");
|
|
|
scanf("%d", &pstu->english);
|
|
|
printf("请输入学生的计算机成绩:");
|
|
|
scanf("%d", &pstu->computer);
|
|
|
pstu->next = NULL;
|
|
|
return pstu;
|
|
|
}
|
|
|
int Add_student(PSTUDENT pstu)
|
|
|
{
|
|
|
PSTUDENT ps = &g_head;
|
|
|
if(!pstu)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
if(GetPrevAddr(pstu->id))
|
|
|
{
|
|
|
printf("编号为%d的学生信息已经存在!\n", pstu->id);
|
|
|
free(pstu);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
while(ps->next)
|
|
|
ps = ps->next;
|
|
|
|
|
|
ps->next = pstu;
|
|
|
pstu->next = NULL;
|
|
|
return 1;
|
|
|
}
|
|
|
PSTUDENT GetPrevAddr(int num)
|
|
|
{
|
|
|
PSTUDENT pstu = &g_head;
|
|
|
while(pstu->next)
|
|
|
{
|
|
|
if(pstu->next->id == num)
|
|
|
return pstu;
|
|
|
else pstu = pstu->next;
|
|
|
}
|
|
|
return NULL;
|
|
|
}
|
|
|
int idShow_student()
|
|
|
{int s_num;
|
|
|
PSTUDENT pst=&g_head;
|
|
|
printf("请输入待查找的学生的学号:\n");
|
|
|
scanf("%d",&s_num);
|
|
|
while(pst->next)
|
|
|
{
|
|
|
if(pst->next->id==s_num)
|
|
|
{
|
|
|
printf("姓名 学号 专业 数学 英语 计算机 \n");
|
|
|
printf("%s %d %s %d %d %d\n",pst->next->name,pst->next->id,pst->next->major,pst->next->math,pst->next->english,pst->next->computer);
|
|
|
return 1;
|
|
|
}
|
|
|
pst=pst->next;
|
|
|
}
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
void Show_all()
|
|
|
{
|
|
|
PSTUDENT pstu = &g_head;
|
|
|
printf("--------------------------------------------------------------------\n");
|
|
|
printf(" 学号 姓名 性别 年龄 专业 数学 英语 计算机\n");
|
|
|
printf("--------------------------------------------------------------------\n");
|
|
|
while(pstu->next)
|
|
|
{
|
|
|
printf(" %-8d ", pstu->next->id);
|
|
|
printf("%-20s", pstu->next->name);
|
|
|
printf("%-6s", pstu->next->sex == MAN ? "男" : "女");
|
|
|
printf("%4d", pstu->next->age);
|
|
|
printf("%20s", pstu->next->major);
|
|
|
printf("%10d",pstu->next->math);
|
|
|
printf("%10d",pstu->next->english);
|
|
|
printf("%10d\n",pstu->next->computer);
|
|
|
pstu = pstu->next;
|
|
|
}
|
|
|
printf("--------------------------------------------------------------------\n");
|
|
|
}
|
|
|
int Show_studentcount()
|
|
|
{
|
|
|
int count = 0;
|
|
|
PSTUDENT pstu = &g_head;
|
|
|
while(pstu->next)
|
|
|
{
|
|
|
++count;
|
|
|
pstu = pstu->next;
|
|
|
}
|
|
|
printf("\n当前共有%d位学生信息。\n", count);
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
void Modity_student(int num)
|
|
|
{
|
|
|
PSTUDENT pstu = GetPrevAddr(num);
|
|
|
|
|
|
int choose;
|
|
|
if(!pstu)
|
|
|
{
|
|
|
printf("没有编号为%d的学生信息。\n", num);
|
|
|
return;
|
|
|
}
|
|
|
pstu = pstu->next;
|
|
|
|
|
|
printf("当前学生的姓名为%s\n", pstu->name);
|
|
|
if(Question("确定要修改吗?"))
|
|
|
{
|
|
|
printf("请输入学生的姓名(小于20字符):");
|
|
|
scanf("%20s", pstu->name);
|
|
|
}
|
|
|
printf("当前学生的性别为%s\n", pstu->sex == MAN ? "男" : "女");
|
|
|
if(Question("确定要修改吗?"))
|
|
|
{
|
|
|
printf("请输入学生的性别(1.男 2.女):");
|
|
|
while(1 != scanf("%d", &choose) || choose < 1 || choose > 2)
|
|
|
{
|
|
|
printf("输入错误,请重新输入学生的性别(1.男 2.女):");
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
if(1 == choose)
|
|
|
pstu->sex = MAN;
|
|
|
else
|
|
|
pstu->sex = WOMAN;
|
|
|
}
|
|
|
printf("当前学生的年龄为%d\n", pstu->age);
|
|
|
if(Question("确定要修改吗?"))
|
|
|
{
|
|
|
printf("请输入学生的年龄(10 ~ 40):");
|
|
|
while(1 != scanf("%d", &pstu->age) || pstu->age < 10 || pstu->age > 40)
|
|
|
{
|
|
|
printf("年龄输入错误!请重新输入学生的年龄(10 ~ 40):");
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
}
|
|
|
printf("当前学生的专业为%s\n", pstu->major);
|
|
|
if(Question("确定要修改吗?"))
|
|
|
{
|
|
|
printf("请输入学生的数学成绩(小于20字符):");
|
|
|
scanf("%20s", pstu->major);
|
|
|
}
|
|
|
printf("当前的数学成绩为%d\n",pstu->math);
|
|
|
if(Question("确定要修改吗?"))
|
|
|
{
|
|
|
printf("请输入学生的数学成绩:");
|
|
|
scanf("%d", &pstu->math);
|
|
|
}
|
|
|
printf("当前的英语成绩为%d\n",pstu->english);
|
|
|
if(Question("确定要修改吗?"))
|
|
|
{
|
|
|
printf("请输入学生的数学成绩:");
|
|
|
scanf("%d", &pstu->english);
|
|
|
}
|
|
|
printf("当前的计算机成绩为%d\n",pstu->computer);
|
|
|
if(Question("确定要修改吗?"))
|
|
|
{
|
|
|
printf("请输入学生的数学成绩:");
|
|
|
scanf("%d", &pstu->computer);
|
|
|
}
|
|
|
printf("修改完毕!\n");
|
|
|
}
|
|
|
int Question(const char *pstr)
|
|
|
{char answer;
|
|
|
printf("%s请选择(y or n):", pstr);
|
|
|
while(1 != scanf(" %c", &answer) || (answer != 'y' && answer != 'n'))
|
|
|
{
|
|
|
printf("输入错误!%s请重新选择(y or n):", pstr);
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
if('y' == answer)
|
|
|
return 1;
|
|
|
else
|
|
|
return 0;
|
|
|
}
|
|
|
int GetInputNum()
|
|
|
{
|
|
|
int num;
|
|
|
printf("请输入学生的编号(整型):");
|
|
|
while(1 != scanf("%d", &num))
|
|
|
{
|
|
|
printf("编号输入错误!请重新输入学生的编号(整型):");
|
|
|
fflush(stdin);
|
|
|
}
|
|
|
return num;
|
|
|
}
|
|
|
void Del_student(int num)
|
|
|
{
|
|
|
PSTUDENT pstu, ptmp;
|
|
|
if(pstu = GetPrevAddr(num))
|
|
|
{
|
|
|
if(!Question("确定要删除该学生信息吗?"))
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
ptmp = pstu->next;
|
|
|
pstu->next = ptmp->next;
|
|
|
free(ptmp);
|
|
|
printf("删除了编号为%d的学生信息。\n", num);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("没有找到编号为%d的学生信息。\n", num);
|
|
|
}
|
|
|
}
|
|
|
void Del_all()
|
|
|
{
|
|
|
PSTUDENT pstu = g_head.next, ptmp;
|
|
|
int count = 0;
|
|
|
if(!Question("确定要删除当前所有的学生信息吗?"))
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
while(pstu)
|
|
|
{
|
|
|
ptmp = pstu;
|
|
|
pstu = pstu->next;
|
|
|
free(ptmp);
|
|
|
++count;
|
|
|
}
|
|
|
printf("共删除了%d位学生信息。\n", count);
|
|
|
g_head.next = NULL;
|
|
|
}
|
|
|
|
|
|
void Save_tofile()
|
|
|
{
|
|
|
FILE *pf = fopen(FILENAME, "wb");
|
|
|
PSTUDENT pstu = &g_head;
|
|
|
int i = 0, count = Show_studentcount();
|
|
|
if(!pf)
|
|
|
{
|
|
|
printf("打开待写入的文件失败!\n");
|
|
|
return;
|
|
|
}
|
|
|
if(!Question("确定要将当前学生信息保存到文件中吗?"))
|
|
|
{
|
|
|
fclose(pf);
|
|
|
return;
|
|
|
}
|
|
|
fwrite(&count, 1, sizeof(count), pf);
|
|
|
while(pstu->next)
|
|
|
{
|
|
|
fwrite(pstu->next, 1, sizeof(STUDENT), pf);
|
|
|
++i;
|
|
|
pstu = pstu->next;
|
|
|
}
|
|
|
fclose(pf);
|
|
|
if(i == count)
|
|
|
{
|
|
|
printf("成功的写入了%d条学生信息。\n", count);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
printf("应写入%d条学生信息,实际写入%d条学生信息。\n", count, i);
|
|
|
}
|
|
|
}
|
|
|
void Load_fromfile()
|
|
|
{
|
|
|
int i, count = 0, repeat = 0;
|
|
|
FILE *pf;
|
|
|
PSTUDENT pstu;
|
|
|
printf("提示:从文件中读取学生信息会询问是否清空当前学生信息(不清空表示合并所有信息)。\n");
|
|
|
if((pf = fopen(FILENAME, "rb")) == NULL)
|
|
|
{
|
|
|
printf("文件还没有创建,请手工输入学生信息并保存吧!\n");
|
|
|
return;
|
|
|
}
|
|
|
Del_all();
|
|
|
fread(&count, 1, sizeof count, pf);
|
|
|
for(i = 0; i < count; ++i)
|
|
|
{
|
|
|
pstu = (PSTUDENT)malloc(sizeof(STUDENT));
|
|
|
fread(pstu, 1, sizeof(STUDENT), pf);
|
|
|
if(!Add_student(pstu))
|
|
|
{
|
|
|
++repeat;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
fclose(pf);
|
|
|
printf("文件读取完毕!新增学生信息%d条。\n", count - repeat);
|
|
|
} |