#include
#include
#include
#include
typedef struct /*定义结构体数组*/
{
char num[10]; /*学号*/
char name[20]; /*姓名*/
int score; /*成绩*/
}Student;
Student stu[80]; /*结构体数组变量*/
int menu_select() /*菜单函数*/
{
char c;
do{
system("cls"); /*运行前清屏*/
printf("\t\t****Students' Grade Management System****\n"); /*菜单选择*/
printf("\t\t | 1. Input Records |\n");
printf("\t\t | 2. Display All Records |\n");
printf("\t\t | 3. Sort |\n");
printf("\t\t | 4. Insert a Record |\n");
printf("\t\t | 5. Delete a Record |\n");
printf("\t\t | 6. Query |\n");
printf("\t\t | 7. Statistic |\n");
printf("\t\t | 8. Add Records from a Text File|\n");
printf("\t\t | 9. Write to a Text file |\n");
printf("\t\t | 0. Quit |\n");
printf("\t\t*****************************************\n");
printf("\t\t\tGive your Choice(0-9):");
c=getchar(); /*读入选择*/
}while(c<'0'||c>'9');
return(c-'0'); /*返回选择*/
}
int Input(Student stud[],int n) /*输入若干条记录*/
{int i=0;
char sign,x[10]; /*x[10]为清除多余的数据所用*/
while(sign!='n'&&sign!='N') /*判断*/
{ printf("\t\t\tstudent's num:"); /*交互输入*/
scanf("\t\t\t%s",stud[n+i].num);
printf("\t\t\tstudent's name:");
scanf("\t\t\t%s",stud[n+i].name);
printf("\t\t\tstudent's score:");
scanf("\t\t\t%d",&stud[n+i].score);
gets(x); /*清除多余的输入*/
printf("\t\t\tany more records?(Y/N)");
scanf("\t\t\t%c",&sign); /*输入判断*/
i++;
}
return(n+i);
}
void Display(Student stud[],int n) /*显示所有记录*/
{
int i;
printf("\t\t\t-----------------------------------\n"); /*格式头*/
printf("\t\t\tnumber name score\n");
printf("\t\t\t-----------------------------------\n");
for(i=1;i
printf("\t\t\t%-16s%-15s%d\n",stud[i-1].num,stud[i-1].name,stud[i-1].score);
if(i>1&&i%10==0) /*每十个暂停*/
{printf("\t\t\t-----------------------------------\n"); /*格式*/
printf("\t\t\t");
system("pause");
printf("\t\t\t-----------------------------------\n");
}
}
printf("\t\t\t");
system("pause");
}
void Sort_by_num(Student stud[],int n) /*按学号排序*/
{ int i,j,*p,*q,s;
char t[10];
for(i=0;i
{strcpy(t,stud[j+1].num);
strcpy(stud[j+1].num,stud[j].num);
strcpy(stud[j].num,t);
strcpy(t,stud[j+1].name);
strcpy(stud[j+1].name,stud[j].name);
strcpy(stud[j].name,t);
p=&stud[j+1].score;
q=&stud[j].score;
s=*p;
*p=*q;
*q=s;
}
}
int Insert_a_record(Student stud[],int n) /*插入一条记录*/
{char x[10]; /*清除多余输入所用*/
printf("\t\t\tstudent's num:"); /*交互式输入*/
scanf("\t\t\t%s",stud[n].num);
printf("\t\t\tstudent's name:");
scanf("\t\t\t%s",stud[n].name);
printf("\t\t\tstudent's score:");
scanf("\t\t\t%d",&stud[n].score);
gets(x);
n++;
Sort_by_num(stud,n); /*调用排序函数*/
printf("\t\t\tInsert Successed!\n"); /*返回成功信息*/
return(n);
}
int Delete_a_record(Student stud[],int n) /*按姓名查找,删除一条记录*/
{ char s[20];
int i=0,j;
printf("\t\t\ttell me his(her) name:"); /*交互式问寻*/
scanf("%s",s);
while(strcmp(stud[i].name,s)!=0&&i
{ printf("\t\t\tnot find!\n"); /*返回失败信息*/
return(n);
}
for(j=i;j
strcpy(stud[j].num,stud[j+1].num);
strcpy(stud[j].name,stud[j+1].name);
stud[j].score=stud[j+1].score;
}
printf("\t\t\tDelete Successed!\n"); /*返回成功信息*/
return(n-1);
}
void Query_a_record(Student stud[],int n) /*查找并显示一个记录*/
{ char s[20];
int i=0;
printf("\t\t\tinput his(her) name:"); /*交互式输入*/
scanf("\t\t\t%s",s);
while(strcmp(stud[i].name,s)!=0&&i
{ printf("\t\t\tnot find!\n"); /*输入失败信息*/
return;
}
printf("\t\t\this(her) number:%s\n",stud[i].num); /*输出该学生信息*/
printf("\t\t\this(her) score:%d\n",stud[i].score);
}
void Statistic(Student stud[],int n) /*新增功能,输出统计信息*/
{ int i,j=0,k=0,sum=0;
float aver; /*成绩平均值*/
for(i=0;i
sum+=stud[i].score;
if(stud[j].score>stud[i].score) j=i;
if(stud[k].score
aver=1.0*sum/n;
printf("\t\t\tthere are %d records.\n",n); /*总共记录数*/
printf("\t\t\tthe hignest score:\n"); /*最高分*/
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[j].num,stud[j].name,stud[j].score);
printf("\t\t\tthe lowest score:\n"); /*最低分*/
printf("\t\t\tnumber:%s name:%s score:%d\n",stud[k].num,stud[k].name,stud[k].score);
printf("\t\t\tthe average score is %5.2f\n",aver); /*平均分*/
}
int AddfromText(Student stud[],int n) /*从文件中读入数据*/
{ int i=0,num;
FILE *fp; /*定义文件指针*/
char filename[20]; /*定义文件名*/
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename); /*输入文件名*/
if((fp=fopen(filename,"rb"))==NULL) /*打开文件*/
{ printf("\t\t\tcann't open the file\n"); /*打开失败信息*/
printf("\t\t\t");
system("pause");
return(n);
}
fscanf(fp,"%d",&num); /*读入总记录量*/
while(i
fscanf(fp,"%s%s%d",stud[n+i].num,stud[n+i].name,&stud[n+i].score);
i++;
}
n+=num;
fclose(fp); /*关闭文件*/
printf("\t\t\tSuccessed!\n");
printf("\t\t\t");
system("pause");
return(n);
}
void WritetoText(Student stud[],int n) /*将所有记录写入文件*/
{
int i=0;
FILE *fp; /*定义文件指针*/
char filename[20]; /*定义文件名*/
printf("\t\t\tWrite Records to a Text File\n"); /*输入文件名*/
printf("\t\t\tInput the filename:");
scanf("\t\t\t%s",filename);
if((fp=fopen(filename,"w"))==NULL) /*打开文件*/
{
printf("\t\t\tcann't open the file\n");
system("pause");
return;
}
fprintf(fp,"%d\n",n); /*循环写入数据*/
while(i
fprintf(fp,"%-16s%-15s%d\n",stud[i].num,stud[i].name,stud[i].score);
i++;
}
fclose(fp); /*关闭文件*/
printf("Successed!\n"); /*返回成功信息*/
}
void main() /*主函数*/
{
int n=0;
for(;;)
{
switch(menu_select()) /*选择判断*/
{
case 1:
printf("\t\t\tInput Records\n"); /*输入若干条记录*/
n=Input(stu,n);
break;
case 2:
printf("\t\t\tDisplay All Records\n"); /*显示所有记录*/
Display(stu,n);
break;
case 3:
printf("\t\t\tSort\n");
Sort_by_num(stu,n); /*按学号排序*/
printf("\t\t\tSort Suceessed!\n");
printf("\t\t\t");
system("pause");
break;
case 4:
printf("\t\t\tInsert a Record\n");
n=Insert_a_record(stu,n); /*插入一条记录*/
printf("\t\t\t");
system("pause");
break;
case 5:
printf("\t\t\tDelete a Record\n");
n=Delete_a_record(stu,n); /*按姓名查找,删除一条记录*/
printf("\t\t\t");
system("pause");
break;
case 6:
printf("\t\t\tQuery\n");
Query_a_record(stu,n); /*查找并显示一个记录*/
printf("\t\t\t");
system("pause");
break;
case 7:
printf("\t\t\tStatistic\n");
Statistic(stu,n); /*新增功能,输出统计信息*/
printf("\t\t\t");
system("pause");
break;
case 8:
printf("\t\t\tAdd Records from a Text File\n");
n=AddfromText(stu,n); /*新增功能,输出统计信息*/
break;
case 9:
printf("\t\t\tWrite to a Text file\n");
WritetoText(stu,n); /*循环写入数据*/
printf("\t\t\t");
system("pause");
break;
case 0:
printf("\t\t\tHave a Good Luck,Bye-bye!\n"); /*结束程序*/
printf("\t\t\t");
system("pause");
exit(0);
}
}
}
鄙人大一时的课程设计
/*TopBoy出品,品质保证*/
/*如果编译失败可将第一句#include
#include
#include
#include
#include
#include
#define NULL 0
#define F10 17408
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define SPACE 0x3920
#define ESC 0x011b
#define ENTER 0x1c0d
#define LEN sizeof(struct student)
struct student *insert(struct student *head);
struct student *print(struct student *head);
struct student *change(struct student *head,int);
struct student *del(struct student *head,int);/*删除记录*/
struct student *search(struct student *head);
void stat(struct student *head);
void save(struct student *head);
void printrecord(struct student *head);
void menuinit(void);
struct student *order(struct student *head);
struct student
{
long num;
char name[20];
char sex[7];
int score;
struct student *next;
};
int n; /*记录总数*/
int x=1,y=1,max;/*x表示显示记录的页数 y表示该页第y个记录*/
char *menutext[]={"Order",
"Insert",
"Change",
"Delete",
"Search",
"Statis",
"Quit"};
void init(void) /* 图形初始化 */
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
}
void screeninit()
{
int i;
window(1,2,80,25);
textbackground(BLUE);
textcolor(7);
clrscr();
gotoxy(1,2);
putch(0xc9);/*输出左上角边框┏*/
for(i=1;i<79;i++)putch(0xcd); /*输出上边框水平线*/
putch(0xbb); /*输出右上角边框 ┓*/
for(i=3;i<24;i++)
{
gotoxy(1,i);putch(0xba); /*输出左垂直线*/
gotoxy(80,i);putch(0xba); /*输出右垂直线*/
}
gotoxy(1,24);putch(0xc8); /*输出左下角边框┗*/
for(i=1;i<79;i++)putch(0xcd); /*输出下边框水平线*/
putch(0xbc); /*输出右下角边框┛*/
textbackground(7);
textcolor(BLACK);
window(1,25,80,25);
clrscr();
cputs(" You can press F10 to use Menu or ESC to quit.");
}
int chooseab(int locx,int locy,char str1[],char str2[])
{
int i,text1=4,text2=14,back1=15,back2=1,key;
while(1)
{
window(locx,locy,locx+strlen(str1)-1,locy);
textbackground(back1);textcolor(text1);
clrscr();gotoxy(1,1);printf("%s",str1);
window(locx+strlen(str1)+3,locy,locx+strlen(str1)+strlen(str2)+2,locy);
textbackground(back2);textcolor(text2);clrscr();gotoxy(1,1);printf("%s",str2);
key=bioskey(0);
if(key==LEFT||key==RIGHT)
{
i=text1;text1=text2;text2=i;
i=back1;back1=back2;back2=i;
}
else if(key==ENTER)break;
}
window(59,5,74,20);
if(text1==4)return 1;
else return 2;
}
void menuinit()
{
int i;
window(1,1,80,1);
textbackground(7);
textcolor(BLACK);
clrscr();
for(i=0;i<7;i++)
{
gotoxy(i*10+5,1);
cprintf("%s",menutext[i]);
}
}
struct student *menu(struct student *head,int choose)
{
int i=0,ii=0,key;
window(1,1,80,1);
gotoxy(i*10+5,1);
textbackground(BLACK);
textcolor(WHITE);
cprintf("%s",menutext[i]);
for(;;)
{
switch(key)
{
case LEFT:
{
ii=i-1;
if(ii<0)ii=6;
break;
}
case RIGHT:
{
ii=i+1;
if(ii>6)ii=0;
break;
}
}
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf("%s",menutext[i]);
gotoxy(ii*10+5,1);
textbackground(BLACK);
textcolor(WHITE);
cprintf("%s",menutext[ii]);
i=ii;
key=bioskey(0);
if(key==ESC)
{
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf("%s",menutext[i]);
break;
}
if(key==ENTER)
{
switch(i)
{
case 0:head=order(head);break;
case 1:head=insert(head);break;
case 2:change(head,choose);break;
case 3:head=del(head,choose);break;
case 4:search(head);break;
case 5:stat(head);break;
case 6:save(head);break;
}
menuinit();
printrecord(head);
break;
}
}
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
return(head);
}
int aboutpro()
{
int i;
window(58,4,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
putch(218);
for(i=3;i<20;i++)putch(196); /*输出上边框水平线*/
putch(191);
for(i=2;i<20;i++)
{
gotoxy(1,i);putch(179); /*输出左垂直线*/
gotoxy(19,i);putch(179); /*输出右垂直线*/
}
gotoxy(1,19);putch(192);
for(i=3;i<20;i++)putch(196);
putch(217);
gotoxy(3,4);cputs("Version:V2.0");
gotoxy(3,6);cputs("Author:");
gotoxy(3,7);cputs("Zhang Shou Song");
gotoxy(5,9);cputs("NEU C.S. 053");
window(1,1,80,1);
}
struct student *order(struct student *head)
{
struct student *p,*q,*t,*h1;
h1=head->next;
head->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=head;
q=head;
while(t->score
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
head=t;
}
else
{
t->next=p;
q->next=t;
}
}
return head;
}
struct student *insert(struct student *head)
{
struct student *p0,*p1,*p2;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
while(1)
{
clrscr();
p0=head;
p1=(struct student *)malloc(LEN);
gotoxy(5,1);printf("Insert");
gotoxy(1,3);printf("Quit by num=0");
gotoxy(1,5);printf("number:");
scanf("%ld",&p1->num);
if(p1->num==0)break;
gotoxy(1,6);printf("name:");
scanf("%s",p1->name);
gotoxy(1,7);printf("sex:");
scanf("%s",&p1->sex);
do{
gotoxy(1,8);printf("score:");
scanf("%d",&p1->score);
}while(p1->score<0||p1->score>100);
if(head==NULL)
{
head=p1;
p1->next=NULL;
}
else
{
while((p1->score
{
p2=p0;
p0=p0->next;
}
if(p1->score>=p0->score)
{
if(head==p0)head=p1;
else p2->next=p1;
p1->next=p0;
}
else
{
p0->next=p1;
p1->next=NULL;
}
}
n++;
}
aboutpro();
clrscr();
return(head);
}
void printrecord(struct student *head)
{
struct student *p;
int i,j;
window(3,7,51,21);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
p=head;
for(i=1;i
p=p->next;
y=1;max=1;
if(head!=NULL)
do
{ gotoxy(3,y);
printf("%-8d%-9ld%-16s%-8s%-7d\n",y+(x-1)*15,p->num,p->name,p->sex,p->score);
p=p->next;
y++;
max=y-1;
if(y>15)
{
max=15;
break;
}
}while(p!=NULL);
window(2,3,78,23);
}
void searchresult(struct student *p)
{
window(4,7,50,20);
textbackground(BLUE);
textcolor(YELLOW);
if(y==1)clrscr();
gotoxy(2,y);
printf("%-8d %-9ld%-16s%-8s%-7d\n",y,p->num,p->name,p->sex,p->score);
}
struct student *chooserecord(struct student *head)
{
int locate=1,key;
gotoxy(2,locate+4);putch(16);gotoxy(2,locate+4);
for(;;)
{
switch(key)
{
case UP:
{
locate--;
if(locate<1&&n<=15)locate=max;
if(locate<1&&n>15)
{
x--;
if(x<1)x=n/15+1;
printrecord(head);
locate=max;
}
break;
}
case DOWN:
{
locate++;
if(locate>max&&n<=15)locate=1;
if(locate>max&&n>15)
{
x++;
if(x>(n/15+1))x=1;/*判断是否到达页尾*/
locate=1;
printrecord(head);
}
break;
}
case F10:{head=menu(head,locate+(x-1)*15);break;}
}
putch(0);
gotoxy(2,locate+4);putch(16);gotoxy(2,locate+4);
key=bioskey(0);
if(key==ESC)save(head);
}
}
struct student *print(struct student *head)
{
window(2,3,78,23);
gotoxy(3,1);printf("******************** Student's Score Manage System **********************\n");
gotoxy(3,2);printf("| record | number | name | sex | score |\n");
gotoxy(3,3);printf("|--------|--------|---------------|-------|-------|\n");
printrecord(head);
chooserecord(head);
}
struct student *change(struct student *head,int choose)
{
struct student *p;
long stunum;
int i,key;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
gotoxy(5,1);printf("Change");
p=head;
for(i=1;i
gotoxy(1,3);printf("input new name:");
gotoxy(1,4);scanf("%s",p->name);
gotoxy(1,5);printf("input new sex:");
gotoxy(1,6);scanf("%s",p->sex);
gotoxy(1,7);printf("input new score:");
gotoxy(1,8);scanf("%d",&p->score);
gotoxy(2,10);printf("Change Success!\n");
getch();
aboutpro();
clrscr();
return(head);
}
struct student *del(struct student *head,int choose)/*删除记录*/
{
struct student *p,*q;
long stunum;
int i;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
q=p=head;
for(i=1;i
q=p;
p=p->next;
}
gotoxy(5,1);printf("Delete");
gotoxy(1,3);printf("Are you sure");/*确认信息*/
gotoxy(2,4);printf(" to delete it?");
if(chooseab(62,11,"Yes","No")==1)
{
if(p==head)head=p->next;
else q->next=p->next;
free(p); /*释放内存*/
n--;
}
aboutpro();
clrscr();
return(head);
}
struct student *search(struct student *head)
{
struct student *p;
long stunum;
char *stuname;
int i,ii,score1,score2,key;
char *searchwith[]={"num","name","score"};
y=1;max=0;
window(59,5,74,20);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
textbackground(BLUE);
textcolor(YELLOW);
for(i=1;i<3;i++)
{
gotoxy(i*5+2,2);
cprintf("%s",searchwith[i]);
}
gotoxy(2,2);
textbackground(RED);
textcolor(WHITE);
cprintf("%s",searchwith[0]);
i=0;ii=0;
key=0;
while (1)
{
switch(key)
{
case LEFT:
{
ii=i-1;
if(ii<0)ii=2;
break;
}
case RIGHT:
{
ii=i+1;
if(ii>2)ii=0;
break;
}
}
gotoxy(i*5+2,2);
textbackground(BLUE);
textcolor(YELLOW);
cprintf("%s",searchwith[i]);
gotoxy(ii*5+2,2);
textbackground(RED);
textcolor(WHITE);
cprintf("%s",searchwith[ii]);
i=ii;
key=bioskey(0);
if(key==ESC)
{
gotoxy(i*10+5,1);
textbackground(7);
textcolor(BLACK);
cprintf("%s",searchwith[i]);
break;
}
if(key==ENTER)
{
switch(i)
{
case 0:
{
gotoxy(1,5);printf("number:");
scanf("%ld",&stunum);
p=head;
while(1)
{
while(p->num!=stunum&&p!=NULL)p=p->next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p->next;
}
}
break;
}
case 1:
{
gotoxy(1,5);printf("name:");
gotoxy(1,6);scanf("%s",stuname);
p=head;
while(1)
{
while(strstr(p->name,stuname)==NULL&&p!=NULL)p=p->next;
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p->next;
}
}
break;
}
case 2:
{
gotoxy(1,5);printf("from score1:");scanf("%d",&score1);
gotoxy(1,6);printf("to score2:");scanf("%d",&score2);
p=head;
while(1)
{
while((p->score
if(p==NULL)break;
else
{
searchresult(p);
y++;max++;
p=p->next;
}
}
break;
}
}
break;
}
}
if(y==1){window(59,5,74,20);gotoxy(4,7);printf("Not Found");}
getch();
aboutpro();
clrscr();
}
void stat(struct student *head)
{
int i,j,begin=0,end=0,stutotal[5]={0,0,0,0,0};
int pointx1=50,pointy1=281,pointx2,pointy2;
char str[5];
struct student *p;
p=head;
for(i=0;i
if(p->score>=0&&p->score<60)stutotal[0]++;
else if(p->score>=60&&p->score<70)stutotal[1]++;
else if(p->score>=70&&p->score<80)stutotal[2]++;
else if(p->score>=80&&p->score<90)stutotal[3]++;
else if(p->score>=90&&p->score<=100)stutotal[4]++;
p=p->next;
}
init();
for(i=0;i<4;i++) /*根据比例画扇形*/
{
setfillstyle(1,i+1);
begin=end; end=end+360*stutotal[i]/n;
pieslice(200,180,begin,end,100);
if(end==360)break;
}
if(end!=360)
{ setfillstyle(1,5);
pieslice(200,180,end,360,100);
}
rectangle(340,90,560,272);
settextstyle(0,0,1);
outtextxy(350,100,"color");/*显示图示的说明*/
outtextxy(410,100,"range");
outtextxy(500,100,"number");
outtextxy(400,132,"0 to 60:");
outtextxy(400,162,"60 to 70:");
outtextxy(400,192,"70 to 80:");
outtextxy(400,222,"80 to 90:");
outtextxy(400,252,"90 to 100:");
outtextxy(420,292,"total:");
sprintf(str,"%d",n);
outtextxy(520,292,str);
for(i=0;i<5;i++)
{
sprintf(str,"%d",stutotal[i]);
outtextxy(520,132+i*30,str);
}
for(i=1;i<=5;i++)
{
setfillstyle(1,i);
bar(350,100+i*30,380,110+i*30);
}
getch();
cleardevice();
setfillstyle(1,0); /*画折线图*/
bar(100,80,300,280);
line(50,281,280,281);
line(50,281,50,80);
outtextxy(60,285,"0-60");
outtextxy(105,285,"60-70");
outtextxy(150,285,"70-80");
outtextxy(195,285,"80-90");
outtextxy(250,285,"90-100");
for(i=0;i<5;i++)
{ setfillstyle(1,i+1);
pointx2=pointx1+45; pointy2=280-200*stutotal[i]/n;
line(45,pointy2,50,pointy2);
sprintf(str,"%d",stutotal[i]);
outtextxy(30,pointy2,str);
line(pointx1,pointy1,pointx2,pointy2);
pointx1=pointx2;pointy1=pointy2;
}
line(45,80,50,80);
sprintf(str,"%d",n);
outtextxy(30,80,str);
outtextxy(30,280,"0"); getch();
closegraph();
screeninit();
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
gotoxy(3,1);printf("******************** Student's Score Manage System **********************\n");
gotoxy(3,2);printf("| record | number | name | sex | score |\n");
gotoxy(3,3);printf("|--------|--------|---------------|-------|-------|\n");
aboutpro();
clrscr();
}
void save(struct student *head)
{
FILE *fp;
int i;
struct student *p;
p=head;
fp=fopen("c:\\studin.txt","wb");
fprintf(fp,"zhangshousong"); /*将验证字符串写入文件*/
fprintf(fp,"\r\n"); /*将换行符号写入文件*/
fprintf(fp,"%d",n); /*将记录数写入文件*/
fprintf(fp,"\r\n"); /*将换行符号写入文件*/
for(i=0;i
fprintf(fp,"%-20ld%-30s%-10s%-10d",p->num,p->name,p->sex,p->score);/*格式写入记录*/
fprintf(fp,"\r\n"); /*将换行符号写入文件*/
p=p->next;
}
fclose(fp);
exit(0);
}
struct student *readdata()
{
FILE *fp;
int i;
char str[20]="zhangshousong",str2[20];
struct student *p,*q,*head;
p=(struct student *)malloc(LEN);
head=p;
if((fp=fopen("c:\\studin.txt","rb"))==NULL)/*打开文件*/
{
head=NULL;
n=0;
}
else
{
fscanf(fp,"%s",str2);
if(strcmp(str,str2)!=0)head=NULL;/*判断文件是不是合法的*/
else
{
fscanf(fp,"%d",&n); /*读入记录数*/
if(n==0)head=NULL;
else
{
for(i=0;i
fscanf(fp,"%20ld%30s%10s%10d",&p->num,p->name,p->sex,&p->score); /*按格式读入记录*/
p->next=(struct student *)malloc(LEN);
q=p;
p=p->next;
}
q->next=NULL;
}
}
}
fclose(fp);
return head;
}
void main()
{
struct student *head;
int key=0,i;
init();
closegraph();
clrscr();
head=readdata();
menuinit();/*菜单初始化*/
screeninit();
window(2,3,78,23);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
aboutpro();
head=print(head);
}