標題:
電費計算機 (二)-使用switch
[打印本頁]
作者:
陳品肇
時間:
2019-5-10 22:46
標題:
電費計算機 (二)-使用switch
針對月份判斷的部分, 以 switch...case 語法改寫.
[attach]6450[/attach]
#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;
}
複製代碼
作者:
吳孟修
時間:
2019-5-11 13:36
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int month,p=0;
float sum;
cout<<"*** 電費計算機 ***"<<endl<<endl;
cout<<"請輸入月份:";
cin>>month;
switch(month)
{
case 6 ... 9:
cout<<"請輸入用電度數:";
cin>>p;
if(p>=0 && p<=120)
{
sum=1.63*p;
}
else if(p>120 && p<=330)
{
sum=1.63*120+2.38*(p-120);
}
else if(p>330 && p<=500)
{
sum=1.63*120+2.38*(330-120)+3.52*(p-330);
}
else if(p>500 && p<=700)
{
sum=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(p-500);
}
else if(p>700 && p<=1000)
{
sum=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(p-700);
}
else if(p>1000)
{
sum=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(1000-700)+6.41*(p-1000);
}
else
{
cout<<"輸入錯誤"<<endl;
system("pause");
goto re;
}
break;
case 1 ... 5:
case 10:
case 11:
case 12:
cout<<"請輸入用電度數:";
cin>>p;
if(p>=0 && p<=120)
{
sum=1.63*p;
}
else if(p>120 && p<=330)
{
sum=1.63*120+2.1*(p-120);
}
else if(p>330 && p<=500)
{
sum=1.63*120+2.1*(330-120)+2.89*(p-330);
}
else if(p>500 && p<=700)
{
sum=1.63*120+2.1*(330-120)+2.89*(500-330)+3.94*(p-500);
}
else if(p>700 && p<=1000)
{
sum=1.63*120+2.1*(330-120)+2.89*(500-330)+3.94*(700-500)+4.6*(p-700);
}
else if(p>1000)
{
sum=1.63*120+2.1*(330-120)+2.89*(500-330)+3.94*(700-500)+4.6*(1000-700)+5.03*(p-1000);
}
else
{
cout<<"輸入錯誤"<<endl;
system("pause");
goto re;
}
break;
default:
cout<<"月份輸入錯誤!"<<endl;
system("pause");
goto re;
}
cout<<endl<<"您要繳交的電費共 "<<sum<<"元!"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
湯郡一
時間:
2019-5-11 13:40
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int degree,month;
float money;
cout<<"*** 電費計算機 ***"<<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;
}
複製代碼
作者:
田宇任
時間:
2019-5-11 13:44
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int du,month;
float money;
cout<<"請輸入月份: ";
cin>>month;
switch(month)
{
case 6:
case 7:
case 8:
case 9:
cout<<"用電度數: ";
cin>>du;
if(du>0 && du<=120)
{
money=du*1.63;
}else if(du>120 && du<=330)
{
money=1.63*120+2.38*(du-120);
}else if(du>330 && du<=500)
{
money=1.63*120+2.38*(330-120)+3.52*(du-330);
}else if(du>500 && du<=700)
{
money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(du-700);
}else if(du>700 && du<=1000)
{
money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(du-700);
}else if(du>=1001)
{
money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(1000-700)+6.41*(du-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<<"用電度數:"<<endl;
cin>>du;
if(du>0 && du<=120)
{
money=du*1.63;
}
else if(du>120 && du<=330)
{
money=1.63*120+2.10*(du-120);
}else if(du>330 && du<=500)
{
money=1.63*120+2.10*(330-120)+2.89*(du-330);
}
else if(du>500 && du<=700)
{
money=1.63*120+2.10*(330-120)+2.89*(500-330)+3.94*(du-700);
}
else if(du>700 && du<=1000)
{
money=1.63*120+2.10*(330-120)+2.89*(500-330)+3.94*(700-500)+4.60*(du-700);
}
else if(du>=1001)
{
money=1.63*120+2.10*(330-120)+2.89*(500-330)+3.94*(700-500)+4.60*(1000-700)+5.03*(du-1000);
}else
{
cout<<"用電度數輸入錯誤!"<<endl;
system("pause");
goto re;
}
break;
default:
cout<<"月份輸入錯誤!"<<endl;
system("pause");
goto re;
}
cout<<"您要繳的電費為"<<money<<"元!"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
王瑞喻
時間:
2019-5-11 13:46
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int month,d;
float sum=0;
cout<<"*****電費計算機*****"<<endl;
cout<<"請輸入月份: ";
cin>>month;
switch(month)
{
case 6:
case 7:
case 8:
case 9:
cout<<"用電度數: ";
cin>>d;
if(d>=0&&d<=120)
{
sum=d*1.63;
}else if(d>=120&&d<=330)
{
sum=120*1.63+(d-120)*2.38;
}else if(d>=330&&d<=500)
{
sum=120*1.63+210*2.38+(d-500)*3.52;
}else if(d>=500&&d<=700)
{
sum=120*1.63+210*2.38+170*3.52+(d-500)*4.80;
}else if(d>=700&&d<=1000)
{
sum=120*1.63+210*2.38+170*3.52+200*4.80+(d-700)*5.66;
}else if(d>=1000)
{
sum=120*1.63+210*2.38+170*3.52+200*4.80+300*5.66+(d-1000)*6.41;
}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>>d;
if(d>=0&&d<=120)
{
sum=d*1.63;
}else if(d>=120&&d<=330)
{
sum=120*1.63+(d-120)*2.10;
}else if(d>=330&&d<=500)
{
sum=120*1.63+210*2.10+(d-500)*2.89;
}else if(d>=500&&d<=700)
{
sum=120*1.63+210*2.10+170*2.89+(d-500)*3.94;
}else if(d>=700&&d<=1000)
{
sum=120*1.63+210*2.10+170*2.89+200*3.94+(d-700)*4.60;
}else if(d>=1000)
{
sum=120*1.63+210*2.10+170*2.89+200*3.94+300*4.60+(d-1000)*5.03;
}else
{
cout<<"用電度數輸入錯誤!"<<endl;
system("pause");
goto re;
}
break;
default:
cout<<"月份輸入錯誤!";
system("pause");
goto re;
}
cout<<"您要繳交的電費共"<<sum<<"元!"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
吳孟書
時間:
2019-5-11 14:00
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls");
int month,d;
float money;
cout<<"***電費計算機***"<<endl<<endl;
cout<<"請輸入月份:";
cin>>month;
switch(month)
{
case 6:
case 7:
case 8:
case 9:
rf:
cout<<"用電度數:";
cin>>d;
if(d>=1 && d<=120)
{
money=1.63*d;
}else if(d>=121 && d<=330)
{
money=1.63*120+2.38*(d-120);
}else if(d>330 && d<=500)
{
money=1.63*120+2.38*(330-120)+3.52*(d-330);
}else if(d>500 && d<=700)
{
money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(d-500);
}else if(d>700 && d<=1000)
{
money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(d-700);
}else if(d>=1001)
{
money=1.63*120+2.38*(330-120)+3.52*(500-330)+4.8*(700-500)+5.66*(1000-700)+6.41*(d-1000);
}else
{
cout<<"用電度數輸入錯誤!"<<endl;
system("pause");
goto rf;
}
break;
case 10:
case 11:
case 12:
case 1:
case 2:
case 3:
case 4:
case 5:
rg:
cout<<"用電度數:";
cin>>d;
if(d>=1 && d<=120)
{
money=1.63*d;
}else if(d>=121 && d<=330)
{
money=1.63*120+2.10*(d-120);
}else if(d>330 && d<=500)
{
money=1.63*120+2.10*(330-120)+2.89*(d-330);
}else if(d>500 && d<=700)
{
money=1.63*120+2.10*(330-120)+2.89*(500-330)+3.94*(d-500);
}else if(d>700 && d<=1000)
{
money=1.63*120+2.10*(330-120)+2.89*(500-330)+3.94*(700-500)+4.6*(d-700);
}else if(d>=1001)
{
money=1.63*120+2.10*(330-120)+2.89*(500-330)+3.94*(700-500)+4.6*(1000-700)+5.03*(d-1000);
}else
{
cout<<"用電度數輸入錯誤!"<<endl;
system("pause");
goto rg;
}
break;
default:
cout<<"月份輸入錯誤!"<<endl;
system("pause");
goto re;
}
cout<<endl<<"您要繳交的電費共 "<<money<<"元!"<<endl;
system("pause");
return 0;
}
複製代碼
作者:
洪寬瀧
時間:
2019-5-11 15:07
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
re:
system("cls"); //⊕ 41458+Alt
int degree,month;
float money;
cout<<"///電費計算機///"<<endl<<endl;
cout<<"請輸入月份"<<endl;
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;
}
cout<<endl<<"您要繳交的電費共"<<money<<"元"<<endl;
}
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2