第六週作業任務五

/* (程序頭部註釋開始)   
* 程序的版權和版本聲明部分   
* Copyright (c) 2011, 煙臺大學計算機學院學生    
* All rights reserved.   
* 文件名稱:   Student.cpp               
* 作    者:   計114-4 張馨  
* 完成日期:    2012年   3  月  28 日   
* 版 本 號:    V 1.0   
   
* 對任務及求解方法的描述部分   
* 輸入描述:  
* 問題描述:    
* 程序輸出:按要求輸出    
* 程序頭部的註釋結束   
*/

class CPoint { private: double x; // 橫座標 double y; // 縱座標 public: CPoint(double xx=0,double yy=0); double getX(){return x;} double getY(){return y;} double Distance(CPoint p1) 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; //三頂點 }; 文件point.cpp,點類的定義 #include"ct.h" #include<iostream> #include<Cmath> using namespace std; CPoint::CPoint(double xx,double yy):x(xx),y(yy){} // 兩點之間的距離(一點是當前點,另一點爲參數p) double CPoint::Distance(CPoint p) const { return sqrt((this->x-p.x)*(this->x-p.x)+(this->y-p.y)*(this->y-p.y)); } void CPoint::input() //以x,y 形式輸入標點 { char ch; while(1) { cin>>x>>ch>>y; if(ch!=',') cout<<"格式錯!"<<endl; else break; } } void CPoint::output() //以(x,y) 形式輸出座標點 { cout<<'('<<x<<','<<y<<')'<<endl; } 文件 triangle.cpp,用於定義三角形類 #include"ct.h" #include<Cmath> void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z) { A=X; B=Y; C=Z; } double CTriangle::perimeter(void)//計算三角形的周長 { double a=B.Distance(C),b=C.Distance(A),c=A.Distance(B); return (a+b+c); } double CTriangle::area(void)//計算並返回三角形的面積 { double a=B.Distance(C),b=C.Distance(A),c=A.Distance(B); double s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c)); } bool CTriangle::isRightTriangle() //是否爲直角三角形 { double a=B.Distance(C),b=C.Distance(A),c=A.Distance(B); if((abs(a*a+b*b-c*c)<1e-6)||(abs(b*b+c*c-a*a)<1e-6)||(abs(c*c+a*a-b*b)<1e-6)) return true; else return false; } bool CTriangle::isIsoscelesTriangle() //是否爲等腰三角形 { double a=B.Distance(C),b=C.Distance(A),c=A.Distance(B); if((abs(a-b)<1e-6)||(abs(b-c)<1e-6)||(abs(c-a)<1e-6)) return true; else return false; } 文件main.cpp,在本題中,僅供用於測試類的功能,提供對類的調用 #include"ct.h" #include<iostream> using namespace std; void main(void) { CTriangle Tri1(CPoint(0,0),CPoint(0,2),CPoint(2,0)); //定義三角形類的一個實例(對象) cout<<"該三角形的周長爲:"<<Tri1.perimeter()<<",面積爲:"<<Tri1.area()<<endl<<endl; cout<<"該三角形"<<(Tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl; cout<<"該三角形"<<(Tri1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl; system("pause"); }

上級感言:感覺沒有很大的區別,可能這樣分文件裝起來比較清楚、有條理。

發佈了63 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章