这是我做的东西,你的游戏程序必须返回一个成绩的结果体数据,可以满足你的所有要求,不过菜单是输入选择性的,不是图形界面。粘贴上来格式有问题,自己调整。在VS2005中编译通过,运行成功。程序如下:#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct{ char name[255 展开
这是我做的东西,你的游戏程序必须返回一个成绩的结果体数据,可以满足你的所有要求,不过菜单是输入选择性的,不是图形界面。粘贴上来格式有问题,自己调整。在VS2005中编译通过,运行成功。程序如下:#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct{ char name[255];long score;} User;void Menu(){ printf("========================made by 小恩 =====================\n");printf("==\t\t\ta:start game \t\t\t==\n");printf("==\t\t\tb:show order \t\t\t==\n");printf("==\t\t\tc:quit game \t\t\t==\n");printf("==========================================================\n");}char choise(){ char ch;printf("Input your choice(a~c): ");while(ch=getchar()) { while('\n'!=getchar());if(ch>'c'||ch<'a') { printf("Error input,Try again(a~c): ");continue;} else break;} return ch;}void Byebye(){ printf("========================made by 小恩 =====================\n\n");printf("========================Thank you!Bye=====================\n");}void game(User *pSean){ //这是你的游戏程序函数, //游戏结束时在这里得到成绩值 printf("Play game... ...Gameover\nInput your Score:");scanf("%d",&(pSean->score));//成绩 getchar();printf("Input your name(Less than 254 letters: )");scanf("%s",pSean->name);getchar();return;}void saveData(User sean){ FILE *fp;long i=0,fPos=0;int flag=3;int lth=sizeof(User);User buf;size_t sz;if(fopen_s(&fp,"F:\\game_data.dat","r+b")) { printf("Can't open F:\\game_data.dat!\n");exit(1);} while(sz=fread(&buf,lth,1,fp)) { if(0==strcmp(sean.name,buf.name)) { if(sean.score<=buf.score) flag=0;//不用改动 else { fPos=ftell(fp);flag=1;//用较大值替换 } break;} else { flag=2;//新玩家数据保存 } } switch(flag) { case 0://老玩家分数没有以前高,不做记录 break;case 1://老玩家分数创新高 rewind(fp);for(;i<fPos;i=ftell(fp)) { fread(&buf,lth,1,fp);if(buf.score>sean.score) continue;else { fseek(fp,(-1)*(lth),SEEK_CUR);fwrite(&sean,lth,1,fp);fseek(fp,0,SEEK_CUR);sean=buf;} } break;case 2://新玩家 fseek(fp,0,SEEK_SET);while(fread(&buf,lth,1,fp)) { if(buf.score>sean.score) continue;fseek(fp,-1*lth,SEEK_CUR);fwrite(&sean,lth,1,fp);fseek(fp,0,SEEK_CUR);sean=buf;} fwrite(&sean,lth,1,fp);break;case 3://以前数据文件无数据 fwrite(&sean,lth,1,fp);break;default: printf("Something Error when save data!\n");break;} fclose(fp);printf("SaveData in F:\\game_data.dat\n");return;}void showOrder(int max){ FILE *fp;int lth=sizeof(User);int n=0;User buf;if(fopen_s(&fp,"F:\\game_data.dat","r")) { printf("Can't open F:\\game_data.dat!\n");exit(1);} printf("This is the order!\n");printf("__________Name\t__________Score\t__________No.__________\n");while(fread(&buf,lth,1,fp)) printf("__________%s\t__________%d\t__________%d__________\n",buf.name,buf.score,++n);fclose(fp);return;}main(){ char item,name[255];User sean;memset(name,0,sizeof(name));Menu();while('c'!=(item=choise())) { switch(item) { case 'a': system("cls");game(&sean);saveData(sean);Menu();break;case 'b': system("cls");showOrder(10);Menu();break;default: printf("Something Error\n");break;} } Byebye();} 收起