A. 计算某个城市的所得税(改进一)
这是用来计算某个城市的所得税的。假设现在税收政策有了变化:收入在10000元以上(不包括10000元)的才需要交纳城市所得税,收入在10000以下的可以免除所得税,则应对例3-1稍加改动。
问题的伪编译算法如下。
print "Enter gross income: "
read gross_income
if gross income is greater than $10 000
compute city_tax = 0.0175 * gross_income
else
set city_tax to 0
end_if
print city_tax
也许读者已经注意到了,这个算法与例3-1的算法相比,唯一的改动就是选择结构:
if gross income is greater than $10 000
compute city_tax = 0.0175 * gross_income
else
set city_tax to 0
end_if
用if语句来实现这个选择结构,整个程序如下。
#include <iostream.h>
const double CITY_TAX_RATE = 0.0175;
void main()
{
double gross_income;
double city_tax;
cout << "Enter gross income: ";
cin >> gross_income;
if (gross_income > 10000.00)
city_tax = CITY_TAX_RATE * gross_income;
else
city_tax = 0.0;
// end if
cout << "City tax is " << city_tax << " dollars. ";
}
//end function main
这段程序与例3-1唯一的不同之处就在于if语句:
if gross_income > 10000
city_tax = CITY_TAX_RATE * gross_income;
else
city_tax = 0.0;
// end if
关键字if之后是一个简单条件表达式:
gross_income > 10000;
该表达式将变量gross_income的值和10000进行比较。其中,>对应于伪编译代码中的“greater than”,它是C++中的一个关系操作符。如果变量gross_income的值大于10000,条件表达式的值为真,将执行语句
city_tax = CITY_TAX_RATE * gross_income;
反之,如果gross_income小于10000,条件表达式为假,计算机将跳过上面这条语句,而执行关键字else后的语句:
city_tax = 0.0;
将变量city_tax的值置0。这样,if语句就实现了双路选择结构。该if语句的流程图如图5-1所示。
图1 if部分的流程图
B. 判断用户输入的数是否能被3整除(一)
下面这段程序的功能是判断用户输入的数是否能被3整除。
1 #include <iostream.h>
2
3 void main()
4 {
5 int number;
6
7 cout << "/nEnter an integer: ";
8 cin >> number;
9
10 if (number % 3 != 0)
11 cout << "Your integer is not dividable by 3. ";
12 else
13 cout << "Your integer is dividable by 3. ";
14 // end if
15
16 cout << "/nTerminating. . . ";
17
18 }
19 // end function main
第10~14行的if语句是整个程序的关键部分,将判断number能否被3整除。表达式
number % 3 != 0
中使用了关系操作符!=,将number除以3所得的余数与0进行比较。
若number不能被3整除,其余数应为1或2,此时该表达式为真,if语句的then-部分即第11行将被执行,在屏幕上显示信息:
Your integer is not dividable by 3.
然后,跳过else部分转至16行继续执行。如果number能被3整除,表达式number % 3的值为0,表达式
number % 3 != 0
的值为假,if语句的else部分将被执行,在屏幕上显示:
Your integer is dividable by 3.
然后,执行第16行的输出语句。
C. 判断用户输入的数是否能被3整除(二)
对B中的程序稍加改动:如果输入的数能被3整除就显示相应信息,如果不能则不显示任何信息。
1 #include <iostream.h>
2
3 void main()
4 {
5 int number;
6
7 cout << "/nEnter an integer: ";
8 cin >> number;
9
10 if (number % 3 == 0)
11 cout << "Your integer is dividable by 3. ";
12 // end if
13
14 cout << "/nTerminating. . . ";
15
16 }
17 // end function main
第10~12行的if语句没有else部分,then部分只有一条语句,如第11行。如果表达式
number % 3 == 0
返回非0值时将执行第11行的语句。
有些情况下,选择表达式为真时不需要进行任何操作,选择表达式为假才需要进行处理。这时就应该使用then部分为空的if语句。下面的程序说明了这种用法。
1 #include <iostream.h>
2
3 void main()
4 {
5 int number;
6
7 cout << "/nEnter an integer: ";
8 cin >> number;
9
10 if (number % 3 == 0)
11 ;
12 else
13 cout << "Your integer is nor dividable by 3. ";
14 // end if
15
16 cout << "/nTerminating. . . ";
17
18 }
19 //end function main
该程序的功能是在变量number不能被3整除时在屏幕上显示出相应信息。if语句中的条件表达式与B中的相同:
number % 3 == 0
如果变量number的值能被3整除,该条件表达式值为真,反之为假。由于这里仅关心不能被3整除的数,那么对于能被3整除的数则不进行任何操作,所以if语句的then部分是空语句。C++中单独一个逗号就表示空语句,空语句不执行任何操作。第13行的输出语句为if语句的else部分,在选择表达式取0值的时候将被执行。
再来看一个例子:
1 #include <iostream.h>
2
3 void main()
4 {
5 int number;
6
7 cout << "/nEnter an integer: ";
8 cin >> number;
9
10 if (number % 3)
11 cout << "Your integer is not dividable by 3. ";
12 else
13 cout << "Your integer is dividable by 3. ";
14 // end if
15
16 cout << "/nTerminating. . . ";
17
18 }
19 // end function main
与B中的程序相比,其中if语句的选择表达式变为
number % 3
但这两个程序实际上是完全等价的。如果变量number的值不能被3整除,该算术表达式的值为1或2。由于非0值在C++中都被认为“真”,if语句的then部分将在屏幕上显示:
Your integer is not dividable by 3.
如果number能被3整除,则该表达式的值为0,系统认为为“假”,将执行第13行的输出语句。
下面的程序将读取一个整数,并判断它是否为线性方程式的解。
1 #include <iostream.h>
2
3 void main()
4 {
5 int number;
6
7 cout << "/nEnter an integer: ";
8 cin >> number;
9
10 if (3 * number – 15)
11 {
12 cout << "Your integer is not the solution of 3x – 15 = 0. ";
13 cout << "/nThe computed value of the expression 3x – 15 is: "
14 << (3 * number – 15);
15 }
16 else
17 cout << "Your integer is the solution of 3x – 15 = 0. ";
18 // end if
19
20 cout << "/nTerminating. . . ";
21
22 }
23 //end function main
执行if语句时,将首先计算算术表达式
3 * number – 15
的值。若值不为0,则执行if语句的then部分。if语句的then部分是一个由两条输出语句组成的复合语句,大括号标志着复合语句的起止。如果计算得表达式的值为0,程序将跳过复合语句,转而执行第17行的else部分。不管表达式为逻辑真或逻辑假,执行完if语句后都将执行第20行的cout语句。
D. 计算某个城市的所得税(改进二)
再来看看计算城市所得税的例子。现在用户要求无需重新编译就能进行多次计算,则这个问题的算法用伪码表示如下。
while the user wants to continue
begin
print "Enter gross income: "
read gross_income
if gross income is greater than $10,000
compute city_tax = 0.0175 * gross_income
else
set city_tax to 0
end_if
print city_tax
end
end_while
将该算法进一步细化:引入变量want_to_continue以控制循环的结束,初始化为y,表示需要继续计算。细化后的算法如下。
set want_to_continue to 'y'
while want_to_continue is equal to 'y'
begin
print "Enter gross income: "
read gross_income
if gross income is greater than $10 000
compute city_tax = 0.0175 * gross_income
else
set city_tax to 0
end_if
print city_tax
print "Do you want to continue? (y/n): "
read want_to_continue
end
end_while
下面是对该算法对应的C++程序。
#include <iostream.h>
const double CITY_TAX_RATE = 0.0175;
void main()
{
double gross_income;
double city_tax;
char want_to_continue;
want_to_continue = 'y';
while (want_to_continue == 'y')
{
cout << "Enter gross income: ";
cin >> gross_income;
if (gross_income > 10000.00)
city_tax = CITY_TAX_RATE * gross_income;
else
city_tax = 0.0;
// end if
cout << "City tax is " << city_tax << " RMB. ";
cout << "/nDo you want to continue? Type y/n: ";
cin >> want_to_continue;
}
// end while
}
//end function main
该程序较以前的程序只添加了以下代码:
char want_to_continue;
want_to_continue = 'y';
while (want_to_continue == 'y')
{
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
cout << "/nDo you want to continue? Type y/n: ";
cin >> want_to_continue;
}
// end while
其中,定义语句
char want_to_continue;
定义循环控制变量want_to_continue为字符型,语句
want_to_continue = 'y';
将其初始化为y,接着是while语句。while语句是C++中的一种循环语句,以关键字while开头,关键字while后是括号括起来的条件表达式,然后是一个复合语句。复合语句就是循环体,其中的内容将被多次执行。
这里的循环控制语句是条件表达式
want_to_continue == 'y';
只要该表达式为逻辑真,即变量want_to_continue的值为y,循环体将被重复执行。第一次循环计算出city_tax的值后,计算机将执行循环体中最后两条语句:
cout << "/nDo you want to continue? Type y/n: "
cin >> want_to_continue;
由cout语句询问用户是否需要继续计算,用户应输入一个字符值,然后计算机再次计算循环控制表达式。若用户输入字符为y,则循环继续进行;若用户输入的是其他字符,计算机将终止循环。
E. 计算给定半径的圆的面积或周长
下面的程序的功能是计算给定半径的圆的面积或周长。
#include <iostream.h>
const double PI = 3.141594;
void main()
{
double radius;
double area, circumference;
int choice;
cout << "Type 1 if you want area of circle, "
<< "/nType 2 if you want circumference of circle: ";
cin >> choice;
if (choice == 1)
goto area_comp;
else
if (choice == 2)
goto circum_comp;
else
cout << "/nWrong choice! ";
// end if
// end if
goto termin;
area_comp: area = PI * radius * radius;
cout << "/nArea is: " << area;
goto termin;
circum_comp: circumference = 2 * PI * radius;
cout << "/nCircumference is: " << circumference;
termin: cout << "/nTerminating. . . ";
}
// end function main
上面程序中使用了4条goto语句,其实不使用goto语句也可以完成同样的功能,而且这样更易于理解,程序的调试和维护也容易得多,如下所示。
#include <iostream.h>
const double PI = 3.141594;
void main()
{
double radius;
double area, circumference;
int choice;
cout << "/nEnter radius of circle: ";
cin >> radius;
cout << "Type 1 if you want area of circle, "
<< "/nType 2 if you want circumference of circle: ";
cin >> choice;
if (choice == 1)
{
area = PI * radius * radius;
cout << "/nArea is: " << area;
}
else
if (choice == 2)
{
circumference = 2 * PI * radius;
cout << "/nCircumference is: " << circumference;
}
else
cout << "/nWrong choice! ";
// end if
// end if
cout << "/nTerminating. . . ";
}
// end function
F. 根据税率表计算联邦所得税
这里将编写一个根据税率表计算联邦所得税的完整应用程序。根据前面所学的知识,可得到如下的程序。
1 /***********************************************************
2 Program Filename : Prog05_2.cpp
3 Date : August 27th, 2002
4 Purpose : Computes Federal Income Tax for 1991 from Tax Rate
5 Schedules
6 Input from : Keyboard
7 Output to : Screen
8 ***********************************************************/
9
10 /*****************************************************************
11 TAX SCHEDULE FORMULAS:
12
13 Filing Status Taxable Income Tax
14 Over But Mot
15 Over
16 ------------- ------- ------- --------------------------------------------------
17 Single 49 300 ------- 11 158.50 + 31% of amount over 49 300
18
19 Married filing 34 000 82 150 5 100.00 + 28% of amount over 34 000
20 jointly 82 150 ------ 18 582.00 + 31% of amount over 82 150
21
22 Married filing 41 075 ------ 9 291.00 + 31% of amount over 41 075
23 separately
24
25 Head of house- 27 300 70 450 4,095.00 + 28% of amount over 27 300
26 hold 70 450 ------ 16 177.00 + 31% of amount over 70 450
27 ***************************************************************/
28
29 //..................................................................................................................................
30
31 // Preprocessor Directives:
32
33 #include <iostream.h>
34
35 // Constant Definitions:
36
37 const double LOWRATE = 0.28;
38 const double HIGHRATE = 0.31;
39
40 //..................................................................................................................................
41 // Main Program:
42
43 void main()
44 {
45 // Variable declarations:
46
47 int status;
48 double income;
49 double tax;
50 char moreTaxComputations;
51 char correctStatusInput;
52 char correctIncomeInput;
53
54 // Function body:
55
56 moreTaxComputations = 'y';
57
58 while (moreTaxComputations == 'y')
59 {
60 // Begin input
61
62 correctIncomeInput = 'n';
63
64 while (correctIncomeInput == 'n')
65 {
66 cout << "/nEnter taxable income; "
67 << "should not be less than $50,000: ";
68 cin >> income;
69
70 if (income >= 50000.00)
71 correctIncomeInput = 'y';
72 // end if
73 }
74 // end while
75
76 correctStatusInput = 'n';
77
78 cout << "/n******************************************"
79 << "/n* FILING STATUS MENU: *"
80 << "/n* Single ----- 1 *"
81 << "/n* Married filing jointly ----- 2 *"
82 << "/n* Married filing separately ----- 3 *"
83 << "/n* Head of household ----- 4 *"
84 << "/n******************************************"
85 << "/n";
86
87 while (correctStatusInput == 'n')
88 {
89 cout << "/nEnter filing status; should be between 1 and 4: ";
90 cin >> status;
91
92 if (status > 0)
93 if (status < 5)
94 correctStatusInput = 'y';
95 // end if
96 // end if
97 }
98 // end while
99
100 // End input
101
102 // Begin tax computations
103
104 if (status == 1)
105 tax = 11158.50 + HIGHRATE * (income - 49300.00);
106 // end if
107
108 if (status == 2)
109 if (income <= 82150)
110 tax = 5100.00 + LOWRATE * (income - 34000.00);
111 else
112 tax = 18582.00 + HIGHRATE * (income - 82150.00);
113 // end if
114 // end if
115
116 if (status == 3)
117 tax = 9291.00 + HIGHRATE * (income - 41075.00);
118 // end if
119
120 if (status == 4)
121 if (income <= 70450.00)
122 tax = 4095.00 + LOWRATE * (income - 27300.00);
123 else
124 tax = 16177.00 + HIGHRATE * (income - 70450.00);
125 // end if
126 // end if
127
128 // End tax computations
129
130 // Begin output
131
132 cout << "/nRESULTS OF COMPUTATIONS: ";
133 cout << "/n Taxable income: " << income;
134
135 cout << "/n Filing Status : ";
136
137 if (status == 1)
138 cout << "Single";
139 // end if
140
141 if (status ==2)
142 cout << "Married filing jointly";
143 // end if
144
145 if (status == 3)
146 cout << "Married filing separately";
147 // end if
148
149 if (status == 4)
150 cout << "Head of household";
151 // end if
152
153 cout << "/n Tax : " << tax;
154
155 // End output
156
157 cout << "/n/nMore tax computations? y/n: ";
158 cin >> moreTaxComputations;
159
160 }
161 // end while
162
163 }
164 // end function main
现在对上面的程序进行分析:
(1)第1行到27行都是注释语句。注释语句的使用可完善程序文档。
(2)第29行和第40行是两个起分隔作用的注释行,它有助于突出显示程序的代码。
(3)第33行是一条预处理指令,为程序中使用的流定向输入输出导入头文件iostream.h。
(4)税率表的公式中需要使用两个常量:0.28和0.31。第37行和第38行的程序分别将这两个常量定义为LOWRATE和HIGHRATE。
(5)这个应用程序是一个整体,它仅包括一个主函数,而没有其他函数。主函数从第43行开始,到164行结束。
(6)第47~52行的代码定义了6个变量。值得借鉴的是这6个变量的标识符都反映出了相应变量的内容。
(7)算法要求:只要用户在提示下输入的值为y,程序就应该继续执行下去。因此,第56行的语句将变量moreTaxComputations的初值设为y,使得第58行的while循环能被执行。while语句的循环控制表达式为
moreTaxComputations == 'y'
(8)计算之前首先要做的就是输入,第60~100行的语句块就是完成这个功能的。第62行的语句将为变量correctIncomeInput赋初值。第64~74行的while语句对输入数据的合法性进行检查,以保证输入的数大于等于50000。注意,程序中第62行的赋值语句首先假定输入的数是错误的。若用户输入的值是正确的,则由第70~72行的if语句把变量correctIncomeInput置为y,然后程序将跳出while循环,继续执行下面的语句。
(9)第78~98行的语句提示用户输入代表纳税人家庭情况的数值,并对用户输入值进行合法性检查。第78~85行的输出语句会在屏幕上显示所有的状态列表。用户完成输入后,第87~98行的while循环将检查用户输入的状态值是否为1,2,3或4中的某一个。如果是,第92~96行的嵌套if语句将变量correctStatusInput的值置为y。while语句中的循环控制表达式为
correctStatusInput == 'n'
此时该表达式值为0即逻辑假,循环终止。现在income和status中都有了合法的值,可以开始计算了。
(10)第102~128行的语句块就是税率公式表在C++中的实现。它包含四条连续的if语句,其中第二个和第四个if语句中又分别嵌套了另外两个if语句。因此,这四条语句实际上确定了六个分支,正好对应着税率表中的六个公式。
(11)第130~155行之间的语句将计算结果输出到屏幕上,格式为:标题、收入、家庭情况,最后是计算所得的税值。其中,由四个连续的if语句动态地将变量status的值转化为文字描述的状态,由153行的cout语句输出计算所得的税值。
(12)第157和158行的语句询问用户是否继续计算。若用户输入y,第58行while语句中循环控制表达式为逻辑真,程序将进行另一次循环计算。否则,跳出while循环,结束程序的执行。
下面是该程序的交互执行过程示意图。
Enter taxable income; should not be less than $50000: 45000
Enter taxable income; should not be less than $50000: 57000
************************************
* FILING STATUS MENU: *
* Single ---- 1 *
* Married filing jointly ---- 2 *
* Married filing separately ---- 3 *
* Head o£ household ---- 4 *
************************************
Enter filing status; should be between 1 and 4: 5
Enter filing status; should be between 1 and 4: 1
RESULTS OF COMPUTATIONS:
Taxable income : 57000
Filing Status : Single
Tax : 13545.5
More tax computations? y/n: y
Enter taxable income; should not be less than $50000: 71373.65
**************************************
* FILING STATUS MENU: *
* Single ---- 1 *
• Married filing jointly ---- 2 *
• Married filing separately ---- 3 *
* Head of household ---- 4 *
**************************************
Enter filing status; should be between 1 and 4: 0
Enter filing status; should be between 1 and 4: 4
RESULTS OF COMPUTATIONS:
Taxable income : 71373.65
Filing Status : Head of household
Tax : 16463.3315
More tax computations? y/n: n