本帖最後由 鄭繼威 於 2023-8-4 19:45 編輯
參考台灣電力公司所公怖的電費計算表格,設計一個計算電費的小工具,
讓使用者輸入月份,與該月份的用電度數,電腦回應該月份的電費。
單位: 元 / 每度
電費是用累進式的
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- int m,d;
- cout<<"請輸入月份:";
- cin>>m;
- cout<<"請輸入用電數:";
- cin>>d;
-
- float price; //電費
- //判斷夏季or非夏季
- if(m>=6 and m<=9)
- {
- //夏季用電
- if(d>=0 and d<=120)
- {
- price=d*2.1;
- }
- else if(d>=121 and d<=330)
- {
- price=120*2.1+(d-120)*3.02;
- }
- else if(d>=331 and d<=500)
- {
- price=120*2.1+(330-121+1)*3.02+(d-330)*4.39;
- }
- else if(d>=501 and d<=700)
- {
- price=120*2.1+(330-121+1)*3.02+(500-331+1)*4.39+(d-500)*5.44;
- }
- else if(d>=701 and d<=1000)
- {
- price=120*2.1+(330-121+1)*3.02+(500-331+1)*4.39+(700-501+1)*5.44+(d-700)*6.16;
- }
- else
- {
- price=120*2.1+(330-121+1)*3.02+(500-331+1)*4.39+(700-501+1)*5.44+(1000-701+1)*6.16+(d-1000)*6.71;
- }
- }
- else
- {
- //非夏季用電
- if(d>=0 and d<=120)
- {
- price=d*2.1;
- }
- else if(d>=121 and d<=330)
- {
- price=120*2.1+(d-120)*2.68;
- }
- else if(d>=331 and d<=500)
- {
- price=120*2.1+(330-121+1)*2.68+(d-330)*3.61;
- }
- //
- else if(d>=501 and d<=700)
- {
- price=120*2.1+(330-121+1)*2.68+(500-331+1)*3.61+(d-500)*4.48;
- }
- else if(d>=701 and d<=1000)
- {
- price=120*2.1+(330-121+1)*2.68+(500-331+1)*3.61+(700-501+1)*4.48+(d-700)*5.03;
- }
- else
- {
- price=120*2.1+(330-121+1)*2.68+(500-331+1)*3.61+(700-501+1)*4.48+(1000-701+1)*5.03+(d-1000)*5.28;
- }
- }
- cout<<endl<<"您要繳交的電費共: "<<price<<"元!"<<endl;
-
- return 0;
- }
複製代碼- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int m,d;
- float p;
- cout<<"***歡迎使用小米的電費計算機***"<<endl<<endl;
- cout<<"請輸入月份: ";
- cin>>m;
- cout<<"用電度數: ";
- cin>>d;
- //判斷是不是夏季
- switch(m)
- {
- //非夏季
- case 1 ... 5:
- case 10 ... 12:
- switch(d)
- {
- case 1001 ... 5000:
- p=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(1000-700)*5.03+(d-1000)*5.28;
- break;
- case 701 ... 1000:
- p=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(d-700)*5.03;
- break;
- case 501 ... 700:
- p=120*2.1+(330-120)*2.68+(500-330)*3.61+(d-500)*4.48;
- break;
- case 331 ... 500:
- p=120*2.1+(330-120)*2.68+(d-330)*3.61;
- break;
- case 121 ... 330:
- p=120*2.1+(d-120)*2.68;
- break;
- case 1 ... 120:
- p=d*2.1;
- break;
- }
- break;
- //夏季
- case 6 ... 9:
- switch(d)
- {
- case 1001 ... 5000:
- p=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(1000-700)*6.16+(d-1000)*6.71;
- break;
- case 701 ... 1000:
- p=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(d-700)*6.16;
- break;
- case 501 ... 700:
- p=120*2.1+(330-120)*3.02+(500-330)*4.39+(d-500)*5.44;
- break;
- case 331 ... 500:
- p=120*2.1+(330-120)*3.02+(d-330)*4.39;
- break;
- case 121 ... 330:
- p=120*2.1+(d-120)*3.02;
- break;
- case 1 ... 120:
- p=d*2.1;
- break;
- }
- break;
- }
- cout<<endl<<"您要繳交的電費共: "<<p<<"元!"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |