第3章 数据决定程序结构

1. 本书出版之时,美国的个人收入所得税分为5种不同的税率,其中最大的税率大约为40%.以前的情况则更为复杂,税率也更高。下面所示的程序文本采用25个if语句的合理方法来计算1978年的美国联邦所得税。税率序列为0.14, 0.15, 0.16, 0.17, 0.18.....。序列中此后的计算大于0.01.有何建议呢?

if income <= 2200

    tax = 0;

else if income <= 2700

    tax = 0.14 * (income - 2200);

else if income <= 3200

    tax = 70 + 0.15 * (income - 2700);

else if income <= 3700

    tax = 145 + 0.16 * (income -3200);

else if income <= 4200

    tax =225 +  0.17 * (income - 3700);

    .......

else

    tax =53090 +  0.70 * (income - 102200);

    

可以将税率等级、基本税收、税率、税收下界定义在一个结构体里,并初始化每个税率级别的相关参数。对于任何个人收入,计算其税率等级level,然后根据等级使用二分搜索。获取其对应的基本税收、税率、税收下界。

typedef struct _TaxItem
{
	int level;
	int lowerbound;
	int basetax;
	int taxrate;
}TaxItem, * pTaxItem;

	if (income <= 2200)
	{
		level = 0;
	}
	else
	{
		level = (income - 2200) / 501 + 1;
	}



你可能感兴趣的:(struct,出版)