第六周实验报告(4)
来源:互联网 发布:linux 安装tar 编辑:程序博客网 时间:2024/05/23 15:42
#include <iostream> #include <Cmath> using namespace std; class CPoint { private: double x; // 横坐标 double y; // 纵坐标 public: CPoint(double xx=0,double yy=0); double distance(CPoint p) const; // 两点之间的距离 void input(); //以x,y 形式输入坐标点 void output(); //以(x,y) 形式输出坐标点 }; class CTriangle { public: CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //给出三点的构造函数 void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);// double perimeter(void);//计算三角形的周长 double area(void);//计算并返回三角形的面积 bool isRightTriangle(); //是否为直角三角形 bool isIsoscelesTriangle(); //是否为等腰三角形 private: CPoint A,B,C; //三顶点 }; CPoint::CPoint(double xx,double yy) { x=xx; y=yy; } void CPoint::input() { char c; cout<<"请输入坐标点(格式x,y )"; while(1) { cin>>x>>c>>y; if (c==',') break; cout<<"输入格式不正确"<<endl; } } void CPoint::output() { cout<<"("<<x<<", "<<y<<")"<<endl; } double CPoint::distance(CPoint p) const { double d; d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y)); return d; } void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z) { A=X; B=Y; C=Z; } double CTriangle::perimeter(void) { double a,b,c;a=B.distance(C);b=C.distance(A);c=A.distance(B); return (a + b + c); } double CTriangle::area(void) { double a,b,c;a=B.distance(C);b=C.distance(A);c=A.distance(B); double s;s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } bool CTriangle::isRightTriangle() { double a,b,c;a=B.distance(C);b=C.distance(A);c=A.distance(B); if((abs(a*a-b*b-c*c)<1e-7)||(abs(b*b-a*a-c*c)<1e-7)||(abs(c*c-b*b-a*a)<1e-7)) return true; else return false; } bool CTriangle::isIsoscelesTriangle() { double a,b,c;a=B.distance(C);b=C.distance(A);c=A.distance(B); if((abs(a-b)<1e-7)||(abs(b-c)<1e-7)||(abs(c-a)<1e-7)) return true; else return false; } void main(void) { cout<<"请输入三角形三点坐标"<<endl;CPoint p1,p2,p3;p1.input();p2.input();p3.input();CTriangle Tri1(p1,p2,p3); cout<<"该三角形的周长为:"<<Tri1.perimeter()<<",面积为:"<<Tri1.area()<<endl<<endl; cout<<"该三角形"<<(Tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl; cout<<"该三角形"<<(Tri1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl; system("pause"); }
总结:学会了同时使用多个类、
- 第六周实验报告4
- 第六周实验报告(4)
- 第六周实验报告4
- 第六周实验报告4
- 第六周实验报告4
- 第六周实验报告4
- 第六周实验报告4
- 第六周实验报告4
- 第六周实验报告4
- 第六周实验报告
- 《第六周实验报告任务4》
- 第六周实验报告(4)
- 第六周实验报告(任务4)
- 第六周C++实验报告(4)
- 第六周实验报告(4)
- 第六周实验报告任务4
- 第六周实验报告4(1)
- 第六周实验报告4(2)
- BT5 set_config各个选项的配置(1)
- poj 1088 滑雪[zz] 记忆化搜索
- 学习笔记——CSS
- 第六周实验报告(3)
- 如何撰写产品需求文档(PRD文档)
- 第六周实验报告(4)
- 判断dataset为空的方法
- XAF之ObjectSpace
- 第六周任务4
- trigger触发图片控制
- 只有程序员才能看得懂的生活哲理
- Java语言基础
- 第6周任务2
- WP7屏幕方向笔记