算法【判断三角形是的类型以及计算面积】

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章