第六周實驗 報告(四)

來源:互聯網
上載者:User
06.#include<iostream>  07.#include<Cmath>  08.using namespace std;  09.  10.class CPoint  11.{  12.private:  13.    double x;      // 橫座標  14.    double y;      // 縱座標  15.public:  16.    void input();        //以x,y 形式輸入座標點  17.    float Distance(CPoint p) const;       // 兩點之間的距離(一點是當前點,另一點為參數p)  18.};  19.  20.class CTriangle  21.{  22.public:  23.    CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //給出三點的建構函式  24.    void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);   //輸入三角形點的座標  25.    float perimeter(void);              //計算三角形的周長  26.    float area(void);                   //計算並返回三角形的面積  27.    bool isRightTriangle();             //是否為直角三角形  28.    bool isIsoscelesTriangle();         //是否為等腰三角形  29.private:  30.    CPoint A,B,C; //三頂點  31.};  32.  33.  34.  35.void main()  36.{  37.    CPoint c1, c2, c3;  38.  39.    c1.input();  40.  41.    c2.input();  42.  43.    c3.input();  44.  45.    CTriangle c(c1, c1, c1);  46.  47.    c.setTriangle(c1, c2, c3);  48.  49.    cout << "三角形的周長是:" << c.perimeter() << endl;  50.  51.    cout << "三角形的面積是:" << c.area() << endl;  52.  53.    cout << (c.isRightTriangle()?"是":"不是") << "直角三角形" <<endl;    54.  55.    cout << (c.isIsoscelesTriangle()?"是":"不是") << "等腰三角形" <<endl;   56.  57.    system("pause");  58.}  59.  60.float CPoint::Distance(CPoint p) const  61.{  62.    return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));  63.}  64.  65.void CPoint::input()              //以x,y 形式輸入座標點  66.{  67.    char comma;  68.    cout << "請輸入焦點座標,格式: x,y" << endl;  69.      70.    while(1)  71.    {  72.        cin >> x >> comma >> y ;  73.        if(comma != ',')  74.        {  75.            cout << "格式不正確,請重新輸入:" << endl;  76.        }  77.        else  78.        {  79.            break;  80.        }  81.    }  82.}  83.  84.void CTriangle::setTriangle(CPoint &X, CPoint &Y, CPoint &Z)  85.{  86.    float s1 = X.Distance(Y);  87.  88.    float s2 = Y.Distance(Z);  89.  90.    float s3 = X.Distance(Z);  91.  92.    if(s1 + s2 > s3 && s2 + s3 > s1 && s1 + s3 > s2)  93.    {  94.        A = X;  95.  96.        B = Y;  97.  98.        C = Z;  99.    }  100.  101.    else  102.    {  103.        cout << "不能構成三角形,退出!" << endl;  104.        exit(1);  105.    }  106.  107.}  108.  109.float CTriangle::perimeter(void)  110.{  111.    return (A.Distance(B) + B.Distance(C) + A.Distance(C));  112.}  113.  114.float CTriangle::area(void)  115.{  116.    float a = A.Distance(B);  117.  118.    float b = B.Distance(C);  119.  120.    float c = A.Distance(C);  121.  122.    float p = (a + b + c) / 2;    123.  124.    return sqrt( p * (p - a) * (p - b) * (p - c) );  125.}  126.  127.bool CTriangle::isRightTriangle()  128.{  129.    float a = A.Distance(B);  130.  131.    float b = B.Distance(C);  132.  133.    float c = A.Distance(C);  134.  135.    if(a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a)  136.    {  137.        return true;  138.    }  139.  140.    else  141.    {  142.        return false;  143.    }  144.}  145.  146.bool CTriangle::isIsoscelesTriangle()  147.{  148.    float a = A.Distance(B);  149.  150.    float b = B.Distance(C);  151.  152.    float c = A.Distance(C);  153.  154.    if(a == b || b == c || a == c)  155.    {  156.        return true;  157.    }  158.  159.    else  160.    {  161.        return false;  162.    }  163.}  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.