最近在做C语言的课程设计
今天搞好了 放上来给大家看看,欢迎拍砖
/*
基本思路:
读
用fread读出一个学生信息写到输入缓存(struct student a)中 判断缓存中的值是否符合
写
输入一个学生信息到输出缓存(struct student b)中,再用fwrite写到文件中;
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stud
{
long int num;
char name[10];
float C_Score;
float M_Score;
float E_Score;
};
int xiugai() //修改学生信息
{
FILE *fp;
struct stud *p;
struct stud a,b;
long int num;
p=&b;
printf("\nplease input num\n ");
scanf("%ld",&num);
printf("\nplease input num,name,C_Score,M_Score,E_score\n");
scanf("%ld%s%f%f%f",&XXXXm,XXXXme,&a.C_Score,&a.M_Score,&a.E_Score);
fp=fopen("student","rb+");
while(1)
{
fread(&b,sizeof(b),1,fp);
if(p->num==num) break;
else if(p->num==0) {break; printf("error");}
}
fseek(fp,-sizeof(a),1); //文件指针定位
fwrite(&a,sizeof(a),1,fp);
fclose(fp);
printf(".....done\n");
return(0);
printf("......done");
}
int tongji() //成绩统计
{
int x; int sum=0 ,all=0;float AVR;
float score1,score2;
struct stud a; struct stud *p; float *q;
FILE *fp;
p=&a;
printf("\nplease input xueke(1.m or 2.c or 3.e)\n");
scanf("%d",&x);
printf("\nplease input score duan(score1,score2)\n");
scanf("%f,%f",&score1,&score2);
if((score1==0)||(score1>score2)) {printf("error"); return(0);}
switch(x)
{
case 1 :q=&p->M_Score;break;
case 2 :q=&p->C_Score;break;
case 3 :q=&p->E_Score;break;
}
fp=fopen("student","rb");
while(1)
{
fread(&a,sizeof(a),1,fp);
while((*q>score1)&&(*q<score2))
{
printf("%ld,%s,%5.1f,%5.1f,%5.1f\n",XXXXm,XXXXme,a.C_Score,a.M_Score,a.E_Score);
sum++;
all=all+p->C_Score+p->M_Score+p->E_Score;
break;
}
if((p->num==0)&&(sum==0))
{
printf("error\n");
break;
}
if(p->num==0) break;
}
AVR=all/sum;
printf("sum is %d\nall is %d\n AVR is %5.3f\n",sum,all,AVR);
printf("......done");
return(0);
}
int namechaxun() //姓名查询
{
char name[10];
struct stud a; struct stud *p;
FILE *fp;
p=&a;
printf("\nplease input name\n");
scanf("%s",name);
fp=fopen("student","rb");
while(1)
{
fread(&a,sizeof(a),1,fp);
if(strcmp(XXXXme,name) == 0)
{
printf("%ld,%s,%5.1f,%5.1f,%5.1f\n",XXXXm,XXXXme,a.C_Score,a.M_Score,a.E_Score);
break;
}
else if(p->num==0)
{
printf("name error\n");
break;
}
}
printf("......done");
return(0);
}
int xuehaochaxun() //学号查询
{
long num;
struct stud a; struct stud *p;
FILE *fp;
p=&a;
printf("\nplease input num\n");
scanf("%ld",&num);
fp=fopen("student","rb");
while(1)
{
fread(&a,sizeof(a),1,fp);
if(p->num == num)
{
printf("%ld,%s,%5.1f,%5.1f,%5.1f\n",XXXXm,XXXXme,a.C_Score,a.M_Score,a.E_Score);
break;
}
else if(num==0)
{
printf("num error\n");
break;
}
}
printf("......done");
return(0);
}
int write() //输入
{
struct stud *p; struct stud a;
FILE *fp;
p=&a;
fp=fopen("student","wb");
printf("\nnum name C_Score M_Score E_score\n");
while(1)
{
scanf("%ld%s%f%f%f",&p->num,p->name,&p->C_Score,&p->M_Score,&p->E_Score);
fwrite(&a,sizeof(a),1,fp);
if(p->num==0) break;
}
fclose(fp);
printf("......done");
return(0);
}
int read() //输出
{
FILE *fp;
struct stud *p; struct stud b;
p=&b;
fp=fopen("student","rb");
printf("\nnum,name,C_Score,M_Score,E_score\n");
while(1)
{
fread(&b,sizeof(b),1,fp);
if(p->num==0) break;
printf("%ld,%s%5.1f%5.1f%5.1f\n",XXXXm,XXXXme,b.C_Score,b.M_Score,b.E_Score);
}
printf("......done");
fclose(fp);
return(0);
}
int main()
{
printf("\n****************************************************************************\n");
printf("\n** welcome **\n");
printf("\n****************************************************************************\n");
while(1)
{
int i;
printf("\n****************************************************************************\n");
printf("\n** XXXXw sheet **\n");
printf("\n** XXXXange sheet **\n");
printf("\n** XXXXaxun(num) **\n");
printf("\n** XXXXaxun(name) **\n");
printf("\n** XXXXngji **\n");
printf("\n** XXXXint sheet **\n");
printf("\n** 7.Exit **\n");
printf("\n****************************************************************************\n");
printf("\nplease input NO.");
scanf("%d",&i);
switch(i)
{
case 1: write(); break;
case 2: xiugai(); break;
case 3: xuehaochaxun();break;
case 4: namechaxun(); break;
case 5: tongji(); break;
case 6: read(); break;
case 7: exit(0); break;
}
}
return(0);
}