本帖最後由 陳品肇 於 2019-1-12 17:27 編輯
針對月份判斷的部分, 以 switch...case 語法改寫.
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- re:
- system("cls");
- int degree,month;
- float 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=1.63*degree;
- }else if(degree>120 && degree<=330)
- {
- money=1.63*120+2.38*(degree-120);
- }else if(degree>330 && degree<=500)
- {
- money=1.63*120+2.38*(330-120)+3.52*(degree-330);
- }else if(degree>500 && degree<=700)
- {
- money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(degree-500);
- }else if(degree>700 && degree<=1000)
- {
- money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(degree-700);
- }else if(degree>=1001)
- {
- money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(1000-700)+6.41*(degree-1000);
- }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=1.63*degree;
- }else if(degree>120 && degree<=330)
- {
- money=1.63*120+2.1*(degree-120);
- }else if(degree>330 && degree<=500)
- {
- money=1.63*120+2.1*(330-120)+2.89*(degree-330);
- }else if(degree>500 && degree<=700)
- {
- money=1.63*120+2.1*(330-120)+2.89*(500-330)+3.94*(degree-500);
- }else if(degree>700 && degree<=1000)
- {
- money=1.63*120+2.1*(330-120)+2.89*(500-330)+3.94*(700-500)+4.6*(degree-700);
- }else if(degree>=1001)
- {
- money=1.63*120+2.1*(330-120)+2.89*(500-330)+3.94*(700-500)+4.6*(1000-700)+5.03*(degree-1000);
- }else
- {
- cout<<"用電度數輸入錯誤!"<<endl;
- system("pause");
- goto re;
- }
- break;
- default:
- cout<<"月份輸入錯誤!"<<endl;
- system("pause");
- goto re;
- }
- cout<<endl<<"您要繳交的電費共 "<<money<<"元!"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |