本帖最後由 tonyh 於 2014-5-10 17:44 編輯
針對月份判斷的部分, 以 switch...case 語法改寫.- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- re:
- system("cls");
- int degree,month,money;
- cout<<"*** 電費計算機 ***"<<endl<<endl;
- cout<<"請輸入月份: ";
- cin>>month;
- switch(month)
- {
- case 6:
- case 7:
- case 8:
- case 9:
- cout<<"用電度數: ";
- cin>>degree;
- if(degree>0 && degree<=120)
- {
- money=2.1*degree;
- }else if(degree>120 && degree<=330)
- {
- money=2.1*120+3.02*(degree-120);
- }else if(degree>330 && degree<=500)
- {
- money=2.1*120+3.02*(330-120)+4.39*(degree-330);
- }else if(degree>500 && degree<=700)
- {
- money=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(degree-500);
- }else if(degree>700)
- {
- money=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(degree-700);
- }else
- {
- cout<<"用電度數輸入錯誤!"<<endl;
- system("pause");
- goto re;
- }
- break;
- case 10:
- case 11:
- case 12:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- cout<<"用電度數: ";
- cin>>degree;
- if(degree>0 && degree<=120)
- {
- money=2.1*degree;
- }else if(degree>120 && degree<=330)
- {
- money=2.1*120+2.68*(degree-120);
- }else if(degree>330 && degree<=500)
- {
- money=2.1*120+2.68*(330-120)+3.61*(degree-330);
- }else if(degree>500 && degree<=700)
- {
- money=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(degree-500);
- }else if(degree>700)
- {
- money=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(degree-700);
- }else
- {
- cout<<"用電度數輸入錯誤!"<<endl;
- system("pause");
- goto re;
- }
- break;
- default:
- cout<<"月份輸入錯誤!"<<endl;
- system("pause");
- goto re;
- }
- cout<<endl<<"您要繳交的電費共 "<<money<<"元!"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |