#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Student
{
char Name[10];
float Score[3];
float Total;
float Ave;
};
typedef struct Node
{
struct Student st;
struct Node *pNext;
}NODE, *PNODE;
PNODE InputStudent(void);
void OutputStudent(PNODE pHead);
void DeleteStudent(PNODE pHead);
void FindStudent(PNODE pHead);
void ChangeStudent(PNODE pHead);
void InsertStudent(PNODE pHead);
void ScortByChinese(PNODE pHead);
void ScortByMath(PNODE pHead);
void ScortByEnglish(PNODE pHead);
void ScortByTotal(PNODE pHead);
int main()
{
printf("Please press any button to enter the student management system");
getchar();
system("cls");
printf("********************************************Menu*********************************************\n");
printf("* Please select the command to operate *\n");
printf("*********************************************************************************************\n");
printf("*************** 1 Input student information ***********************\n");
printf("*************** 2 Output student information ***********************\n");
printf("*************** 3 Delete student information ***********************\n");
printf("*************** 4 Find student information ***********************\n");
printf("*************** 5 Change student information ***********************\n");
printf("*************** 6 insert student information ***********************\n");
printf("*************** 7 Sort Chinese score ***********************\n");
printf("*************** 8 Sort Math score ***********************\n");
printf("*************** 9 Sort English score ***********************\n");
printf("*************** 10 Sort Score ***********************\n");
printf("*********************************************************************************************\n");
int Item;
PNODE pHead = NULL;
while(1)
{
printf("choose:");
scanf("%d",&Item);
system("cls");
switch(Item)
{
case 1:
{
pHead = InputStudent();
}
break;
case 2:
{
OutputStudent(pHead);
}
break;
case 3:
{
DeleteStudent(pHead);
}
break;
case 4:
{
FindStudent(pHead);
}
break;
case 5:
{
ChangeStudent(pHead);
}
break;
case 6:
{
InsertStudent(pHead);
}
break;
case 7:
{
ScortByChinese(pHead);
OutputStudent(pHead);
}
break;
case 8:
{
ScortByMath(pHead);
OutputStudent(pHead);
}
break;
case 9:
{
ScortByEnglish(pHead);
OutputStudent(pHead);
}
break;
case 10:
{
ScortByTotal(pHead);
OutputStudent(pHead);
}
break;
default:
break;
}
}
system("pause");
}
PNODE InputStudent(void)
{
int len;
NODE stu;
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if(NULL == pHead)
{
printf("error! ");
exit(-1);
}
PNODE pTail = pHead;
pTail -> pNext = NULL;
int i;
for(i = 0;;i ++)
{
system("cls");
printf("Please enter no%d student's name:", i+1);
scanf("%s", stu.st.Name);
if(strcmp(stu.st.Name,"quit")==0)
{
break;
}
printf("Please enter no%d student's Chinese score Math score and English score:", i+1);
scanf("%f %f %f", &stu.st.Score[0],&stu.st.Score[1],&stu.st.Score[2]);
stu.st.Total = stu.st.Score[0] + stu.st.Score[1] + stu.st.Score[2];
stu.st.Ave = stu.st.Total / 3.0f;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("error! ");
exit(-1);
}
pNew -> st = stu.st;
pTail -> pNext = pNew;
pNew -> pNext = NULL;
pTail = pNew;
}
return pHead;
}
void OutputStudent(PNODE pHead)
{
PNODE p = pHead -> pNext;
printf("name Chinese Math English Total Ave \n");
while (NULL != p)
{
printf("%s %g %g %g %g %g \n", p->st.Name, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Total, p->st.Ave);
p = p -> pNext;
}
}
void DeleteStudent(PNODE pHead)
{
PNODE p = pHead;
int i = 0;
int pos;
printf("please enter the number of student you need to delete:");
scanf("%d",&pos);
while(NULL != p -> pNext && i< pos - 1)
{
p = p -> pNext;
i++;
}
if(NULL == p -> pNext || i > pos -1)
{
printf("no find!\n");
return;
}
PNODE q = p -> pNext;
p -> pNext = q -> pNext;
free(q);
q == NULL;
printf("Successfully deleted");
}
void FindStudent(PNODE pHead)
{
char Name[10];
printf("enter the name you want to find:");
scanf("%s",Name);
PNODE p = pHead -> pNext;
while(NULL != p)
{
if(0 == strcmp(Name,p->st.Name))
{
printf("%s %g %g %g %g %g\n",p->st.Name, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Total, p->st.Ave);
}
p = p -> pNext;
}
}
void ChangeStudent(PNODE pHead)
{
char Name[10];
printf("please enter the name of student you need to change");
scanf("s%",&Name);
PNODE p = pHead -> pNext;
while(NULL != p)
{
if(0 == strcmp(Name,p -> st.Name))
{
printf("Student information before modification \n");
printf("name Chinese Math English Total Ave \n");
printf("%s %g %g %g %g %g ", p->st.Name, p->st.Score[0], p->st.Score[1], p->st.Score[2], p->st.Total, p->st.Ave);
system("pause");
system("cls");
printf("please enter new name:");
scanf("%s", p->st.Name);
printf("plsase enter new Chinese score:");
scanf("%f", &p->st.Score[0]);
printf("plsase enter new Chinese score:");
scanf("%f", &p->st.Score[1]);
printf("plsase enter new Chinese score:");
scanf("%f", &p->st.Score[2]);
p->st.Total = p->st.Score[0] + p->st.Score[1] + p->st.Score[2];
p->st.Ave = p->st.Total / 3.0f;
break;
}
p = p -> pNext;
}
}
void InsertStudent(PNODE pHead)
{
PNODE p = pHead;
int i = 0;
struct Student stu;
while(NULL != p)
{
p = p -> pNext;
i++;
}
if(NULL == pHead)
{
printf("error");
return;
}
printf("please enter student's name:");
scanf("%s",stu.Name);
printf("Please enter student's Chinese score:");
scanf("%f",&stu.Score[0]);
printf("Please enter student's Math score:");
scanf("%f",&stu.Score[1]);
printf("Please enter student's English score:");
scanf("%f",&stu.Score[2]);
stu.Total = stu.Score[0] + stu.Score[1] + stu.Score[2];
stu.Ave = stu.Total / 3.0f;
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf("error!\n");
exit(-1);
}
pNew -> st = stu;
PNODE q = p-> pNext;
p -> pNext = pNew;
pNew -> pNext = q;
}
void ScortByChinese(PNODE pHead)
{
PNODE p, q;
NODE temp;
for(p = pHead -> pNext; NULL != p; p = p -> pNext)
{
for(q = p -> pNext; NULL != q; q = q -> pNext)
{
if(p -> st.Score[0] < q -> st.Score[0])
{
temp.st = p->st;
p->st = q->st;
q->st = temp.st;
}
}
}
}
void ScortByMath(PNODE pHead)
{
PNODE p, q;
NODE temp;
for(p = pHead -> pNext; NULL != p; p = p -> pNext)
{
for(q = p -> pNext; NULL != q; q = q -> pNext)
{
if(p -> st.Score[1] < q -> st.Score[1])
{
temp.st = p->st;
p->st = q->st;
q->st = temp.st;
}
}
}
}
void ScortByEnglish(PNODE pHead)
{
PNODE p, q;
NODE temp;
for(p = pHead -> pNext; NULL != p; p = p -> pNext)
{
for(q = p -> pNext; NULL != q; q = q -> pNext)
{
if(p -> st.Score[2] < q -> st.Score[2])
{
temp.st = p->st;
p->st = q->st;
q->st = temp.st;
}
}
}
}
void ScortByTotal(PNODE pHead)
{
PNODE p, q;
NODE temp;
for(p=pHead->pNext; NULL != p; p=p->pNext)
{
for(q=p->pNext; NULL !=q; q=q->pNext)
{
if(p->st.Total < q->st.Total)
{
temp.st = p->st;
p->st = q->st;
q->st = temp.st;
}
}
}
}
void LoadInf(PNODE pHead)
{
PNODE *p = pHead;
PNODE *q;
FILE* file = fopen("./Information.dat", "rb");
if (!file)
{
printf("Failer!");
return ;
}
q = (PNODE *)malloc(sizeof(PNODE));
fread(q, sizeof(PNODE), 1, file);
while (!feof(file))
{
p -> pNext = q;
p = q;
q = (PNODE *)malloc(sizeof(PNODE));
fread(q, sizeof(PNODE), 1, file);
}
p->pNext = NULL;
fclose(file);
}
void SaveInf(PNODE pHead)
{
PNODE *p = pHead -> pNext;
int flag;
FILE* file = fopen("./Information.dat", "wb");
if (!file)
{
printf("Failer!");
return;
}
while (p != NULL)
{
flag = fwrite(p, sizeof(PNODE), 1, file);
if (flag != 1)
{
break;
}
p = p -> pNext;
}
fclose(file);
}
代码在这