標題:
[隨堂練習] 多載-函數的練習
[打印本頁]
作者:
周政輝
時間:
2018-6-23 14:55
標題:
[隨堂練習] 多載-函數的練習
請利用多載函式 撰寫 名為area函數
讓使用者輸入 1 正方形2 三角形 3 梯形 根據不同的函數引數,計算面積
作者:
王駿愷
時間:
2018-6-23 15:14
#include<iostream>
#include<cstdlib>
using namespace std;
double area(double num1)
{
return num1*num1;
}
double area(double num2,double num3)
{
return (num2*num3)/2;
}
double area(double num4,double num5,double num6)
{
return (num4+num5)*num6/2;
}
main()
{
int num=0,num1=0,num2=0,num3=0,num4=0,num5=0,num6=0;
cout<<"請輸入形狀(1)正方形(2)三角形(3)梯形"<<endl;
cin>>num;
if(num==1)
{
cout<<"請輸入正方形的邊長"<<endl;
cin>>num1;
cout<<area(num1)<<endl;
}
else if(num==2)
{
cout<<"請輸入三角形的底高" <<endl;
cout<<"底:";
cin>>num2;
cout<<endl;
cout<<"高:";
cin>>num3;
cout<<endl;
cout<<area(num2,num3)<<endl;
}
else if(num==3)
{
cout<<"請輸入梯形的上底,下底,高"<<endl;
cout<<"上底:";
cin>>num4;
cout<<endl;
cout<<"下底:";
cin>>num5;
cout<<endl;
cout<<"高:";
cin>>num6;
cout<<endl;
cout<<area(num4,num5,num6)<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
吳秉翰
時間:
2018-6-23 15:26
#include<iostream>
#include<cstdlib>
using namespace std;
int area(double ground,double height)
{
return ground*height/2;
}
int area(double side)
{
return side*side;
}
int area(double top,double ground,double height)
{
return (top+ground)*height/2;
}
int main()
{
int select;
cout<<"(1)三角形(2)正方形(3)梯形:";
cin>>select;
if(select==1)
{
double ground,height;
cin>>ground;
cin>>height;
cout<<area(ground,height);
}
if(select==2)
{
double side;
cin>>side;
cout<<area(side);
}
if(select==3)
{
double top,ground,height;
cin>>top;
cin>>ground;
cin>>height;
cout<<area(top,ground,height);
}
system("pause");
return 0;
}
複製代碼
作者:
戴嘉禾
時間:
2018-6-24 13:05
#include<iostream>
#include<cstdlib>
using namespace std;
double area (int a)
{
return a*a;
}
double area (int a,int b)
{
return a*b/2;
}
double area (int a,int b,int c)
{
return (a+b)*c/2;
}
int main()
{
int num,a,b,c;
cout<<"☆★☆嘉禾豬面積計算器☆★☆"<<endl;
cout<<"請輸入代號 => (1)正方形 (2)三角形 (3)梯形"<<endl;
cin>>num;
switch(num)
{
case 1:
cout<<"邊長:";
cin >>a;
cout<<"面積:"<<area(a)<<endl;
break;
case 2:
cout<<"底:";
cin >>a;
cout<<"高:";
cin >>b;
cout<<"面積:"<<area(a,b)<<endl;
break;
case 3:
cout<<"上底:";
cin >>a;
cout<<"下底:";
cin >>b;
cout<<"高:";
cin >>c;
cout<<"面積:"<<area(a,b,c)<<endl;
break;
}
system("pause");
return 0;
}
複製代碼
作者:
顏詢
時間:
2018-6-24 20:03
本帖最後由 顏詢 於 2018-7-7 15:29 編輯
#include<iostream>
#include<cstdlib>
using namespace std;
double area(double num1)
{
return num1*num1;
}
double area(double num2,double num3)
{
return (num2*num3)/2;
}
double area(double num4,double num5,double num6)
{
return (num4+num5)*num6/2;
}
main()
{
int num=0,num1=0, num2=0,num3=0, num4=0,num5=0, num6=0;
cout<<"輸入要運算的形狀(1)正方形(2)三角形(3)梯形"<<endl;
cin>>num;
if(num==1){
cout<<area(num1)<<endl;
cout<<"請輸入正方形的邊長"<<endl;
cin>>num1;
cout<<area(num1)<<endl;
}
else if(num==2)
{
cout<<"請輸入三角形的底高" <<endl;
cout<<"底:";
cin>>num2;
cout<<endl;
cout<<"高:";
cin>>num3;
cout<<endl;
cout<<area(num2,num3)<<endl;
}
else if(num==3)
{
cout<<"請輸入梯形的上底,下底,高"<<endl;
cout<<"上底:";
cin>>num4;
cout<<endl;
cout<<"下底:";
cin>>num5;
cout<<endl;
cout<<"高:";
cin>>num6;
cout<<endl;
cout<<area(num4,num5,num6)<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
鄭楀諺
時間:
2018-7-5 08:54
#include<iostream>
#include<cstdlib>
using namespace std;
double area(double num1)
{
return num1*num1;
}
double area(double num2,double num3)
{
return (num2*num3)/2;
}
double area(double num4,double num5,double num6)
{
return (num4+num5)*num6/2;
}
int main()
{
int ans=0;
int num1=0,num2=0,num3=0,num4=0,num5=0,num6=0;
while(true)
{
cout<<"(1)正方形(2)三角形(3)梯形,請輸入數字,會依照數字對應到的形狀蒜面積:";
cin>>ans;
if(ans==1)
{
cout<<"請輸入邊長:";
cin>>num1;
cout<<"正方形面積:"<<area(num1)<<endl;
break;
}
if(ans==2)
{
cout<<"請輸入底長:";
cin>>num2;
cout<<"請輸入高長:";
cin>>num3;
cout<<"三角形面積:"<<area(num2,num3)<<endl;
break;
}
if(ans==3)
{
cout<<"請輸入上底:";
cin>>num4;
cout<<"請輸入下底:";
cin>>num5;
cout<<"請輸入高長:";
cin>>num6;
cout<<"梯形面積:"<<area(num4,num5,num6)<<endl;
break;
}
if(ans!=1||2||3)
{
cout<<"輸入錯誤,請重新輸入";
_sleep(500);
system("cls");
}
}
system("pause");
return 0;
}
複製代碼
作者:
黃安立
時間:
2018-7-7 15:31
#include<iostream>
#include<cstdlib>
using namespace std;
double area(double num1)
{
return num1*num1;
}
double area(double num2,double num3)
{
return (num2*num3)/2;
}
double area(double num4,double num5,double num6)
{
return (num4+num5)*num6/2;}
int main()
{
int num=0,num1=0,num2=0,num3=0,num4=0,num5=0,num6=0;
cout<<"請輸入形狀(1)正方形(2)三角形(3)梯形"<<endl;
cin>>num;
if(num==1)
{
cout<<"請輸入正方形的lenth"<<endl;
cin>>num1;
cout<<area(num1)<<endl;
}
else if(num==2)
{
cout<<"請輸入三角形的Bottom height" <<endl;
cout<<"Bottom :";
cin>>num2;
cout<<endl;
cout<<"height:";
cin>>num3;
cout<<endl;
cout<<area(num2,num3)<<endl;
}
else if(num==3)
{
cout<<"請輸入梯形的上底,下底,高"<<endl;
cout<<"上底:";
cin>>num4;
cout<<endl;
cout<<"下底:";
cin>>num5;
cout<<endl;
cout<<"高:";
cin>>num6;
cout<<endl;
cout<<area(num4,num5,num6)<<endl;
}
system("pause");
return 0;
}
複製代碼
作者:
湯東緯
時間:
2018-7-9 20:59
#include<iostream>
#include<cstdlib>
using namespace std;
int area (int x)
{
return x*x;
}
int area (int x,int y)
{
return x*y/2;
}
int area (int x,int y,int z)
{
return (x+y)*z/2;
}
int main()
{
int num,x,y,z;
cout<<"計算(1)正方形 (2)三角形 (3)梯形"<<endl;
cin>>num;
if(area==1)
{
cout<<"邊長:";
cin >>x;
cout<<"面積:"<<area(x)<<endl;
break;
}
if(area==2)
{
cout<<"底:";
cin >>x;
cout<<"高:";
cin >>y;
cout<<"面積:"<<area(x,y)<<endl;
break;
}
if(area==3)
}
cout<<"上底:";
cin >>x;
cout<<"下底:";
cin >>y;
cout<<"高:";
cin >>z;
cout<<"面積:"<<area(x,y,z)<<endl;
break;
}
system("pause");
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2