输入一个三角形三条边长,判断是否是等腰三角形并计算其面积。注意若输入数据非等腰三角形数据,要求使用exception处理。
#include <iostream> #include <stdexcept> #include <cmath> 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 <cmath> #include <stdexcept> #include<iomanip> using namespace std; using namespace std; /*完善此函数*/ double calArea(double a, double b, double c) { 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"); //由Heron公式计算三角形面积 double s=(a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-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; } return 0; }
