輸入三角形三邊長,計算面積,C++ 實現。
s = (a+b+c)/2; Area = sqrt(s*(s-a)*(s-b)*(s-c))
#include
#include
using namespace std;
int main() {
double a, b, c, s;
cout << "請輸入三角形的三邊長度: ";
cin >> a >> b >> c;
// 合法性判斷:任意兩邊之和大於第三邊
if (a + b <= c || a + c <= b || b + c <= a) {
cout << "輸入邊長無法構成三角形!" << endl;
return 1;
}
s = (a + b + c) / 2.0;
double area = sqrt(s * (s - a) * (s - b) * (s - c));
cout << "三角形面積 = " << area << endl;
return 0;
}
#include 提供 sqrt。double 支持小數。cout << fixed << setprecision(2) 保留小數位。