Board logo

標題: 函式的建立與執行 (一) [打印本頁]

作者: 陳品肇    時間: 2019-4-19 22:18     標題: 函式的建立與執行 (一)

利用自訂函式, 建立一個專門計算三角形面積的程式.
[attach]6313[/attach]
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. float tri(float,float);  //宣告一自訂函式
  5. int main()
  6. {
  7.     float x,y;
  8.     cout<<"請輸入三角形的底(公分): ";
  9.     cin>>x;
  10.     cout<<"請輸入三角形的高(公分): ";
  11.     cin>>y;
  12.     cout<<"此三角形的面積為"<<tri(x,y)<<"平方公分!"<<endl;
  13.     system("pause");  
  14.     return 0;   
  15. }
  16. float tri(float a, float b)
  17. {   
  18.     return a*b/2;     
  19. }
複製代碼

作者: 蔡季庭    時間: 2019-4-20 17:19

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. float tri(float a,float b);
  5. int main()
  6. {
  7.     float x,y;
  8.     cout<<"請輸入三角形的底(公分): ";
  9.     cin>>x;
  10.     cout<<"請輸入三角形的高(公分): ";
  11.     cin>>y;
  12.     cout<<"此三角形的面積為"<<tri(x,y)<<"平方公分!"<<endl;
  13.     system("pause");  
  14.     return 0;   
  15. }
  16. float tri(float a, float b)
  17. {   
  18.     return a*b/2;     
  19. }
複製代碼

作者: 蔡依宸    時間: 2019-4-20 17:23

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cmath>
  4. using namespace std;
  5. float triangle(float a,float b);
  6. int main()
  7. {
  8.     float a,b;
  9.     cout<<"請輸入三角形的底(公分): "<<endl;
  10.     cin>>a;
  11.     cout<<"請輸入三角形的高(公分): "<<endl;
  12.     cin>>b;
  13.     cout<<"此三角形的面積為"<<triangle(a,b)<<"平方公分!"<<endl;                        
  14.     system("pause");
  15.     return 0;   
  16. }
  17.    float triangle(float a,float b)
  18.    {
  19.          return a*b/2;
  20.    }
複製代碼

作者: 譚詩澐    時間: 2019-4-20 17:27

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. float tri(float,float);
  5. int main()
  6. {
  7.     float a,b;
  8.     cout<<"請輸入三角形的底(cm): ";
  9.     cin>>a;
  10.     cout<<"請輸入三角形的高(cm): ";
  11.     cin>>b;
  12.     cout<<"此三角形的面積為"<<tri(a,b)<<"平方公分"<<endl;
  13.     system("pause");  
  14.     return 0;   
  15. }
  16. float tri(float a, float b)
  17. {   
  18.     return a*b/2;     
  19. }
複製代碼

作者: 戴偉宸    時間: 2019-4-20 17:30

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. float tri(float,float);
  5. int main()
  6. {
  7.     float x,y;
  8.     cout<<"請輸入三角形的底(公分):";
  9.     cin>>x;
  10.     cout<<"請輸入三角形的高(公分):";
  11.     cin>>y;
  12.     cout<<"此三角形的面積為"<<tri(x,y)<<"平方公分"<<endl;
  13.     system("pause");  
  14.     return 0;   
  15. }
  16. float tri(float a, float b)
  17. {   
  18.     return a*b/2;     
  19. }
複製代碼

作者: 戴安利    時間: 2019-4-20 17:31

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. float getArea(float a, float b);
  5. int main()
  6. {
  7.    float a, b;
  8.    re:
  9.    cout<<"請輸入三角形底(cm): ";
  10.    cin>>a;
  11.    cout<<"請輸入三角形高(cm): ";
  12.    cin>>b;
  13.    
  14.    cout<<"三角形面積(cm): "<<getArea(a,b)<<endl;
  15.    cout<<"+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+"<<endl;
  16.    goto re;
  17.    system("pause");  
  18.    return 0;   
  19. }
  20.    float getArea(float c, float d)
  21. {
  22.    return (c*d)/2;
  23. }
複製代碼

作者: 陳柏霖    時間: 2019-5-2 17:42

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. float tri(float x ,float y)
  5. {
  6.       return x*y/2;
  7. }
  8. int main()
  9. {
  10.     float x ,y;
  11.     cout<<"請輸入三角形的底:";
  12.     cin>>x;
  13.     cout<<"請輸入三角形的高:";
  14.     cin>>y;
  15.     cout<<"底"<<x<<"公分,高"<<y<<"公分的三角形面積為:"<<tri(x,y)<<"平方公分"<<endl;
  16.     system("pause");
  17.     return 0;
  18. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2