计算梯形面积的公式是:
\[ \text{面积} = \frac{(上底 + 下底) \times 高}{2} \]
根据这个公式,可以编写不同编程语言的代码来计算梯形的面积。以下是几种常见编程语言的示例代码:
C++ 代码示例
```cpp
include using namespace std; int main() { double a, b, c; cout << "请输入梯形的上底: "; cin >> a; cout << "请输入梯形的下底: "; cin >> b; cout << "请输入梯形的高: "; cin >> c; double area = (a + b) * c / 2; cout << "梯形的面积为: " << fixed << setprecision(2) << area << endl; return 0; } ``` Python 代码示例 ```python def trapezoid_area(base1, base2, height): area = (base1 + base2) * height / 2 return area base1 = float(input("请输入梯形的上底: ")) base2 = float(input("请输入梯形的下底: ")) height = float(input("请输入梯形的高: ")) result = trapezoid_area(base1, base2, height) print("梯形的面积为:", result) ``` Java 代码示例 ```java import java.util.Scanner; public class TrapezoidArea { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入梯形的上底: "); double base1 = scanner.nextDouble(); System.out.print("请输入梯形的下底: "); double base2 = scanner.nextDouble(); System.out.print("请输入梯形的高: "); double height = scanner.nextDouble(); double area = (base1 + base2) * height / 2; System.out.printf("梯形的面积为: %.2f%n", area); } } ``` C 代码示例 ```csharp using System; class TrapezoidArea { static void Main() { Console.Write("请输入梯形的上底: "); double base1 = Convert.ToDouble(Console.ReadLine()); Console.Write("请输入梯形的下底: "); double base2 = Convert.ToDouble(Console.ReadLine()); Console.Write("请输入梯形的高: "); double height = Convert.ToDouble(Console.ReadLine()); double area = (base1 + base2) * height / 2; Console.WriteLine("梯形的面积为: {0:F2}", area); } } ``` 这些代码示例都遵循了梯形面积的计算公式,并通过用户输入获取上底、下底和高的值,最后计算并输出梯形的面积。