C++第3次实验(基础班)—选择结构程序设计(参考答案)-项目3:定期存款利息计算器

参考解答:

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     int  type;  
  6.     double money, period, rate, interest;  
  7.     cout << "欢迎使用利息计算器!"<<endl;  
  8.     cout << "请输入存款金额:";  
  9.     cin >> money;  
  10.     cout << "======存款期限======" << endl;  
  11.     cout << "1. 3个月 " << endl;  
  12.     cout << "2. 6个月" << endl;  
  13.     cout << "3. 一年 " << endl;  
  14.     cout << "4. 二年" << endl;  
  15.     cout << "5. 三年" << endl;  
  16.     cout << "6. 五年" << endl;  
  17.     cout << "请输入存款期限的代号:";  
  18.     cin >> type;  
  19.     if (type>=1 && type <=6)  
  20.     {  
  21.         switch(type)   //在if中嵌入了switch分情况处理  
  22.         {  
  23.         case 1:  
  24.             period = 0.25;  
  25.             rate = 0.031;  
  26.             break;  
  27.         case 2:  
  28.             period = 0.5;  
  29.             rate = 0.033;  
  30.             break;  
  31.         case 3:  
  32.             period = 1;  
  33.             rate = 0.035;  
  34.             break;  
  35.         case 4:  
  36.             period = 2;  
  37.             rate = 0.044;  
  38.             break;  
  39.         case 5:  
  40.             period = 3;  
  41.             rate = 0.05;  
  42.             break;  
  43.         case 6:  
  44.             period = 5;  
  45.             rate = 0.055;  
  46.             break;  
  47.         }  
  48.         interest = money * period * rate;  
  49.         cout << "到期利息为:" << interest << "元,本息合计共"<< interest + money <<"元。"<<endl;  
  50.     }  
  51.     else  
  52.         cout << "选择存款类型错误!"<<endl;  
  53.     cout << "感谢您的使用,欢迎下次光临!"<<endl;  
  54.     return 0;  
  55. }  

猜你喜欢

转载自blog.csdn.net/macrohui2017/article/details/70332113