头文件
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
typedef struct _Student
{
int number;
char name[20];
char gender[7];
float math;
float English;
float c_language;
float data_structure;
float total;
float average;
struct _Student *next;
} Student;
Student *InitStudent();
void OperatingMenu();
int InputStudent(Student *Q);
int AlterStudent(Student *Q,int x);
void OutputStudent(Student *Q);
Student *SortStudent(Student *Q,int course);
void FindStudent(Student *Q,int x);
void StatisticStudent(Student *data);
int SaveStudentInfoTofile(Student *Q);
void LoadStudentInFromfile(Student *Q);
源文件
#include"xsglxt.h"
float c_language,data_structure;
Student *InitStudent()
{
Student *Q;
Q=(Student *)malloc(sizeof(Student));
if(Q!=NULL)
{
Q->number=0;
strcpy(Q->name,"- ");
strcpy(Q->gender,"- ");
Q->math=0;
Q->English=0;
Q->c_language=0;
Q->data_structure=0;
Q->total=0;
Q->average=0;
Q->next=NULL;
return Q;
}
else
return NULL;
}
void OperatingMenu()
{
int i;
printf("/n/t/t Student Management System Operating Menu/n");
printf("/t");
for(i=0;i<60;i++)
putchar('*');
putchar('/n');
printf(" 1: Input student info 6: Statistic student info/n");
printf(" 2: alter student info 7: Save info to file/n");
printf(" 3: OutPut student info 8: Load info form file/n");
printf(" 4: Sort student info 0: Exit/n");
printf(" 5: find student info/n");
printf("/t");
for(i=0;i<60;i++)
putchar('*');
putchar('/n');
printf("%s","Please enter the command:");
}
int InputStudent(Student *Q)
{
int go_on=1;
Student *s,*temp;
int input_num=0;
while(go_on)
{
s=(Student *)malloc(sizeof(Student));
if(s==NULL)
{
printf("Error: can not create student");
return 0;
}
printf("please enter student`s number(if enter 0 then return):");
scanf("%d",&s->number);
if(s->number==0)
{
printf("Input %d student./n",input_num);
break;
}
temp=Q->next;
while(temp!=NULL&&temp->number!=s->number)
temp=temp->next;
if(temp)
{
printf("the number already exist!/n");
printf("please enter 1 to continue, enter 0 to return:");
scanf("%d",&go_on);
while(go_on!=0&&go_on!=1)
scanf("%d",&go_on);
if(go_on==0)
{
printf("input %d students,total is %d./n",input_num,Q->number);
break;
}
else
if(go_on==1)
continue;
}
if(s->number<0)
{
printf("you enter wrong student`s number/n");
printf("please enter 1 to continue,enter 0 to return:");
scanf("%d",&go_on);
while(go_on!=0&&go_on!=1)
{
printf("please enter 1 to continue,enter 0 to return:");
scanf("%d",&go_on);
}
if(go_on==0)
{
printf("Input %d students,total is %d./n",input_num,Q->number);
break;
}
else
if(go_on==1)
continue;
}
printf("Please enter student`s name:");
scanf("%s",s->name);
while(strlen(s->name)<=0)
{
printf("Please enter student`s name:");
scanf("%s",s->name);
}
printf("Please enter student`s genter(male or female):");
scanf("%s",s->gender);
while(strcmp(s->gender,"female")&&strcmp(s->gender,"male"))
{
printf("Please enter student`s gender(male or female):");
scanf("%s",s->gender);
}
printf("please enter math mark:");
scanf("%f",&s->math);
while(s->math>100||s->math<0)
{
printf("Please enter math mark:");
scanf("%f",&s->math);
}
printf("Please enter English mark:");
scanf("%f",&s->English);
while(s->English>100||s->English<0)
{
printf("Please enter English mark:");
scanf("%f",&s->English);
}
printf("please enter c_language mark:");
scanf("%f",&c_language);
while(c_language>100||c_language<0)
{
printf("please enter c_language mark:");
scanf("%f",&c_language);
}
printf("Please enter data_structure mark:");
scanf("%f",&data_structure);
while(s->data_structure>100||s->data_structure<0)
{
printf("Please enter data_structure mark:");
scanf("%f",&data_structure);
}
s->total=s->math+s->English+s->c_language+s->data_structure;
s->average=s->total/4;
s->next=Q->next;
Q->next=s;
input_num++;
Q->number++;
}
return 1;
}
int AlterStudent(Student *Q,int num)
{
Student *be_altered;
int flag=0,go_on=1;
be_altered=Q->next;
while(be_altered!=NULL&&be_altered->number!=num)
be_altered=be_altered->next;
if(be_altered!=NULL)
{
while(go_on)
{
flag=0;
while(flag<1||flag>6)
{
printf("/nPlease choice item be altered:/n");
printf(" 1.name 2.gender 3.math/n");
printf(" 4.English 5.c_language 6.data_structure/n");
getchar();
scanf("%d",&flag);
}
if(flag==1)
{
printf("Please enter stdent`s name:");
scanf("%s",be_altered->name);
while(strlen(be_altered->name)<=1)
{
printf("Please enter studet`s name:");
scanf("%s",be_altered->name);
}
}
else
if(flag==2)
{
printf("Please enter student`s genter(male or female):");
scanf("%s",be_altered->gender);
while(strcmp(be_altered->gender,"female")&&strcmp(be_altered->gender,"male"))
{
printf("Please enter student`s genter(male or female):");
scanf("%s",be_altered->gender);
}
}
else
if(flag==3)
{
printf("Please enter math mark:");
scanf("%d",&be_altered->math);
while(be_altered->math>100||be_altered->math<0)
{
printf("Please enter math mark:");
scanf("%d",&be_altered->math);
}
}
else
if(flag==4)
{
printf("Please enter English mark:");
scanf("%d",&be_altered->English);
while(be_altered->English>100||be_altered->English<0)
{
printf("Please enter English mark:");
scanf("%d",&be_altered->English);
}
}
else
if(flag==5)
{
printf("Please enter c_language mark:");
scanf("%d",&be_altered->c_language);
while(be_altered->c_language>100||be_altered->c_language<0)
{
printf("Please enter c_language mark:");
scanf("%d",&be_altered->c_language);
}
}
else
if(flag==6)
{
printf("Please enter data_structure mark:");
scanf("%d",&be_altered->data_structure);
while(be_altered->data_structure>100||be_altered->data_structure<0)
{
printf("Please enter data_structure mark:");
scanf("%d",&be_altered->data_structure);
}
}
be_altered->total=be_altered->math+be_altered->English+be_altered->c_language+be_altered->data_structure;
be_altered->average=be_altered->total/4;
printf("enter 1 to continue alter info,enter 0 to return:");
scanf("%d",&go_on);
}
return 1;
}
else
printf("Error: enter a wrong student`s number/n");
return 0;
}
void OutputStudent(Student *Q)
{
Student *temp;
temp=Q->next;
if(temp==NULL)
printf("there is no student info!/n");
else
{
printf("/***********All Student Info***********//n");
if(temp)
printf("number/tname/tgender/tmath/tEnglish/tc_language data_structuer total auerage/n");
while(temp)
{
printf("%d/t%s/t%s/t%0.1f/t%0.1d/t%0.1f/t/t%0.1f/t/t%0.1f/t%0.2f/n",
temp->number,temp->name,temp->gender,temp->math,
temp->English,temp->c_language,temp->data_structure,
temp->total,temp->average);
temp=temp->next;
}
}
}
Student *SortStudent(Student *Q,int course)
{
Student *p,*q,*temp;
int number;
char name[20];
char gender[7];
float math,English,c_language;
float data_structure,total,average;
p=Q->next;
if(p==NULL)
{
printf("there is no student info!/n");
return NULL;
}
switch(course)
{
case 1:
for(p;p;p=p->next)
{
temp=p;
for(q=temp->next;q;q=q->next)
{
if(temp->number>q->number)
{
number=temp->number;
strcpy(name,temp->name);
strcpy(gender,temp->gender);
math=temp->math;
English=temp->English;
c_language=temp->c_language;
data_structure=temp->data_structure;
total=temp->total;
average=temp->average;
temp->number=q->number;
strcpy(temp->name,q->name);
strcpy(temp->gender,q->gender);
temp->math=q->math;
temp->English=q->English;
temp->c_language=q->c_language;
temp->data_structure=q->data_structure;
temp->total=q->total;
temp->average=q->average;
q->number=number;
strcpy(q->name,name);
strcpy(q->gender,gender);
q->math=math;
q->English=English;
q->c_language=c_language;
q->data_structure=data_structure;
q->total=total;
q->average=average;
}
}
}
break;
case 2:
for(p;p;p=p->next)
{
temp=p;
for(q=temp->next;q;q=q->next)
{
if(temp->math>q->math)
{
number=temp->number;
strcpy(name,temp->name);
strcpy(gender,temp->gender);
math=temp->math;
English=temp->English;
c_language=temp->c_language;
data_structure=temp->data_structure;
total=temp->total;
average=temp->average;
temp->number=q->number;
strcpy(temp->name,q->name);
strcpy(temp->gender,q->gender);
temp->math=q->math;
temp->English=q->English;
temp->c_language=q->c_language;
temp->data_structure=q->data_structure;
temp->total=q->total;
temp->average=q->average;
q->number=number;
strcpy(q->name,name);
strcpy(q->gender,gender);
q->math=math;
q->English=English;
q->c_language=c_language;
q->data_structure=data_structure;
q->total=total;
q->average=average;
}
}
}
break;
case 3:
for(p;p;p=p->next)
{
temp=p;
for(q=temp->next;q;q=q->next)
{
if(temp->English>q->English)
{
number=temp->number;
strcpy(name,temp->name);
strcpy(gender,temp->gender);
math=temp->math;
English=temp->English;
c_language=temp->c_language;
data_structure=temp->data_structure;
total=temp->total;
average=temp->average;
temp->number=q->number;
strcpy(temp->name,q->name);
strcpy(temp->gender,q->gender);
temp->math=q->math;
temp->English=q->English;
temp->c_language=q->c_language;
temp->data_structure=q->data_structure;
temp->total=q->total;
temp->average=q->average;
q->number=number;
strcpy(q->name,name);
strcpy(q->gender,gender);
q->math=math;
q->English=English;
q->c_language=c_language;
q->data_structure=data_structure;
q->total=total;
q->average=average;
}
}
}
break;
case 4:
for(p;p;p=p->next)
{
temp=p;
for(q=temp->next;q;q=q->next)
{
if(temp->c_language>q->c_language)
{
number=temp->number;
strcpy(name,temp->name);
strcpy(gender,temp->gender);
math=temp->math;
English=temp->English;
c_language=temp->c_language;
data_structure=temp->data_structure;
total=temp->total;
average=temp->average;
temp->number=q->number;
strcpy(temp->name,q->name);
strcpy(temp->gender,q->gender);
temp->math=q->math;
temp->English=q->English;
temp->c_language=q->c_language;
temp->data_structure=q->data_structure;
temp->total=q->total;
temp->average=q->average;
q->number=number;
strcpy(q->name,name);
strcpy(q->gender,gender);
q->math=math;
q->English=English;
q->c_language=c_language;
q->data_structure=data_structure;
q->total=total;
q->average=average;
}
}
}
break;
case 5:
for(p;p;p=p->next)
{
temp=p;
for(q=temp->next;q;q=q->next)
{
if(temp->data_structure>q->data_structure)
{
number=temp->number;
strcpy(name,temp->name);
strcpy(gender,temp->gender);
math=temp->math;
English=temp->English;
c_language=temp->c_language;
data_structure=temp->data_structure;
total=temp->total;
average=temp->average;
temp->number=q->number;
strcpy(temp->name,q->name);
strcpy(temp->gender,q->gender);
temp->math=q->math;
temp->English=q->English;
temp->c_language=q->c_language;
temp->data_structure=q->data_structure;
temp->total=q->total;
temp->average=q->average;
q->number=number;
strcpy(q->name,name);
strcpy(q->gender,gender);
q->math=math;
q->English=English;
q->c_language=c_language;
q->data_structure=data_structure;
q->total=total;
q->average=average;
}
}
}
break;
}
return Q;
}
void FindStudent(Student *Q,int num)
{
Student *temp;
temp=Q->next;
while(temp&&temp->number!=num)
temp=temp->next;
if(temp!=NULL)
{
printf("number/tname/tgender/tmath/tEnglish/tc_language data_structure total average/n");
printf("%d/t%s/t%s/t%0.1f/t%0.1f/t%0.1f/t/t%0.1f/t/t%0.1f/t%0.2f/n",
temp->number,temp->name,temp->gender,temp->math,
temp->English,temp->c_language,temp->data_structure,
temp->total,temp->average);
}
else
printf("Error:not find the student./n");
}
void StatisticStudent(Student *Q)
{
int num_lessthan60_math=0,
num_lessthan70_math=0,
num_lessthan80_math=0,
num_lessthan90_math=0,
num_lessthan100_math=0;
int num_lessthan60_English=0,
num_lessthan70_English=0,
num_lessthan80_English=0,
num_lessthan90_English=0,
num_lessthan100_English=0;
int num_lessthan60_c_language=0,
num_lessthan70_c_language=0,
num_lessthan80_c_language=0,
num_lessthan90_c_language=0,
num_lessthan100_c_language=0;
int num_lessthan60_data_structure=0,
num_lessthan70_data_structure=0,
num_lessthan80_data_structure=0,
num_lessthan90_data_structure=0,
num_lessthan100_data_structure=0;
int num_lessthan240_total=0,
num_lessthan300_total=0,
num_lessthan350_total=0,
num_lessthan400_total=0;
int num_lessthan60_average=0,
num_lessthan70_average=0,
num_lessthan80_average=0,
num_lessthan90_average=0,
num_lessthan100_average=0;
Student *temp;
temp=Q->next;
if(temp==NULL)
printf("there is no student info!/n");
else
{
while(temp)
{
if(temp->math<60)
num_lessthan60_math++;
else
if(temp->math<70)
num_lessthan70_math++;
else
if(temp->math<80)
num_lessthan80_math++;
else
if(temp->math>90)
num_lessthan90_math++;
else
num_lessthan100_math++;
if(temp->English<60)
num_lessthan60_English++;
else
if(temp->English<70)
num_lessthan70_English++;
else
if(temp->English<80)
num_lessthan80_English++;
else
if(temp->English>90)
num_lessthan90_English++;
else
num_lessthan100_English++;
if(temp->c_language<60)
num_lessthan60_c_language++;
else
if(temp->c_language<70)
num_lessthan70_c_language++;
else
if(temp->c_language<80)
num_lessthan80_c_language++;
else
if(temp->c_language>90)
num_lessthan90_c_language++;
else
num_lessthan100_c_language++;
if(temp->data_structure<60)
num_lessthan60_data_structure++;
else
if(temp->data_structure<70)
num_lessthan70_data_structure++;
else
if(temp->data_structure<80)
num_lessthan80_data_structure++;
else
if(temp->data_structure>90)
num_lessthan90_data_structure++;
else
num_lessthan100_data_structure++;
if(temp->total<240)
num_lessthan240_total++;
else
if(temp->total<300)
num_lessthan300_total++;
else
if(temp->total<350)
num_lessthan350_total++;
else
num_lessthan400_total++;
if(temp->average<60)
num_lessthan60_average++;
else
if(temp->average<70)
num_lessthan70_average++;
else
if(temp->average<80)
num_lessthan80_average++;
else
if(temp->average>90)
num_lessthan90_average++;
else
num_lessthan100_average++;
temp=temp->next;
}
}
temp=Q->next;
if(temp)
{
printf("/***********All Studend Statistic Info***********//n/n");
printf("Number of math in 0-59: %d/n",num_lessthan60_math);
printf("Number of math in 60-69: %d/n",num_lessthan70_math);
printf("Number of math in 70-79: %d/n",num_lessthan80_math);
printf("Number of math in 80-89: %d/n",num_lessthan90_math);
printf("Number of math in 90-100: %d/n",num_lessthan100_math);
printf("Number of English in 0-59: %d/n",num_lessthan60_English);
printf("Number of English in 60-69: %d/n",num_lessthan70_English);
printf("Number of English in 70-79: %d/n",num_lessthan80_English);
printf("Number of English in 80-89: %d/n",num_lessthan90_English);
printf("Number of english in 90-100: %d/n",num_lessthan100_English);
printf("Number of c_language in 0-59: %d/n",num_lessthan60_c_language);
printf("Number of c_language in 60-69: %d/n",num_lessthan70_c_language);
printf("Number of c_language in 70-79: %d/n",num_lessthan80_c_language);
printf("Number of c_language in 80-89: %d/n",num_lessthan90_c_language);
printf("Number of c_language in 90-100: %d/n",num_lessthan100_c_language);
printf("Number of data_structure in 0-59: %d/n",num_lessthan60_data_structure);
printf("Number of data_structure in 60-69: %d/n",num_lessthan70_data_structure);
printf("Number of data_structure in 70-79: %d/n",num_lessthan80_data_structure);
printf("Number of data_structure in 80-89: %d/n",num_lessthan90_data_structure);
printf("Number of data_structure in 90-100: %d/n",num_lessthan100_data_structure);
printf("Number of total in 0-239: %d/n",num_lessthan240_total);
printf("Number of total in 240-299: %d/n",num_lessthan300_total);
printf("Number of total in 300-349: %d/n",num_lessthan350_total);
printf("Number of total in 350-400: %d/n",num_lessthan400_total);
printf("Number of average in 0-59: %d/n",num_lessthan60_average);
printf("Number of average in 60-69: %d/n",num_lessthan70_average);
printf("Number of average in 70-79: %d/n",num_lessthan80_average);
printf("Number of average in 80-89: %d/n",num_lessthan90_average);
printf("Number of average in 90-100: %d/n",num_lessthan100_average);
while(temp)
{
if(temp->average>=90)
{
printf("number/tname/tgender/tmath/tEnglish/tc_language/tdata_structure/ttotal/taverage/n");
printf("%d/t%s/t%s/t%0.1f/t%0.1f/t%0.1f/t%0.1f/t%0.1f/t%0.2f/t/n",
temp->number,temp->name,temp->gender,temp->math,
temp->English,temp->c_language,temp->data_structure,
temp->total,temp->average);
}
temp=temp->next;
}
}
}
int SaveStudentInfoToFile(Student *Q)
{
Student *s=Q->next;
FILE *fp;
char FilePath[50];
printf("Please enter file path:");
scanf("%s",FilePath);
if((fp=fopen(FilePath,"wb"))==NULL)
{
printf("Error: Can not open the file, or the file is not exish./n");
return 0;
}
while(s)
{
if(fwrite(s,sizeof(Student),1,fp)!=1)
{
printf("Error: Can not write./n");
return 0;
}
else
printf("write./n");
s=s->next;
}
fclose(fp);
return 1;
}
void LoadStudentInfoFromFile(Student *Q)
{
int flag=1;
char FilePath[50];
Student *s,*p=Q,*temp=Q;
FILE *fp;
printf("Please enter file path:");
scanf("%s",FilePath);
if((fp=fopen(FilePath,"rb"))!=NULL)
{
while(flag)
{
s=(Student *)malloc(sizeof(Student));
temp->next=s;
temp=s;
if(fread(s,sizeof(Student),1,fp)!=1)
{
/* printf("Error:Can not read./n"); */
free(s);
p->next=NULL;
flag=0;
}
else
{
printf("read./n");
p=p->next;
}
}
fclose(fp);
}
else
printf("Error: Can not load info from file/n");
}
int main(int argc,char *argv[])
{
int num=0,altered,course=-1;
Student *Q,*p,*T;
char choice;
Q=InitStudent();
OperatingMenu();
scanf("%c",&choice);
while(choice!='0')
{
switch(choice)
{
case '1':
InputStudent(Q);
break;
case '2':
while(num==0)
{
printf("Please enter student`s number:");
getchar();
scanf("%d",&num);
}
altered=AlterStudent(Q,num);
if(altered==1)
printf("Success: Alter student info/n");
else
printf("Error: Alter student info/n");
num=0;
break;
case '3':
OutputStudent(Q);
break;
case '4':
while(course<0||course>5)
{
printf("/nPlease choice item while ordered by:/n");
printf(" 0.Exit 1.number 2.math 3.English/n");
printf(" 4.c_language 5.data_structure/n");
scanf("%d",&course);
}
if(course==0)
{
course=-1;
break;
}
else
{
T=SortStudent(Q,course);
if(T!=NULL)
OutputStudent(T);
}
course=-1;
break;
case '5':
while(num==0)
{
printf("Please enter student`s number:");
scanf("%d",&num);
}
FindStudent(Q,num);
num=0;
break;
case '6':
StatisticStudent(Q);
break;
case '7':
SaveStudentInfoToFile(Q);
break;
case '8':
LoadStudentInfoFromFile(Q);
break;
default:
printf("Please give correct choice./n");
break;
}
OperatingMenu();
getchar();
scanf("%c",&choice);
}
return 0;
}