#include
#include
#include
using namespace std;
/完善此函数/
double calArea(double a, double b, double c) {
}
int main(){
double a, b, c;
cin >> a >> b >> c;
try{
double area = calArea(a, b, c);
cout << area << endl;
}catch(exception& e){
cout << e.what() << endl;
}
}
输入描述
输入三角形三条边
输出描述
若输入确实为等腰三角形,输出其面积,保留两位小数。
若不是等腰三角形,抛出异常并输出字符串"The input is illegal"
样例输入
3 4 5
样例输出
The input is illegal
#include <iostream>
#include <stdexcept>
#include <cmath>
#include <iomanip>
using namespace std;
double calArea(double a, double b, double c) throw (invalid_argument)
{
if (a <= 0 || b <= 0 || c <= 0)
throw invalid_argument("The input is illegal");
if (a + b <= c || b + c <= a || c + a <= b)
throw invalid_argument("The input is illegal");
if (a != b && a != c && b != c)
throw invalid_argument("The input is illegal");
else
{
double p = (a + b + c) / 2;
return sqrt(p*(p - a)*(p - b)*(p - c));
}
}
int main(){
double a, b, c;
cin >> a >> b >> c;
try{
double area = calArea(a, b, c);
cout << fixed << setprecision(2) << area << endl;
}catch(exception& e){
cout << e.what() << endl;
}
}