當前位置:成語大全網 - 新華字典 - 學生信息管理系統

學生信息管理系統

#include<stdio.h>/*我們也做過的*/

#include<stdlib.h>

#include<string.h>

#define MAX_LEN 10

#define STU_NUM 30

#define COURSE_NUM 6

typedef struct student

{

long num;

char name[MAX_LEN];

float score[COURSE_NUM];

float sum;

float aver;

}STU;

int Menu(void);

void ReadScore(STU stu[],int n,int m);

void AverSumofEveryStudent(STU stu[],int n,int m);

void AverSumofEveryCourse(STU stu[],int n,int m);

void SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b));

int Ascending(float a,float b);

int Descending(float a,float b);

void SwapFloat(float *x,float *y);

void SwapLong(long *x,long *y);

void SwapChar(char x[],char y[]);

void AsSortbyNum(STU stu[],int n,int m);

void SortbyName(STU stu[],int n,int m);

void SearchbyNum(STU stu[],int n,int m);

void SearchbyName(STU stu[],int n,int m);

void StatisticAnalysis(STU stu[],int n,int m);

void PrintScore(STU stu[],int n,int m);

void WritetoFile(STU record[],int n,int m);

void ReadfromFile(STU record[],int *n,int *m);

int main()

{

char ch;

int n=0,m=0;

STU stu[STU_NUM];

while(1)

{

ch=Menu();

switch (ch)

{

case 1:printf("Input student number(n<=%d):",STU_NUM);

scanf("%d",&n);

printf("Input course number(m<=%d):",COURSE_NUM);

scanf("%d",&m);

ReadScore(stu,n,m);

break;

case 2:AverSumofEveryCourse(stu,n,m);

break;

case 3:AverSumofEveryStudent(stu,n,m);

break;

case 4:SortbyScore(stu,n,m,Descending);

printf("\nSort in descending order by score:\n");

PrintScore(stu,n,m);

break;

case 5:SortbyScore(stu,n,m,Ascending);

printf("\nSort in ascending order by score:\n");

PrintScore(stu,n,m);

break;

case 6:AsSortbyNum(stu,n,m);

printf("\nSort in ascending order by score:\n");

PrintScore(stu,n,m);

break;

case 7:SortbyName(stu,n,m);

printf("\nSort in dictionary order by score:\n");

PrintScore(stu,n,m);

break;

case 8:SearchbyNum(stu,n,m);

break;

case 9:SearchbyName(stu,n,m);

break;

case 10:StatisticAnalysis(stu,n,m);

break;

case 11:PrintScore(stu,n,m);

break;

case 12:WritetoFile(stu,n,m);

break;

case 13:ReadfromFile(stu,&n,&m);

break;

case 14:printf("End of program!\n");

exit(0);

default :printf("Input error!\n");

}

}

return 0;

}

/*函數功能:顯示菜單並獲得用戶鍵盤輸入的選項*/

int Menu(void)

{

int itemSelected;

printf("Management for Students' scores\n");

printf("1.Input record\n");

printf("2.Caculate total and average score of every course \n");

printf("3.Caculate total and average score of every student \n");

printf("4.Sort in descending order by score \n");

printf("5.Sort in ascending order by score \n");

printf("6.Sort in ascending order by number \n");

printf("7.Sort in dictionary order by name \n");

printf("8.Search by number \n");

printf("9.Search by name \n");

printf("10.Statistic analysis \n");

printf("11.List record \n");

printf("12.write to a file \n");

printf("13.read from a file \n");

printf("0.Exit \n");

printf("Please Input your choice:"); //讀入用戶輸入

scanf("%d",&itemSelected);

return itemSelected;

}

/*函數功能:輸入n個學生的m門課程成績*/

void ReadScore(STU stu[],int n,int m)

{

int i,j;

printf("Input students' ID,name and score:\n");

for(i=0;i<n;i++)

{

scanf("%ld%s",&stu[i].num,stu[i].name);

for(j=0;j<m;j++)

{

scanf("%f",&stu[i].score[j]);

}

}

}

/*函數功能:計算每個學生各門課程的總分和平均分*/

void AverSumofEveryStudent(STU stu[],int n,int m)

{

int i,j;

for(i=0;i<n;i++)

{

stu[i].sum=0;

for(j=0;j<m;j++)

{

stu[i].sum=stu[i].sum+stu[i].score[j];

}

stu[i].aver=m>0?stu[i].sum/m:-1;

printf("student %d: sum = %.0f,aver = %.0f\n",

i+1,stu[i].sum,stu[i].aver);

}

}

/*函數功能:計算每門課程的總分和平均分*/

void AverSumofEveryCourse(STU stu[],int n,int m)

{

int i,j;

float sum[COURSE_NUM],aver[COURSE_NUM];

for(j=0;j<m;j++)

{

sum[j]=0;

for(i=0;i<n;i++)

{

sum[j]=sum[j]+stu[i].score[j];

}

aver[j] = n>0 ? sum[j]/n:-1;

printf("course %d:sum = %.0f,aver = %.0f\n",j+1,sum[j],aver[j]);

}

}

/*函數功能:按選擇法將數組sum中的元素值排序*/

void SortbyScore(STU stu[],int n,int m,int (*compare)(float a,float b))

{

int i,j,k,t;

for(i=0;i<n-1;i++)

{

k=i;

for(j=i+1;j<n;j++)

{

if((*compare)(stu[j].sum,stu[k].sum)) k=j;

}

if(k!=i)

{

for(t=0;t<m;t++)

{

SwapFloat(&stu[k].score[t],&stu[i].score[t]);

}

SwapFloat(&stu[k].sum,&stu[i].sum);

SwapFloat(&stu[k].aver,&stu[i].aver);

SwapLong(&stu[k].num,&stu[i].num);

SwapChar(stu[k].name,stu[i].name);

}

}

}

/*使數據按升序排序*/

int Ascending(float a,float b)

{

return a<b;

}

/*使數據按降序排序*/

int Descending(float a,float b)

{

return a>b;

}

/*交換兩個單精度浮點型數據*/

void SwapFloat(float *x,float *y)

{

float temp;

temp=*x;

*x=*y;

*y=temp;

}

/*交換兩個長整形數據*/

void SwapLong(long *x,long *y)

{

long temp;

temp=*x;

*x=*y;

*y=temp;

}

/*交換兩個字符串*/

void SwapChar(char x[],char y[])

{

char temp[MAX_LEN];

strcpy(temp,x);

strcpy(x,y);

strcpy(y,temp);

}

/*函數功能:按選擇法將數組num的元素值按從低到排高序*/

void AsSortbyNum(STU stu[],int n,int m)

{

int i,j,k,t;

for(i=0;i<n-1;i++)

{

k=i;

for(j=i+1;j<n;j++)

{

if(stu[j].num < stu[k].num) k=j;

}

if(k!=i)

{

for(t=0;t<m;t++)

{

SwapFloat(&stu[k].score[t],&stu[i].score[t]);

}

SwapFloat(&stu[k].sum,&stu[i].sum);

SwapFloat(&stu[k].aver,&stu[i].aver);

SwapLong(&stu[k].num,&stu[i].num);

SwapChar(stu[k].name,stu[i].name);

}

}

}

/*函數功能:交換法實現字符串按字典順序排序*/

void SortbyName(STU stu[],int n,int m)

{

int i,j,t;

for(i=0;i<n-1;i++)

{

for(j=i+1;j<n;j++)

{

if(strcmp(stu[j].name, stu[i].name)<0)

{

for(t=0;t<m;t++)

{

SwapFloat(&stu[i].score[t],&stu[j].score[t]);

}

SwapFloat(&stu[i].sum,&stu[j].sum);

SwapFloat(&stu[i].aver,&stu[j].aver);

SwapLong(&stu[i].num,&stu[j].num);

SwapChar(stu[i].name,stu[j].name);

}

}

}

}

/*函數功能:按學號查找學生成績並顯示查找結果*/

void SearchbyNum(STU stu[],int n,int m)

{

long number;

int i,j;

printf("Input the number you want to search");

scanf("%ld",&number);

for(i=0;i<n;i++)

{

if(stu[i].num==number)

{

printf("%ld \t%s \t",stu[i].num,stu[i].name);

for(j=0;j<m;j++)

{

printf("%.0f \t",stu[i].score[j]);

}

printf("%.0f\t%.0f\n",stu[i].sum,stu[i].aver);

return;

}

}

printf("\n Not Found! \n");

}

/*函數功能:按姓名的字典順序排出成績表*/

void SearchbyName(STU stu[],int n,int m)

{

char x[MAX_LEN];

int i,j;

printf("Input the number you want to search");

scanf("%s",x);

for(i=0;i<n;i++)

{

if(strcmp(stu[i].name,x)==0)

{

printf("%ld \t%s \t",stu[i].num,stu[i].name);

for(j=0;j<m;j++)

{

printf("%.0f \t",stu[i].score[j]);

}

printf("%.0f\t%.0f\n",stu[i].sum,stu[i].aver);

return;

}

}

printf("\n Not Found! \n");

}

/*函數功能:統計各分數段的學生人數及所占的百分比*/

void StatisticAnalysis(STU stu[],int n,int m)

{

int i,j,total,t[6];

for(j=0;j<m;j++)

{

printf("For course %d:\n",j+1);

memset(t,0,sizeof(t)); //將數組t的全部元素初始化為0

for(i=0;i<n;i++)

{

if(stu[i].score[j]>=0&&stu[i].score[j]<60) t[0]++;

else if (stu[i].score[j]<70) t[1]++;

else if (stu[i].score[j]<80) t[2]++;

else if (stu[i].score[j]<90) t[3]++;

else if (stu[i].score[j]<100) t[4]++;

else if (stu[i].score[j]=100) t[5]++;

}

for(total=0,i=0;i<=5;i++)

{

total=total+t[i];

}

for(i=0;i<=5;i++)

{

if(i==0) printf("<60\t%d\t%.2f%%\n",t[i],(float)t[i]/n*100);

else if(i==5)

printf("%d\t%d\t%.2f%%\n",(i+5)*10,t[i],(float)t[i]/n*100);

else

printf("%d-%d\t%d\t%.2f%%\n",(i+5)*10,(i+5)*10+9,t[i],(float)t[i]/n*100);

}

}

}

/*函數功能:打印學生成績*/

void PrintScore(STU stu[],int n,int m)

{

int i,j;

for(i=0;i<n;i++)

{

printf("%ld\t%s\t",stu[i].num,stu[i].name);

for(j=0;j<m;j++)

{

printf("%.0f\t",stu[i].score[j]);

}

printf("%.0f\t%.0f\n",stu[i].sum,stu[i].aver);

}

}

/*輸出n個學生的學號,姓名及m門課程的成績到文件student.txt中*/

void WritetoFile(STU stu[],int n,int m)

{

FILE *fp;

int i,j;

if((fp=fopen("student.txt","w"))==NULL)

{

printf("Failure to open score.txt!\n");

exit(0);

}

fprintf(fp,"%d\t%d\n",n,m); //將學生人數和課程門數寫入文件

for(i=0;i<n;i++)

{

fprintf(fp,"%10ld%10s",stu[i].num,stu[i].name);

for(j=0;j<m;j++)

{

fprintf(fp,"%10.0f",stu[i].score[j]);

}

fprintf(fp,"%10.0f%10.0f\n",stu[i].sum,stu[i].aver);

}

fclose(fp);

}

/*從文件中讀取學生的學號,姓名及成績等信息寫入到結構體數組stu中*/

void ReadfromFile(STU stu[],int *n,int *m)

{

FILE *fp;

int i,j;

if((fp=fopen("student.txt","r"))==NULL)

{

printf("Failure to open score.txt!\n");

exit(0);

}

fscanf(fp,"%d\t%d",n,m); //從文件中讀出學生人數和課程門數

for(i=0;i< *n;i++)

{

fscanf(fp,"%10ld",&stu[i].num);

fscanf(fp,"%10s",stu[i].name);

for(j=0;j< *m;j++)

{

fscanf(fp,"%10f",&stu[i].score[j]);

}

fscanf(fp,"%10f%10f",&stu[i].sum,&stu[i].aver); //不能用%10.0f

}

fclose(fp);

}