C language and algorithm design course experiment three: the simplest C programming - sequential programming (2)
- 1. Purpose of the experiment
- 2. Experimental content
- 3. Experimental steps
- 3.2. Sequential programming experiment topic 2: Experimental procedures for calculating the circumference of a circle, the area of a circle, the surface area of a sphere, the volume of a sphere, and the volume of a cylinder
- 4. Experimental summary
- 5. The complete procedure of the experiment
1. Purpose of the experiment
(1) Master the usage method of one of the most used statements in C language - assignment statement.
(2) Master the input and output methods of various types of data, and be able to use various format conversion symbols correctly.
(3) Further master the methods of writing and debugging programs.
2. Experimental content
2.2. Experiment content 2: find the circumference of a circle, the area of a circle, the surface area of a sphere, the volume of a sphere, and the volume of a cylinder
(2) Suppose the radius of the circle is r=l. 5, and the height of the cylinder is 9=3. Find the circumference, area of the circle, surface area of the sphere, volume of the sphere, and volume of the cylinder. Write a program, use scanf
input data, and output calculation results. There must be a text description when outputting, and take two digits after the decimal point.
3. Experimental steps
3.2. Sequential programming experiment topic 2: Experimental procedures for calculating the circumference of a circle, the area of a circle, the surface area of a sphere, the volume of a sphere, and the volume of a cylinder
(2) Suppose the radius of the circle is r=l. 5, and the height of the cylinder is h=3. Find the circumference, area of the circle, surface area of the sphere, volume of the sphere, and volume of the cylinder. Write a program, use scanf
input data, and output calculation results. There must be a text description when outputting, and take two digits after the decimal point.
3.2.1. Define macros
The code to define the macro is as follows
#define PI 3.1415926 // π
3.2.2. Define variables
The code to define the variable is as follows
float R; // 定义半径变量
float Height; // 定义高变量
float CP; // 定义圆周长变量
float CA; // 定义圆面积变量
float BSA; // 定义圆球表面积变量
float BV; // 定义圆球体积变量
float CCV; // 定义圆柱体变量
3.2.3. Input the radius and height of the circle
The code for entering the radius and height of the circle is as follows
printf("输入圆的半径,圆柱的高: ");
scanf("%f%f", &R, &Height); // 输入圆的半径、高
3.2.4. Calculate the circumference, area and other parameters of the circle
The code for calculating parameters such as the circumference and area of a circle is as follows
CP = 2 * PI * R; // 计算圆周长
CA = PI * R * R; // 计算圆面积
BSA = 4 * PI * R * R; // 计算圆球表面积
BV = 3.0 / 4.0 * PI * R * R * R; // 计算圆球体积
CCV = PI * R * R * Height; // 计算圆柱体积
3.2.5. Parameters such as the circumference and area of the output circle
The code to output parameters such as the circumference and area of a circle is as follows
// 输出数据
printf("圆周长 = %.2f\n", CP);
printf("圆面积 = %.2f\n", CA);
printf("圆球表面积 = %.2f\n", BSA);
printf("圆球体积 = %.2f\n", BV);
printf("圆柱体积 = %.2f\n", CCV);
3.2.6. Program running results
The experimental results of calculating the circumference, area of a circle, surface area of a sphere, volume of a sphere, and volume of a cylinder are as follows
4. Experimental summary
Through this experiment: C language and algorithm design course experiment three: the simplest C programming - 4 topics of sequential programming, mastered the following points.
-(1) Master the usage method of one of the most used statement-assignment statement in C language.
-(2) Master the methods of input and output of various types of data, and be able to use various format conversion symbols correctly.
-(3) Further master the methods of writing and debugging programs.
5. The complete procedure of the experiment
5.2. Sequential program design experiment topic 2: The complete program for finding the circumference of a circle, the area of a circle, the surface area of a sphere, the volume of a sphere, and the volume of a cylinder
The complete program is as follows
#define _CRT_SECURE_NO_WARNINGS
#define PI 3.1415926 // π
#include <stdio.h>
int main()
{
float R; // 定义半径变量
float Height; // 定义高变量
float CP; // 定义圆周长变量
float CA; // 定义圆面积变量
float BSA; // 定义圆球表面积变量
float BV; // 定义圆球体积变量
float CCV; // 定义圆柱体变量
printf("输入圆的半径,圆柱的高: ");
scanf("%f%f", &R, &Height); // 输入圆的半径、高
CP = 2 * PI * R; // 计算圆周长
CA = PI * R * R; // 计算圆面积
BSA = 4 * PI * R * R; // 计算圆球表面积
BV = 3.0 / 4.0 * PI * R * R * R; // 计算圆球体积
CCV = PI * R * R * Height; // 计算圆柱体积
// 输出数据
printf("圆周长 = %.2f\n", CP);
printf("圆面积 = %.2f\n", CA);
printf("圆球表面积 = %.2f\n", BSA);
printf("圆球体积 = %.2f\n", BV);
printf("圆柱体积 = %.2f\n", CCV);
return 0;
}