#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
main()
{
int a[4],b[4],i,j;
int num,time=0;
int m=0,n=0,o=0;
int ch=1,chch;
char change[100];
clrscr();
/*clean screen*/
a[0]=rand()%10;
/*get a four number*/
a[1]=rand()%10;
a[2]=rand()%10;
a[3]=rand()%10;
while(1)
/*the number of every position not be same*/
{
if((a[0]==0||a[0]==a[1]||a[0]==a[2]||a[0]==a[3]||a[1]==a[2]||a[1]==a[3]||a[2]==a[3])==0)
{
printf("Notice:The first number is %d:",a[0]);
break;
}
else
{
a[0]=rand()%10;
a[1]=rand()%10;
a[2]=rand()%10;
a[3]=rand()%10;
}
}
while(ch==1) /*circulate for guess or not*/
{
n=0;
m=0;
o=0;
printf("Please input the num:");
scanf("%d",&num);
time++;
while(1) /*input the right number of four*/
{
if(num<1000||num>9999)
{
printf("Number input wrong,input again:");
scanf("%d",&num);
}
else
break;
}
b[0]=num/1000;
/* deposit num to array b*/
b[1]=(num-b[0]*1000)/100;
b[2]=(num-b[0]*1000-b[1]*100)/10;
b[3]=num-b[0]*1000-b[1]*100-b[2]*10;
for(i=0;i<4;i++)
if(a[i]==b[i])
m++;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i]==b[j])
o++;
if(m==4)
{
printf(" Aright Grameover!\n");
printf("You have guess %d times\n",time);
exit(0);
}
else if(time==8)
{
n=o-m;
printf("Sorry,guess wrong:%dA%dB\n",m,n);
printf("You have guess 8 times,do you want again(1-Yes,0-No):");
scanf("%s",&change[100]);
}
else if(time==15)
{
printf("You have guess 15 times.Game Over!\n");
exit(0);
}
else
{
n=o-m;
printf("Sorry,guess wrong: %dA%dB\n",m,n);
printf(" Guess %d times,again(1-Yes,0-No)?",time);
}
do
{
scanf("%s",&change[100]);
switch(change[100])
{
case '0': exit(0);break;
case '1': ch=1;break;
default:{chch=1;ch=0;printf("Input Wrong input again(1-Yes,0-No):");}
}
}while(chch==1&&ch!=1);
}
}
楼主看看啊,可是能运行的哦,我也是新手,有什么问题望楼主和各大高手指教
[
本帖最后由 wibx化吉 于 2010-6-15 15:56 编辑 ]