分别定义两个函数: (1)计算长方形面积的函数int chang(int x,int y); (2)计算三角形面积的函数float san(int a,int h); 在main()函数中输入用户选择(1,求长方形面积;2.求三角形面积),根据用户选择转去调用相应的函数并输出结果。
时间: 2024-02-19 11:57:24 浏览: 19
好的,以下是一个简单的 C++ 示例代码,用于实现你的需求:
```c++
#include <iostream>
using namespace std;
// 计算长方形面积的函数
int chang(int x, int y) {
return x * y;
}
// 计算三角形面积的函数
float san(int a, int h) {
return 0.5 * a * h;
}
int main() {
int choice; // 用户选择
int x, y; // 长方形的长和宽
int a, h; // 三角形的底和高
float result; // 结果
cout << "请选择计算面积的图形(1.长方形;2.三角形):" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入长方形的长和宽:" << endl;
cin >> x >> y;
result = chang(x, y);
cout << "长方形的面积为:" << result << endl;
break;
case 2:
cout << "请输入三角形的底和高:" << endl;
cin >> a >> h;
result = san(a, h);
cout << "三角形的面积为:" << result << endl;
break;
default:
cout << "无效的选择!" << endl;
break;
}
return 0;
}
```
在这个程序中,我们定义了两个函数 `chang` 和 `san`,分别用于计算长方形和三角形的面积。在主函数中,我们首先让用户选择要计算面积的图形,然后根据用户的选择调用相应的函数进行计算,并输出结果。
希望这个示例代码能够帮助到你!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)