第六周实验报告(任务4)
2012-03-27 20:09
260 查看
程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 三角形类
* 作 者: 郭广建
* 完成日期: 2012年 3 月 26 日
* 版 本 号: 1.0
任务描述:
【任务4】设计一个三角形类,能够输入三角形的三个顶点,求出其面积、周长,并判断其是否为直角三角形和等腰三角形。
提示:(1)这个问题需要用到两个类,顶点类参照任务3中的CPoint类;(2)三角形类参考下面CTriangle类的声明;(3)充分利用CPoint类中已有的代码实现;(4)关于三条边的处理,可以增加三个私有属性,在初始化时求出来备用,也可以在需要时计算得到。
class CTriangle
{
public:
CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //给出三点的构造函数
void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
float perimeter(void);//计算三角形的周长
float area(void);//计算并返回三角形的面积
bool isRightTriangle(); //是否为直角三角形
bool isIsoscelesTriangle(); //是否为等腰三角形
private:
CPoint A,B,C; //三顶点
};
源程序:
运行结果:
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 三角形类
* 作 者: 郭广建
* 完成日期: 2012年 3 月 26 日
* 版 本 号: 1.0
任务描述:
【任务4】设计一个三角形类,能够输入三角形的三个顶点,求出其面积、周长,并判断其是否为直角三角形和等腰三角形。
提示:(1)这个问题需要用到两个类,顶点类参照任务3中的CPoint类;(2)三角形类参考下面CTriangle类的声明;(3)充分利用CPoint类中已有的代码实现;(4)关于三条边的处理,可以增加三个私有属性,在初始化时求出来备用,也可以在需要时计算得到。
class CTriangle
{
public:
CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //给出三点的构造函数
void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
float perimeter(void);//计算三角形的周长
float area(void);//计算并返回三角形的面积
bool isRightTriangle(); //是否为直角三角形
bool isIsoscelesTriangle(); //是否为等腰三角形
private:
CPoint A,B,C; //三顶点
};
源程序:
#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; // 两点之间的距离(一点是当前点,另一点为参数p) 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; //三顶点 double a, b, c; }; void CTriangle::setTriangle(CPoint &X, CPoint &Y, CPoint &Z)// { A = X; B = Y; C = Z; } double CTriangle::perimeter(void)//计算三角形的周长 { a = B.Distance(C),b = C.Distance(A),c = A.Distance(B); return (a+ b+ c); } double CTriangle::area(void)//计算并返回三角形的面积 { a = B.Distance(C),b = C.Distance(A),c = A.Distance(B); double q = (a+ b+ c)/2; return sqrt(q* (q - a)* (q - b)* (q - c)); } bool CTriangle::isRightTriangle() //是否为直角三角形 { a = B.Distance(C),b = C.Distance(A),c = A.Distance(B); if((a*a + b*b - c*c < 1e-7) || (a*a + c*c - b*b< 1e-7) || (b*b + c*c - a*a < 1e-7)) return true; else return false; } bool CTriangle::isIsoscelesTriangle() //是否为等腰三角形 { a = B.Distance(C),b = C.Distance(A),c = A.Distance(B); if((a - b < 1e-7) || (b - c < 1e-7) || (c - a < 1e-7)) return true; else return false; } CPoint::CPoint(double xx ,double yy ) { x = xx; y = yy; } double CPoint::Distance(CPoint p) const { double dis; dis = sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)); return dis; } void CPoint::input() { char c; cout << "以x,y形式输入坐标" <<endl; cin >> x >> c >> y; while(1) { if( c != ',') cout << "error!"; else break; } } void CPoint::output() { cout << "以(x,y)的形式输出坐标点" <<endl; cout << '(' << x << ',' << y << ')' <<endl; } void main() { CTriangle Tr1 (CPoint (0,2), CPoint (0,0), CPoint (2,0)); cout << "三角形的面积是:" << Tr1.area() << " " << "三角形的周长是:" << Tr1.perimeter() <<endl; cout << "这个三角形" << (Tr1.isRightTriangle()? "是" : "不是" )<< "直角三角形" <<endl; cout << "这个三角形" << (Tr1.isIsoscelesTriangle()? "是" : "不是") << "等腰三角形"<<endl; }
运行结果:
相关文章推荐
- 第六周实验报告任务3
- 第六周实验报告(任务四)【三角形类】
- 第六周实验报告任务4
- 第六周实验报告任务4
- 第六周实验报告(任务一)【this指针】
- 第六周实验报告任务5
- C++程序设计实验报告(四十三)---第六周任务一
- 第六周实验报告任务 1 下面程序存在编译错误。有两种修改方案,更倾向于那一种?为什么?
- 《C++第六周实验报告5-1》---将任务4的解决用一个项目多个文件的方式实现
- 第六周实验报告 任务四
- 第六周实验报告(任务1)
- 第六周实验报告(任务5)
- 第六周实验报告任务1
- C++程序设计实验报告(四十四)---第六周任务二
- 《第六周实验报告任务3》
- 第六周实验报告(任务2)
- 第六周实验报告任务2
- 《第六周实验报告任务1》
- 第六周实验报告(任务3)
- C++程序设计实验报告(四十七)---第六周任务五