#include<stdioh>
#include<stringh>
void judge(char,char);
char stry = "yes";
char strn = "no";
int main()
{
char str[100];
printf("GAME \"letter guessing\" start: \n");
printf("Q:退出,其他:开始\n");
gets(str);
while(strcmp(str,"Q") != 0)
{
printf("yes:在当前字母前 no:在当前字母后 B:返回\n");
judge('A','Z'+1);
printf("Q:退出,其他:开始\n");
gets(str);
}
}
void judge(char c,char p)
{
char str[100];
char cp = (p - c )/2 - 1 + c ;
while(1)
{
if(p == c || p - c == 1)
{
printf("答案是 :%c\n",c);
return;
}
printf("%c:",cp);
gets(str);
if(strcmp(str,"B") == 0)
return;
if(strcmp(str,stry) == 0)
{
p = cp;
cp = (p - c + 1)/2 -1 + c;
}
else if(strcmp(str,strn) == 0)
{
c = cp;
cp = (p - c)/2 + c;
}
else
printf("输入错误,请重新输入\n");
}
}
#include <stdioh>
#include <stdlibh>
#include <timeh>
void guess()
{
int original;//记录原始数据
int times = 0;//猜测的次数
int guess; //猜测的数字
char e; //退出标志
srand((unsigned)time(NULL));
original = rand()%100; //产生原始数
printf("%d\n",original);
printf("Input the number you guess\n");
times = 0; //开始计数
while (1)
{
scanf("%d",&guess); //输入猜测的数字
if (guess>original) //大于
{
printf("answer is HIGH,try again\n");
times++;
}
else if (guess<original)//小于
{
printf("answer is LOW,try again\n");
times++;
}
else //等于
{
times++; //统计次数
if (times<=7)
{
printf("Congratulation\n");
}
if ((times>7)&&(times<=15))
{
printf("I can bet you can do it better\n");
}
if (times>15)
{
printf("So many times you guess\n");
}
getchar();
scanf("%c",&e);
if (e=='e')
{
break;
}
}
}
}
int main()
{
printf("!!!Welcome to Use!!!\n");
while (1)
{
guess();
}
return 0;
}
#include <stdlibh>
#include <stdioh>
//#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!\r\n",i);
else
{
printf("%d is a correct data\r\n",getNum[i]);
}
}
else
{
if(i == 3&&j==3)
{printf("/small game/\<br/> input 4 data gess the random data,input h can get help\<br/> ");}
}
}
}
void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
/ Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run
/
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 num\r\n");
//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 another\r\n"); break;}
}
if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N\r\n",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语言生成1~100的随机数就很好办了!
附代码如下!#include<stdioh>
#include <stdlibh>
#include <mathh>
int main()
{
index:
printf("请选择是否进行猜数!\n1:是\t0:否\n");
int j;
scanf("%d",&j);
if(j == 0)
return 0;
int i,x = rand() % 100 + 1,input;
for(i = 0;i <10;i ++)
{
printf("请输入猜想的1至100之间的整数:");
scanf("%d",&input);
if(input == x)
{
printf("猜想正确!\t你一共猜想了%d次\n",i + 1);
goto index;
}
if(input < x)
{
if((i + 1) >= 10)
{
printf("\n此次猜想失败!\n");
goto index;
}
else
printf("所猜数过小!\t请再猜一次!\n");
}
if(input > x)
{
if((i + 1) >= 10)
{
printf("\n此次猜想失败!\n");
goto index;
}
else
printf("所猜数过大!\t请再猜一次!\n");
}
}
}
为了简化,使用了goto语句。
c++的,
srand(clock()); // 设置随机数种子
n = rand() %100000; // 生成10万以内随机数,这样更利于测试
然后循环,每次输入一个数,if判断如果大于n,输出大了,continue执行下一次循环;
如果小于n,输出小了,continue执行下一次循环;
如果相等,输出猜对了,return 0;结束程序;
如果是其他语言,思路都一样,我就不写了
以上就是关于用C语言编写一个猜字母的程序全部的内容,包括:用C语言编写一个猜字母的程序、求一个猜数程序(c语言)、用C语言编写一个猜数字游戏的程序(最好能够解释一下)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)