本帖最後由 tonyh 於 2013-12-7 15:21 編輯
參考台灣電力公司所公怖的電費計算表格, 設計一個計算電費的小工具,
讓使用者輸入月份, 與該月份的用電度數, 電腦回應該月份的電費.- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int m,d,cost;
- cout<<"***歡迎使用小米的電費計算機***"<<endl<<endl;
- re1:
- cout<<"請輸入月份: ";
- cin>>m;
- re2:
- switch(m)
- {
- case 6:
- case 7:
- case 8:
- case 9:
- cout<<"用電度數: ";
- cin>>d;
- if(d>0 && d<=120)
- cost=2.1*d;
- else if(d>=121 && d<=330)
- cost=2.1*120+3.02*(d-120);
- else if(d>=331 && d<=500)
- cost=2.1*120+3.02*(330-120)+4.39*(d-330);
- else if(d>=501 && d<=700)
- cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
- else if(d>700)
- cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
- else
- {
- cout<<"用電度數輸入錯誤!"<<endl;
- goto re2;
- }
- break;
- case 10:
- case 11:
- case 12:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- cout<<"用電度數: ";
- cin>>d;
- if(d>0 && d<=120)
- cost=2.1*d;
- else if(d>=121 && d<=330)
- cost=2.1*120+2.68*(d-120);
- else if(d>=331 && d<=500)
- cost=2.1*120+2.68*(330-120)+3.61*(d-330);
- else if(d>=501 && d<=700)
- cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
- else if(d>700)
- cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
- else
- {
- cout<<"用電度數輸入錯誤!"<<endl;
- goto re2;
- }
- break;
- default:
- cout<<"月份輸入錯誤!"<<endl;
- goto re1;
- }
- cout<<endl<<"您要繳交的電費共: "<<cost<<"元!"<<endl<<endl;
- system("pause");
- return 0;
- }
複製代碼 |