/* Copyright (c) 2015, Yantai University School of Computer * All rights reserved. * File name: Test.cpp *: Liu Chang * completion Date: April 5, 2015 * version number: v1.0 * * Problem Description: This project design a triangle class whose data members are no longer the edge length of the triangle three edges , but the three vertices of a triangle. using the Design triangle class, input the three vertices of the triangle, find out its area, perimeter, and determine whether it is right triangle and isosceles triangle. The Project 2 is implemented in the form of "multiple files per project", where the declarations of two classes are placed in the same. h file, each of which has a file for the member function of each class , and a file for the main () function. Appreciate the advantages of this arrangement. * Input Description: Three vertices of the input triangle; * Program output: Output its area, perimeter, and determine if it is right triangle and isosceles triangle.
The code is as follows:
(1) main.cpp
#include <iostream> #include "CPointandCTriangle.h" using namespace Std;int Main () { Cpoint x, y, Z; X.input (); Y.input (); Z.input (); Ctriangle Tri1 (x, y, z); cout<< "The circumference of the triangle is:" <<tri1.perimeter () << ", Area:" <<tri1.area () <<endl<<endl; if (Tri1.isrighttriangle () &&tri1.isisoscelestriangle ()) cout<< "The triangle is isosceles right triangle." <<endl; else if (Tri1.isrighttriangle ()) cout<< "The triangle is right triangle." <<endl; else if (Tri1.isisoscelestriangle ()) cout<< "The triangle is isosceles triangle." <<endl; else cout<< "The triangle is neither isosceles triangle nor right triangle." <<endl; return 0;}
(2) CPointandCTriangle.h
#ifndef cpointandctriangle_h_included#define cpointandctriangle_h_includedclass cpoint{private:double x; horizontal double y; Ordinate Public:cpoint (double xx=0,double yy=0); Double Distance1 (Cpoint p) const; The distance between two points. void input (); Enter the coordinate point in X, y form. void output (); Output coordinate points in (x, y)};class ctriangle{public:ctriangle (Cpoint &x,cpoint &y,cpoint &z): A (×), B (y), C (Z) {}//given three The constructor of the point is void Settriangle (Cpoint &x,cpoint &y,cpoint &z); float perimeter (void); Calculates the circumference of the Triangle float area (void); Calculates and returns the area of the triangle bool Isrighttriangle (); Whether it is right triangle bool Isisoscelestriangle (); Whether it is isosceles triangle Private:cpoint a,b,c; Three vertices}; #endif//Cpointandctriangle_h_included
(3) Cpoint.cpp:
#include <iostream> #include "CPointandCTriangle.h" #include <cmath>using namespace std; Cpoint::cpoint (double xx,double yy) { x=xx; Y=yy;} 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 Cpoint::input () { char C; cout<< "input x, Y (in the form of X, y):"; while (1) { cin>>x>>c>>y; if (c== ', ') break ; else cout<< "input format is not correct, please reenter:"; }} void Cpoint::output () { cout<< "(" <<x<< "," <<y<< ")" <<ENDL;}
(4) CTriangle.cpp:
#include <iostream> #include "CPointandCTriangle.h" #include <cmath>using namespace std;void ctriangle:: Settriangle (Cpoint &x,cpoint &y,cpoint &z) {a=x; B=y; C=z;} Float Ctriangle::p erimeter (void) {return (A.distance1 (B) +b.distance1 (C) +c.distance1 (A)); float Ctriangle::area (void) {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); Double p = (A + B + c)/2; return sqrt (P * (p-a) * (p-b) * (P-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&& ((b*b+c*c-a*a) <1e-7)) | | (max==b&& ((a*a+c*c-b*b) <1e-7)) | | (max==c&& ((a*a+b*b-c*c) <1e-7))) return true; else return false;} BOOL Ctriangle::isisoscelestriangle () {double A=b.distance1 (C), B=c.distance1 (a), C=a.distance1 (B); if (a==b| | b==c| | A==C) return true; else return false;}
Operation Result:
Summary of Knowledge points:
Experience multi-file organization operations again
5th week Item 3-Programs that organize multiple classes with multiple Files-object Count Group members