📄 main.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*设置要用到的结构*/
struct score
{
int Math;
int Physics;
int English;
int Computer;
int average;
};
struct stud
{
char name[20];
char ID[20];
char sex[8];
char age[5];
char dorm[20];
char tel[20];
struct score *scores;
struct stud *next;
};
struct classes
{
char ID[20];
struct classes *next;
struct stud *student;
};
/*全局变量声明*/
FILE *file;
char path[]="D:\\info.txt";
struct stud *frontStud=NULL;
struct stud *currentStud=NULL;
struct classes *head=NULL;
struct classes *frontClass=NULL;
struct classes *currentClass=NULL;
int mainPage();
int createClass();
int registerScore();
int saveFile();
int readFile();
int rankStudents();
int insertStud();
int delStud();
int delClass();
int modifyStudInfo();
int modifyScore();
int rankAll();
int classAverage();
int showFailed();
/*MAIN函数*/
int main ()
{
mainPage();
exit(-1);
}
/*主页面函数*/
int mainPage ()
{
int i,n;
clrscr();
printf("\n\n");
printf("\t\t\tEDUCATIONAL ADMINISTRATION SYSTEM\n");
printf("\t ");
for(i=0;i<57;i++)
{
printf("*");
}
printf("\n");
printf("\t * 1.Create classes and students in it.\t\t *\n");
printf("\t * 2.Insert a student in some certain class.\t *\n");
printf("\t * 3.Modify a class's students' information.\t *\n");
printf("\t * 4.Delete a certain student's information.\t *\n");
printf("\t * 5.Delete a certain class.\t\t\t *\n");
printf("\t * 6.Register the scores of the students.\t\t *\n");
printf("\t * 7.Modify a certain student's score.\t\t *\n");
printf("\t * 8.Rank a certain class's students.\t\t *\n");
printf("\t * 9.Show all average score of any subject.\t *\n");
printf("\t * 10.Show failures' bill.\t\t\t\t *\n");
printf("\t * Others.Exit the program.\t\t\t *\n");
printf("\t ");
for(i=0;i<57;i++)
{
printf("*");
}
printf("\n\n");
if((file=fopen(path,"r"))!=NULL)
{
readFile();
printf("Read existent infomation successfully!\n\n");
fclose(file);
}
printf("Please choose the operation you want to do: ");
scanf("%d%*c",&n);
switch(n)
{
case 1: createClass();
break;
case 2: insertStud();
break;
case 3: modifyStudInfo();
break;
case 4: delStud();
break;
case 5: delClass();
break;
case 6: registerScore();
break;
case 7: modifyScore();
break;
case 8: rankAll();
break;
case 9: classAverage();
break;
case 10: showFailed();
break;
default: exit(-1);
}
}
/*创建班级和它的学生的函数*/
int createClass()
{
char temp,continueStud,continueClass,rb[10];
int i=0,j=0;
for(continueClass='Y',i=1;continueClass=='Y'||continueClass=='y';i++)
{
/*创建一个班级*/
clrscr();
if(currentClass!=NULL) /*如果已经读到了数据,要用此部*/
{
i++;
frontClass=currentClass;
}
printf("\n\n");
currentClass=(struct classes *)malloc(sizeof(struct classes));
if(currentClass==NULL)
{
printf("Can't assign the place for the class!\n");
exit(-1);
}
if(head==NULL)
head=currentClass;
if(i!=1)
frontClass->next=currentClass;
printf("Please input the number of the class: ");
scanf("%s%*c",currentClass->ID);
currentClass->next=NULL;
currentClass->student=NULL;
printf("\nNow, begin to create students.\n");
for(continueStud='Y',j=1;continueStud=='Y'||continueStud=='y';j++)
{
currentStud=(struct stud *)malloc(sizeof(struct stud));
if(currentStud==NULL)
{
printf("Can't assign the place for the student!\n");
exit(-1);
}
if(j!=1)
frontStud->next=currentStud;
/*录入学生的基本情况*/
if(currentClass->student==NULL)
currentClass->student=currentStud;
printf("\nPlease input the name of the student: ");
gets(currentStud->name);
printf("Please input the ID of the student: ");
gets(currentStud->ID);
currentSex:
printf("If male then input \"M\" or else \"F\": ");
scanf("%c%*c",&temp);
switch(temp)
{
case 'M':
case 'm': strcpy(currentStud->sex,"Male");
break;
case 'F':
case 'f': strcpy(currentStud->sex,"Female");
break;
default: gets(rb);
goto currentSex;
}
printf("Please input the age of the student: ");
gets(currentStud->age);
printf("Please input the number of the student's dormitory: ");
gets(currentStud->dorm);
printf("Please input the telephone of the student: ");
gets(currentStud->tel);
currentStud->next=NULL;
currentStud->scores=NULL;
frontStud=currentStud;
printf("\nContinue to create students (Y/N)? ");
scanf("%c%*c",&continueStud);
currentStud->next=NULL;
}
frontClass=currentClass;
printf("\nContinue to create classes (Y/N)? ");
scanf("%c%*c",&continueClass);
currentClass->next=NULL;
}
rankStudents();
saveFile();
mainPage();
}
/*登记各班学生成绩的函数*/
int registerScore()
{
char classID[20],temp='';
continueFind:
clrscr();
printf("\n\nPlease input the number of the class you want to register: ");
gets(classID);
if(head==NULL) /*库中没有数据*/
{
printf("No information!\n\n");
printf("Go to the main page (Y/N)? ");
temp='';
scanf("%c%*c",&temp);
if(temp=='Y'||temp=='y')
mainPage();
else
exit(-1);
}
currentClass=head;
while(currentClass->next!=NULL&&strcmp(currentClass->ID,classID)!=0) /*遍历链表*/
{
frontClass=currentClass;
currentClass=frontClass->next;
}
if(currentClass->next==NULL&&strcmp(currentClass->ID,classID)!=0) /*找不到此班*/
{
printf("There is no accordant class!\n\n");
printf("Continue to find the class (Y/N)? ");
temp='';
scanf("%c%*c",&temp);
if(temp=='Y'||temp=='y')
goto continueFind;
else
mainPage();
}
/*找到该班并为该班同学登记成绩*/
for(currentStud=currentClass->student;currentStud!=NULL;)
{
currentStud->scores=(struct score *)malloc(sizeof(struct score));
if(currentStud->scores==NULL)
{
printf("Can't assign the place for the student!\n");
exit(-1);
}
printf("\nThe current student--%s ID--%s\n",currentStud->name,currentStud->ID);
printf("Please input the score for Math: ");
scanf("%d",&(currentStud->scores->Math));
printf("Please input the score for Physics: ");
scanf("%d",&(currentStud->scores->Physics));
printf("Please input the score for English: ");
scanf("%d",&(currentStud->scores->English));
printf("Please input the score for Computer: ");
scanf("%d",&(currentStud->scores->Computer));
currentStud->scores->average=(currentStud->scores->Math+currentStud->scores->Physics+currentStud->scores->English+currentStud->scores->Computer)/4;
frontStud=currentStud;
currentStud=frontStud->next;
}
printf("\nAll of the students in the class have been registered!\n\n");
printf("Continue to register another class (Y/N)? ");
temp='';
scanf("%*c%c%*c",&temp);
if(temp=='Y'||temp=='y')
{
saveFile();
readFile();
goto continueFind;
}
else
{
saveFile();
mainPage();
}
}
/*将数据存入文件的函数*/
int saveFile()
{
int num=0; /*一个班的学生总数*/
if((file=fopen(path,"w"))==NULL)
{
printf("Can't open the file: %s!\n",path);
exit(-1);
}
/*打开了文件,开始写入信息*/
for(currentClass=head,num=1;currentClass!=NULL;)
{
fprintf(file,"*%s\n",currentClass->ID);
for(currentStud=currentClass->student;currentStud!=NULL;num++)
{
fprintf(file,"~%s;%s;%s;%s;",currentStud->name,currentStud->ID,currentStud->sex,currentStud->age);
fprintf(file,"%s;%s;",currentStud->dorm,currentStud->tel);
if(currentStud->scores!=NULL)
{
fprintf(file,"%d;%d;",currentStud->scores->Math,currentStud->scores->Physics);
fprintf(file,"%d;%d;",currentStud->scores->English,currentStud->scores->Computer);
fprintf(file,"%d;\n",currentStud->scores->average);
}
else
{
fprintf(file,"\n");
}
frontStud=currentStud;
currentStud=frontStud->next;
}
frontClass=currentClass;
currentClass=frontClass->next;
}
fclose(file);
}
/*从文件中读取数据的函数*/
int readFile()
{
char string[130],c,*token;
char step[]=";\n"; /*一行数据的分界符*/
int r=0,flag=0;
if((file=fopen(path,"r"))==NULL)
{
printf("Can't open the file!\n");
exit(-1);
}
head=NULL;
while((c=fgetc(file))!=-1)
{
flag=0;
switch(c)
{
case '*': currentClass=(struct classes *)malloc(sizeof(struct classes));
if(currentClass==NULL)
{
printf("Can't assign the place for the class!\n");
fclose(file);
exit(-1);
}
currentClass->next=NULL;
currentClass->student=NULL;
if(head==NULL)
head=currentClass;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -