2.猜数(字符串)游戏:程序功能要求:使用随机函数random()生成1-n之间的整数,输入数字猜数。
# include & ltstdio.h & gt
# include & lttime.h & gt
# include & ltwindows.h & gt
# include & ltstring.h & gt
#定义最多5个
结构记录
{
字符名称[10];
inter sorce;
}rec[MAX],Rec;
int n;//记录行数
int num//输入的数字
void显示();//启动界面
void玄泽();//选择功能
void sys(int a);//操作功能
void new game();//新游戏
void v display();//浏览记录界面
void VX uaze();//浏览选择功能
void help();//帮助描述
void vsys(int a);//浏览操作功能
void viewsorce();//按记录顺序浏览
void view name();//按记录的名称浏览
void views name();//按记录和名称浏览
void delrec();//删除记录
int qushu();//获取函数
int tishi(int aa);//提示功能
void addrec(int a);//判断并添加记录
void recread();//读取日志文件
void recwrite();//写入日志文件
///////////////////////////////////////////////////////////////////////////////////////////////
///////////函数定义//////////////
Void display() //启动界面
{
系统(“cls”);//清除屏幕
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" \ n ");
Printf(" 1:新游戏\ n ");
printf(" \ n ");
Printf(" 2:浏览记录\ n ");
printf(" \ n ");
printf(" 3:Help \ n ");
printf(" \ n ");
Printf(" 4:清除记录\ n ");
printf(" \ n ");
printf(" 5:exit \ n ");
printf(" \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
玄泽();
}
void xuaze()//选择函数
{
int a;
Printf("请输入您选择的操作数:\ n ");
scanf("%d ",& ampa);
if(a = = 1 | | a = = 2 | | a = = 3 | | a = = 4 | | a = = 5)
{
sys(a);
}//选择相应的操作数执行函数。
其他
{
Printf("错误的操作数!\ n ");;
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
}
Void sys(int a) //操作函数
{
开关(a)
{
案例1:new game();打破;
案例二:vdisplay();打破;
案例三:求助();打破;
案例四:delrec();打破;
情况五:出口(0);打破;//退出
}
}
Void newgame() //新游戏
{
recread();
系统(“cls”);//清除屏幕
printf(" \ n ");
Printf("记录列表如下所示:\ n ");
printf(" \ n ");
for(int I = 0;我& ltMAXi++)
{
if(建议[i])。sorce = = 0)printf(" & lt;-空记录-> \ n ");
其他
printf(" title % d:name:% s score % d \ n ",I+1,rec [I]。名称,建议[I]。SORCE);
}
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();//////////上面列出的日志文件中的记录。
/////////////////////////////////////////////////////////////////////////////
int snum//输入的数字
int sn = 10;//机会的数量
num = qushu();//获取
l:系统(“cls”);//清除屏幕
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" \ n ");
Printf(" 1:显示答案\ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" \ n ");
Printf("请输入你猜的四个数字(九千九百九十九式),还有%d次机会:\n ",sn);
scanf("%d ",& ampsnum);
如果(snum & lt10000。& ampsnum & gt1000 || snum==1)
{
if(snum==1)
{
Printf("这个局你放弃了游戏!这四个数字是:%d\n”,num);
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
其他
{
int flag = tishi(snum);
如果(旗帜!= 0 & amp& ampsn!=0)
{
sn-;
如果(sn==0)
{
Printf("真可惜!十个机会没了!没关系下次继续!\ n ");
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
Printf("请按任意键继续猜...\ n ");
getchar();
getchar();
goto L;
}
其他
{
如果(sn!= 0)add rec(sn);
其他
{
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
}
}
}
其他
{
Printf("错误的操作数!\ n ");;
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
goto L;
}
}
Void help() //帮助描述
{
系统(“cls”);//清除屏幕
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
Printf ("= = = = = = = = = = = =数字问答游戏帮助= = = = = = = = = = = = \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" \ n ");
Printf("1。在新游戏中,你可以输入四位数来猜测系统自动生成的四位数!,只有十次机会,\ n”);
Printf("每个机会都有相应的提示。用了十次机会还没猜对,就失败了!\n”的四位数);
Printf("游戏中会提示相应的格式!当你成功猜中系统给出的数字,你的成绩就会被计入。
Printf("当分数优于记录文件中的记录时,会提示是否在记录文件中记录该记录,选择是时\ n ");
Printf(")将提示您输入姓名。然后按回车。如果不出意外,会提示保存成功!\ n ");
Printf("还需要注意的是,记录中的分数越低,分数越好!\ n ");
Printf("2。浏览记录有三种方式!第一个只是列出记录中的名字;二是列出战绩的先后顺序\ n”);
Printf("第三种是把名字和成绩一起列出来!\ n ");
Printf("3。清除记录意味着初始化记录文件。也就是说,让记录文件为空!\ n ");
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
Void vdisplay() //浏览记录接口
{
系统(“cls”);//清除屏幕
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
printf(" \ n ");
Printf(" 1:按记录顺序浏览\ n ");
printf(" \ n ");
Printf(" 2:按记录名浏览\ n ");
printf(" \ n ");
Printf(" 3:浏览所有姓名记录\ n ");
printf(" \ n ");
Printf(" 4:返回上级\ n ");
printf(" \ n ");
printf(" = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ n ");
VX uaze();
}
voidvvxuaze()//浏览选择函数
{
int a;
Printf("请输入您选择的操作数:\ n ");
scanf("%d ",& ampa);
if(a = = 1 | | a = = 2 | | a = = 3 | | a = = 4)
{
vsys(a);
}//选择相应的操作数执行函数。
其他
{
Printf("错误的操作数!\ n ");;
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
vdisplay();
}
}
Void vsys(int a) //浏览操作函数
{
开关(a)
{
案例1:viewsorce();打破;
案例二:viewname();打破;
案例三:views name();打破;
情况四:显示();打破;//返回上级
}
}
Void viewname() //按记录顺序浏览。
{
recread();
系统(“cls”);//清除屏幕
printf(" \ n ");
Printf("记录列表如下所示:\ n ");
printf(" \ n ");
for(int I = 0;我& ltMAXi++)
{
if(建议[i])。sorce = = 0)printf(" & lt;-空记录-> \ n ");
其他
Printf ("name %d: %s\n ",i+1,rec[i]。姓名);
}
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
vdisplay();
}
void view sort()//按记录名称浏览
{
recread();
系统(“cls”);//清除屏幕
printf(" \ n ");
Printf("记录列表如下所示:\ n ");
printf(" \ n ");
for(int I = 0;我& ltMAXi++)
{
if(建议[i])。sorce = = 0)printf(" & lt;-空记录-> \ n ");
其他
Printf ("%d title: score %d\n ",i+1,rec[i]。sorce);
}
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
vdisplay();
}
Void viewsname() //按名称和记录浏览
{
recread();
系统(“cls”);//清除屏幕
printf(" \ n ");
Printf("记录列表如下所示:\ n ");
printf(" \ n ");
for(int I = 0;我& ltMAXi++)
{
if(建议[i])。sorce = = 0)printf(" & lt;-空记录-> \ n ");
其他
printf(" title % d:name:% s score % d \ n ",I+1,rec [I]。名称,建议[I]。SORCE);
}
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
vdisplay();
}
Void delrec() //删除记录
{
recread();
for(int I = 0;我& ltMAXi++)
{
建议[一]。sorce = 0;
}
rec write();
Printf("删除成功!\ n ");
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
Int qushu() // fetch函数
{
int a = 0;
int I = 0;
srand((无符号)时间(空));//随机数种子设置
while(i==0)
{
I = rand();//这样做是为了增加随机性。
I = I % 100;
}
l:for(int j = 0;j & lt我;j++)
{
a = a+rand();//判断随机数是否为四位数,如果不是,继续取数。
}
a =(a/I+rand())% 10000;
如果(a & lt1000)goto L;
返回(a);
}
Int tishi(int aa) //提示函数
{
int a,b,c,d;
int af,bf,cf,df;
if(aa==num)
{
Printf("恭喜!你猜对了!这四个数字是:%d\n”,num);
return(0);
}
其他
{
a=(数字-数字% 1000)/1000;
b=(数量%1000-(数量% 1000)% 100)/100;
c=((数字%1000)%100-((数字% 1000)% 100)% 10)/10;//获取默认的四位数。
d =((num % 1000)% 100)% 10;
af =(aa-aa % 1000)/1000;
BF =(aa % 1000-(aa % 1000)% 100)/100;
cf =((aa % 1000)% 100-((aa % 1000)% 100)% 10)/10;//获取输入的四个数字。
df =((aa % 1000)% 100)% 10;
if(af & gt;a)printf(" \ n第一个数字应该小于%d!\n”,af);
If(af==a)printf("恭喜!第一个数字是对的!应该是:%d\n”,af);//数字比较
if(af & lt;A)printf("第一个数字应该比%d!\n”,af);
如果(bf & gtB)printf("第二个数字应该小于%d!\n ",BF);
If(bf==b)printf("恭喜!第二个数字是对的!即:%d\n”,BF);//数字比较
如果(bf & ltB)printf("第二个数应该比%d大一点!\n ",BF);
如果(cf & gtC)printf("第三个数字应该小于%d!\n”,cf);
If(cf==c)printf("恭喜!第三个数字是对的!即:%d\n”,cf);//数字比较
如果(cf & ltC)printf("第三个数应该比%d大一点!\n”,cf);
if(df & gt;D)printf("第四个数字应该小于%d!\n”,df);
If(df==d)printf("恭喜!第四个数字是对的!即:%d\n”,df);//数字比较
if(df & lt;D)printf("第四个数应该比%d大一点!\n\n ",df);
返回(1);
}
}
voidadrec(int a)//判断并添加记录。
{
char c;
int flag = 0;//标志位
a = 11-a;
recread();
for(int I = 0;我& ltMAXi++)
{
如果(a & lt建议[一]。sorce || rec[i]。sorce==0)
{
if(i==4 || rec[i].sorce==0)
{
printf(" \ n ");
Printf("恭喜!您的分数%d已进入前五名!\n ",a);
Printf("请按' y '确认您要保存记录,并按任意键返回...!\ n ");
scanf("%s ",& ampc);
如果(c!='y ')显示();
rec[i+1]。sorce=rec[i]。sorce//向后记录
strcpy(rec[i+1])。名称,建议[i]。姓名);
Printf("请输入您的姓名\ n ");
scanf("%s ",& amp建议[一]。姓名);
建议[一]。sorce = a;
rec write();//写
Printf("恭喜!添加成功!\n ",a);
flag++;
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
其他
{
printf(" \ n ");
Printf("恭喜!您的分数%d已进入前五名!\n ",a);
Printf("请按' y '确认您要保存记录,并按任意键返回...!\ n ");
scanf("%s ",& ampc);
如果(c!='y ')显示();
for(int j = I;j & ltMAX-1;j++)
{
rec[j+1]。sorce=rec[j]。sorce//向后记录
strcpy(rec[j+1])。名称,rec[j]。姓名);
if(rec[j+2]。sorce = = 0)break;
}
printf(" \ n ");
Printf("请输入您的姓名\ n ");
scanf("%s ",& amp建议[一]。姓名);
建议[一]。sorce = a;
rec write();//写
Printf("恭喜!添加成功!\n ",a);
flag++;
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
}
}
如果(标志==0)
{
printf(" \ n ");
Printf("请按任意键继续...\ n ");
getchar();
getchar();
显示();
}
}
void Read()//读取日志文件
{
FILE * fp
L: fp=fopen("E:\\rec.rec "," Rb ");
if(fp==NULL)
{
rec write();
goto L;//初始化
}
if(fread(& amp;rec,sizeof(记录),MAX,fp)==MAX)
{
}
其他
{
Printf("文件读取失败!");
}
fclose(FP);
}
Void recwrite() //写入日志文件。
{
FILE * fp
fp=fopen("E:\\rec.rec "," WB ");
if(fp==NULL)
Printf("写入失败!\ n ");
fwrite(rec,sizeof(record),MAX,FP);
fclose(FP);
}
void main()
{
显示();
}