咏艾CorL
2024-05-10 19:24:22
最佳回答
展开全部#include
include
include
include
格式化时间函数
char_format_time_[40];char*getformattime(long t)
{
strftime(_format_time_,sizeof(_format_time_)/sizeof(char),"%y.%m.%d%h:%m:%s",gmtime(&t));return_format_time_;}
保存**信息的文件
char player_data_file_name[]="player.txt;系统默认的用户名
const char palyer_default_system_name[]="player;用户名最大长度
const int player_max_name_length=20;密码最大长度
const int player_max_pass_length=20;**信息
struct player
{
唯一的账号名称
char name[player_max_name_length+1];登陆密码
char pass[player_max_pass_length+1];struct player*next;player()
{
strcpy(name,"");strcpy(pass,"");next=**;}
};全局变量:游戏**链表
struct player*_players=**;全局变量:当前登陆的游戏**
struct player_loginplayer;保存游戏信息的文件
char game_data_file_name[]="game.txt;排行榜上记录保存个数
const int game_max_top_players=5;猜价格最多尝试次数
const int game_max_try_time=3;猜价格的最小值
const int game_min_price=1000;猜价格的最大值
const int game_max_price=9999;一局游戏的信息
struct game
{
游戏开始时间
long starttime;游戏结束时间
long endtime;**账号
char playername[player_max_name_length+1];真实价格
int realprice;是否成功猜出
int **success;**一共猜的次数
int guesstime;**猜了 guesstime 次,每次才的价格
int guessprice[game_max_try_time];struct game*next;game()
{
starttime=time(**);strcpy(playername,"");srand(time(**));**success=0;realprice=rand()%(game_max_price-game_min_price)+game_min_price;guesstime=0;next=**;}
};全局变量:猜价格游戏链表
struct game*_games=**;初始化 游戏**链表
void initplayer()
{
file*playerfile=fopen(player_data_file_name,"r");struct player*p,tempplayer;if(playerfile)
{
while(fscanf(playerfile,"%s%s",tempplayer.name,tempplayer.pass)!eof)
{
if(_players=**)
{
players=(struct player*)malloc(sizeof(struct player));players=tempplayer;p=_players;}
else
{
p->next=(struct player*)malloc(sizeof(struct player));p->next=tempplayer;p=p->next;}
}
fclose(playerfile);}
}
保存 游戏**链表
void s**eplayer()
{
file*playerfile=fopen(player_data_file_name,"w");struct player*p;if(playerfile)
{
p=_players;while(p)
{
fprintf(playerfile,"%s%s\n",p->name,p->pass);p=p->next;}
fclose(playerfile);}
}
初始化 猜价格游戏链表
void initgame()
{
file*gamefile=fopen(game_data_file_name,"r");struct game*p,tempgame;int i;if(gamefile)
{
while
fscanf(gamefile,"%ld%ld%s%d%d%d",
tempgame.starttime,
tempgame.endtime,
tempgame.playername,
tempgame.realprice,
tempgame.**success,
tempgame.guesstime
eof)
{
for(i=0;i;i+)
{
fscanf(gamefile,"%d",&tempgame.guessprice[i]);}
if(_games=**)
{
games=(struct game*)malloc(sizeof(struct game));games=tempgame;p=_games;}
else
{
p->next=(struct game*)malloc(sizeof(struct game));p->next=tempgame;p=p->next;}
}
fclose(gamefile);}
}
保存 猜价格游戏链表
void s**egame()
{
file*gamefile=fopen(game_data_file_name,"w");struct game*p;int i;if(gamefile)
{
p=_games;while(p)
{
fprintf(gamefile,"%ld%ld%s%d%d%d",
p->starttime,
p->endtime,
p->playername,
p->realprice,
p->**success,
p->guesstime
for(i=0;i<p->guesstime;i+)
{
fprintf(gamefile,"%d",p->guessprice[i]);}
fprintf(gamefile,"\n");p=p->next;}
fclose(gamefile);}
}
猜价格游戏主菜单
int printmainmenu()
{
char strchoice[100];int intchoice;do
{
printf("\n*\n");printf("*主菜单\n");printf("*\n");printf("*1)查看排行榜\n");printf("*2)清除排行榜\n");printf("*3)登录账号开始游戏\n");printf("*4)申请账号开始游戏\n");printf("*5)退出游戏\n");printf("*\n");scanf("%s",strchoice);intchoice=atoi(strchoice);if(intchoice|intchoice>5)
{
printf("*命令错误!请重新输入命令!}
}
while(intchoice|intchoice>5);return intchoice;}
对已有的游戏记录根据【猜价格的次数】递增排序
如果【猜价格的次数】相等,
根据【使用的时间】递增排序,
如果【使用的时间】相等,
根据【开始的时间】递增排序,
void sortgames()
{
struct game*p,*q,tempgame1,tempgame2;p=_games;while(p)
{
q=p->next;while(q)
{
if
p->guesstime>q->guesstime|
(p->guesstime=q->guesstime&(p->endtime-p->starttime)>(q->endtime-q->starttime))|
(p->guesstime=q->guesstime&(p->endtime-p->starttime)=(q->endtime-q->starttime)&p->starttime>q->starttime)
{
tempgame1=*p;tempgame2=*q;注意交换的时候,next 指针不能交换
p=tempgame2;p->next=tempgame1.next;注意交换的时候,next 指针不能交换
q=tempgame1;q->next=tempgame2.next;}
q=q->next;}
p=p->next;}
}
查看排行榜
void printrankl**t()
{
int counter=0;struct game*p;printf("\n*\n");printf("***排名\n");printf("*\n");没有记录
if!games)
{
printf("*排名:%4d 用户:%10s(default)用了%4d次\n",1,palyer_default_system_name,game_max_try_time);}
else
{
排序
sortgames();p=_games;counter=0;查看前 game_max_top_players 名(如果有那么多),并且成功猜出价格的
while(p&counter< game_max_top_players && p->**success)
{
counter+;printf("*排名:%4d 用户:%10s 用了%4d次\n",counter,p->playername,p->guesstime);printf("开始时间:%s\n",getformattime(p->starttime));printf("结束时间:%s\n\n",getformattime(p->endtime));p=p->next;}
}
printf("*\n");}
清除排行榜
void clearrankl**t()
{
struct game*p=_games,*q;while(p)
{
q=p->next;free(p);p=q;}
games=**;printf("\n*\n");printf("*清空排名榜\n");printf("*\n");printf("*排名榜已经清空!\n");printf("*\n");}
猜价格游戏用户登陆菜单
int printloginmenu()
{
struct player*p;char name[player_max_name_length+1];char pass[player_max_pass_length+1];int i;char ch;printf("\n*\n");printf("*登录菜单\n");printf("*\n");printf("*输入用户名称:");scanf("%s",name);printf("*输入用户密码:");scanf("%s",pass);i=0;while((ch=getch())!'\n')
{
pass[i+]=ch;}
pass[i]='\n';p=_players;while(p)
{
if(strcmp(p->name,name)=0&strcmp(p->pass,pass)=0)
{
break;}
else
{
p=p->next;}
}
if(p)
{
printf("*登陆成功!\n");printf("*\n");strcpy(_loginplayer.name,name);strcpy(_loginplayer.pass,pass);return 1;}
else
{
printf("*登陆失败!用户名不存在或者密码错误!\n");printf("*\n");return 0;}
}
猜价格游戏用户注册菜单
int printreg**termenu()
{
struct player*p;char name[player_max_name_length+1];char pass[player_max_pass_length+1];int i;char ch;printf("\n*\n");printf("*注册菜单\n");printf("*\n");do
{
printf("*输入要注册的用户名称:");scanf("%s",name);if(strcmp(palyer_default_system_name,name)=0)
{
printf("*%s 是系统默认的用户名,不能被注册!\n",palyer_default_system_name);}
else if(strlen(name)=0)
{
printf("*用户名不能为空!\n");}
}
while(strlen(name)=0|strcmp(palyer_default_... 20210311