C语言初试(三)

  按理说,接下来只需要排除异常数据干扰和做界面了,但总感觉函数都太笨重了,需要整理一下。用结构体来记录town的状态就可以了。

  gold、material、luxury、research 这些是不需人为控制的变量……合并到 resource

  needgold、needmat、needlux、needres 这些是另一个时间段上的上述变量

  matwork、luxwork、scient 这些是需要人为控制的变量,也是程序的输出……合并到 arrange

  population、satisfaction、houses 这些是 town 的属性……合并到 town

typedef struct
{
	int gold;
	int material;
	int luxury;
	int research;
}resource;

typedef struct
{
	int matworker;
	int luxworker;
	int scientist;
	
	float time;
}arrange;

typedef struct
{
	int population;
	int satisfaction;
	int houses;
}town;

 相应地,函数有所变化:

float gettfull(town polis){/*...*/}

resource increase(resource now,town polis,arrange plan){/*...*/}

arrange getplan(resource now,resource need,town polis){/*...*/}

void input(resource *now,resource *need,town *polis){/*...*/}

void output(arrange plan,resource get){/*...*/}

 这样下来,main函数就简洁多了:

main()
{
	resource now,need,get;
	town polis;
	arrange plan;
	
	input(&now,&need,&polis);
	
	plan=getplan(now,need,polis);
	get=increase(now,polis,plan);
	
	output(plan,get);
}

 到现在,这个核已经完成了。

 

 做界面的话,就得把几年前看的那一点MFC拿来用了,我是一定要把它做出来的,哪怕现学现卖都好。

 

 我到给忘了,还要排除异常的啊……

 

1.学院不够大,计算得到的 scientist 不能实现时:

  这种情况下,research points 不可能与其他 resources 同时达到要求,对玩家的建议应该是:

“fufill the academy,”

同理,当岛上的矿等级不足时,有:

“fufill the sawmill,”

“fufill the Quarry(Vine、Sulfur pit、Crystal mine),”

这样的提示。

但是,这些并不是必要数据,所以,不宜在 input 函数中要求用户输入。

 

可以显示结果后,提示用户“Is there anything wrong?”然后做多选题

(1) my academy is not big enough.

(2) the sawmill is not big enough.

(3) the Quarry(Vine、Sulfur pit、Crystal mine) is not big enough.

根据选择的情况来要求用户输入相应的数据,再做出相应的提示。

 

2.population+satisfaction <= houses 时,gettfull 会抛出 log 函数的异常:

 

 

 

 

 避免之:

 

if(h-p<=s)
	t=-50*log(1-(h-p)/s);
else
	t=-1.0

 

3.当satisfaction较大时,求方程A=Bt+Cexp(-0.02t)的解的过程中溢出了:

 

 

 

 

 

 

 

 

 

 真无语了!为什么 C 的 int 的上限是如此的小!

 

 

 

 C++的 int 要顺手多了。

 

 

 

 

 

A=1.0*(need.gold-now.gold)+3.0*(need.material-now.material)+3.0*(need.luxury-now.luxury)+9.0*(need.research-now.research)+150.0*polis.satisfaction;

 用 float 常量代替 int 常量就可以了。

 

4.needgold <= gold 时,

一样可以用这个算法得到 arrange.time

但是,会不会出现负数解呢?

A-Bt=Cexp(-0.02t) 当A<C时,出现负数解;既然如此,A==C 时,自然出现 0 解,这表明matwork+luxwork+scient 可能比 population 还大,这种情况下,可以按比例减少matwork、luxwork、scient的数量,达到 matwork+luxwork+scient==population 的状态。

分 A<=C、A>C 两种情况计算……

  实际上,当 A<=C 或 (A>C 且matwork+luxwork+scient>=population) 或 Gold(t)的极小值小于 0 时,都不能用Gold(t)==need.gold  来处理。这样看来,以上的解耦是失败的。得重新做一个算法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值