用c++分别求三角形、矩形,圆形的图形面积
时间: 2023-05-17 15:03:17 浏览: 74
好的,以下是用 C++ 求解三角形、矩形和圆形面积的代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c, r, l, w, s_tri, s_rect, s_circ;
// 计算三角形面积
cout << "请输入三角形的三条边长:";
cin >> a >> b >> c;
double p = (a + b + c) / 2;
s_tri = sqrt(p * (p - a) * (p - b) * (p - c));
cout << "三角形的面积为:" << s_tri << endl;
// 计算矩形面积
cout << "请输入矩形的长和宽:";
cin >> l >> w;
s_rect = l * w;
cout << "矩形的面积为:" << s_rect << endl;
// 计算圆形面积
cout << "请输入圆的半径:";
cin >> r;
s_circ = M_PI * r * r;
cout << "圆的面积为:" << s_circ << endl;
return 0;
}
```
希望这个代码能够帮到你!
相关问题
用c++编写一个函数分别求三角形、矩形,圆形的图形面积
好的,以下是用C++编写的函数分别求三角形、矩形和圆形的面积:
```c++
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159265358979323846;
double triangleArea(double base, double height) {
return 0.5 * base * height;
}
double rectangleArea(double length, double width) {
return length * width;
}
double circleArea(double radius) {
return PI * pow(radius, 2);
}
int main() {
double base, height, length, width, radius;
cout << "Enter base and height of triangle: ";
cin >> base >> height;
cout << "Area of triangle: " << triangleArea(base, height) << endl;
cout << "Enter length and width of rectangle: ";
cin >> length >> width;
cout << "Area of rectangle: " << rectangleArea(length, width) << endl;
cout << "Enter radius of circle: ";
cin >> radius;
cout << "Area of circle: " << circleArea(radius) << endl;
return 0;
}
```
希望这个函数可以帮助你计算图形的面积。
用C++编程计算图形的面积
计算图形面积需要根据图形的种类进行不同的计算方法,下面分别给出几种常见图形的计算方法:
1. 矩形面积计算
矩形的面积等于长乘以宽,可以使用以下的C++代码进行计算:
```c++
#include <iostream>
using namespace std;
int main() {
double length = 5; // 矩形的长
double width = 3; // 矩形的宽
double area = length * width; // 矩形的面积
cout << "矩形的面积为:" << area << endl;
return 0;
}
```
2. 三角形面积计算
三角形的面积等于底边乘以高除以二,可以使用以下的C++代码进行计算:
```c++
#include <iostream>
using namespace std;
int main() {
double base = 5; // 三角形的底
double height = 3; // 三角形的高
double area = base * height / 2; // 三角形的面积
cout << "三角形的面积为:" << area << endl;
return 0;
}
```
3. 圆形面积计算
圆形的面积等于半径的平方乘以π,可以使用以下的C++代码进行计算:
```c++
#include <iostream>
using namespace std;
int main() {
double radius = 5; // 圆的半径
double pi = 3.1415926; // π的值
double area = radius * radius * pi; // 圆的面积
cout << "圆的面积为:" << area << endl;
return 0;
}
```
以上是一些常见图形的面积计算方法,你可以根据自己需要进行修改。
相关推荐
![](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)