/* (程序头部注释开始) * 程序的版权和版本声明部分 * 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; else return false; }