返回列表 發帖

多載函數 - 面積計算

利用多載函數的方式, 設計一面積計算程式, 分別計算圓形, 三角形, 與梯形的面積.

  1. #include<iostream>
  2. using namespace std;
  3. float tri(float, float);
  4. float cir(float);
  5. float rec(float, float);
  6. float tra(float, float, float);
  7. int main()
  8. {
  9.     float x, y ,z ;
  10.     int a;
  11.     cout<<"請問您要算哪一種的面積??(1)三角形(2)圓形(3)長方形(4)梯形"<<endl;   
  12.     cin>>a;
  13.     switch(a)
  14.     {
  15.              case 1:
  16.                   tri(x,y);
  17.                   break;
  18.              case 2:
  19.                   cir(x);
  20.                   break;      
  21.              case 3:
  22.                   rec(x,y);
  23.                   break;
  24.              case 4:
  25.                   tra(x,y,z);
  26.                   break;
  27.              default:
  28.                    cout<<"選項有第五嗎?";
  29.                    break;            
  30.     }
  31.     system("pause");   
  32.     return 0;
  33. }
  34. float tri(float x, float y)
  35. {
  36.     cout<<"請輸入三角形的底: ";
  37.     cin>>x;
  38.     cout<<"請輸入三角形的高: ";
  39.     cin>>y;
  40.     cout<<"三角形的面積為: "<<x*y/2<<"平方公分"<<endl;
  41. }
  42. float cir(float x)
  43. {
  44.     cout<<"請輸入圓的半徑: ";
  45.     cin>>x;
  46.     cout<<"圓面積為: "<<x*x*3.14<<"平方公分"<<endl;
  47. }
  48. float rec(float x, float y)
  49. {
  50.     cout<<"請輸入長方形的底: ";
  51.     cin>>x;
  52.     cout<<"請輸入長方形的高: ";
  53.     cin>>y;
  54.     cout<<"長方形的面積為: "<<x*y<<"平方公分"<<endl;
  55. }float tra(float x, float y ,float z)
  56. {
  57.     cout<<"請輸入梯形的上底: ";
  58.     cin>>x;
  59.     cout<<"請輸入梯形的下底: ";
  60.     cin>>y;
  61.     cout<<"請輸入梯形的高: ";
  62.     cin>>z;
  63.     cout<<"梯形的面積為: "<<(x+y)*z/2<<"平方公分"<<endl;
  64. }
複製代碼

TOP

  1. #include<iostream>
  2. using namespace std;
  3. float form(float);
  4. float form(float, float);
  5. float form(float, float, float);
  6. int main()
  7. {
  8.     float x, y ,z ;
  9.     int a;
  10.     cout<<"請問您要算哪一種圖形的面積? (1)圓形(2)三角形(3)梯形"<<endl;   
  11.     cin>>a;
  12.     switch(a)
  13.     {
  14.              case 1:
  15.                   form(x);
  16.                   break;
  17.              case 2:
  18.                   form(x,y);
  19.                   break;      
  20.              case 3:
  21.                   form(x,y,z);
  22.                   break;
  23.              default:
  24.                    cout<<"來亂的?";
  25.                    break;            
  26.     }
  27.     system("pause");   
  28.     return 0;
  29. }
  30. float form(float x)
  31. {
  32.     cout<<"請輸入圓的半徑: ";
  33.     cin>>x;
  34.     cout<<"圓面積為: "<<x*x*3.14<<"平方公分"<<endl;
  35. }
  36. float form(float x, float y)
  37. {
  38.     cout<<"請輸入三角形的底: ";
  39.     cin>>x;
  40.     cout<<"請輸入三角形的高: ";
  41.     cin>>y;
  42.     cout<<"三角形的面積為: "<<x*y/2<<"平方公分"<<endl;
  43. }
  44. float form(float x, float y ,float z)
  45. {
  46.     cout<<"請輸入梯形的上底: ";
  47.     cin>>x;
  48.     cout<<"請輸入梯形的下底: ";
  49.     cin>>y;
  50.     cout<<"請輸入梯形的高: ";
  51.     cin>>z;
  52.     cout<<"梯形的面積為: "<<(x+y)*z/2<<"平方公分"<<endl;
  53. }
複製代碼

TOP

  1. #include<iostream>
  2. using namespace std;
  3. float tri(float, float);
  4. float cir(float);
  5. float rec(float, float);
  6. float tra(float, float, float);
  7. int main()
  8. {
  9.     float x, y ,z ;
  10.     int a;
  11.     cout<<"請問您要算哪一種的面積??(1)三角形(2)圓形(3)長方形(4)梯形"<<endl;   
  12.     cin>>a;
  13.     switch(a)
  14.     {
  15.              case 1:
  16.                   tri(x,y);
  17.                   break;
  18.              case 2:
  19.                   cir(x);
  20.                   break;      
  21.              case 3:
  22.                   rec(x,y);
  23.                   break;
  24.              case 4:
  25.                   tra(x,y,z);
  26.                   break;
  27.              default:
  28.                    cout<<"來鬧的??";
  29.                    break;            
  30.     }
  31.     system("pause");   
  32.     return 0;
  33. }
  34. float tri(float x, float y)
  35. {
  36.     cout<<"請輸入三角形的底: ";
  37.     cin>>x;
  38.     cout<<"請輸入三角形的高: ";
  39.     cin>>y;
  40.     cout<<"三角形的面積為: "<<x*y/2<<"平方公分"<<endl;
  41. }
  42. float cir(float x)
  43. {
  44.     cout<<"請輸入圓的半徑: ";
  45.     cin>>x;
  46.     cout<<"圓面積為: "<<x*x*3.14<<"平方公分"<<endl;
  47. }
  48. float rec(float x, float y)
  49. {
  50.     cout<<"請輸入長方形的底: ";
  51.     cin>>x;
  52.     cout<<"請輸入長方形的高: ";
  53.     cin>>y;
  54.     cout<<"長方形的面積為: "<<x*y<<"平方公分"<<endl;
  55. }float tra(float x, float y ,float z)
  56. {
  57.     cout<<"請輸入梯形的上底: ";
  58.     cin>>x;
  59.     cout<<"請輸入梯形的下底: ";
  60.     cin>>y;
  61.     cout<<"請輸入梯形的高: ";
  62.     cin>>z;
  63.     cout<<"梯形的面積為: "<<(x+y)*z/2<<"平方公分"<<endl;
  64. }
複製代碼

TOP

返回列表