📄 stuf.c
字号:
i++;
gotoxy(43,i+5);
printf("%-6s%-15s%-8.2f%-8.2f", p->no,p->name,p->sum,p->average);
p=p->next;
}
}
/*Copy data from one file to another*/
void copy()
{
char outfile[10],infile[10];
FILE *sfp,*tfp;
STUDENT *p=NULL;
puttext(2,2,22,8,ssmenu);
BlackText(0,1,"File");
BlackText(0,6,"Copy file ");
RedText(0,3,"Load ");
DrawFrame(4,7,60,9,0,7);
gotoxy(5,8);
cprintf("Enter infile name,for example c:\\f1\\te.txt:");
gotoxy(49,8);
printf(" ");
gotoxy(49,8);
scanf("%s",infile);
if((sfp=fopen(infile,"rb"))==NULL)
{
gotoxy(5,8);
cprintf("can not open input file ");
getch();
}
else
{
gotoxy(5,8);
cprintf("Enter outfile name,for example c:\\f1\\te.txt: ");
gotoxy(50,8);
printf(" ");
gotoxy(50,8);
scanf("%s",outfile);
if((tfp=fopen(outfile,"wb"))==NULL)
{
gotoxy(5,8);
cprintf("can not open output file ");
getch();
}
else
{
while(!feof(sfp))
{
if(1!=fread(p,sizeof(STUDENT),1,sfp))
break;
fwrite(p,sizeof(STUDENT),1,tfp);
}
fclose(sfp);
fclose(tfp);
gotoxy(5,8);
cprintf("File copied from \"%s\" to \"%s\" !! ",infile,outfile);
getch();
}
}
}
/*Update a record*/
STUDENT *update(STUDENT *h)
{
int i; int s;
STUDENT *p;
char n[11];
char *ch[]={"Student Number: ","Student Name: ","Score1: ","Score2: ","Score3: "};
gotoxy(2,11);
printf("Input No. you want to update: ");
scanf("%s",n);
p=h;
while(strcmp(p->no,n)&&p!=NULL)
{
p=p->next;
}
if(p==NULL)
{
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("list no %s student",n);
getch();
}
else
{
ClrScr();
gotoxy(43,3);
printf("No. Name sc1 sc2 sc3 ");
gotoxy(43,4);
printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
for(i=0;i<5;i++)
{
gotoxy(5,3*i+5);
printf("%s",ch[i]);
}
gotoxy(2,22);
printf("Don't press ESC before you finish it.");
gotoxy(2,23);
printf("ID no more than 11 characters. Name no more than 15 characters.");
gettext(5,5,36,17,Fuleft1);
gotoxy(21,5);
inputs(p->no,11);
gotoxy(19,8);
inputs(p->name,15);
s=0;
for(i=0;i<N;i++)
{
do
{
gotoxy(13,i*3+11);
scanf("%d",&p->score[i]);
if(p->score[i]>100||p->score[i]<0)
{
DrawFrame(25,9,54,12,WHITE,RED);
gotoxy(26,10);
cprintf("Bad data, please try again. ");
gotoxy(26,11);
cprintf(" ");
getch();
puttext(25,9,54,12,infow);
puttext(2,3,39,20,Fuleft);
}
}while(p->score[i]>100||p->score[i]<0);
s=s+p->score[i];
}
puttext(5,5,36,17,Fuleft1);
p->sum=s;
p->average=(float)s/N;
p->order=0;
gotoxy(43,3);
printf("No. Name sc1 sc2 sc3 ");
gotoxy(43,4);
printf("%-6s",p->no);
printf("%-15s",p->name);
for(i=0;i<3;i++)
{
printf("%-5d",p->score[i]);
}
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("have updated No %s student",s);
gotoxy(26,11);
cprintf("Don't forget save!");
getch();
}
return(h);
}
/*Query by ID*/
void searchID(STUDENT *h)
{
STUDENT *p;
char s[11];
gotoxy(2,11);
printf("Input No. you want to search: ");
scanf("%s",s);
p=h;
while(strcmp(p->no,s)&&p!=NULL)
p=p->next;
if(p==NULL)
{
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("list no %s student",s);
getch();
ClrScr();
}
else
{
ClrScr();
gotoxy(43,3);
printf("No. Name sc1 sc2 sc3 ");
gotoxy(43,4);
printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
getch();
}
}
/*Query by name*/
void searchName(STUDENT *h)
{
STUDENT *p;
char s[15];
gotoxy(5,10);
printf("Input name you want to search: ");
gotoxy(11,12);
scanf("%s",s);
p=h;
while(strcmp(p->name,s)&&p!=NULL)
p=p->next;
if(p==NULL)
{
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("list no %s student",s);
getch();
ClrScr();
}
else
{
ClrScr();
gotoxy(43,3);
printf("No. Name sc1 sc2 sc3 ");
gotoxy(43,4);
printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
getch();
}
}
/*Query by score*/
void searchGrades(STUDENT *h)
{
STUDENT *p;
int sc;
int i;
char flag;
sc=0;
i=4;
gotoxy(3,10);
printf("Which score you want to search:(1~3)");
gotoxy(14,12);
flag=getch();
puttext(2,3,39,20,Nuleft);
gotoxy(3,10);
printf(" input score: ");
gotoxy(20,10);
scanf("%d",&sc);
p=h;
ClrScr();
gotoxy(43,3);
printf("No. Name sc1 sc2 sc3 ");
switch(flag)
{
case '1':while(p!=NULL)
{
p=p->next;
if(p->score[0]==sc)
{
gotoxy(43,i);
printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
i++;
}
}
break;
case '2':while(p!=NULL)
{
p=p->next;
if(p->score[1]==sc)
{
gotoxy(43,i);
printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
i++;
}
}
break;
case '3':while(p!=NULL)
{
p=p->next;
if(p->score[2]==sc)
{
gotoxy(43,i);
printf("%-6s%-15s%-5d%-5d%-5d", p->no,p->name,p->score[0],p->score[1],p->score[2]);
i++;
}
}
break;
default: {
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("list no score %d student",sc);
getch();
ClrScr();
}
}
getch();
}
/*create an index on ID*/
STUDENT *indexID(STUDENT *h)
{
STUDENT *p,*q,*t,*h1;
h1=h->next;
h->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while(strcmp(t->no,p->no)>0&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("index sucess!!!");
getch();
ClrScr();
return h;
}
/*create an index on scores*/
STUDENT *indexGrades(STUDENT *h)
{
STUDENT *p,*q,*t,*h1;
char flag;
h1=h->next;
h->next=NULL;
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("Which score: (1~3)");
gotoxy(45,10);
flag=getch();
if(flag=='1')
{
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while((t->score[0]<p->score[0])&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
}
else if(flag=='2')
{
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while((t->score[1]<p->score[1])&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
}
else if(flag=='3')
{
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while((t->score[2]<p->score[2])&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
}
else
{
gotoxy(26,10);
cprintf("Which score: (1~3)" );
getch();
ClrScr();
}
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("index sucess!!! ");
getch();
ClrScr();
return h;
}
/*create an index on sum and average*/
STUDENT *indexSUM(STUDENT *h)
{
STUDENT *p,*q,*t,*h1;
h1=h->next;
h->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while((t->sum<p->sum)&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf("index sucess!!!");
getch();
ClrScr();
return h;
}
/*Save before exit*/
void saveChange(STUDENT *h)
{
FILE *fp;
STUDENT *p;
char outfile[10];
DrawFrame(25,9,54,12,0,7);
gotoxy(26,10);
cprintf(" Save change? Y/N");
if(getch()=='n')print(head);
else
{
gotoxy(26,10);
printf("file: ");
gotoxy(32,10);
textbackground(BLACK);
textcolor(WHITE);
cprintf("No more than 8 chs. ");
getch();
gotoxy(32,10);
textbackground(WHITE);
textcolor(BLACK);
cprintf(" ");
gotoxy(32,10);
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
gotoxy(26,10);
printf("can not open file!!");
getch();
puttext(1,1,80,25,save);
}
p=h;
while(p!=NULL)
{
fwrite(p,sizeof(STUDENT),1,fp);
p=p->next;
}
fclose(fp);
gotoxy(26,10);
cprintf(" File saved!! ");
getch();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -