📅  最后修改于: 2023-12-03 15:12:02.040000             🧑  作者: Mango
这是一个针对圆和圆柱体的 C 语言程序。通过用户输入半径和高度,程序可以计算出圆的面积和圆柱体的体积。
以下是用 C 语言编写圆的面积的程序。该程序使用了 #define
预处理器,定义了圆周率 π 的值为 3.14159。
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, area;
printf("Enter radius of circle: ");
scanf("%lf", &radius);
area = PI * radius * radius;
printf("Area of circle of radius %.2f is %.2f\n", radius, area);
return 0;
}
代码片段的markdown格式如下:
```c
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, area;
printf("Enter radius of circle: ");
scanf("%lf", &radius);
area = PI * radius * radius;
printf("Area of circle of radius %.2f is %.2f\n", radius, area);
return 0;
}
### 计算圆柱体的体积
以下是用 C 语言编写圆柱体的体积的程序。
```c
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, height, volume;
printf("Enter radius of cylinder: ");
scanf("%lf", &radius);
printf("Enter height of cylinder: ");
scanf("%lf", &height);
volume = PI * radius * radius * height;
printf("Volume of cylinder of radius %.2f and height %.2f is %.2f\n", radius, height, volume);
return 0;
}
代码片段的markdown格式如下:
```c
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, height, volume;
printf("Enter radius of cylinder: ");
scanf("%lf", &radius);
printf("Enter height of cylinder: ");
scanf("%lf", &height);
volume = PI * radius * radius * height;
printf("Volume of cylinder of radius %.2f and height %.2f is %.2f\n", radius, height, volume);
return 0;
}
在程序中,我们使用 `scanf()` 函数接收用户输入的半径和高度。然后,我们使用预处理器定义的一个名为 `PI` 的常量,其值为圆周率 3.14159。计算圆的面积和圆柱体的体积,并使用 `printf()` 函数将结果输出到控制台。
值得注意的是,无论是计算圆的面积还是圆柱体的体积,都需要用到圆的半径。因此,这两段代码有很多相同的部分。将它们分开编写会浪费时间和劳动力。实际上,我们可以将公共代码抽象为一个函数,将需要计算的参数传递给函数,然后返回结果。
```c
#include <stdio.h>
#define PI 3.141592
double area_of_circle(double radius) {
return PI * radius * radius;
}
double volume_of_cylinder(double radius, double height) {
return PI * radius * radius * height;
}
int main() {
double radius, height;
printf("Enter radius of circle: ");
scanf("%lf", &radius);
printf("Enter height of cylinder: ");
scanf("%lf", &height);
double area = area_of_circle(radius);
printf("Area of circle of radius %.2f is %.2f\n", radius, area);
double volume = volume_of_cylinder(radius, height);
printf("Volume of cylinder of radius %.2f and height %.2f is %.2f\n", radius, height, volume);
return 0;
}
代码片段的markdown格式如下:
```c
#include <stdio.h>
#define PI 3.141592
double area_of_circle(double radius) {
return PI * radius * radius;
}
double volume_of_cylinder(double radius, double height) {
return PI * radius * radius * height;
}
int main() {
double radius, height;
printf("Enter radius of circle: ");
scanf("%lf", &radius);
printf("Enter height of cylinder: ");
scanf("%lf", &height);
double area = area_of_circle(radius);
printf("Area of circle of radius %.2f is %.2f\n", radius, area);
double volume = volume_of_cylinder(radius, height);
printf("Volume of cylinder of radius %.2f and height %.2f is %.2f\n", radius, height, volume);
return 0;
}
不仅是在计算圆和圆柱体的情况下,函数的抽象化也适用于各种计算任务,特别是在有许多相同的代码的情况下。