码迷,mamicode.com
首页 > 其他好文 > 详细

The Area of an Arbitrary Triangle-任意三角形的面积

时间:2014-09-01 02:50:43      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:the area of an arbitrary triangle-任意三角形的面积

The Area of an Arbitrary Triangle-任意三角形的面积,允许重复计算:

//The Area of an Arbitrary Triangle-任意三角形的面积
#include<iostream>
#include<cmath>
using namespace std;

bool IsTriangle(double a,double b,double c);
void areafun(double a,double b,double c,double& area,double& s);

int main()
{
    double a,b,c,s,area;
    char ans;
    do{
        cout<<"Enter the three length of the Triangle:\n";
        cin>>a>>b>>c;
    
        if(IsTriangle(a,b,c))
            areafun(a,b,c,area,s);
        else
            cout<<"The three length are error!\n";
        
        cout<<"Do you want again?";
        cin>>ans;
    }while(‘Y‘ == ans || ‘y‘ == ans);
    return 0;
    
}

bool IsTriangle(double a,double b,double c)
{
    if((a+b>c && a+c>b && b+c>a && abs(a-b)<c && abs(a-c)<b && abs(b-c)<a) )
        return 1;
    return 0;
}

void areafun(double a,double b,double c,double& area,double& s)
{
    s = (a+b+c)/2;
    area = sqrt(s*(s-a)*(s-b)*(s-c));
    cout<<"The area of the Triangle is:"<<area<<endl;
}

结果:

Enter the three length of the Triangle:
3 4 5
The area of the Triangle is:6
Do you want again?y
Enter the three length of the Triangle:
3 4 4
The area of the Triangle is:5.56215
Do you want again?y
Enter the three length of the Triangle:
3 4 9
The three length are error!
Do you want again?y
Enter the three length of the Triangle:
4 4 4
The area of the Triangle is:6.9282
Do you want again?


The Area of an Arbitrary Triangle-任意三角形的面积

标签:the area of an arbitrary triangle-任意三角形的面积

原文地址:http://9320314.blog.51cto.com/9310314/1547183

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!