switch-case的选择用法

企业发放的奖金根据利润提成。利润I低于或等于100000元的,奖金可提0.1;利润高于100000元,低于200000(100000<I<=200000)时,低于100000元的部分按10%提成,高于100000元的部分,可提成7.5%;200000<I<=400000时,低于200000元的部分仍按上述办法提成(下同)。高于200000元的部分按5%提成;400000<I<=600000元时,高于400000元的部分按3%提成;600000<I<=1000000时,高于600000元的部分按1.5%提成;I>1000000时,超过1000000元的部分按1%提成。从键盘输入当月利润I,求应发奖金总数。
要求:
(1)用if语句编程序;
(2)用switch语句编程序。

 1 include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int opt;
 6     float profit;
 7     cout<<"please choose the mode: 1[if]/2[switch] \n";
 8     cin>>opt;
 9     switch(opt)
10     {
11     case 1:
12         {
13             cout<<"MODE \"if\" chosen\n";
14             cout<<"please enter monthly profit($):";
15             cin>>profit;
16             float sum;
17             if(profit<=1e5)
18             {sum=profit*0.1;}
19             else if(profit<=2e5)
20             {sum=1e5*0.1+(profit-1e5)*0.075;}
21             else if(profit<=4e5)
22             {sum=1e5*0.1+1e5*0.075+(profit-2e5)*0.05;}
23             else if(profit<=6e5)
24             {sum=1e5*0.1+1e5*0.075+2e5*0.05+(profit-4e5)*0.03;}
25             else if(profit<=1e6)
26             {sum=1e5*0.1+1e5*0.075+2e5*0.05+2e5*0.03+(profit-6e5)*0.015;}
27             else
28             {sum=1e5*0.1+1e5*0.075+2e5*0.05+2e5*0.03+4e5*0.015+(profit-1e6)*0.01;}
29             cout<<"your bonus is: "<<sum<<'$'<<endl;
30         };break;
31     case 2:
32         {
33             cout<<"MODE \"switch\" chosen\n";
34             cout<<"please enter monthly profit($):";
35             cin>>profit;
36             float sum;
37             switch(int(profit/1e5))
38             {
39             case 0:
40                 {sum=profit*0.1;};break;
41             case 1:
42                 {sum=1e5*0.1+(profit-1e5)*0.075;};break;
43             case 2:
44                 {sum=1e5*0.1+1e5*0.075+(profit-2e5)*0.05;};
45             case 3:
46                 break;
47             case 4:
48             case 5:
49                 {sum=1e5*0.1+1e5*0.075+2e5*0.05+(profit-4e5)*0.03;};break;
50             case 6:                
51             case 7:
52             case 8:
53             case 9:
54                 {sum=1e5*0.1+1e5*0.075+2e5*0.05+2e5*0.03+(profit-6e5)*0.015;};break;
55             default :
56                 {sum=1e5*0.1+1e5*0.075+2e5*0.05+2e5*0.03+4e5*0.015+(profit-1e6)*0.01;};break;
57             }
58             cout<<"your bonus is: "<<sum<<'$'<<endl;
59         };break;
60     }
61         
62        return 0;
63 }   

 

 

posted @ 2019-07-07 14:59  runsdeep  阅读(622)  评论(0编辑  收藏  举报