我去年课程设计时作了一格学生成绩管理系统,使用了文件,
现在将代码给你,好好看看吧,有些不一样的地方自己去改一下,
但完成的功能一样
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<graphics.h>
#define length sizeof(struct student)
struct student
{int code;
char name[20];
int english;
int maths;
int computer;
int ave;
int tot;
}stud[50];
int k,n;
void begin()//创建文件,记录学生信息
{ FILE *fp;
struct student *p;
int i=0;
clrscr();
p=stud;
clrscr();
gotoxy(30,5);textcolor(5);cprintf("GEGIN");
if((fp=fopen("d:\record","rb"))==NULL)
{
fp=fopen("d:\record","wb+");
gotoxy(10,6);
textcolor(2);
cprintf("tell me how long has the list?");
gotoxy(10,7);
scanf("%d",&n);
gotoxy(10,8);
textcolor(5);
cprintf(" Code English Maths Computer Name");
textcolor(14);
gotoxy(10,9);cprintf("----------------------------------------");
for(i=0;i<n;i++)
{
gotoxy(10,i+10);textcolor(1+i);
scanf("%d %d %d %d %s",&(i+p)->code,&(p+i)->english,&(p+i)->maths,
&(p+i)->computer,(p+i)->name);
}
fwrite(&n,2,1,fp);
for(i=0;i<n;i++)
fwrite((p+i),length,1,fp) ;
fclose(fp);
}
fclose(fp);
}
void display()//分屏显示学生信息
{
FILE *fp;
struct student *p;
int i,j=0,m=0;
p=stud;
clrscr();
gotoxy(35,5);textcolor(2);cprintf("DISPLAY");
fp=fopen("d:\record" ,"rb");
fread(&n,2,1,fp);
gotoxy(10,6);
textcolor(13);
cprintf("the list has %d records",n);
if(n%8==0)
k=n/8;
else
k=1+n/8;
if(k==1) {
for(i=0;i<n;i++)
{ gotoxy(10,7);
textcolor(5);
cprintf("Code Name English Maths Computer Tot Ave");
gotoxy(10,8);
textcolor(14);
cprintf("-------------------------------------------------->");
fread((p+i),length ,1,fp);
gotoxy(10,i+9);
textcolor(1+i);
cprintf("%d %s %d %d %d %d %d",(p+i)->code,(p+i)->name,
(p+i)->english,(p+i)->maths,(p+i)->computer,(p+i)->tot ,(p+i)->ave);
}
}
else {
for(j=0;j<k-1;j++)
{
gotoxy(10,7);
textcolor(5);
cprintf("code name english maths computer tot ave");
gotoxy(10,8);
textcolor(14);
cprintf("-------------------------------------------------->");
m=0;
for(i=j*8;i<8*j+8;i++)
{
fread((p+i),length ,1,fp);
gotoxy(10,m+9); textcolor(1+i);
cprintf("%d %s %d %d %d %d %d",(p+i)->code,(p+i)->name, (p+i)->english,
(p+i)->maths,(p+i)->computer,(p+i)->tot ,(p+i)->ave);
m++;
}
getch();
clrscr();
}
for(i=8*j;i<8*j+n;i++)
{ m=0;
gotoxy(10,7);
textcolor(5);
cprintf("Code Name English Maths Computer Tot Ave");
gotoxy(10,8);
textcolor(4);
fread((p+i),length ,1,fp);
gotoxy(10,m+9); textcolor(i+1);
cprintf("%d %s %d %d %d %d %d",(p+i)->code,(p+i)->name,
(p+i)->english,(p+i)->maths,(p+i)->computer,(p+i)->tot ,(p+i)->ave);
m++;
}
}
fclose(fp);
gotoxy(10,22);
textcolor(89999);cprintf("press any key to exit");
getch();
}
void insert()//增加学生信息记录
{ FILE *fp;
struct student *p;
int i,j,t;
int m;
p=stud;
clrscr();
gotoxy(20,5); textcolor(2);cprintf("Insert");
fp=fopen("D:\record","rb");
fread(&n,2,1,fp);
gotoxy(10,6);
textcolor(13);
cprintf("the list has %d records",n);
gotoxy(10,7);
textcolor(4);
cprintf("put the number that you want to add:") ;
gotoxy(10,8);
textcolor(5);
j=n;
fclose(fp);
scanf("%d",&t);
n=n+t;
gotoxy(10,9);
textcolor(5);
cprintf("code english maths computer name");
gotoxy(10,10);
textcolor(14);
cprintf("-------------------------------------------------->");
for(i=0;i<t;i++)
{ gotoxy(10,i+11);
scanf("%d %d %d %d %s",&(p+i+j)->code,&(p+i+j)->english,
&(p+i+j)->maths,&(p+i+j)->computer,(p+i+j)->name);
}
fp=fopen("d:\record","rb+");
fwrite(&n,2,1,fp);
fclose(fp);
fp=fopen("d:\record","ab+");
fseek(fp,j*sizeof(struct student)+2,0);
for(i=0;i<t;i++)
fwrite((p+i+j),length,1,fp);
fclose(fp);
}
int search ()//在文件中查找学生信息
{ FILE *fp;
struct student *p;
int num,i,t=-1;
p=stud;
clrscr();
gotoxy(30,5);textcolor(5);cprintf("SEARTH");
fp=fopen("d:\record" ,"rb");
fread(&n,2,1,fp);
gotoxy(10,6);
textcolor(13);
cprintf("the list has %d records",n);
gotoxy(10,8);
textcolor(2);
cprintf("put the number you want search:");
gotoxy(10,9);
scanf("%d",&num);
for(i=0;i<n;i++)
{
fread((p+i),length,1,fp);
if((p+i)->code==num)
{
gotoxy(10,10);textcolor(13);
t=i+1;
cprintf("the record which you want to searth is %dth !!!",t);
gotoxy(10,11);
textcolor(5);
cprintf("code name english maths computer tot ave");
gotoxy(10,12);
textcolor(14);
cprintf("-------------------------------------------------->");
gotoxy(10,13);
cprintf("%d %s %d %d %d ",(p+i)->code,
(p+i)->name, (p+i)->english,(p+i)->maths,(p+i)->computer);
}
}
if(t==-1)
{ gotoxy(10,13);
textcolor(5);
cprintf("sorry ,can not find the record,please display the record:");
t=-1;
}
fclose(fp);
gotoxy(10,22) ;
textcolor(89999);
cprintf("press any key to exit");
getch();
return t;
}
void delete1()//删除学生信息
{ FILE *fp,*fp1;
struct student *p,*p1;
int i=0,t,j=0 ;
char ch;
clrscr();
gotoxy(30,5);
textcolor(2);
cprintf("DELETE");
p=stud; p1=stud;
fp=fopen("d:\record" ,"rb");
t=search();
fread(&n,2,1,fp);
printf("the list has %d records",n);
fclose(fp);
if(t!=-1)
{ gotoxy(10,15);
textcolor(2);
cprintf("did you want delete %dth record really?",t);
gotoxy(10,17);textcolor(5);
cprintf("please enter your choice Y or N ? ");
gotoxy(25,18);
ch=getch();
if(ch!=n)
{n-- ;
fp=fopen("d:\record","rb");
for(i=0;i<t-1;i++)
{fread((p+i),length,1,fp);
(p1+j)->code=(p+i)->code;
strcpy((p1+j)->name,(p+i)->name);
(p1+j)->english=(p+i)->english;
(p1+j)->computer=(p+i)->computer;
(p1+j)->maths=(p+i)->maths;
(p1+j)->tot=(p+i)->tot;
(p1+j)->ave=(p+i)->ave;
j++;
}
fread((p+i),length,1,fp);
for(i=t;i<n+1;i++)
{
fread((p+i),length,1,fp);
(p1+j)->code=(p+i)->code;
strcpy((p1+j)->name,(p+i)->name);
(p1+j)->english=(p+i)->english;
(p1+j)->computer=(p+i)->computer;
(p1+j)->maths=(p+i)->maths;
(p1+j)->tot=(p+i)->tot;
(p1+j)->ave=(p+i)->ave;
j++;
}
fclose(fp);
fp1=fopen("d:\temp","wb");
fwrite(&n,2,1,fp1);
for(i=0;i<j;i++)
fwrite((p1+i),length,1,fp1);
fclose(fp1);
remove("d:\record");
rename("d:\temp","d:\record");
}
}
getch();
}
void modify()//修改学生信息,并保存至文件
{
FILE *fp;
struct student *p;
int t,i;
char ch;
clrscr();
gotoxy(30,5);textcolor(14);cprintf("MODIFY");
p=stud;
fp=fopen("d:\record","rb");
fread(&n,2,1,fp);
t=search();
if(t!=-1)
{
gotoxy(10,15); textcolor(5);
cprintf("did you want delete %dth record really?",t);
gotoxy(10,16);textcolor(4);
cprintf("please enter your choice Y or N ?");
gotoxy(10,17); ch=getch();
if(ch!=n)
{ fread((p+t-1),length,1,fp);
gotoxy(22,16);textcolor(2);
gotoxy(10,18);
cprintf("Code English Maths Computer Name");
textcolor(14);
gotoxy(10,19);cprintf("----------------------------------------");
gotoxy(10,20);
scanf("%d %d %d %d %s",&(p+t-1)->code, &(p+t-1)->english,
&(p+t-1)->maths,&(p+t-1)->computer,(p+t-1)->name);
}
}
fclose(fp);
fp=fopen("d:\record","wb") ;
fwrite(&n,2,1,fp);
for(i=0;i<n;i++)
fwrite((p+i),length,1,fp);
fclose(fp);
getch();
}
void count()//计算学生成绩,并写入文件
{ FILE *fp;
struct student *p;
int E,M,C,T,A,NUM;
char NAME[20];
int i,j;
gotoxy(30,5);textcolor(4);cprintf("COUNT");
p=stud;
fp=fopen("d:\record" ,"rb");
fread(&n,2,1,fp);
for(i=0;i<n;i++)
{ fread((p+i),length,1,fp);
(p+i)->tot=(p+i)->computer +(p+i)->english+(p+i)->maths;
(p+i)->ave=(p+i)->tot/3;
}
for(j=0;j<n;j++)
{fread(p,length,n,fp);
for(i=0;i<n-j-1;i++)
if((p+i)->tot>(p+i+1)->tot)
{ NUM=(p+i)->code;(p+i)->code=(p+i+1)->code;(p+i+1)->code=NUM;
E=(p+i)->english;(p+i)->english=(p+i+1)->english;(p+i+1)->english=E;
M=(p+i)->maths;(p+i)->maths=(p+i+1)->maths;(p+i+1)->maths=M;
C=(p+i)->computer;(p+i)->computer=(p+i+1)->computer;(p+i+1)->computer=C;
T=(p+i)->tot; (p+i)->tot=(p+i+1)->tot;(p+i+1)->tot=T;
A=(p+i)->ave;(p+i)->ave=(p+i+1)->ave;(p+i+1)->ave=A;
strcpy(NAME,(p+i)->name); strcpy((p+i)->name,(p+i+1)->name);strcpy((p+i+1)->name,NAME);
}
}
fclose(fp);
fp=fopen("d:\record","wb");
fwrite(&n,2,1,fp);
for(i=0;i<n;i++)
fwrite((p+i),length,1,fp);
fclose(fp);
display();
}
void menu()//菜单
{ int t;
gotoxy(35,6);
cprintf("menu");
gotoxy(20,9);
textcolor(4);cprintf("1:dispaly");
gotoxy(50,9);
textcolor(4); cprintf("2:insert");
gotoxy(20,12);
textcolor(2); cprintf("3:search");
gotoxy(50,12);
textcolor(2); cprintf("4:delete1");
gotoxy(20,15);
textcolor(9); cprintf("5:modify");
gotoxy(50,15);
textcolor(9);cprintf("6:count");
gotoxy(32,18);
textcolor(3); cprintf("0-------exit");
gotoxy(24,20);
textcolor(13); cprintf("please chose the number:0----6");
cprintf("
");
gotoxy(24,22);
textcolor(89998);cprintf(" enter the number: ");
}
main()
{ int driver ,mode,t;
driver=DETECT;
mode=0;
initgraph(&driver ,&mode, "C:\tc20");
restorecrtmode();
begin();
clrscr();
while(1)
{menu();
while(1)
{scanf("%d",&t);
if(t>=0&&t<=6)
break;
}
switch(t)
{
case 1: display(); clrscr(); break;
case 2: insert(); clrscr(); break;
case 3: search(); clrscr(); break;
case 4: delete1(); clrscr(); break;
case 5: modify(); clrscr(); break;
case 6: count(); clrscr(); break;
case 0: exit(0);
}
}
getch();
}