第6周实验报告4

来源:互联网 发布:有哪些相亲软件 编辑:程序博客网 时间:2024/05/17 06:27
/* (程序头部注释开始)  * 程序的版权和版本声明部分  * Copyright (c) 2011, 烟台大学计算机学院学生   * All rights reserved.  * 文件名称:三角形坐标类                              * 作    者:张旭                                * 完成日期:  2012   年   3   月    19  日  * 版 本 号:略             * 对任务及求解方法的描述部分  * 输入描述:略   * 问题描述:略   * 程序输出:略   * 程序头部的注释结束  */#include <iostream>#include <cmath>using namespace std;enum SymmetricStyle {axisx,axisy,point};//分别表示按x轴, y轴, 原点对称class CPoint{private:double x;  // 横坐标double y;  // 纵坐标public:CPoint(double xx=0,double yy=0):x(xx), y(yy){}double Distance(CPoint p) const;   // 两点之间的距离(一点是当前点,另一点为参数p)double Distance0() const;          // 到原点的距离CPoint SymmetricAxis(SymmetricStyle style) 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; //三顶点};int main (){CTriangle q (CPoint(3,0),CPoint(0,0),CPoint(0,4));cout<<"该三角形的周长为:"<<q.perimeter()<<",面积为:"<<q.area()<< endl;cout<<"该三角形"<<(q.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;cout<<"该三角形"<<(q.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;system ("pause");return 0;}void CPoint::input(){cout << "请输入横坐标和纵坐标(x,y):" << endl;double a = 0, b = 0;char q;while (250){cin >> a >> q >> b;if (a != 0 && b != 0 && q == ','){break;}cout << "error! try again." << endl;}x = a;y = b;}void CPoint::output(){cout << '(' << x << ',' << y << ')';}double CPoint::Distance(CPoint p) const{return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));}double CPoint::Distance0() const{return sqrt(x * x + y * y);}CPoint CPoint::SymmetricAxis(SymmetricStyle style) const{switch (style){case 0:cout << '(' << x << ',' << -y << ')';break;case 1:cout << '(' << -x << ',' << y << ')';break;case 2:cout << '(' << -x << ',' << -y << ')';break;default:cout << "error!" << endl;}return 0;}void CTriangle::setTriangle (CPoint &X,CPoint &Y,CPoint &Z){A = X;B = Y;C = Z;}double CTriangle::perimeter (){return A.Distance (B) + B.Distance (C) + C.Distance (A);}double CTriangle::area(){double p = perimeter () / 2;return sqrt (p * (p - A.Distance (B)) * (p - B.Distance (C)) * (p - C.Distance (A)));}bool CTriangle::isIsoscelesTriangle () {if (A.Distance (B) == B.Distance (C) ||B.Distance (C) == C.Distance (A) ||A.Distance (B) == C.Distance (A))return true;else return false;}bool CTriangle::isRightTriangle (){if (A.Distance (B) * A.Distance (B) + B.Distance (C) * B.Distance (C) == C.Distance (A) * C.Distance (A) ||C.Distance (A) * C.Distance (A) + A.Distance (B) * A.Distance (B) == B.Distance (C) * B.Distance (C) ||B.Distance (C) * B.Distance (C) + C.Distance (A) * C.Distance (A) == A.Distance (B) * A.Distance (B))return true;elsereturn false;}

原创粉丝点击