Main.cpp
#include <iostream> #include "header.h" using namespace Std;int Main () { CPoint X (2,5), Y (5,2), Z (7,8); Ctriangle Tri1 (x, y, z); cout<< "The circumference of the triangle is:" <<tri1.perimeter () << ", Area:" <<tri1.area () <<endl<<endl; cout<< "The Triangle" << (Tri1.isrighttriangle ()? " Yes ":" not ") <<" right triangle "<<endl; cout<< "The Triangle" << (Tri1.isisoscelestriangle ()? " Yes ":" not ") <<" isosceles triangle "<<endl; return 0;}
Header.cpp
#include <iostream> #include <cmath> #include "header.h" using namespace std; Cpoint::cpoint (double xx,double yy) {x=xx; Y=yy;} void Cpoint::input () {char ch; cout<< "Please enter the coordinate point (format x, y):"; while (1) {cin>>x>>ch>>y; if (ch== ', ') break; cout<< "Input data format does not conform to specifications, please re-enter \ n"; }}void Cpoint::output () {cout<< "(" <<x<< "," <<y<< ")" <<ENDL;} Double CPoint::d Istance1 (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::p erimeter (void) {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); return (A + B + c);} Double Ctriangle::area (void) {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); Double s = (A + B + c)/2; return sqrt (S * (s-a) * (s-b) * (S-C));} BOOL Ctriangle::isrighttriangle () {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); Double max=a; If(B>max) max=b; if (C>max) max=c; if ((max==a) && (ABS (A*A-B*B-C*C) <1e-7) | | ((max==b) && (ABS (B*B-A*A-C*C) <1e-7)) | | ((max==c) && (ABS (C*C-B*B-A*A) <1e-7))) return true; else return false;} BOOL Ctriangle::isisoscelestriangle () {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); if (ABS (a) <1e-7) | | (ABS (B-C) <1e-7) | | (ABS (C-A) <1e-7)) return true; else return false;}
Header.h
#ifndef header_h_included#define header_h_includedclass cpoint{private: double x; Double y;public: CPoint (double xx=0,double yy=0); Double Distance1 (CPoint p) const; void input (); void output ();}; 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 ();p rivate: CPoint a,b,c;}; #endif//header_h_included
Operation Result:
Learning experience:
Good study Day Day up
Fifth week item three-multi-file organization multiple classes