C++程序设计 (第三版) 谭浩强 习题3.13
习题 3.13 企业发放的奖金根据利润提成。利润 i i i低于或等于10万元的,奖金可提10%;利润高于10万元,低于20万元( 100000 < i ≤ 200000 100000 \lt i \le 200000 100000<i≤200000)时,低于10万元的部分按10%提成,高于100000元的部分,可提成7.5%; 200000 < i ≤ 400000 200000 \lt i \le 400000 200000<i≤400000时,低于20万元的部分仍按上述办法提成(下同)。高于20万元的部分按5%提成; 400000 < i ≤ 600000 400000 \lt i \le 600000 400000<i≤600000元时,高于40万元的部分按3%提成; 600000 < i ≤ 1000000 600000 \lt i \le 1000000 600000<i≤1000000时,高于60万元的部分按1.5%提成; i > 1000000 i \gt 1000000 i>1000000时,超过100万元的部分按1%提成。从键盘输入当月利润 i i i,求应发奖金总数。
要求:
(1)用if语句编程序;
(2)用switch语句编程序。
IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。
代码块
1. 用if语句
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
double i, bonus;
cout<<"Please enter profit: ";
cin>>i;
//输入低于0的数字,报错并重新输入
while(i < 0){
cout<<"Error!\n"<<"Please enter profit: ";
cin>>i;
}
if(i <= 100000)
bonus = i * 0.1;
else if(i > 100000 && i <= 200000)
bonus = 10000 + (i - 100000) * 0.075;
else if(i > 200000 && i <= 400000)
bonus = 17500 + (i - 200000) * 0.05;
else if(i > 400000 && i <= 600000)
bonus = 27500 + (i - 400000) * 0.03;
else if(i > 600000 && i <= 1000000)
bonus = 39500 + (i - 600000) * 0.015;
else
bonus = 48500 + (i - 1000000) * 0.01;
cout<<"Bonus= "<<setiosflags(ios::fixed)<<setprecision(2)<<bonus<<endl;
system("pause");
return 0;
}
2. 用switch语句
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int i, s;
double bonus;
cout<<"Please enter profit: ";
cin>>i;
while(i < 0){
cout<<"Error!\n"<<"Please enter profit: ";
cin>>i;
}
//通过分档给各档级别赋值
if(i <= 100000)
s = 1;
else if(i > 100000 && i <= 200000)
s = 2;
else if(i > 200000 && i <= 400000)
s = 3;
else if(i > 400000 && i <= 600000)
s = 4;
else if(i > 600000 && i <= 1000000)
s = 5;
else
s = 6;
//根据级别值执行相应语句
switch(s){
case 1: bonus = i * 0.1; break;
case 2: bonus = 10000 + (i - 100000) * 0.075; break;
case 3: bonus = 17500 + (i - 200000) * 0.05; break;
case 4: bonus = 27500 + (i - 400000) * 0.03; break;
case 5: bonus = 39500 + (i - 600000) * 0.015; break;
case 6: bonus = 48500 + (i - 1000000) * 0.01; break;
}
cout<<"Bonus= "<<setiosflags(ios::fixed)<<setprecision(2)<<bonus<<endl;
system("pause");
return 0;
}
3. 使用函数的模块化设计
#include <iostream>
#include <iomanip>
using namespace std;
void inputProfit(double *profit){
cout<<"Enter Profit: ";
cin>>*profit;
while(*profit < 0){
cout<<"Profit cannot be less than 0! Retry!"<<endl;
cout<<"Enter Profit: ";
cin>>*profit;
}
}
void calBonus1(double *profit){
double bonus;
if(*profit <= 100000){
bonus = *profit * 0.1;
}
else if(*profit > 100000 && *profit <= 200000){
bonus = (*profit - 100000) * 0.075 + 100000 * 0.1;
}
else if(*profit > 200000 && *profit <= 400000){
bonus = (*profit - 200000) * 0.05 + 100000 * 0.075 + 100000 * 0.1;
}
else if(*profit > 400000 && *profit <= 600000){
bonus = (*profit - 400000) * 0.03 + 200000 * 0.05 + 100000 * 0.075 + 100000 * 0.1;
}
else if(*profit > 600000 && *profit <= 1000000){
bonus = (*profit - 600000) * 0.015 + 200000 * 0.03 + 200000 * 0.05 + 100000 * 0.075 + 100000 * 0.1;
}
else{
bonus = (*profit - 1000000) * 0.01 + 400000 * 0.015 + 200000 * 0.03 + 200000 * 0.05 + 100000 * 0.075 + 100000 * 0.1;
}
cout<<"Use If Statement: "<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<"Bonus = "<<bonus<<endl;
}
void calBonus2(double *profit){
double bonus;
if(*profit > 1000000){
bonus = (*profit - 1000000) * 0.01 + 400000 * 0.015 + 200000 * 0.03 + 200000 * 0.05 + 100000 * 0.075 + 100000 * 0.1;
}
int group = int(*profit / 100000);
switch(group){
case 0:
case 1: bonus = *profit * 0.1; break;
case 2: bonus = (*profit - 100000) * 0.075 + 100000 * 0.1; break;
case 3:
case 4: bonus = (*profit - 200000) * 0.05 + 100000 * 0.075 + 100000 * 0.1; break;
case 5:
case 6: bonus = (*profit - 400000) * 0.03 + 200000 * 0.05 + 100000 * 0.075 + 100000 * 0.1; break;
case 7:
case 8:
case 9:
case 10: bonus = (*profit - 600000) * 0.015 + 200000 * 0.03 + 200000 * 0.05 + 100000 * 0.075 + 100000 * 0.1; break;
}
cout<<"Use Switch Statement: "<<endl;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<"Bonus = "<<bonus<<endl;
}
int main(){
double *profit = new double;
inputProfit(profit);
calBonus1(profit);
calBonus2(profit);
delete(profit);
system("pause");
return 0;
}