用两种方式编写程序,要求:输入圆形半径,计算圆形的面积和周长,其中,要求使用符号 常量 PI。 函数原型:void calc(int r, int& perimeter, double& area)的解题思路
时间: 2023-06-20 07:10:19 浏览: 60
第一种方式是使用C++中的cmath库中已经定义好的常量PI,直接使用该常量进行计算。代码如下:
```c++
#include <iostream>
#include <cmath>
using namespace std;
void calc(int r, int& perimeter, double& area){
perimeter = 2 * M_PI * r;
area = M_PI * r * r;
}
int main(){
int r, perimeter;
double area;
cout << "请输入圆形半径:" << endl;
cin >> r;
calc(r, perimeter, area);
cout << "圆形周长为:" << perimeter << endl;
cout << "圆形面积为:" << area << endl;
return 0;
}
```
第二种方式是手动定义常量PI,即将PI的值定义为3.14159265358979323846,然后进行计算。代码如下:
```c++
#include <iostream>
using namespace std;
const double PI = 3.14159265358979323846;
void calc(int r, int& perimeter, double& area){
perimeter = 2 * PI * r;
area = PI * r * r;
}
int main(){
int r, perimeter;
double area;
cout << "请输入圆形半径:" << endl;
cin >> r;
calc(r, perimeter, area);
cout << "圆形周长为:" << perimeter << endl;
cout << "圆形面积为:" << area << endl;
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)