#include "conio.h" #include "malloc.h" #include "stdio.h" #define back textbackground struct student {int num; char name[8]; float c_program; float english; float total; float avg; struct student *next;}; typedef struct student stu; int first=1; int dqx=1; stu *head,*wei; FILE *fp; char filename[30]; char a[]="新建",b[]="打开",c[]="保存",d[]="添加",e[]="显示",f[]="查找_学号", g[]="查找_分数",h[]="排序_学号",i[]="排序_分数",j[]="删除",k[]="退出";
void clsuser() {window(1,1,64,25); back(0); textcolor(7); clrscr(); }
void winone(int qian,int bei,int xiang) {window(65,5+xiang,79,5+xiang); back(bei); textcolor(qian); clrscr(); switch(xiang) {case 1: cprintf("%s",a); break; case 2: cprintf("%s",b); break; case 3: cprintf("%s",c); break; case 4: cprintf("%s",d); break; case 5: cprintf("%s",e); break; case 6: cprintf("%s",f); break; case 7: cprintf("%s",g); break; case 8: cprintf("%s",h); break; case 9: cprintf("%s",i); break; case 10: cprintf("%s",j); break; case 11: cprintf("%s",k); break;} }
void createuser() {int ii; for (ii=0;ii<30;ii++) filename[ii]='\0'; clsuser(); window(10,10,50,10); back(4); textcolor(7); clrscr(); cprintf("请输入新建学生库名:"); scanf("%s",filename); fp=fopen(filename,"w+"); }
void wintwo(l1,h1,l2,h2) {window(l1,h1,l2,h2); back(4); textcolor(7); clrscr(); }
void openuser() {stu *zhen; int ii; for (ii=0;ii<30;ii++) filename[ii]='\0'; if (fp!=NULL) fclose(fp); clsuser(); wintwo(10,10,50,10); cprintf("输入要打开的学生库名:"); scanf("%s",filename); fp=fopen(filename,"r+"); if (fp==NULL) {wintwo(15,12,36,12); cprintf("File not find!");} else {wei=(stu *)malloc(sizeof(stu)); head=NULL; head=zhen=wei; do {fread(wei,sizeof(stu),1,fp); if (feof(fp)) break; wei->next=(stu *)malloc(sizeof(stu)); zhen=wei; wei=wei->next;} while (!feof(fp)); wei=zhen; wei->next=NULL; rewind(fp); first=0;} }
void saveuser() {stu *zhen; int ii; zhen=wei=head; if (fp!=NULL) {fclose(fp); if (remove(filename)!=0) printf("保存文件可能失败!"); fp=fopen(filename,"w+"); while(wei!=NULL) {fwrite(wei,sizeof(stu),1,fp); zhen=wei; wei=wei->next;} wei=zhen; fclose(fp); fp=fopen(filename,"r+"); } }
void adduser() {int l1=2,h1=2,l2=4,h2=2; float nn; int ii; char cchh[8]; char hc; clsuser(); while(1) {wintwo(l1+5,h1,l2+9,h2); wintwo(l1+5+9,h1,l2+5+9,h2); wintwo(l1+14+5,h1,l2+14+5,h2); wintwo(l1+19+5,h1,l2+19+8,h2); wintwo(l1+24+8,h1,l2+24+10,h2); wintwo(l1,h1,l2,h2); if (first==1) {first=0; head=NULL; wei=(stu *)malloc(sizeof(stu)); head=wei;} else {wei->next=(stu *)malloc(sizeof(stu)); wei=wei->next;} while(1) {switch(l1) {case 2: scanf("%d",&wei->num);l1=l1+5;l2=l2+5;wintwo(l1,h1,l2,h2); break; case 7: scanf("%s",wei->name);l1=l1+9;l2=l2+9;wintwo(l1,h1,l2,h2); break; case 16: scanf("%f",&nn);wei->c_program=nn; l1=l1+5;l2=l2+5;wintwo(l1,h1,l2,h2);break; case 21: scanf("%f",&nn);wei->english=nn; l1=l1+5;l2=l2+8;wintwo(l1,h1,l2,h2); break; } if (l1==26) break;} wei->total=wei->c_program+wei->english; cprintf("%5.1f",wei->total); l1=l1+8; l2=l2+10; wintwo(l1,h1,l2,h2); wei->avg=wei->total/2; cprintf("%5.1f",wei->avg); wintwo(43,h1,60,h2); cprintf("回车继续,其他退出"); hc=getch(); if (hc==13) {h1++; h2++; l1=2;l2=4;} else break; } wei->next=NULL; }
void shuchu(stu *sc) {cprintf("%4d",sc->num); cprintf("%8s",sc->name); cprintf("%5.1f",sc->c_program); cprintf("%5.1f",sc->english); cprintf("%5.1f",sc->total); cprintf("%5.1f",sc->avg); }
void xianshiuser() {stu *xs; int h1=1,h2=1; xs=head; clsuser(); while(xs!=NULL) {wintwo(1,h1,55,h2); h1++; h2++; shuchu(xs); xs=xs->next; if (h1>=25) {wintwo(45,25,60,25); cprintf("pree any key"); getch(); wintwo(1,1,64,25); h1=h2=1;} } }
void numsearch() {int start,end,ii; stu *zhen; zhen=head; clsuser(); wintwo(10,5,30,5); cprintf("查找的学号from:"); scanf("%d",&start); wintwo(32,5,40,5); cprintf("to:"); scanf("%d",&end);
window(1,1,64,25); back(0); textcolor(7); clrscr(); if (start>end) {wintwo(10,7,19,7); cprintf("not find");} else {while(zhen!=NULL) {if (zhen->num>=start && zhen->num<=end) {ii=1; cprintf("\n\r"); shuchu(zhen);} zhen=zhen->next; } } if (ii==0) {wintwo(10,7,19,7); cprintf("not find");} }
void fensearch() {stu *zhen; wintwo(1,1,25,1); clsuser(); cprintf("平均分<60如下:"); zhen=head; while(zhen!=NULL) {cprintf("\n\r"); if (zhen->num<60) shuchu(zhen); zhen=zhen->next;} }
void numsort() {stu *head2,*zhen,*p2,*min,*zhen2; int nn=0; clsuser(); while(head!=NULL) { zhen=min=wei=head;
while(wei->next!=NULL) {zhen2=wei; wei=wei->next; if (min->num>wei->num) {zhen=zhen2; min=wei;} }
if (nn==0) {head2=p2=min; if (head==min) head=min->next; else zhen->next=wei->next; p2->next=NULL; nn=1;} else {if (head==min) {p2->next=head; head=min->next; p2=p2->next; p2->next=NULL; } else {p2->next=min; zhen->next=min->next; p2=p2->next; p2->next=NULL; } }
printf("1"); } printf("2222"); p2->next=NULL; head=head2; }
void fensort() {stu *head2,*zhen,*p2,*min,*zhen2,*zhen3; int nn=0; clsuser(); while(head!=NULL) { zhen=min=wei=head;
while(wei->next!=NULL) {zhen2=wei; wei=wei->next; if (min->avg>wei->avg) {zhen=zhen2; min=wei;} }
if (nn==0) {head2=p2=min; if (head==min) head=min->next; else zhen->next=min->next; p2->next=NULL; nn=1;} else {if (head==min) {p2->next=head; head=min->next; p2=p2->next; p2->next=NULL; } else {p2->next=min; zhen->next=min->next; p2=p2->next; p2->next=NULL; } }
printf("1"); } printf("2222"); p2->next=NULL; head=head2;
}
void del() {int del; stu *zhen,*p2; p2=zhen=head; zhen=zhen->next; clsuser(); wintwo(10,5,30,5); cprintf("输入要删的学号:"); scanf("%d",&del);
if (head!=NULL) {zhen=head; while(del!=zhen->num && zhen->next!=NULL) {p2=zhen; zhen=zhen->next;} } if (del==zhen->num) {if (zhen==head) {head=zhen->next; free(p2); } else p2->next=zhen->next; } }
void select() {char cc; while(1) {cc=getch(); if (cc==27) break; if (cc==13) {switch(dqx) {case 1: createuser();break; case 2: openuser(); break; case 3: saveuser(); break; case 4: adduser(); break; case 5: xianshiuser(); break; case 6: numsearch(); break; case 7: fensearch(); break; case 8: numsort(); break; case 9: fensort(); break; case 10: del(); break; case 11: if (fp!=NULL) fclose(fp); exit(0);}} if (cc==0) {cc=getch(); if (cc==72) if (dqx>1) {winone(15,1,dqx); dqx--; winone(1,15,dqx);} if (cc==80) if (dqx<11) {winone(15,1,dqx); dqx++; winone(1,15,dqx);} } } }
void caidan() {int ii; for (ii=2;ii<=11;ii++) winone(7,1,ii); winone(1,7,1); }
void main() {window(1,1,80,25); back(0); clrscr(); caidan(); select(); }
| | |