c语言 结构体程序求修改

#include<time.h>#include<stdio.h>#include<stdlib.h>structhuman{intblood;boolstate;//状... #include <time.h>
#include <stdio.h>
#include <stdlib.h>

struct human
{
int blood;
bool state;//状态
int rate;
};

#define ID 1 //角色ID
#define ID1 2 //角色ID
#define ID2 3 //角色ID
#define ID3 4  //角色ID

int num=0; //回合数
struct human one={400,0,50};
struct human two={400,0,50};
struct human three={400,0,50};
struct human four={400,0,50};

void show(human &h)
{
printf("第%d回合\n角色%d 体力%d %s 回复率%d%%\n",++num,ID,h.blood, h.state?"中毒":"正常",h.rate);
}
void show1(human &h1)
{
printf("\n角色%d 体力%d %s 回复率%d%%\n",ID1,h1.blood, h1.state?"中毒":"正常",h1.rate);
}
void show2(human &h2)
{
printf("\n角色%d 体力%d %s 回复率%d%%\n",ID2,h2.blood, h2.state?"中毒":"正常",h2.rate);
}
void show3(human &h3)
{
printf("\n角色%d 体力%d %s 回复率%d%%\n",ID3,h3.blood, h3.state?"中毒":"正常",h3.rate);
}

int main()
{
srand( (unsigned)time( NULL ) );
char ch;

show(one);
show1(two);
show2(three);
show3(four);

while(ch=getchar())
{
if(ch==10)
{
int r=rand();
if(r>16384)
{
one.state=1;
one.blood=one.blood-r/100;
if(one.blood<=0)
{
one.blood=0;

}
}
else one.state=0;
show(one);

if(r<16384)
{
two.state=1;
two.blood=two.blood-r/130;
if(two.blood<=0)
{
two.blood=0;

}
}
else two.state=0;
show1(two);

if(r>16384)
{
three.state=1;
three.blood=three.blood-r/105;
if(three.blood<=0)
{
three.blood=0;

}
}
else three.state=0;
show2(three);

if(r<16384)
{
four.state=1;
four.blood=four.blood-r/150;
if(four.blood<=0)
{
four.blood=0;
show3(four);
return 0;
}
}
else four.state=0;
show3(four);

}
}
return 0;
}
问题就一个
在上面程序输出格式不改变的情况下 状态 就是正常 和 中毒 采用函数指针形式来判 定
展开
 我来答
百度网友b79519e
推荐于2016-11-23 · TA获得超过3399个赞
知道大有可为答主
回答量:1417
采纳率:100%
帮助的人:1309万
展开全部
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#define ID 1
#define ID1 2
#define ID2 3
#define ID3 4
struct human
{
int blood;
int state;//状态
int rate;
};
int num=0; //回合数
struct human one={400,0,50};
struct human two={400,0,50};
struct human three={400,0,50};
struct human four={400,0,50};
void show(struct human h)
{
printf("第%d回合\n角色%d 体力%d %s 回复率%d%\n",++num,ID,h.blood,h.state?"正常":"中毒",h.rate);
}
void show1(struct human h1)
{
printf("\n角色%d 体力%d %s 回复率%d%%\n",ID1,h1.blood,h1.state?"正常":"中毒",h1.rate);
}
void show2( struct human h2)
{
printf("\n角色%d 体力%d %s 回复率%d%%\n",ID2,h2.blood, (h2.state ? "中毒":"正常"),h2.rate);
}
void show3(struct human h3)
{
printf("\n角色%d 体力%d %s 回复率%d%%\n",ID3,h3.blood,h3.state?"正常":"中毒",h3.rate);
}
int main()
{
char ch;
srand( (unsigned)time( NULL ) );
show(one);
show1(two);
show2(three);
show3(four);
while(ch=getchar())
{
if(ch==10)
{
int r=rand();
if(r>16384)
{
one.state=1;
one.blood=one.blood-r/100;
if(one.blood<=0)
{
one.blood=0;
}
}
else one.state=0;
show(one);
if(r<16384)
{
two.state=1;
two.blood=two.blood-r/130;
if(two.blood<=0)
{
two.blood=0;
}
}
else two.state=0;
show1(two);

if(r>16384)
{
three.state=1;
three.blood=three.blood-r/105;
if(three.blood<=0)
{
three.blood=0;

}
}
else three.state=0;
show2(three);

if(r<16384)
{
four.state=1;
four.blood=four.blood-r/150;
if(four.blood<=0)
{
four.blood=0;
show3(four);
return 0;
}
}
else four.state=0;
show3(four);

}
}
return 0;
}
你的程序我基本上看懂了,上面的代码能够正常执行
1.c语言不支持bool类型,所以定义state时,应该定义成int state
2.char ch,应该写在函数前面,因为c语言不支持变量的随定义随引用(即如果要使用一个变量,只需先写一定义变量的语句即可,并不需要在函数开头定义)
3.c语言不支持引用类型,引用是c++增加的特性
void show(human &h)
应该写成void show( struct human h)
4.较低版本的c语言不支持直接使用自定义的结构体类型定义变量,结构体类型前还需要加struct关键字 如human one是不行的,必须是struct human one
更多追问追答
追问
大神 我想问的是 如果状态的判定用 指针来做怎么做好 请大神赐教
追答
我认为你的程序很不错了,用结构体就很好了
我不知道你为什么一定要用指针
如果想学指针的话,有很多的例子程序,你的这个并不是最好的例子...
风随南寒
2011-06-14 · TA获得超过702个赞
知道小有建树答主
回答量:308
采纳率:0%
帮助的人:150万
展开全部
指针木有初始化。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式