海伦公式用于计算三角形的面积,公式如下:
\[ S = \sqrt{p \times (p - a) \times (p - b) \times (p - c)} \]
其中,\( S \) 是三角形的面积,\( a \)、\( b \) 和 \( c \) 是三角形的三条边长,\( p \) 是半周长,计算公式为:
\[ p = \frac{a + b + c}{2} \]
Java 示例
```java
public class TriangleAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入三角形的三个边长:");
double side1 = scanner.nextDouble();
double side2 = scanner.nextDouble();
double side3 = scanner.nextDouble();
double semiPerimeter = (side1 + side2 + side3) / 2;
double area = Math.sqrt(semiPerimeter * (semiPerimeter - side1) * (semiPerimeter - side2) * (semiPerimeter - side3));
System.out.println("三角形的面积为: " + area);
}
}
```
C++ 示例
```cpp
include include double calculateArea(double a, double b, double c) { double s = (a + b + c) / 2; return std::sqrt(s * (s - a) * (s - b) * (s - c)); } int main() { double a, b, c; std::cout << "请输入三角形的三个边长:" << std::endl; std::cin >> a >> b >> c; double area = calculateArea(a, b, c); std::cout << "三角形的面积为: " << std::fixed << std::setprecision(2) << area << std::endl; return 0; } ``` C 示例 ```csharp using System; class TriangleAreaCalculator { static void Main() { Console.Write("请输入三角形的三个边长:"); double side1 = Convert.ToDouble(Console.ReadLine()); double side2 = Convert.ToDouble(Console.ReadLine()); double side3 = Convert.ToDouble(Console.ReadLine()); double semiPerimeter = (side1 + side2 + side3) / 2; double area = Math.Sqrt(semiPerimeter * (semiPerimeter - side1) * (semiPerimeter - side2) * (semiPerimeter - side3)); Console.WriteLine("三角形的面积为: " + area); } } ``` Python 示例 ```python import math def calculate_area(a, b, c): s = (a + b + c) / 2 return math.sqrt(s * (s - a) * (s - b) * (s - c)) if __name__ == "__main__": a = float(input("请输入三角形的第一个边长: ")) b = float(input("请输入三角形的第二个边长: ")) c = float(input("请输入三角形的第三个边长: ")) area = calculate_area(a, b, c) print(f"三角形的面积为: {area}") ``` 这些示例展示了如何在不同的编程语言中实现海伦公式来计算三角形的面积。请根据你使用的编程语言选择合适的示例。