向c语言高手求教---文曲星上的猜数字的游戏怎么就实现不出来呢?

nifengfeiyang2 2007-11-21 07:09:51
/*用c语言编写文曲星上的猜数字的游戏的代码*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int compare1(int c[4],int d[4]);
int compare2(int c[4],int d[4]);
int a[4],b[4],i,m,n,k=1,x,y;
srand(time(NULL));
a[0]=rand()%10;
a[1]=rand()%10;
a[2]=rand()%10;
a[3]=rand()%10;
printf("how many times i want to guess");
scanf("%d",&m);
while(k<=m)
{
for(n=0;n<4;n++)
scanf("%d",&b[k]);
x=compare1( a[4], b[4]);
y=compare2( a[4], b[4]);
printf("%dA%dB",x,y);
if(x=4)
{
printf("congratulation!");
break;

}
}
printf("sorry i have not guess that right number");
printf("the right number is ");
for(i=0;i<=4;i++)
printf("%d",a[i]);
getch();

}
int compare1(int c[4],int d[4])
int i,j;
for(i=0;i<4;i++)

{
if(c[i]=d[i])
x++;
}
int compare2(int c[4],int d[4])
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(c[i]=d[j])
y++;
}
...全文
280 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kekedou 2007-11-21
  • 打赏
  • 举报
回复
int compare1(int c[4],int d[4])
int i,j;
for(i=0;i <4;i++)

{
if(c[i]=d[i])
x++;
}
int compare2(int c[4],int d[4])
int i,j;
for(i=0;i <4;i++)
for(j=0;j <4;j++)
{
if(c[i]=d[j])
y++;
}

你这里的几个打括号是不是加的地方不对啊。
kekedou 2007-11-21
  • 打赏
  • 举报
回复
我写了一个,你可以看一下。


#include<time.h>
#include<stdio.h>
int ans[4];
int cs(int a,int b,int c,int d)
{ int x=0;
if (a==b) x=1;
if (a==c) x=1;
if (a==d) x=1;
if (b==c) x=1;
if (b==d) x=1;
if (c==d) x=1;
if (a==0) x=1;
return(x);
}
void ank()
{
int i,a,b,c,d;
srand(time(0));
a=0;
a=(rand()+11)%10;
do
{ b=(rand()+10)%10;
c=(rand()+10)%10;
d=(rand()+10)%10;
} while(cs(a,b,c,d));
ans[0]=a;
ans[1]=b;
ans[2]=c;
ans[3]=d;
}
int try(int w,int x,int y,int z)
{ int ia=0,ib=0,c=0;
if (ans[0]==w && ans[1]==x && ans[2]==y && ans[3]==z)
{ c=1; goto loop_1;}
if (w==ans[0]) ia++;
if (x==ans[1]) ia++;
if (y==ans[2]) ia++;
if (z==ans[3]) ia++;
if (ans[0]==x || ans[0]==y || ans[0]==z) ib++;
if (ans[1]==w || ans[1]==y || ans[1]==z) ib++;
if (ans[2]==w || ans[2]==x || ans[2]==z) ib++;
if (ans[3]==w || ans[3]==x || ans[3]==y) ib++;
printf("\nThe number you input is wrong\n\n");
printf("But %d are right and %d put in wrong places !\n",ia,ib);
loop_1: return(c);
}
void main()
{
int iia,ti=0,temp=0,iib[4];
clrscr();
ank();
printf("Please Input a 4 vers nember for guessing !\nYou have 8 chances !\n");
printf("If you want exit by midway please input 0 !\n\n\n");
for (;ti<=7;ti=ti+1)
{
rou: printf("Your %dth chance: ",ti+1);
scanf("%d",&iia);
if (iia==0) goto rou2;
printf("\n");
if (iia<1000 || iia>9999)
{printf("\nThe nember you inputed is wrong !\nPlease input again !\n");
goto rou;};
iib[3]=iia%10;iia=iia/10;
iib[2]=iia%10;iia=iia/10;
iib[1]=iia%10;iia=iia/10;
iib[0]=iia;
if (cs(iib[0],iib[1],iib[2],iib[3]))
{printf("\nThe nember you inputed is wrong !\nPlease input again !");
goto rou;}
temp=try(iib[0],iib[1],iib[2],iib[3]);
if (temp) break;
}
if (temp==1) printf("\nYes! you made it in %d times !",ti);
else printf("\nSorry ! You lose your chance !");
rou2: printf("\nThe number is %d%d%d%d",ans[0],ans[1],ans[2],ans[3]);
getchar();
getch();
}
独孤过儿 2007-11-21
  • 打赏
  • 举报
回复
给你写个小例子,看明白就会修改你的了:

#include <stdio.h>

void print(int ay[], int s); //函数声明

int main()
{
int array[5] = {1, 2, 3, 4, 5}; //定义数组
int size = 5;
print(array, size); //调用函数,传递数组参数

return 0;
}

void print(int ay[], int s) //函数定义
{
for(int i = 0; i < s; i++)
{
printf("%d ", ay[i]);
}
printf("\n");
}
nifengfeiyang2 2007-11-21
  • 打赏
  • 举报
回复
对,我就是对数组的定义调用声名时三者的形式搞不清,还请指教!(谢谢)
独孤过儿 2007-11-21
  • 打赏
  • 举报
回复
C不支持函数的嵌套定义,所以:

int compare1(int c[4],int d[4]);
int compare2(int c[4],int d[4]);

要拿到main()的外面。

另外,如果你是想这两个函数接受int型的参数,要这样写:

int compare1(int c, int d);
int compare2(int c, int d);

如果是想传过来一个数组,要这样写:

int compare1(int c[], int d[]);
int compare2(int c[], int d[]);

而在调用的时候,要这样写:

x=compare1(a, b);
y=compare2(a, b);

还有,if判断里面是==,不是=

还有,后面的函数定义里面x和y是哪里来的?你要使用它,总得先定义或者传递过来啊

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧