第六週實驗報告(5)

* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生 
* All rights reserved.
* 文件名稱:設計一個三角形類,能夠輸入三角形的三個頂點,求出其面積、周長,並判斷其是否爲直角三角形和等腰三角形。

* 作    者:        王琦                      
* 完成日期:     2012    年 03      月    28  日
* 版 本 號:       V1.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; //三頂點      
};  


*.cpp文件

<pre class="cpp" name="code">
#include"shd.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;    
}  

*.cpp文件

<pre class="cpp" name="code">
#include"shd.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文件

<pre class="cpp" name="code">
#include"shd.h"     
#include<iostream>      
using namespace std;    
void main(void)    
{    
    CTriangle Tri1(CPoint(4,8),CPoint(5,6),CPoint(1,-6));    //定義三角形類的一個實例(對象)      
    cout<<"該三角形的周長爲:"<<Tri1.perimeter()<<endl;  
    cout<<"該三角形的面積爲:"<<Tri1.area()<<endl;    
    cout<<"該三角形"<<(Tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;    
    cout<<"該三角形"<<(Tri1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;    
    system("pause");    
}  

程序顯示:



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