现在的位置: 首页 > 综合 > 正文

实验报告6——4

2014年02月16日 ⁄ 综合 ⁄ 共 9942字 ⁄ 字号 评论关闭

6-4 三角形类(求面积,周长,判断直角,等腰...) 多文件组织项目


87人阅读
评论(0)
收藏
举报

/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张鲁
* 完成日期: 2012 年 4月 1日
* 版 本 号:

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:……
* 算法设计:……
*/

Triangle.h

  1. using namespace std;  
  2. class CPoint  
  3. {  
  4. private:  
  5.     double x;  // 横坐标
      
  6.     double y;  // 纵坐标
      
  7. public:  
  8.     CPoint(double xx=1,double yy=2):x(xx),y(yy){}  
  9.     double Distance(CPoint p) const;   // 两点之间的距离
      
  10.     void input();  //以x,y 形式输入坐标点
      
  11.     void output(); //以(x,y) 形式输出坐标点
      
  12. };  
  13.   
  14. class CTriangle  
  15. {  
  16. public:  
  17.     CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //给出三点的构造函数
      
  18.     void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
      
  19.     float perimeter(void);//计算三角形的周长
      
  20.     float area(void);//计算并返回三角形的面积
      
  21.     bool isRightTriangle(); //是否为直角三角形
      
  22.     bool isIsoscelesTriangle(); //是否为等腰三角形
      
  23. private:  
  24.     CPoint A,B,C; //三顶点
      
  25.     double a,b,c; //三条边 
      
  26. };  

main.cpp

  1. #include<iostream>
      
  2. #include"Triangle.h"
      
  3. using namespace std;  
  4. void main()  
  5. {  
  6.     CPoint a1;  
  7.     CPoint a2;  
  8.     CPoint a3;  
  9.     a1.input();  
  10.     a2.input();  
  11.     a3.input();  
  12.     cout<<"三角形的三个顶点为:"<<endl;  
  13.     a1.output();  
  14.     a2.output();  
  15.     a3.output();  
  16.     CTriangle A(a1,a2,a3);  
  17.     a1.Distance(a2);  
  18.     a2.Distance(a3);  
  19.     a1.Distance(a3);  
  20.     A.perimeter();  
  21.     A.area();  
  22.     cout<<"该三角形"<<(A.isRightTriangle()?"是":"不是")<<"直角三角形。"<<endl;  
  23.     cout<<"该三角形"<<(A.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形。"<<endl;  
  24.     system("pause");  
  25. }  

CPoint.cpp

  1. #include<iostream>
      
  2. #include<cmath>
      
  3. #include"Triangle.h"
      
  4. using namespace std;  
  5.   
  6. // 求边长
      
  7. double CPoint::Distance(CPoint p) const    
  8. {  
  9.     double l;  
  10.     l = sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));  
  11.     return l;  
  12. }  
  13.   
  14. //以x,y 形式输入坐标点
      
  15. void CPoint::input()    
  16. {  
  17.     char a;  
  18.     cout<<"请以“x,y ”形式输入坐标点:"<<endl;  
  19.     cin>>x>>a>>y;  
  20.     if(a!=',')  
  21.     {  
  22.         exit(0);  
  23.     }  
  24. }  
  25.   
  26. //以(x,y) 形式输出坐标点
      
  27. void CPoint::output()   
  28. {  
  29.     cout<<"("<<x<<","<<y<<")"<<endl;  
  30. }  

CTriangle.cpp

  1. #include<iostream>
      
  2. #include<cmath>
      
  3. #include"Triangle.h"
      
  4. using namespace std;  
  5.   
  6. void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)  
  7. {  
  8.     A = X;  
  9.     B = Y;  
  10.     C = Z;  
  11. }  
  12.   
  13. //计算三角形的周长
      
  14. float CTriangle::perimeter(void)  
  15. {  
  16.     a = A.Distance(B);  
  17.     b = B.Distance(C);  
  18.     c = A.Distance(C);  
  19.     cout<<"三角形的周长为:"<<endl;  
  20.     cout<<a+b+c<<endl;  
  21.     return 0;  
  22. }  
  23.   
  24. //计算并返回三角形的面积
      
  25. float CTriangle::area(void)  
  26. {  
  27.     double p;  
  28.     p = (a +b +c)/2;  
  29.     a = A.Distance(B);  
  30.     b = B.Distance(C);  
  31.     c = A.Distance(C);  
  32.     cout<<"三角形的面积为:"<<endl;  
  33.     cout<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;  
  34.     return 0;  
  35. }  
  36.   
  37. //是否为直角三角形
      
  38. bool CTriangle::isRightTriangle()  
  39. {  
  40.     if(a*a+b*b == c*c || a*a+c*c == b*b || b*b+c*c == a*a)  
  41.     {  
  42.         return true;  
  43.     }  
  44.     else  
  45.     {  
  46.     return false;  
  47.     }  
  48. }  
  49.   
  50. //是否为等腰三角形
      
  51. bool CTriangle::isIsoscelesTriangle()   
  52. {  
  53.     if(a==b||a==c||b==c)  
  54.     {  
  55.         return true;  
  56.     }  
  57.     else  
  58.     {  
  59.     return false;  
  60.     }  
  61. }  

运行结果:

上机感言:一定要赶上,不能拉下  。  加油

/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张鲁
* 完成日期: 2012 年 4月 1日
* 版 本 号:

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:……
* 算法设计:……
*/

Triangle.h

  1. using namespace std;  
  2. class CPoint  
  3. {  
  4. private:  
  5.     double x;  // 横坐标
      
  6.     double y;  // 纵坐标
      
  7. public:  
  8.     CPoint(double xx=1,double yy=2):x(xx),y(yy){}  
  9.     double Distance(CPoint p) const;   // 两点之间的距离
      
  10.     void input();  //以x,y 形式输入坐标点
      
  11.     void output(); //以(x,y) 形式输出坐标点
      
  12. };  
  13.   
  14. class CTriangle  
  15. {  
  16. public:  
  17.     CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //给出三点的构造函数
      
  18.     void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
      
  19.     float perimeter(void);//计算三角形的周长
      
  20.     float area(void);//计算并返回三角形的面积
      
  21.     bool isRightTriangle(); //是否为直角三角形
      
  22.     bool isIsoscelesTriangle(); //是否为等腰三角形
      
  23. private:  
  24.     CPoint A,B,C; //三顶点
      
  25.     double a,b,c; //三条边 
      
  26. };  

main.cpp

  1. #include<iostream>
      
  2. #include"Triangle.h"
      
  3. using namespace std;  
  4. void main()  
  5. {  
  6.     CPoint a1;  
  7.     CPoint a2;  
  8.     CPoint a3;  
  9.     a1.input();  
  10.     a2.input();  
  11.     a3.input();  
  12.     cout<<"三角形的三个顶点为:"<<endl;  
  13.     a1.output();  
  14.     a2.output();  
  15.     a3.output();  
  16.     CTriangle A(a1,a2,a3);  
  17.     a1.Distance(a2);  
  18.     a2.Distance(a3);  
  19.     a1.Distance(a3);  
  20.     A.perimeter();  
  21.     A.area();  
  22.     cout<<"该三角形"<<(A.isRightTriangle()?"是":"不是")<<"直角三角形。"<<endl;  
  23.     cout<<"该三角形"<<(A.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形。"<<endl;  
  24.     system("pause");  
  25. }  

CPoint.cpp

  1. #include<iostream>
      
  2. #include<cmath>
      
  3. #include"Triangle.h"
      
  4. using namespace std;  
  5.   
  6. // 求边长
      
  7. double CPoint::Distance(CPoint p) const    
  8. {  
  9.     double l;  
  10.     l = sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));  
  11.     return l;  
  12. }  
  13.   
  14. //以x,y 形式输入坐标点
      
  15. void CPoint::input()    
  16. {  
  17.     char a;  
  18.     cout<<"请以“x,y ”形式输入坐标点:"<<endl;  
  19.     cin>>x>>a>>y;  
  20.     if(a!=',')  
  21.     {  
  22.         exit(0);  
  23.     }  
  24. }  
  25.   
  26. //以(x,y) 形式输出坐标点
      
  27. void CPoint::output()   
  28. {  
  29.     cout<<"("<<x<<","<<y<<")"<<endl;  
  30. }  

CTriangle.cpp

  1. #include<iostream>
      
  2. #include<cmath>
      
  3. #include"Triangle.h"
      
  4. using namespace std;  
  5.   
  6. void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)  
  7. {  
  8.     A = X;  
  9.     B = Y;  
  10.     C = Z;  
  11. }  
  12.   
  13. //计算三角形的周长
      
  14. float CTriangle::perimeter(void)  
  15. {  
  16.     a = A.Distance(B);  
  17.     b = B.Distance(C);  
  18.     c = A.Distance(C);  
  19.     cout<<"三角形的周长为:"<<endl;  
  20.     cout<<a+b+c<<endl;  
  21.     return 0;  
  22. }  
  23.   
  24. //计算并返回三角形的面积
      
  25. float CTriangle::area(void)  
  26. {  
  27.     double p;  
  28.     p = (a +b +c)/2;  
  29.     a = A.Distance(B);  
  30.     b = B.Distance(C);  
  31.     c = A.Distance(C);  
  32.     cout<<"三角形的面积为:"<<endl;  
  33.     cout<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;  
  34.     return 0;  
  35. }  
  36.   
  37. //是否为直角三角形
      
  38. bool CTriangle::isRightTriangle()  
  39. {  
  40.     if(a*a+b*b == c*c || a*a+c*c == b*b || b*b+c*c == a*a)  
  41.     {  
  42.         return true;  
  43.     }  
  44.     else  
  45.     {  
  46.     return false;  
  47.     }  
  48. }  
  49.   
  50. //是否为等腰三角形
      
  51. bool CTriangle::isIsoscelesTriangle()   
  52. {  
  53.     if(a==b||a==c||b==c)  
  54.     {  
  55.         return true;  
  56.     }  
  57.     else  
  58.     {  
  59.     return false;  
  60.     }  
  61. }  

运行结果:

上机感言:一定要赶上,不能拉下  。  加油

抱歉!评论已关闭.