⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 order.h

📁 有排行榜
💻 H
字号:
#include "stdio.h"
#include "ctype.h"
#include "graphics.h"
#include "bios.h"
#include "mkey.h"
#include "stdlib.h"
#include "hzwrite.h"
typedef struct tagrecord
{
char num[2];
char name[9];
char score[5];
}record;
record newrecord[3];
record oldrecord[3];
record currecord;
FILE *forder;
int thisscore;
char namebuffer[9];
char *author="LCG\n";
int fileexist;
int oldcount;                                 
char *head;
char *none[3]={"1welcome!0000","2welcome!0000","3welcome!0000"};



/*初始化本程序*/
void initorder()
{int i;
 for(i=0;i<8;i++){namebuffer[i]='\0';}
fileexist=1;
oldcount=0;
setfillstyle(1,CYAN);
bar(220,160,420,320);
hzwrite("恭喜",250,175);
hzwrite("您已挤身三甲",250,205);
hzwrite("请输入姓名:",250,235);
}


/*获取输入的姓名*/
void getname()
{
int namekey;
for(;;)
{namekey=bioskey(0);
 if(namekey==KEYReturn)break;
 else if(oldcount>8)oldcount=8;
 else if(namekey!=KEYBackspace){namebuffer[oldcount++]=getbioskey(namekey);namebuffer[8]='\0';setfillstyle(1,YELLOW);bar(250,280,340,300);outtextxy(250,280,namebuffer);}
 else {if(oldcount>0){namebuffer[--oldcount]='\0';setfillstyle(1,YELLOW);bar(250,250,340,300);outtextxy(250,280,namebuffer);}
       else continue;}
}
 if(oldcount==0)strcpy(namebuffer,"noname  ");
 else if(oldcount<7)for(;oldcount<8;oldcount++){namebuffer[oldcount]=' ';}
}
/*获取旧的记录*/
void getoldrecord()
{
      int i;
      fileexist=1;
      fgets(head,3,forder);
      if(!strcmp(head,"LCG")){fileexist=0;}
      else {fseek(forder,5,SEEK_SET);
             for(i=0;i<3;i++){fgets(oldrecord[i].num,2,forder);
                              fgets(oldrecord[i].name,9,forder);
                              fgets(oldrecord[i].score,5,forder);}
            }
}
/*重建记录*/
void rebuildrecord()
{
int i,j;
fseek(forder,0,SEEK_SET);
fputs(author,forder);
for(i=1;i<4;i++)fputs(none[i-1],forder);
}

/*写新记录*/
void writenewrecord()
{
 int i,j;
 fseek(forder,0,0);
 fputs(author,forder);
 for(i=0;i<3;i++){
                 fputs(newrecord[i].num,forder);
                 fputs(newrecord[i].name,forder);
                 fputs(newrecord[i].score,forder);}
}

void getnewrecord()
{int i,k;
for(k=1;k<4;k++){if(atoi(oldrecord[k-1].score)<thisscore){getname();
                                                   itoa(thisscore,currecord.score,10);
                                                   strcpy(currecord.name,namebuffer);
                                                   switch(k)
                                                   {case 1: strcpy(currecord.num,"1");break;
                                                    case 2: strcpy(currecord.num,"2");break;
                                                    case 3: strcpy(currecord.num,"3");break;}


                     if(strlen(currecord.score)==3){currecord.score[3]=currecord.score[2];
                                            currecord.score[2]=currecord.score[1];
                                            currecord.score[1]=currecord.score[0];
                                            currecord.score[4]='\0';
                                            currecord.score[0]='0';}
                        else if (strlen(currecord.score)==2){currecord.score[3]=currecord.score[1];
                                                     currecord.score[2]=currecord.score[0];
                                                     currecord.score[1]='0';
                                                     currecord.score[4]='\0';
                                                     currecord.score[0]='0';}
                             else {;}
                 strcpy(newrecord[k-1].num,currecord.num);
                 strcpy(newrecord[k-1].score,currecord.score);
                 strcpy(newrecord[k-1].name,currecord.name);
                 break;}
                 }

for(i=0;i<3;i++){if(i<k-1){strcpy(newrecord[i].num,oldrecord[i].num);
                           strcpy(newrecord[i].score,oldrecord[i].score);
                           strcpy(newrecord[i].name,oldrecord[i].name);}
                 if(i>k-1){strcpy(newrecord[i].num,oldrecord[i].num);
                           strcpy(newrecord[i].score,oldrecord[i-1].score);
                           strcpy(newrecord[i].name,oldrecord[i-1].name);}
                 }
}
void rewriterecord()
{
int i;
fseek(forder,0,0);
fputs(author,forder);
for(i=0;i<3;i++){fputs(newrecord[i].num,forder);
                 fputs(newrecord[i].name,forder);
                 fputs(newrecord[i].score,forder);}
}

void showrecord()
{
int i;
setfillstyle(1,LIGHTGRAY);bar(220,160,420,320);
hzwrite("积分榜",305,165);
hzwrite("名次",230,190);hzwrite("积分",305,190);hzwrite("分数",380,190);
for(i=0;i<3;i++){
outtextxy(230,220+40*i,newrecord[i].num);
outtextxy(230+75,220+40*i,newrecord[i].name);
outtextxy(230+150,220+40*i,newrecord[i].score);}
getch();
}


int order(int gotscore)
{int i,j;
initorder();
thisscore=gotscore;

if((forder=fopen("morder.dat","r"))==NULL){fileexist=0;}
else {getoldrecord();}
fclose(forder);

if((forder=fopen("morder.dat","w"))==NULL){printf("Cannot open file order.dat!\n");getch();exit(0);}
else{if(!fileexist)rebuildrecord();}
fclose(forder);

if((forder=fopen("morder.dat","r"))==NULL){fileexist=0;}
else {getoldrecord();}
fclose(forder);

getnewrecord();

if((forder=fopen("morder.dat","w"))==NULL){printf("Cannot open file order.dat!\n");getch();exit(0);}
rewriterecord();
fclose(forder);

showrecord();

return (0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -