#include <stdlib.h>
#include <stdio.h>
//#include <iostream>
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20};
void help(void)
{
char i =0,j = 0;
for(i =0;i<4;i++)
{
for(j = 0;j<4;j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!rn",i);
else
{
printf("%d is a correct datarn",getNum[i]);
}
}
else
{
if(i == 3&&j==3)
{printf("
input 4 data gess the random data,input h can get help
");}
}
}
}
void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878*101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
srand( (unsigned)time( NULL ) );
orginal = rand();
Num[3] = orginal%10000/1000;
Num[2] = orginal%1000/100;
Num[1] = orginal%1000%100/10;
Num[0] = orginal%10;
// printf(" %d",orginal);
for(i = 0;i<4;)
{
for(j = i+1;j<4;j++)
{
if(Num[i]==Num[j])
{ Num[i]++;if(Num[i]>9) Num[i] = 0;i=0;break;}
}
if(j == 4)i++;
}
get: for(i = 0;i<4;)
{
printf("input Num %d ",i);
scanf("%s",&getNum[i]);
if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help();
}
else
printf("input one numrn");
//if(i>0)i--;
}
else
{
getNum[i] -=0x30;
for(j = 0;j<i;j++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input anotherrn"); break;}
}
if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N?rn",getNum[0],getNum[1],getNum[2],getNum[3]);
do{
scanf("%c",&j);
printf("%c",j);
if((j == 'N') || (j == 'n'))goto get;
}while( !((j == 'Y') || (j == 'y')));
printf("over");
}
c语言设计一个猜数游戏,电脑会自动提示大了或小了,直到猜对为止
加了得分榜,没写排序,没时间了。自己加一下就OK
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h> //以上的是要用到的头文件
int score=0;
void SELECT(int);
void Guess(int,int);
void Select();
void letsgo();
void NB(int); //以上的是函数原型
struct PLAYER
{
char name[10];
int plscore;
};
void save()
{ printf("您的得分是%i",score);
PLAYER player;
printf("请输入姓名:");
scanf("%s",player.name);
player.plscore=score;
FILE *fp;
if((fp=fopen("record.txt","ab"))==NULL)
{
printf("不能打开文件!");
exit(0);
}
fwrite(&player,sizeof(PLAYER),1,fp);
printf("记录保存完毕!");
fclose(fp);
}
void print()
{
printf("分数榜:");
FILE *fp;
if((fp=fopen("record.txt","rb"))==NULL)
{
printf("不能打开文件!");
exit(0);
}
PLAYER player;
while(fread(&player,sizeof(PLAYER),1,fp)==1)
{
printf("n姓名:%sn",player.name);
printf("分数:%in",player.plscore);
}
fclose(fp);
}
void main()
{
char end;
printf(" tt欢迎光临!!nt 在这里我将来测试你的运气!n");
printf(" 请你猜下数字,但机会就只有三次!!n");
printf("n 1、开始游戏^__^n 2、我是倒霉鬼,我不玩n 3、查看分数榜");
printf(" 请选择1或2n");
Select();
printf("n按任意键退出");
fflush(stdin);
end=getchar();
if(score>0)
save();
printf("Thanks For Playing 2 886n");
}
void Select()
{
int s;
static int lihai=0;
fflush(stdin);
scanf("%d",&s);
switch(s)
{
case 1:
letsgo();break;
case 2:
printf("唉,你不想玩,我就不逼你咯n");exit(0);
case 3:print();
break;
default:
lihai++;
if(lihai==3||lihai==5)
{
NB(lihai);
break;
}
printf("你输入的是什么啊!重新输入< 1 或 2 >n ");
Select();
}
}
void letsgo()
{
int select;
printf("请你选择猜数字的难度n ");
printf("1、简单->猜测 0 到 9 的数字n ");
printf("2、一般->猜测 0 到 99 的数字n ");
printf("3、困难->猜测 0 到 999 的数字n ");
printf("<请不要乱选!!>n");
scanf("%d",&select);
switch(select)
{
case 1:SELECT(10);break;
case 2:SELECT(100);break;
case 3:SELECT(1000);break;
case 4:print();
default:
{
printf("请不要乱选!!n");
letsgo();
}
}
}
void NB(int l)
{
char a[20];
static int jihui=0;
printf("n老大!我服了你,%d 次都输入错误。n",l);
printf("n<可能你在捣乱~~>n");
printf("(如果不想结束,请输入[sorry]<中括号内的>否则结束游戏)n");
fflush(stdin);
gets(a);
if (strcmp(a,"sorry")==0||strcmp(a,"SORRY")==0)
{
jihui++;
if(jihui==2)
{
printf("n死性不改。道歉也没有用了!n");
goto end;
}
printf("好吧。给你次机会<请输入 1 或 2 >n ");
Select();
}
end:
printf("");
}
void SELECT(int n)
{
int num;
srand((unsigned)time(NULL));
num=rand()%n;
if(n==10)
{
n=3;
printf("n你有 %d 次猜测的机会n",n);
}
if(n==100)
{
n=5;
printf("n你有 %d 次猜测的机会n",n);
}
if(n==1000)
{
n=7;
printf("n你有 %d 次猜测的机会n",n);
}
Guess(num,n);
}
void Guess(int num,int n)
{
int search,k,o;
static int i=1;
char ans;
if(n==3)
k=10;
if(n==5)
k=100;
if(n==7)
k=1000;
o=(num-k/1)%k;
if(o<0)
o=0;
printf("n请猜这个数是多少(在<%d>到<%d>):",o,(k/5+num)%k);
fflush(stdin);
scanf("%d",&search);
if(num>search)
printf("n小了n");
if(num<search)
printf("n大了n");
if(num==search)
{
printf("n恭喜您了!你在第 %d 次猜对了n",i);
score+=10;
printf("是否继续<y / n>:n");
fflush(stdin);
ans=getchar();
if(ans=='y'||ans=='Y')
{
i=1;
letsgo();
}
}
else
{
i++;
if(i<=n)
Guess(num,n);
else
{
printf("n你的 %d 次机会已经没有了!!,答案是 %d n",i-1,num);
printf("是否继续<y / n>:n");
fflush(stdin);
ans=getchar();
if(ans=='y'||ans=='Y')
{
i=1;
letsgo();
}
}
}
}
满意请采纳。
#include "stdio.h"
#include "time.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int r;
int g;
srand(time(0));
r = rand()%1000;//产生一个小于1000的随机数,让大家猜
printf("请输入你猜的数:");
scanf("%d",&g);
while(g != r)//没猜中就循环
{
if(g > r)
{
printf("大了,请重新输入:");
}
else if(g < r)
{
printf("小了,请重新输入:");
}
scanf("%d",&g);
}
printf("不大不小n");
return 0;
}
免责声明:本平台仅供信息发布交流之途,请谨慎判断信息真伪。如遇虚假诈骗信息,请立即举报
举报