標題:
[作業] 電費計算機 (二)
[打印本頁]
作者:
tonyh
時間:
2013-12-7 15:23
標題:
[作業] 電費計算機 (二)
本帖最後由 tonyh 於 2013-12-14 15:20 編輯
將今天上課寫的程式碼改寫,
移除 switch...case 敘述, 全部用 if...else if...else 取代.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int m,d,cost;
cout<<"***歡迎使用小米的電費計算機***"<<endl<<endl;
re1:
cout<<"請輸入月份: ";
cin>>m;
re2:
if(m>=6 && m<=9)
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+3.02*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+3.02*(330-120)+4.39*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
else if(d>700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else if((m>=10 && m<=12) || (m>=1 && m<=5))
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+2.68*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+2.68*(330-120)+3.61*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
else if(d>700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else
{
cout<<"月份輸入錯誤!"<<endl;
goto re1;
}
cout<<endl<<"您要繳交的電費共: "<<cost<<"元!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
鎧言
時間:
2013-12-7 15:50
本帖最後由 鎧言 於 2013-12-14 14:36 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
double m,d,cost;
cout<<"***歡迎使用帥哥電費計算機***"<<endl<<endl;
re1:
cout<<"請輸入月份: ";
cin>>m;
re2:
if(m>=6 && m<=9)
{
cout<<"用電度數: ";
cin>>d;
if(d>=1 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+3.02*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+3.02*(330-120)+4.39*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
else if(d>700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else if((m<=5 && m>=1) || (m<=12 && m>=10))
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+2.68*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+2.68*(330-120)+3.61*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
else if(d>700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else
{
cout<<"月份輸入錯誤!"<<endl;
goto re1;
}
cout<<endl<<"您要繳交的電費共: "<<cost<<"元!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
許逸群
時間:
2013-12-14 12:15
本帖最後由 許逸群 於 2013-12-14 14:33 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
double m,d,cost;
cout<<"***歡迎使用電費計算機***"<<endl<<endl;
re1:
cout<<"請輸入月份: ";
cin>>m;
re2:
if(m>=6 && m<=9)
{
cout<<"用電度數: ";
cin>>d;
if(d>=1 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+3.02*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+3.02*(330-120)+4.39*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
else if(d>700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else if((m<=5 && m>=1) || (m<=12 && m>=10))
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+2.68*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+2.68*(330-120)+3.61*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
else if(d>700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else
{
cout<<"月份輸入錯誤!"<<endl;
goto re1;
}
cout<<endl<<"您要繳交的電費共: "<<cost<<"元!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
劉泳鱔
時間:
2013-12-14 14:13
本帖最後由 劉泳鱔 於 2013-12-14 14:28 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int a,b;
cout<<"$歡迎使用昂貴又高級電費計算器$"<<endl;
re1:
cout<<"請輸入月份:";
cin>>a;
if(a>12 || a<=0)
{
cout<<"輸入錯誤!"<<endl;
goto re1;
}
re2:
cout<<"請輸入用電度數:";
cin>>b;
if(b<=0)
{
cout<<"輸入錯誤!"<<endl;
goto re2;
}
if(b<120)
{
cout<<b*2.1<<"元"<<endl;
}else if(a>=6 && a<=9 && b>=121 && b<=330)
{
cout<<b*3.02<<"元"<<endl;
}else if((a>=10 && a<=12 || a>=1 && a<=5)&& b>=121 && b<=330)
{
cout<<b*2.68<<"元"<<endl;
}else if(a>=6 && a<=9 && b>=331 && b<=500)
{
cout<<b*4.39<<"元"<<endl;
}else if((a>=10 && a<=12 || a>=1 && a<=5)&& b>=121 && b<=330)
{
cout<<b*3.61<<"元"<<endl;
}else if(a>=6 && a<=9 && b>=501 && b<=700)
{
cout<<b*4.97<<"元"<<endl;
}else if((a>=10 && a<=12 || a>=1 && a<=5)&& b>=121 && b<=330)
{
cout<<b*4.01<<"元"<<endl;
}else if(a>=6 && a<=9 && b<=700)
{
cout<<b*5.63<<"元"<<endl;
}else if(a>=10 && a<=12 || a>=1 && a<=5&&b<=700)
{
cout<<b*4.5<<"元"<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
張瀚仁
時間:
2013-12-14 14:27
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
double m,d,cost;
cout<<"***歡迎使用電費計算機***"<<endl<<endl;
re1:
cout<<"請輸入月份: ";
cin>>m;
re2:
if(m>=6 && m<=9)
{
cout<<"用電度數: ";
cin>>d;
if(d>=1 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+3.02*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+3.02*(330-120)+4.39*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
else if(d>700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else if((m<=5 && m>=1) || (m<=12 && m>=10))
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+2.68*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+2.68*(330-120)+3.61*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
else if(d>700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else
{
cout<<"月份輸入錯誤!"<<endl;
goto re1;
}
cout<<endl<<"您要繳交的電費共: "<<cost<<"元!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
林以諾
時間:
2013-12-14 14:28
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int m,d,cost;
cout<<"***歡迎使用小米的電費計算機***"<<endl<<endl;
re1:
cout<<"請輸入月份: ";
cin>>m;
re2:
if(m>=6 || m<=9)
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+3.02*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+3.02*(330-120)+4.39*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
else if(d>700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}
else if(m>=10 || m<=12 || m>=1 || m<=5)
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+2.68*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+2.68*(330-120)+3.61*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
else if(d>700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else
{
cout<<"月份輸入錯誤!"<<endl;
goto re1;
}
cout<<endl<<"您要繳交的電費共: "<<cost<<"元!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
黃崇維
時間:
2013-12-14 14:34
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int m,d,cost;
cout<<"***歡迎使用小米的電費計算機***"<<endl<<endl;
re1:
cout<<"請輸入月份: ";
cin>>m;
re2:
if(m>=6 || m<=9)
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+3.02*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+3.02*(330-120)+4.39*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
else if(d>700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}
else if(m>=10 || m<=12 || m>=1 || m<=5)
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+2.68*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+2.68*(330-120)+3.61*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
else if(d>700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else
{
cout<<"月份輸入錯誤!"<<endl;
goto re1;
}
cout<<endl<<"您要繳交的電費共: "<<cost<<"元!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
作者:
郭凡瑛
時間:
2013-12-14 17:19
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int m,d,cost
cout<<"!!!歡迎使用電費計算機!!!" <<endl<<endl;
re1:
cout<<"請輸入月份: ";
cin>>m;
re2:
if(m>=6 && m<=9)
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+3.02*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+3.02*(330-120)+4.39*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(d-500);
else if(d>700)
cost=2.1*120+3.02*(330-120)+4.39*(500-330)+4.97*(700-500)+5.63*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else if((m>=10 && m<=12) || (m>=1 && m<=5))
{
cout<<"用電度數: ";
cin>>d;
if(d>0 && d<=120)
cost=2.1*d;
else if(d>=121 && d<=330)
cost=2.1*120+2.68*(d-120);
else if(d>=331 && d<=500)
cost=2.1*120+2.68*(330-120)+3.61*(d-330);
else if(d>=501 && d<=700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(d-500);
else if(d>700)
cost=2.1*120+2.68*(330-120)+3.61*(500-330)+4.01*(700-500)+4.5*(d-700);
else
{
cout<<"用電度數輸入錯誤!"<<endl;
goto re2;
}
}else
{
cout<<"月份輸入錯誤!"<<endl;
goto re1;
}
cout<<endl<<"要交的電費共: "<<cost<<"元!"<<endl<<endl;
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2