#include<stdio.h>

#include<stdlib.h>

#include<string.h>


#define TURE 1


typedef struct userinformation

{

char name[30];

char passwd[30];

double money;

int total;

int win;

}user_t;


//总体菜单控制函数

void all_menu_ctrl(user_t *pu,int *count,int *index);

//注册函数

int user_register(user_t *pu,int *count);

//清除缓存

void fflush_in();

//登录函数

int user_enter(user_t *pu,int *count,int *index);

//登录菜单控制函数

void enter_menu_ctrl(user_t *pu,int *count,int *index);

//查看用户信息菜单函数

void check_user_menu(user_t *pu,int *count,int *index);

//查看个人信息函数

void check_slf(user_t *pu,int *count,int *index);

//查看所有人信息函数

void check_all(user_t *pu,int *count);

//充值函数

int pay(user_t *pu,int *index);

//修改用户信息菜单函数

int modify_menu(user_t *pu,int *count,int *index);

//修改密码函数

int modify_passwd_ctrl(user_t *pu,int *index);

//注销用户函数

int delete_user_ctrl(user_t *pu,int *count,int *index);

//排序菜单函数

void sort_menu(user_t *pu,int *count);

//金额排序比较函数

int sort_money(const void *px,const void *py);

//胜率排序比较函数

int sort_win(const void *px,const void *py);

//游戏控制函数

int game_ctrl(user_t *pu,int *index);

//开机初始化

int begin(user_t *pu);

//关机保存

void end(user_t *pu,int *count);


int main()

{

user_t user[100] = {0};

int count = 0;//注册用户数

int index = 0;

count = begin(user);

all_menu_ctrl(user,&count,&index);



return 0;

}

//开机初始化

//返回值:count 用户数量

int begin(user_t *pu)

{

FILE *fp = NULL;

int count = 0;


fp = fopen("temp","a+");

if(NULL == fp)

exit(0);

while(fread(&pu[count],sizeof(user_t),1,fp) != 0)

count++;

fclose(fp);


return count;

}

//关机保存

void end(user_t *pu,int *count)

{

FILE *fp = NULL;


fp = fopen("temp","w");

if(NULL == fp)

exit(0);

fwrite(pu,sizeof(user_t),*count,fp);


fclose(fp);

}

//清除缓存

void fflush_in()

{

char c = '\0';

c = getchar();


while(c != '\n')

c = getchar();

}

//登录菜单控制函数

void enter_menu_ctrl(user_t *pu,int *count,int *index)

{

int chose = 0;


while(TURE)

{

chose = 0;

printf("1.查看用户信息 2.开始游戏 3.充值 4.修改用户信息 "

"5.排序 6.返回\n");

printf("选择:");

scanf("%d",&chose);

fflush_in();


switch(chose)

{

case 1://查看用户信息

check_user_menu(pu,count,index);

break;

case 2://开始游戏

if(1 == game_ctrl(pu,index))

printf("游戏失败!\n");

else

printf("祝您玩的愉快!\n");

break;

case 3://充值

if(pay(pu,index) == 1)

printf("充值失败\n");

else

printf("充值成功\n");

break;

case 4://修改用户信息

if(1 == modify_menu(pu,count,index))

return;

break;

case 5://排序

sort_menu(pu,count);

break;

case 6://返回

return;

break;

default:

printf("输入错误!\n");

}

}

}

//游戏控制函数

//返回值:1-游戏失败  0-游戏成功

int game_ctrl(user_t *pu,int *index)

{

double money = 0.0;

char *quan[] = {"剪刀","石头","布"};

char user[30] = "\0";

int uindex = 0,pcindex = 0;


if(pu[*index].money <= 0)

{

printf("余额不足,请充值!\n");

return 1;

}

printf("您的余额为%.2lf\n",pu[*index].money);

printf("请下注:\n");

scanf("%lf",&money);

fflush_in();

if(money <= 0 || money > pu[*index].money)

{

printf("下注失败!\n");

return 1;

}

printf("游戏开始,请出拳(剪刀/石头/布):\n");//游戏开始

fgets(user,30,stdin);

user[strlen(user)-1] = '\0';

for(uindex = 0;uindex < 3;uindex++)

{

if(strcmp(user,quan[uindex]) == 0)

break;

}

if(uindex >= 3)

{

printf("输入错误!\n");

return 1;

}

srand((unsigned)time(NULL));

pcindex = rand() % 3;

printf("电脑:%s\n",quan[pcindex]);

if(uindex==2&&pcindex==1 || uindex==1&&pcindex==0 || uindex==0&&pcindex==2)

{

srand((unsigned)time(NULL));

money *= (rand()%5 + 1);

pu[*index].money += money;

pu[*index].win += 1;

printf("恭喜您赢得本次猜拳!\n");

}

else if(uindex == pcindex)

printf("平局!\n");

else

{

pu[*index].money -= money;

printf("请再接再厉!\n");

}


pu[*index].total += 1;


return 0;


}

//排序菜单函数

void sort_menu(user_t *pu,int *count)

{

int chose = 0;


while(TURE)

{

chose = 0;

printf("1.按金额排序 2.按胜率排序 3.返回\n");

printf("选择:");

scanf("%d",&chose);

fflush_in();


switch(chose)

{

case 1://金额

qsort(pu,*count,sizeof(user_t),sort_money);

printf("排序成功!\n");

break;

case 2://胜率

qsort(pu,*count,sizeof(user_t),sort_win);

printf("排序成功!\n");

break;

case 3://返回

return;

break;

default:

printf("输入错误!\n");

}

}

}

//金额排序比较函数

int sort_money(const void *px,const void *py)

{

const user_t *p1 = px;

const user_t *p2 = py;


if(p1->money == p2->money)

return 0;

if(p2 -> money > p1 -> money)

return 1;

else

return -1;

}

//胜率排序比较函数

int sort_win(const void *px,const void *py)

{

const user_t *p1 = px;

const user_t *p2 = py;

double w1 = 0.0,w2 = 0.0;

w1 = (p1 -> total) == 0?0:100*((double)p1 -> win / p1 -> total);

w2 = (p2 -> total) == 0?0:100*((double)p2 -> win / p2 -> total);


if(w1 == w2)

return 0;

if(w2 > w1)

return 1;

else

return -1;

}

//充值函数

//返回值:1-充值失败 0-充值成功

int pay(user_t *pu,int *index)

{

double money = 0;

char pass[30] = "0";

char input[30] = "0";

int i = 0;


printf("充值金额:");

scanf("%lf",&money);

fflush_in();

if(money < 0)

return 1;

for(i = 0;i < 5;i++)

{

srand((unsigned)time(NULL) + i);

pass[i] = "3456789abcdefghijkmnpqrstuvwxyz"[rand()%31];

}

printf("%s\t",pass);

fgets(input,30,stdin);

input[strlen(input)-1] = '\0';

if(0 != strcmp(input,pass))

{

printf("验证码输入错误!\n");

return 1;

}

pu[*index].money += money;

money = 0;


return 0;

}

//总体菜单控制函数

void all_menu_ctrl(user_t *pu,int *count,int *index)

{

int chose = 0;


while(TURE)

{

chose = 0;

printf("1.注册 2.登录 3.退出\n");

printf("选择:");

scanf("%d",&chose);

fflush_in();


switch(chose)

{

case 1://注册

if(1 == user_register(pu,count))

printf("注册失败!\n");

else

printf("注册成功!\n");

break;

case 2://登录

if(1 == user_enter(pu,count,index))

printf("登录失败!\n");

else

{

printf("登录成功!\n");

enter_menu_ctrl(pu,count,index);

}

break;

case 3://退出

end(pu,count);

exit(0);

break;

default:

printf("输入错误!\n");

}

}

}

//查看用户信息菜单函数

void check_user_menu(user_t *pu,int *count,int *index)

{

int chose = 0;


while(TURE)

{

chose = 0;

printf("1.查看个人信息 2.查看所有人信息 3.返回\n");

printf("选择:");

scanf("%d",&chose);


switch(chose)

{

case 1://查看个人信息

check_slf(pu,count,index);

break;

case 2://查看所有人信息

check_all(pu,count);

break;

case 3://返回

return;

break;

default:

printf("输入错误\n");

}

}

}

//修改用户信息菜单函数

int modify_menu(user_t *pu,int *count,int *index)

{

int chose = 0;

char current_passwd[30] = "\0";


printf("输入当前密码:\n");

fgets(current_passwd,30,stdin);

current_passwd[strlen(current_passwd)-1] = '\0';


if(strcmp(current_passwd,pu[*index].passwd) != 0)

{

printf("输入错误!\n");

return;

}


while(TURE)

{

chose = 0;

printf("1.修改密码 2.注销用户 3.返回\n");

printf("选择:");

scanf("%d",&chose);

fflush_in();


switch(chose)

{

case 1://修改密码

if(modify_passwd_ctrl(pu,index) == 1)

printf("修改密码失败!\n");

else

printf("修改密码成功!\n");

break;

case 2://注销用户

if(1 == delete_user_ctrl(pu,count,index))

printf("注销失败!\n");

else

printf("注销成功!\n");

return 1;

//exit(0);

break;

case 3://返回

return;

break;

default:

printf("输入错误\n");

}

}

return 0;

}

//修改密码函数

//返回值:1-修改失败 0-修改成功

int modify_passwd_ctrl(user_t *pu,int *index)

{

char new_passwd1[30] = "\0";

char new_passwd2[30] = "\0";


printf("输入新密码:\n");

fgets(new_passwd1,30,stdin);

new_passwd1[strlen(new_passwd1)-1] = '\0';

if(strlen(new_passwd1)<3||strlen(new_passwd1)>16)

{

printf("密码长度为3-16位!\n");

return 1;

}

printf("请重新确认:\n");

fgets(new_passwd2,30,stdin);

new_passwd2[strlen(new_passwd2)-1] = '\0';

if(strcmp(new_passwd1,new_passwd2) != 0)

{

printf("两次输入不一致!\n");

return 1;

}


strcpy(pu[*index].passwd,new_passwd1);


return 0;

}

//注销用户函数

//返回值:1-注销失败 0-注销成功

int delete_user_ctrl(user_t *pu,int *count,int *index)

{

int i = 0;

char c = '\0';


printf("是否注销该用户(y/n):");

if((c = getchar()) == 'n')

return 1;

for(i = *index;i<*count-1;i++)

{

pu[i] = pu[i+1];

}

*count -= 1;

return 0;

}

//查看个人信息函数

void check_slf(user_t *pu,int *count,int *index)

{

printf("用户名\t密码\t帐户余额\t胜率\n");

printf("%s\t%s\t%.2lf\t\t%.2lf%%\n",pu[*index].name,pu[*index].passwd,

pu[*index].money,

pu[*index].total==0?0:100*(double)pu[*index].win/pu[*index].total);

}

//查看所有人信息函数

void check_all(user_t *pu,int *count)

{

int i = 0;


printf("用户名\t帐户余额\t胜率\n");

for(i=0;i<*count;i++)

{

printf("%s\t%.2lf\t\t%.2lf%%\n",pu[i].name,pu[i].money,

pu[i].total==0?0:100*(double)pu[i].win/pu[i].total);

}

}

//登录函数

//返回值:1-登录失败 0-登录成功

int user_enter(user_t *pu,int *count,int *index)

{

int i = 0;

char enter_name[30] = "\0";

char enter_passwd[30] = "\0";


printf("用户名:\n");

fgets(enter_name,30,stdin);

enter_name[strlen(enter_name)-1] = '\0';


printf("密码:\n");

fgets(enter_passwd,30,stdin);

enter_passwd[strlen(enter_passwd)-1] = '\0';

for(i = 0;i < *count;i++)

{

if(strcmp(enter_name,pu[i].name) == 0 && strcmp(enter_passwd,pu[i].passwd) == 0)

{

*index = i;

return 0;

}

}

return 1;


}

//注册函数

//返回值: 1 - 失败   0 - 成功

int user_register(user_t *pu,int *count)

{

int i = 0;


char temp_name[30] = {'\0'};

char temp_passwd1[30] = {'\0'};

char temp_passwd2[30] = {'\0'};


printf("用户名:\n");

fgets(temp_name,30,stdin);

temp_name[strlen(temp_name)-1] = '\0';

if(strlen(temp_name) < 3||strlen(temp_name) > 16)

{

printf("用户名为3-16位!\n");

return 1;

}

for(i = 0;i < *count;i++)

if(0 == strcmp(temp_name,pu[i].name))

{

printf("用户已存在!\n");

return 1;

}

printf("密码:\n");

fgets(temp_passwd1,30,stdin);

temp_passwd1[strlen(temp_passwd1)-1] = '\0';

if(strlen(temp_passwd1)<3||strlen(temp_passwd1)>16)

{

printf("密码长度为3-16位!\n");

return 1;

}

printf("请确认\n");

fgets(temp_passwd2,30,stdin);

temp_passwd2[strlen(temp_passwd2)-1] = '\0';

if(strcmp(temp_passwd1,temp_passwd2) != 0)

{

printf("两次输入密码不一致!\n");

return 1;

}

strcpy(pu[*count].passwd,temp_passwd1);

strcpy(pu[*count].name,temp_name);

*count += 1;


return 0;

}