求修改关于学生管理系统的程序
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
#pragma once
struct data
{ int year; //年
int month; //月
int day; //日
};
struct student
{ int snum; //学号
char name[20]; //姓名
char sex; //性别
struct data birthday; //出生日期
int score[5] ; //5门课程成绩
};
typedef struct node
{ struct student data;
struct node * next;
}Node,*PNode;
//链表实现
//链表初始化
void SListInit(PNode *Head);
//申请节点
PNode BuySListNode(int snum, char* name, char sex, int year, int month, int day,int* score);
//创建学生信息(列表创建)
int CreateStudentMessageList(PNode* head, int n);
//增加学生信息
int AddStudentMessageList(PNode* head, int snum, char* name, char sex, int year, int month, int day,int* score);
//删除学生信息
int DelStudentMessageList(PNode* head,int snum);
//修改学生信息
void AlterStudentMessageList(PNode pAlt, char* name, char sex, int year, int month, int day,int* score);
//查找学生信息(按学号)
void FindStudentMessageBySnumList(PNode* head,int snum);
//查找学生信息(按姓名)
void FindStudentMessageByNameList(PNode* head,char* name);
//打印学生信息
void PrintStudentMessageList(PNode pArr);
//学生按总分从大到小排序
void StudentMessageTotalSortList(PNode* head);
//学生按姓名排序
void StudentMessageNameSortList(PNode* head);
//创建学生信息(文件创建)
int CreateStudentMessageFile(PNode* head);
void _CreateStudentMessage(PNode* head,char msg[11][20]);
//最终封装函数
//主界面打印
void Printwindow(int n);
//创建学生信息
void CreateStudentMessage(PNode* head);
//增加学生信息
void AddStudentMessage(PNode* head);
//查看学生信息
void BrowseStudentMessage(PNode* head);
//修改学生信息
void AlterStudentMessage(PNode head);
//查找学生信息
void FindStudentMessage(PNode* head);
//删除学生信息
void DelStudentMessage(PNode* head);
//按总分排序后显示,或者按姓名排序后显示
void SortBrowseStudentMessage(PNode* head);
//保存信息
void SaveMessage(PNode* head);
//辅助函数
//字符串转换整型
int ChangeCharforInt(char* arr);
//整型转换字符串
char* ChangeIntforChar(int num);
//释放空间
void FreeMessageList(PNode* head);
int main()
{
int n;
PNode head;
SListInit(&head);
do
{
system("cls");
Printwindow(1);
scanf("%d",&n);
if(n == 1)
{
CreateStudentMessage(&head);
}
else if(n == 2)
{
AddStudentMessage(&head);
}
else if(n == 3)
{
BrowseStudentMessage(&head);
}
else if(n == 4)
{
AlterStudentMessage(head);
}
else if(n == 5)
{
FindStudentMessage(&head);
}
else if(n == 6)
{
DelStudentMessage(&head);
}
else if(n == 7)
{
SortBrowseStudentMessage(&head);
}
}while(n);
printf("保存信息并退出\n");
SaveMessage(&head);
FreeMessageList(&head);
return 0;
}
void SListInit(PNode *Head)
{
assert(Head);
*Head = NULL;
}
PNode BuySListNodeList(int snum, char* name, char sex, int year, int month, int day,int* score)
{
int i;
PNode pNewNode = (PNode)malloc(sizeof(Node));
if(NULL == pNewNode)
{
assert(0);
return NULL;
}
pNewNode->next = NULL;
pNewNode->data.snum = snum;
strcpy(pNewNode->data.name,name);
pNewNode->data.sex = sex;
pNewNode->data.birthday.day = day;
pNewNode->data.birthday.month = month;
pNewNode->data.birthday.year = year;
for(i=0; i<5; i++)
{
pNewNode->data.score[i] = score[i];
}
return pNewNode;
}
int CreateStudentMessageList(PNode* head,int n)
{
int i = 0;
int flag = 0;
int t,k,j;
char message[100];
char msg[11][20];
while(i<n)
{
printf("请按照 学号 姓名 性别 出生日期 语文 数学 英语 科学 体育 的顺序输入每个学生的信息:");
if(0 == flag)
getchar();
gets(message);
t = 0;
k = 0;
while(message[k] != '\0' && t<11)
{
j = 0;
while(message[k] != ' ' && message[k] != '-' && message[k] != '\0')
{
msg[t][j] = message[k];
k++;
j++;
}
msg[t][j] = '\0';
t++;
k++;
}
_CreateStudentMessage(head,msg);
i++;
flag = 1;
}
return 1;
}
int AddStudentMessageList(PNode* head, int snum, char* name, char sex, int year, int month, int day,int* score)
{
if(NULL == *head)
*head = BuySListNodeList(snum,name,sex,year,month,day,score);
else
{
PNode pArr = *head;
while(pArr->next != NULL)
{
pArr = pArr->next;
}
pArr->next = BuySListNodeList(snum,name,sex,year,month,day,score);
}
return 1;
}
int DelStudentMessageList(PNode* head,int snum)
{
PNode pDel = *head;
PNode pArr;
if(NULL == *head)
return 0;
if(pDel->data.snum == snum)
{
*head = pDel->next;
}
else
{
while(pDel->data.snum != snum && pDel->next !=NULL)
{
pArr = pDel;
pDel = pDel->next;
}
if(pDel->data.snum == snum)
{
if(NULL == pDel->next)
pArr->next = NULL;
else
pArr->next = pDel->next;
}
else
{
printf("没找到学号为%d的学生\n",snum);
return 0;
}
}
free(pDel);
return 1;
}
void AlterStudentMessageList(PNode pAlt, char* name, char sex, int year, int month, int day,int* score)
{
int i;
if(*name != '0')
{
strcpy(pAlt->data.name,name);
}
if(sex != '0')
pAlt->data.sex = sex;
if(year)
pAlt->data.birthday.year = year;
if(month)
pAlt->data.birthday.month = month;
if(day)
pAlt->data.birthday.day = day;
for(i=0; i<5 ;i++)
{
if(*(score+i))
pAlt->data.score[i] = *(score+i);
}
}
void FindStudentMessageBySnumList(PNode* head,int snum)
{
PNode pArr = *head;
if(NULL == head)
return;
while(pArr->data.snum != snum && NULL != pArr->next)
{
pArr = pArr->next;
}
if(pArr->data.snum != snum)
{
printf("没找到学号为%d的学生\n",snum);
return;
}
printf("找到了\n");
printf("具体信息如下:\n");
printf("学号\t姓名\t性别\t出生日期\t语文\t数学\t英语\t科学\t体育\n");
PrintStudentMessageList(pArr);
}
void FindStudentMessageByNameList(PNode* head,char* name)
{
PNode pArr = *head;
if(NULL == head)
return;
while(pArr->next != NULL)
{
if(0 == strcmp(name,pArr->data.name))
{
printf("找到了\n");
printf("具体信息如下:\n");
printf("学号\t姓名\t性别\t出生日期\t语文\t数学\t英语\t科学\t体育\n");
PrintStudentMessageList(pArr);
return;
}
}
printf("没找到姓名为%s的学生\n",name);
}
void PrintStudentMessageList(PNode pArr)
{
if(pArr == NULL)
return;
printf("%d\t%s\t%c\t%4d-%02d-%02d\t%3d\t%3d\t%3d\t%3d\t%3d\n",pArr->data.snum,pArr->data.name,pArr->data.sex,
pArr->data.birthday.year,pArr->data.birthday.month,pArr->data.birthday.day,
pArr->data.score[0],pArr->data.score[1],pArr->data.score[2],pArr->data.score[3],pArr->data.score[4]);
}
int Totalscore(int* score)
{
int num = 0;
int i;
for(i=0; i<5; i++)
{
num += score[i];
}
return num;
}
void StudentMessageTotalSortList(PNode* head)
{
PNode pArr = *head;
PNode pTest;
if(Totalscore(pArr->data.score) < Totalscore(pArr->next->data.score))
{
*head = pArr->next;
pArr->next = pArr->next->next;
(*head)->next =pArr;
}
pArr = *head;
while(pArr->next->next != NULL)
{
pTest = pArr;
pArr = pArr->next;
if(Totalscore(pArr->data.score) < Totalscore(pArr->next->data.score))
{
pTest->next =pArr->next;
pArr->next = pArr->next->next;
pTest->next->next = pArr;
}
}
}
void StudentMessageNameSortList(PNode* head)
{
PNode pArr = *head;
PNode pTest;
if(strcmp(pArr->data.name,pArr->next->data.name) > 0)
{
*head = pArr->next;
pArr->next = pArr->next->next;
(*head)->next =pArr;
}
pArr = *head;
while(pArr->next->next != NULL)
{
pTest = pArr;
pArr = pArr->next;
if(strcmp(pArr->data.name,pArr->next->data.name) > 0)
{
pTest->next =pArr->next;
pArr->next = pArr->next->next;
pTest->next->next = pArr;
}
}
}
void Printwindow(int n)
{
if(n == 1)
{
printf("======================================== \n");
printf(" 学生信息管理系统 \n");
printf("======================================== \n");
printf("功能选项: \n");
printf(" 0---保存信息并退出 \n");
printf(" 1---创建学生信息 2---增加学生信息 \n");
printf(" 3---浏览学生信息 4---修改学生信息 \n");
printf(" 5---查找学生信息 6---删除学生信息 \n");
printf(" 7-按总分排序后显示,或者按姓名排序后显示\n");
printf("======================================== \n");
printf("请输入选项(0--7): \n");
}
if(n == 2)
{
printf(" **********学生信息的创建********** \n");
printf("功能选项: \n");
printf("--------------------------------------------------\n");
printf(" 1---从键盘输入数据信息 \n");
printf(" 2---从文件读取数据信息 \n");
printf("--------------------------------------------------\n");
printf("请输入选项(1--2):");
}
if(n == 3)
{
printf("**********学生信息的查找**********\n");
printf("功能选项: \n");
printf("----------------------------------\n");
printf(" 1---按照学号查找 \n");
printf(" 2---按照姓名查找 \n");
printf("----------------------------------\n");
printf("请输入选项(1--2):");
}
if(n == 4)
{
printf("******************学生信息的显示*****************\n");
printf("-------------------------------------------------\n");
printf(" 1---根据学生总分排序并显示学生的所有信息 \n");
printf(" 2---根据提供的姓名排序并显示学生的所有信息 \n");
printf("-------------------------------------------------\n");
printf("请输入选项(1--2):");
}
}
int CreateStudentMessageFile(PNode* head)
{
FILE *pFile;
char message[100];
char msg[11][20];
int i,j,k;
pFile = fopen("Student.txt","r");
if(NULL == pFile)
{
printf("文件打开失败!\n");
return 0;
}
while(fgets(message,100,pFile) != NULL)
{
i = 0;
k = 0;
while(message[k] != '\0' && i<11)
{
j = 0;
while(message[k] != ' ' && message[k] != '-')
{
msg[i][j] = message[k];
k++;
j++;
}
msg[i][j] = '\0';
i++;
k++;
}
_CreateStudentMessage(head,msg);
//学号 姓名 性别 出生日期 语文 数学 英语 科学 体育
//int char* char int-int-int int int int int int
}
fclose(pFile);
return 1;
}
void _CreateStudentMessage(PNode* head,char msg[11][20])
{
int score[5];
char sex,name[20];
int i;
for(i=0; i<5; i++)
score[i] = ChangeCharforInt(msg[6+i]);
sex = msg[2][0];
strcpy(name,msg[1]);
AddStudentMessageList(head,ChangeCharforInt(&msg[0][0]),name,sex,ChangeCharforInt(&msg[3][0]),ChangeCharforInt(&msg[4][0]),ChangeCharforInt(&msg[5][0]),score);
}
int ChangeCharforInt(char* arr)
{
int n = 0;
int i = 0;
while(arr[i] != '\0')
{
n *= 10;
n += arr[i] - '0';
i++;
}
return n;
}
void CreateStudentMessage(PNode* head)
{
int n;
int t;
int ret;
system("cls");
do
{
Printwindow(2);
scanf("%d",&n);
if(n != 1 && n != 2)
printf("输入错误!\n");
}while(n != 1 && n != 2);
if(n == 1)
{
printf("你选择了从键盘输入数据,请继续......\n");
printf("请输入学生的人数:");
scanf("%d",&t);
ret = CreateStudentMessageList(head,t);
}
else if(n == 2)
{
printf("你选择从文件读取信息\n");
ret = CreateStudentMessageFile(head);
}
if(ret)
printf("创建成功!\n");
else
printf("创建失败!\n");
printf("按回车键继续.......返回主界面\n");
_getch();
}
void AddStudentMessage(PNode* head)
{
int j = 0;
int t,k;
int snum = 0,year = 0,month = 0,day = 0,score[5] = {0};
char name[30] = {0},sex = 0;
char message[100];
char msg[11][20];
system("cls");
printf("请按照 学号 姓名 性别 出生日期 语文 数学 英语 科学 体育 的顺序输入每个学生的信息:");
getchar();
gets(message);
t = 0;
k = 0;
while(message[k] != '\0' && t<11)
{
j = 0;
while(message[k] != ' ' && message[k] != '-' && message[k] != '\0')
{
msg[t][j] = message[k];
k++;
j++;
}
msg[t][j] = '\0';
t++;
k++;
}
_CreateStudentMessage(head,msg);
printf("信息增加成功!\n");
printf("按回车键继续.......返回主界面\n");
_getch();
}
void BrowseStudentMessage(PNode* head)
{
PNode pArr = *head;
system("cls");
printf("具体信息如下:\n");
printf("学号\t姓名\t性别\t出生日期\t语文\t数学\t英语\t科学\t体育\n");
while(pArr != NULL)
{
PrintStudentMessageList(pArr);
pArr = pArr->next;
}
printf("按回车键继续.......返回主界面\n");
_getch();
}
void AlterStudentMessage(PNode head)
{
PNode pAlt = head;
int j = 0;
int t,k;
int snum = 0;
char message[100];
char msg[10][20];
int score[5];
char sex,name[20];
int i;
system("cls");
if(NULL == head)
{
printf("学生列表不存在!\n");
return;
}
printf("请输入需要修改的学生学号:");
scanf("%d",&snum);
while(pAlt->data.snum != snum && pAlt->next != NULL)
{
pAlt = pAlt->next;
}
if(pAlt->data.snum != snum)
{
printf("没找到学号为%d的学生\n",snum);
printf("按回车键继续.......返回主界面\n");
_getch();
return;
}
printf("请按照 姓名 性别 出生日期 语文 数学 英语 科学 体育 的顺序输入所需修改的信息:\n");
printf("不需要修改的信息请用0代替\n");
getchar();
gets(message);
t = 0;
k = 0;
while(message[k] != '\0' && t<10)
{
j = 0;
while(message[k] != ' ' && message[k] != '-' && message[k] != '\0')
{
msg[t][j] = message[k];
k++;
j++;
}
msg[t][j] = '\0';
t++;
k++;
}
for(i=0; i<5; i++)
score[i] = ChangeCharforInt(msg[5+i]);
sex = msg[1][0];
strcpy(name,msg[0]);
printf("修改前的信息为:\n");
printf("学号\t姓名\t性别\t出生日期\t语文\t数学\t英语\t科学\t体育\n");
PrintStudentMessageList(pAlt);
AlterStudentMessageList(pAlt,name,sex,ChangeCharforInt(&msg[2][0]),ChangeCharforInt(&msg[3][0]),ChangeCharforInt(&msg[4][0]),score);
printf("修改后的信息为:\n");
printf("学号\t姓名\t性别\t出生日期\t语文\t数学\t英语\t科学\t体育\n");
PrintStudentMessageList(pAlt);
printf("按回车键继续.......返回主界面\n");
_getch();
}
void FindStudentMessage(PNode* head)
{
int n;
int snum;
char name[30];
system("cls");
do
{
Printwindow(3);
scanf("%d",&n);
if(n != 1 && n != 2)
printf("输入错误!\n");
}while(n != 1 && n != 2);
if(n == 1)
{
printf("你选择按照学号查找,请输入学号:");
scanf("%d",&snum);
FindStudentMessageBySnumList(head,snum);
}
else if(n == 2)
{
printf("你选择按照姓名查找,请输入姓名:");
scanf("%s",name);
FindStudentMessageByNameList(head,name);
}
printf("按回车键继续.......返回主界面\n");
_getch();
}
void DelStudentMessage(PNode* head)
{
int snum;
int ret;
system("cls");
printf("请输入学号:");
scanf("%d",&snum);
ret = DelStudentMessageList(head,snum);
if(ret)
printf("删除成功!\n");
else
printf("删除失败!\n");
printf("按回车键继续.......返回主界面\n");
_getch();
}
void SortBrowseStudentMessage(PNode* head)
{
PNode pArr;
int n;
system("cls");
do
{
Printwindow(4);
scanf("%d",&n);
if(n != 1 && n != 2)
printf("输入错误!\n");
}while(n != 1 && n != 2);
if(n == 1)
{
StudentMessageTotalSortList(head);
pArr = *head;
printf("具体信息如下:\n");
printf("学号\t姓名\t性别\t出生日期\t语文\t数学\t英语\t科学\t体育\n");
while(pArr != NULL)
{
PrintStudentMessageList(pArr);
pArr = pArr->next;
}
}
else if(n == 2)
{
StudentMessageNameSortList(head);
pArr = *head;
printf("具体信息如下:\n");
printf("学号\t姓名\t性别\t出生日期\t语文\t数学\t英语\t科学\t体育\n");
while(pArr != NULL)
{
PrintStudentMessageList(pArr);
pArr = pArr->next;
}
}
printf("按回车键继续.......返回主界面\n");
_getch();
}
void FreeMessageList(PNode* head)
{
PNode pArr = *head;
PNode pFree;
while(pArr != NULL)
{
pFree = pArr;
pArr = pArr->next;
free(pFree);
}
*head = NULL;
}
char* ChangeIntforChar(int num)
{
int t = num;
int i,sz = 1;
char ch[10];
while(t)
{
t /= 10;
sz *= 10;
}
for(i=0;sz != 1;i++)
{
ch[i] = num%sz/(sz/10)+'0';
sz /= 10;
}
//123 123%1000/100=1 123%100/10=2 123%10/1=3
ch[i] = '\0';
return &ch[i];
}
void SaveMessage(PNode* head)
{
FILE * pFile;
PNode pArr = *head;
char message[100];
char* ch1 = " ";
char* ch2 = "-";
char ch3[2] = {pArr->data.sex,'\0'};
pFile = fopen ("Student.txt","w");
if(NULL == *head)
return;
do
{
strcpy(message,ChangeIntforChar(pArr->data.snum));//学号
strcat(message,ch1);
strcat(message,pArr->data.name);//姓名
strcat(message,ch1);
strcat(message,ch3);//性别
strcat(message,ch1);
strcat(message,ChangeIntforChar(pArr->data.birthday.year));//年
strcat(message,ch2);
strcat(message,ChangeIntforChar(pArr->data.birthday.month));//月
strcat(message,ch2);
strcat(message,ChangeIntforChar(pArr->data.birthday.day));//日
strcat(message,ch1);
strcat(message,ChangeIntforChar(pArr->data.score[0]));//成绩1
strcat(message,ch1);
strcat(message,ChangeIntforChar(pArr->data.score[1]));//成绩2
strcat(message,ch1);
strcat(message,ChangeIntforChar(pArr->data.score[2]));//成绩3
strcat(message,ch1);
strcat(message,ChangeIntforChar(pArr->data.score[3]));//成绩4
strcat(message,ch1);
strcat(message,ChangeIntforChar(pArr->data.score[4]));//成绩5
strcat(message,ch1);
fputs(message,pFile);
fputc('\n',pFile);
}while(pArr->next != NULL);
fclose (pFile);
}