学生成绩管理系统(C语言)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#define NUM_SUBJECT 4
typedef struct zl
{
char no[5]; /*学号*/
char name[6]; /*姓名*/
int score[NUM_SUBJECT]; /*各门学科成绩*/
float total; /*总分*/
float average; /*平均分*/
int order; /*名次*/
}STUDENT;
int REC_NUM=0; /*学生记录数*/
char*subject[]={"数学","电脑","物理","电工学"};
char*menu[]={"**************MENU***************",
"1. Enter new record",
"2. Display record",
"3. Search record",
"4. Modify record",
"5. Delete a record to list",
"6. Add a record to list",
"7. Sort to make new file",
"8. Save the file",
"9. Load the file",
"10. Quit"};
STUDENT records[10];
void newRec();
void showTable();
void showTable2(Iint);
void display();
void searchRec();
void modifyRec();
void deleteRec();
void addRec();
void orderRec();
void sortRec();
void saveRec();
void loadRec();
void copyRec(STUDENT*,STUDENT*);
void quit();
int menu_select();
int findRec(char*,int,int);
void main()
{
system("cls");
for(;;)
{
switch(menu_select())
{
case 1: newRec();break;
case 2: display();break;
case 3: searchRec();break;
case 4: modifyRec();break;
case 5: deleteRec();break;
case 6: addRec();break;
case 7: sortRec();break;
case 8: saveRec();break;
case 9: loadRec();break;
case 10: quit();
}
}
}
int menu_select()
{
char s[3];
int c,i;
for(i=0;i<11;i++)
{
printf("%s",menu[i]);
}
window(1,1,80,25);
do
{
printf(" Enter you choice(1-10):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>11);
return c;
}
void showTable1()
{
system("cls");
printf("*****************STUDENT INFORMATION****************");
printf("D?D?D?D?D?D?D?D?D?D?\n");
printf("|Rec | No | Name | Computer | Physics | English | total | Average | Order\n");
printf("?¨¤?¨¤?¨¤?¨¤?¨¤?¨¤?¨¤?¨¨\n");
}
void showTable2(int n)
{
if(n==0)n=1;
if(REC_NUM==0)n=-1;
printf("\n");
}
void quit()
{
char str[5];
printf("Save records?(Y/N)");
scanf("%s",str);
if(str[0]=='Y'||str[0]=='y')
saveRec();
exit(0);
}
void newRec()
{
int i, j, sum;
system("cls");
printf("Please input the REC_NUM:");
scanf("%d",&REC_NUM);
for(i=0;i<REC_NUM;i++)
{
system("cls");
sum=0;
for(j=0;j<NUM_SUBJECT;j++);
{
scanf("%d",&records[i].score[j]);
sum=sum+records[i].score[j];
}
records[i].total=sum;
records[i].average=records[i].total/NUM_SUBJECT;
records[i].order=0;
}
}
void display()
{
int i=0;
showTable1();
for(i=0;i<REC_NUM;i++)
{
printf("|%2d |%4s |6s?%4d | %4d | %4d | %5.1f | %5.1f | %2d \n",i+1,records[i].no,records[i].name,records[i].score[0],records[i].score[1],records[i].score[2],records[i].score[3],records[i].total,records[i].average,records[i].order);
}
showTable2(i);
printf("Press any key to return!");
getch();
}
void saveRec()
{
FILE*fp1,*fp2;
if((fp1=fopen("d:\\cjt\\tc\\lianxiti\\keshe\\keshe.dat","wb"))==NULL)
{
printf("Cannot open this file!");
exit(1);
}
if((fp2=fopen("d:\\cjt\\tc\\lianxiti\\keshe\\keshe.dat","wb"))==NULL)
{
printf("Cannot open this file!");
exit(1);
}
printf("\nSaving file......\n");
fwrite(&REC_NUM,sizeof(REC_NUM),1,fp2);
fwrite(records,sizeof(STUDENT),REC_NUM,fp1);
fclose(fp1);fclose(fp2);
printf("n\Save success!!\n");delay(1000);
printf("\nPress any key to return!");
getch();
}
void loadRec()
{
FILE*fp1,*fp2;
if((fp1=fopen("d:\\cjt\\tc\\lianxiti\\keshe\\keshe.dat","rb"))==NULL)
{
printf("Cannot open this file!");
exit(1);
}
if((fp2=fopen("d:\\cjt\\tc\\lianxiti\\keshe\\keshe.dat","rb"))==NULL)
{
printf("Cannot open this file!");
exit(1);
}
system("cls");
printf("\nLoading file.....\n");
fread(&REC_NUM,sizeof(REC_NUM),1,fp2);
fread(records,sizeof(STUDENT),REC_NUM,fp1);
fclose(fp1);fclose(fp2);
delay(2000);
printf("\nYou have success read date from file!!!\n");
printf("\press any key to return!");
getch();
}
void searchRec()
{
char str[20];
int i;
system("cls");
printf("Please input the number you want to get:");
scanf("%s",str);
i=findRec(str,1,0);
if(i!=-1)
{
printf("|%2d |%4s | 6s| %4d | %4d | %4d | %4d| %5.1f | %5.1f | %2d \n",i+1,records[i].no,records[i].name,records[i].score[0],records[i].score[1],records[i].score[2],records[i].score[3],records[i].total,records[i].average,records[i].order);
showTable2(0);
}
else printf("Not find!\n");
printf("Press any key to return!");
getch();
}
void deleteRec()
{
int i, j;
char str[20];
system("cls");
printf("Please input the number you want to delete:");
scanf("%S",str);
i=findRec(str,1,0);
for(j=i;j<REC_NUM;j++)
records[j]=records[j+1];
REC_NUM--;
saveRec();
}
void copyRec(STUDENT*src,STUDENT*dest)
{
int i;
strcpy(dest->no,src->no);
strcpy(dest->name,src->name);
for(i=0;i<NUM_SUBJECT;i++)
dest->score[i]=src->score[i];
dest->total=src->total;
dest->average=src->average;
dest->order=src->order;
}
void orderRec()
{
int i;
records[0].order=1;
for(i=1;i<REC_NUM;i++)
if(records[i].total==records[i-1].total)
records[i].order=records[i-1].order;
else
records[i].order=i+1;
}
void sortRec()
{
int i,j,flag;
STUDENT t;
system("cls");
printf("\nsorting file\n");
for(i=0;i<REC_NUM;i++)
{
flag=0;
for(j=REC_NUM-1;j>i;j--)
if(records[j].total>records[j-1].total)
{
copyRec(&records[j],&t);
copyRec(&records[j-1],&records[j]);
copyRec(&t,&records[j-1]);
flag=1;
}
if(!flag) break;
}
orderRec();
delay(2000);
printf("\nsorting success\n");
saveRec();
}
int findRec(char *target,int tarType,int from)
{
int i;
for(i=from;i<REC_NUM;i++)
{
if((tarType==1&&strcmp(target,records[i].no)==0)
||tarType==2&&strcmp(target,records[i].name)==0)
return(i);
}
return(-1);
}
void modifyRec()
{
int i,j,sum;
char no[20],str[8];
system("cls");
printf("Please input the number you want to modify:");
scanf("%s",no);
i=findRec(no,1,0);
showTable1();
printf("| %2d | %4s | 6s |%4d |%4d | %4d | %4d | %5.1f | %5.1f|%2d |\n",i+1,records[i].no,records[i].name,records[i].score[0],records[i].score[1],records[i].score[2],records[i].score[3],records[i].total,records[i].average,records[i].order);
showTable2(0);
printf("Do you wang to modify this student(Y/N):");
scanf("%s",str);
if(str[0]=='y'||str[0]=='Y')
{
printf("enter number:");scanf("%s",records[i].no);
printf("enter name:");scanf("%s",records[i].name);
sum=0;
for(j=0;j<NUM_SUBJECT;j++)
{
printf("%s:",subject[j]);
scanf("%d",&records[i].score[j]);
sum=sum+records[i].score[j];
}
records[i].total=sum;
records[i].average=records[i].total/NUM_SUBJECT;
records[i].order=0;
}
printf("Press any key to return!");
getch();
}
void addRec()
{
int i,j,sum=0;
char no[20],str[8];
for(;;)
{
system("cls");
printf("Please input the number you want to add:");
scanf("%s",str);
i=findRec(str,1,0);
if(i==-1)break;
else printf("This number is exist!\n");
getch();
}
strcpy(records[REC_NUM].no,str);
printf("enter name:");scanf("%s",records[REC_NUM].name);
for(j=0;j<NUM_SUBJECT;j++)
{
printf("%s:",subject[j]);
scanf("%d",&records[REC_NUM].score[j]);
sum=sum+records[REC_NUM].score[j];
}
records[REC_NUM].total=sum;
records[REC_NUM].average=records[REC_NUM].total/NUM_SUBJECT;
records[REC_NUM].order=0;
REC_NUM++;
saveRec();
}