高考成绩管理系统,用文件
程序代码:
#include<stdio.h> #include<string.h> #include<ctype.h> #include<stdlib.h> #define N 2 typedef struct stu { char name[10]; char num[13]; char sex[4]; char c; float math,chinese,english,sum,comp; } STD; void sub(char a[],char e[],int b,int c) { int i; for(i=0;i<c;i++) e[i]=a[b+i-1]; e[c]='\0'; } void menu() { printf("\n\n\n\t\t\t******欢迎使用高考成绩查询系统******\n"); printf("\t\t\t1.建立学生档案.\n"); printf("\t\t\t2.查询成绩.\n"); printf("\t\t\t3.排序.\n"); printf("\t\t\t4.输出全部学生信息.\n"); printf("\t\t\t5.退出.\n"); } int input(STD d[],STD h[]) { FILE *fp1,*fp2; int i,b=0; char n[13],a[3]; for(i=0;i<N;i++) { printf("\n\t\t\t请输入第%d名学生信息\n",i+1); printf("\t\t\t学号:"); scanf("%s",n); sub(n,a,7,2); if(strcmp(a,"01")==0) { strcpy(d[i].num,n); printf("\t\t\t姓名:"); scanf("%s",d[i].name); printf("\t\t\t性别:"); scanf("%s",d[i].sex); printf("\t\t\t语文成绩:"); scanf("%f",&d[i].chinese); printf("\t\t\t数学成绩:"); scanf("%f",&d[i].math); printf("\t\t\t英语成绩:"); scanf("%f",&d[i].english); getchar(); printf("\t\t\t理综:"); scanf("%f",&d[i].comp); d[i].sum=d[i].chinese+d[i].english+d[i].math+d[i].comp; b++; } else { strcpy(h[i].num,n); printf("\t\t\t姓名:"); scanf("%s",h[i].name); printf("\t\t\t性别:"); scanf("%s",h[i].sex); printf("\t\t\t语文成绩:"); scanf("%f",&h[i].chinese); printf("\t\t\t数学成绩:"); scanf("%f",&h[i].math); printf("\t\t\t英语成绩:"); scanf("%f",&h[i].english); getchar(); printf("\t\t\t文综:"); scanf("%f",&h[i].comp); h[i].sum=h[i].chinese+h[i].english+h[i].math+h[i].comp; } system("cls"); } if((fp1=fopen("d:\\daniel.dat","wb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } if((fp2=fopen("d:\\heddle.dat","wb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } fwrite(d,sizeof(STD),b,fp1); fwrite(h,sizeof(STD),N-b,fp2); fclose(fp1); fclose(fp2); system("cls"); printf("\n\t\t\t建立成功!\n"); getch(); return b; } void sort(STD d[],STD h[],int a) { int i,j; FILE *fp1,*fp2; STD t; if((fp1=fopen("d:\\heddle.dat","wb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } if((fp2=fopen("d:\\daniel.dat","wb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } for(i=0;i<N-a-1;i++) for(j=i+1;j<N-a;j++) if(h[i].sum<h[j].sum) { t=h[i]; h[i]=h[j]; h[j]=t; } for(i=0;i<N-a-1;i++) for(j=i+1;j<a;j++) if(d[i].sum<d[j].sum) { t=d[i]; d[i]=d[j]; d[j]=t; } fwrite(h,sizeof(STD),N-a,fp1); fwrite(d,sizeof(STD),a,fp2); fclose(fp1); fclose(fp2); getch(); } void output(STD d[],STD h[],int a) { int i; FILE *fp1,*fp2; if((fp1=fopen("d:\\heddle.dat","rb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } fread(h,sizeof(STD),N-a,fp1); printf("\n文科\n\n"); printf("学号\t\t姓名\t语文\t数学\t英语\t文综\t\t总分\n"); for(i=0;i<N-a;i++) printf("%12s\t%6s %6.1f %6.1f %6.1f %6.1f \t%6.1f\n",h[i].num,h[i].name,h[i].chinese,h[i].math,h[i].english,h[i].comp,h[i].sum); printf("\n\n"); if((fp2=fopen("d:\\daniel.dat","rb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } fread(d,sizeof(STD),a,fp2); printf("\n理科\n\n"); printf("学号\t\t姓名\t语文\t数学\t英语\t理综\t\t总分\n"); for(i=0;i<a;i++) printf("%12s\t%6s %6.1f %6.1f %6.1f %6.1f \t%6.1f\n",d[i].num,d[i].name,d[i].chinese,d[i].math,d[i].english,d[i].comp,d[i].sum); fclose(fp1); fclose(fp2); getch(); } void search(STD h[],STD d[],int b) { int i; char n[10],m[13],a[3]; FILE *fp; printf("\t\t\t请输入要查询的姓名:"); scanf("%s",n); printf("\t\t\t学号:"); scanf("%s",m); sub(m,a,7,2); if(strcmp(a,"01")==0) { if((fp=fopen("d:\\daniel.dat","rb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } fread(d,sizeof(STD),b,fp); for(i=0;i<b;i++) if(strcmp(d[i].name,n)==0 && strcmp(d[i].num,n)==0) { printf("学号\t\t姓名\t语文\t数学\t英语\t理综\t\t总分\n"); printf("%12s\t%6s %6.1f %6.1f %6.1f %6.1f \t%6.1f\n",d[i].num,d[i].name,d[i].chinese,d[i].math,d[i].english,d[i].comp,d[i].sum); } if(i>=b) printf("\t\t\t查无此人,请核对学生姓名或学号!\n"); } else { if((fp=fopen("d:\\heddle.dat","rb"))==NULL) { printf("\t\t\t错误!\n"); exit(1); } fread(h,sizeof(STD),N-b,fp); for(i=0;i<N-b;i++) if(strcmp(h[i].name,n)==0 && strcmp(h[i].num,n)==0) { printf("学号\t\t姓名\t语文\t数学\t英语\t文综\t\t总分\n"); printf("%12s\t%6s %6.1f %6.1f %6.1f %6.1f \t%6.1f\n",h[i].num,h[i].name,h[i].chinese,h[i].math,h[i].english,h[i].comp,h[i].sum); } if(i>=N-b) printf("\t\t\t查无此人,请核对学生姓名或学号!\n"); } fclose(fp); getch(); } int main(void) { int a,i; STD H[N],D[N]; while(1) { system("cls"); menu(); printf("\t\t\t请输入您的选择:"); scanf("%d",&a); system("cls"); switch(a) { case 1:i=input(H,D);break; case 2:search(H,D,i);break; case 3:sort(H,D,i);printf("\t\t\t排序成功!\n");break; case 4:output(H,D,i);break; case 5:exit(0); default :printf("\t\t\t输入错误!\n"); } } return 0; }
求大神解,输入一个文科一个理科的时候就不行了,输入两个理科就可以,两个文科也可以,求解。。