標題:
[作業] 電費計算機 (二)
[打印本頁]
作者:
方浩葦
時間:
2024-7-27 11:17
標題:
[作業] 電費計算機 (二)
改寫
電費計算機 (一)
的程式碼,將
所有
if...else 判斷的部分,以 switch...case 語法改寫。
本帖隱藏的內容需要回復才可以瀏覽
作者:
陳妍蓁
時間:
2024-8-10 11:55
本帖最後由 陳妍蓁 於 2024-8-10 15:32 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int month,w;
float sum=0.0;
cout<<"***歡迎使用小米電費計算機***"<<endl<<endl;
cout<<"請輸入月份:";
cin>>month;
cout<<"用電度數:";
cin>>w;
switch (month) {
case 10 ... 12:
case 1 ... 5:
switch(w){
case 1001 ... 50000000:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(1000-700)*5.03+(w-1000)*5.28;
break;
case 701 ... 1000:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(w-700)*5.03;
break;
case 501 ... 700:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(w-500)*4.48;
break;
case 331 ... 500:
sum=120*2.1+(330-120)*2.68+(w-330)*3.61;
break;
case 121 ... 330:
sum=120*2.1+(w-120)*2.68;
break;
case 1 ... 120:
sum=w*2.1;
default:
cout<<"輸入錯誤"<<endl;
}
case 6 ... 9:
switch(w)
{
case 1001 ... 50000000:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(1000-700)*6.16+(w-1000)*6.71;
break;
case 701 ... 1000:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(w-700)*6.16;
break;
case 501 ... 700:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(w-500)*5.44;
break;
case 331 ... 500:
sum=120*2.1+(330-120)*3.02+(w-330)*4.39;
break;
case 121 ... 330:
sum=120*2.1+(w-120)*3.02;
break;
case 1 ... 120:
sum=w*2.1;
break;
default:
cout<<"輸入錯誤"<<endl;
}
default:
cout<<"輸入錯誤"<<endl;
}
cout<<endl<<"您要交的電費共:"<<sum<<"元"<<endl;
system("pause");
goto re;
return 0;
}
複製代碼
作者:
李唯銘
時間:
2024-8-10 15:32
本帖最後由 李唯銘 於 2024-8-10 15:36 編輯
#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 ... 9999:
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;
default:
p=d*2.1;
break;
}
case 6 ... 9:
switch(d){
case 1001 ... 9999:
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 120 ... 330:
p=120*2.1+(d-120)*3.02;
break;
default:
p=d*2.1;
break;
}
}
cout<<endl<<"您要繳交的電費共: "<<p<<"元!"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
劉奕劭
時間:
2024-8-10 15:53
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int month,w;
float sum=0.0;
cout<<"**小米電費計算機**"<<endl<<endl;
cout<<"請輸入月份:";
cin>>month;
cout<<"用電度數:";
cin>>w;
switch (month) {
case 10 ... 12:
case 1 ... 5:
switch(w){
case 1001 ... 50000000:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(1000-700)*5.03+(w-1000)*5.28;
break;
case 701 ... 1000:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(w-700)*5.03;
break;
case 501 ... 700:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(w-500)*4.48;
break;
case 331 ... 500:
sum=120*2.1+(330-120)*2.68+(w-330)*3.61;
break;
case 121 ... 330:
sum=120*2.1+(w-120)*2.68;
break;
case 1 ... 120:
sum=w*2.1;
}
case 6 ... 9:
switch(w)
{
case 1001 ... 50000000:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(1000-700)*6.16+(w-1000)*6.71;
break;
case 701 ... 1000:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(w-700)*6.16;
break;
case 501 ... 700:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(w-500)*5.44;
break;
case 331 ... 500:
sum=120*2.1+(330-120)*3.02+(w-330)*4.39;
break;
case 121 ... 330:
sum=120*2.1+(w-120)*3.02;
break;
case 1 ... 120:
sum=w*2.1;
break;
}
break;
}
cout<<endl<<"您的電費共:"<<sum<<"元"<<endl;
system("pause");
goto re;
return 0;
}
複製代碼
作者:
洪榮辰
時間:
2024-8-11 16:31
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int m,d;
float x;
re:
cout<<"***歡迎使用小米的電費計算機***"<<endl<<endl;
cout<<"請輸入月份: ";
cin>>m;
cout<<"用電度數: ";
cin>>d;
switch(m)
{
case 1 ... 5:
case 10 ... 12:
switch(d)
{
case 1001 ... 9999:
x=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:
x=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(d-700)*5.03;
break;
case 501 ... 700:
x=120*2.1+(330-120)*2.68+(500-330)*3.61+(d-500)*4.48;
break;
case 331 ... 500:
x=120*2.1+(330-120)*2.68+(d-330)*3.61;
break;
case 121 ... 330:
x=120*2.1+(d-120)*2.68;
break;
default:
x=d*2.1;
break;
}
case 6 ... 9:
switch(d)
{
case 1001 ... 9999:
x=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:
x=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(d-700)*6.16;
break;
case 501 ... 700:
x=120*2.1+(330-120)*3.02+(500-330)*4.39+(d-500)*5.44;
break;
case 331 ... 500:
x=120*2.1+(330-120)*3.02+(d-330)*4.39;
break;
case 120 ... 330:
x=120*2.1+(d-120)*3.02;
break;
default:
x=d*2.1;
break;
}
}
cout<<endl<<"您要繳交的電費共: "<<x<<"元!"<<endl;
system("pause");
system("cls");
goto re;
return 0;
}
複製代碼
作者:
林少謙
時間:
2024-8-11 20:36
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int month,w;
float sum=0.0;
cout<<"***歡迎使用小米電費計算機***"<<endl<<endl;
cout<<"請輸入月份:";
cin>>month;
cout<<"用電度數:";
cin>>w;
switch (month) {
case 10 ... 12:
case 1 ... 5:
switch(w){
case 1001 ... 50000000:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(1000-700)*5.03+(w-1000)*5.28;
break;
case 701 ... 1000:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(700-500)*4.48+(w-700)*5.03;
break;
case 501 ... 700:
sum=120*2.1+(330-120)*2.68+(500-330)*3.61+(w-500)*4.48;
break;
case 331 ... 500:
sum=120*2.1+(330-120)*2.68+(w-330)*3.61;
break;
case 121 ... 330:
sum=120*2.1+(w-120)*2.68;
break;
case 1 ... 120:
sum=w*2.1;
default:
cout<<"輸入錯誤"<<endl;
}
case 6 ... 9:
switch(w)
{
case 1001 ... 50000000:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(1000-700)*6.16+(w-1000)*6.71;
break;
case 701 ... 1000:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(700-500)*5.44+(w-700)*6.16;
break;
case 501 ... 700:
sum=120*2.1+(330-120)*3.02+(500-330)*4.39+(w-500)*5.44;
break;
case 331 ... 500:
sum=120*2.1+(330-120)*3.02+(w-330)*4.39;
break;
case 121 ... 330:
sum=120*2.1+(w-120)*3.02;
break;
case 1 ... 120:
sum=w*2.1;
break;
default:
cout<<"輸入錯誤"<<endl;
}
}
cout<<endl<<"您要交的電費共:"<<sum<<"元"<<endl;
system("pause");
goto re;
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2