算法【判斷三角形是的類型以及計算面積】

Q:在這裏插入圖片描述
A:

首先,根據三角形判定定理:兩邊之和大於第三邊,判斷是否是三角形。然後根據海倫定理,求得三角形的面積。接着,如果三邊相等是等邊三角形,兩邊相等爲等腰三角形,兩邊之和等於第三邊爲直角三角形。

#include<iostream>
#include <math.h>
using namespace std;

// Is it a triangle?
int main() {
    //the edge of triangle.
    float a, b, c,s,area;
    cin>>a>>b>>c;
    if (a + b > c && a + c > b && b + c > a) {
        // Heron's formula
        s = (a+b+c)/2.0;
        area = sqrtf(s*(s-a)*(s-b)*(s-c));
        cout<<"the area is: "<<area<<endl;
        if(a==b && b ==c){
            cout<<"the triangle is Equilateral triangle!"<<endl;
        }else if(a==b || b==c || a==c){
            if(pow(a,2)+pow(b,2)==pow(c,2)||pow(a,2)+pow(c,2)==pow(b,2)||pow(b,2)+pow(c,2)==pow(a,2)){
                cout<<"the triangle is Isosceles right triangle!"<<endl;
            }else{
                cout<<"the triangle is Isosceles triangle!"<<endl;
            }
        }else if(pow(a,2)+pow(b,2)==pow(c,2)||pow(a,2)+pow(c,2)==pow(b,2)||pow(b,2)+pow(c,2)==pow(a,2)){
            cout<<"the triangle is Right triangle!"<<endl;
        }else {
            cout<<"the triangle is ordinary triangle!"<<endl;
        }
    }else{
        cout<<"not a triangle"<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章