返回列表 發帖

函式的建立與執行 (一)

利用自訂函式, 建立一個專門計算三角形面積的程式.
  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. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

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

TOP

  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.    
  14.     system("pause");
  15.     return 0;   
  16. }
  17. float tri(float a,float b)
  18. {
  19.     return a*b/2;  
  20. }
複製代碼

TOP

本帖最後由 曲書辰 於 2019-7-6 14:44 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cmath>
  4. using namespace std;
  5. float tri(float,float);
  6. int main()
  7. {
  8.   int a,b;
  9.   cout<<"輸入三角形的底和高:";
  10.   cin>>a;
  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.   }
複製代碼

TOP

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

TOP

返回列表