第六週實驗報告4

  1. 1./* (程序頭部註釋開始)   
    2.* 程序的版權和版本聲明部分 
    3.* Copyright (c) 2011, 煙臺大學計算機學院學生    
    4.* All rights reserved.   
    5.* 文件名稱:   Student.cpp               
    6.* 作    者:   計114-4 劉程程    
    7.* 完成日期:    2012年   3  月  28日   
    8.* 版 本 號:    V 1.0   
    9.   
    10.* 對任務及求解方法的描述部分   
    11.* 輸入描述:  
    12.* 問題描述:    
    13.* 程序輸出:按要求輸出    
    14.* 程序頭部的註釋結束   
    15.*/  
    1.#include <iostream>    
    2.#include <Cmath>    
    3.using namespace std;    
    4.    
    5.class CPoint    
    6.{    
    7.private:    
    8.    double x;  // 橫座標    
    9.    double y;  // 縱座標    
    10.public:    
    11.    CPoint(double xx=0,double yy=0);    
    12.    double distance(CPoint p) const;   // 兩點之間的距離    
    13.    void input();  //以x,y 形式輸入座標點    
    14.    void output(); //以(x,y) 形式輸出座標點    
    15.};    
    16.    
    17.class CTriangle    
    18.{    
    19.public:    
    20.    CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //給出三點的構造函數    
    21.    void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//    
    22.    double perimeter(void);//計算三角形的周長    
    23.    double area(void);//計算並返回三角形的面積    
    24.    bool isRightTriangle(); //是否爲直角三角形    
    25.    bool isIsoscelesTriangle(); //是否爲等腰三角形    
    26.private:    
    27.    CPoint A,B,C; //三頂點    
    28.};    
    29.    
    30.CPoint::CPoint(double xx,double yy)    
    31.{    
    32.    x=xx;    
    33.    y=yy;    
    34.}    
    35.    
    36.// 輸入座標點    
    37.void CPoint::input()    
    38.{    
    39.    char ch;    
    40.    cout<<"請輸入座標點(格式x,y ):";    
    41.    while(1)    
    42.    {    
    43.        cin>>x>>ch>>y;    
    44.        if (ch==',') break;    
    45.        cout<<"輸入的數據格式不符合規範,請重新輸入\n";    
    46.    }    
    47.}    
    48.    
    49.// 輸出座標點    
    50.void CPoint::output()    
    51.{    
    52.    cout<<"("<<x<<", "<<y<<")"<<endl;    
    53.}    
    54.    
    55.// 求兩點之間的距離    
    56.double CPoint::distance(CPoint p) const    
    57.{    
    58.    double d;    
    59.    d=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));    
    60.    return d;    
    61.}    
    62.    
    63.void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)    
    64.{    
    65.    A=X;    
    66.    B=Y;    
    67.    C=Z;    
    68.}    
    69.    
    70.double CTriangle::perimeter(void)      
    71.{      
    72.    double a=B.distance(C),b=C.distance(A),c=A.distance(B); //求邊長    
    73.    return (a + b + c);      
    74.}      
    75.    
    76.double CTriangle::area(void)      
    77.{      
    78.       double a=B.distance(C),b=C.distance(A),c=A.distance(B);    
    79.    double s = (a + b + c) / 2;        
    80.    return sqrt(s * (s - a) * (s - b) * (s - c));       
    81.}     
    82.    
    83.bool CTriangle::isRightTriangle()//是否爲直角三角形    
    84.{    
    85.    double a=B.distance(C),b=C.distance(A),c=A.distance(B);    
    86.    if((abs(a*a-b*b-c*c)<1e-7)||(abs(b*b-a*a-c*c)<1e-7)||(abs(c*c-b*b-a*a)<1e-7))    
    87.        return true;    
    88.    else    
    89.        return false;    
    90.}    
    91.bool CTriangle::isIsoscelesTriangle() //是否爲等腰三角形    
    92.{    
    93.    double a=B.distance(C),b=C.distance(A),c=A.distance(B);    
    94.    if((abs(a-b)<1e-7)||(abs(b-c)<1e-7)||(abs(c-a)<1e-7))    
    95.        return true;    
    96.    else    
    97.        return false;    
    98.}    
    99.    
    100.void main(void)    
    101.{    
    102.    CTriangle Tri1(CPoint(2,5),CPoint(5,2),CPoint(7,8));    //定義三角形類的一個實例(對象)    
    103.    cout<<"該三角形的周長爲:"<<Tri1.perimeter()<<",面積爲:"<<Tri1.area()<<endl<<endl;    
    104.    cout<<"該三角形"<<(Tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;    
    105.    cout<<"該三角形"<<(Tri1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;    
    106.    system("pause"); 
    107.}
    

 

感悟:仔細看程序,求兩點之間的距離是重點!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章