一个c中宏定义的赋值问题,求解答。送分!

lj10193019 2011-07-17 02:19:22

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#define REC 0 //宏定义
int fun1() //定义被调用函数fun1
{
printf("Welcome To The Word Game!\n");
printf("1--Play\n2--Refresh\n3--Record\n4--Exit\n");
return 0;
}
int fun2() //定义被调用函数fun2
{
int a,b,r=0,i,j,fun4(int r); //fun4 函数声明
char ch[4],s[5];
srand(time(0)); //调用srand()函数,产生伪随机数
for(i=0;i<4;i++)
{
s[i]=rand()%10;
s[i]+='0';
for(j=0;j<i;j++)
{
if(s[i]==s[j])break;
}
if(j<i)i--;
}
s[i]='\0';
while(r<8)
{
printf("please input your number:");
a=b=0;
r++;
for(i=0;i<4;i++)
{
ch[i]=getch();
for(j=0;j<i;j++)
if(ch[i]==ch[j]) break;
if(j<i) i--;
else
{
putchar(ch[i]);
for(j=0;j<4;j++)
{
if(ch[i]==s[j])
if(i==j) a++;
else b++;
}
}
}
printf("\t\t%dA%dB\n",a,b);
if(a==4)break;
}
fun4(r); //调用fun4()函数
if(b==4)
printf("Come on!You will win next!\nRight answer is :%s\n",s);
else if(r<=8&&a==4)
printf("Congratulation!\nYour score:%d\n",9-r);
else if(r==8&&a<4&&b<4)
printf("Right answer is :%s\nBlieve yourself!Try again!",s);
return 0;
}
int fun3()
{
fun1(); //调用fun1()函数
printf("The best score : %d\n",REC);
return 0;
}
int fun4(int r)
{
if(REC<9-r)
REC = 9-r; //宏定义赋值错误,不会弄
return REC;
}
void main() //定义主函数
{
int x; //游戏开始
fun1(); //调用fun1函数
do
{
scanf("%d",&x);
switch(x)
{
case 1:fun2();break;
case 2:fun1();break;
case 3:fun3();break;
case 4:break;
}
}while(x!=4); // 若x不等于四,跳出循环
printf("Thanks\n");
} //游戏结束

或者是别的地方出错?我调试的时候就是这不合适!
...全文
423 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lj10193019 2011-07-17
  • 打赏
  • 举报
回复
俺结贴了!
至善者善之敌 2011-07-17
  • 打赏
  • 举报
回复
那楼主结贴吧
lj10193019 2011-07-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xunxun1982 的回复:]
#define REC 0
REC = 9-r;

………………
尴尬啊
[/Quote]
哈哈,刚才查书发现这是个错误,用全局变量就可以了,这个是我同学发过来的,额现在学到这一步我也感觉到很尴尬啊。额这个。
later_equals_never 2011-07-17
  • 打赏
  • 举报
回复
宏还能这么用?
…………
xunxun 2011-07-17
  • 打赏
  • 举报
回复
#define REC 0
REC = 9-r;

………………
尴尬啊
至善者善之敌 2011-07-17
  • 打赏
  • 举报
回复
宏是在编译前就行了响应的替换,
所以你
REC = 9-r; 写相当于 0=9-r;显然不合乎逻辑
干嘛不用全局变量呢
如果非要这样写就

int fun4(int r)
{
int a;
if(REC<9-r)
//REC = 9-r; //宏定义赋值错误,不会弄
a=9-r;
#define REC a;
return REC;
}

69,381

社区成员

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

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