PTA-R7-1 圆形体体积计算器

R7-1 圆形体体积计算器

分数 20

全屏浏览题目

切换布局

作者 张高燕

单位 浙江大学

本题要求实现一个常用圆形体体积的计算器。计算公式如下:

  • 球体体积 V=4​π*r3/3,其中r是球体半径。
  • 圆柱体体积 V=π *r2h,其中r是底圆半径,h是高。
  • 圆锥体体积 V=​π *r2h/3,其中r是底圆半径,h是高。

输入格式:

在每次计算之前,要求输出如下界面:

1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:

然后从标准输入读进一个整数指令。

输出格式:

如果读入的指令是1或2或3,则执行相应的体积计算;如果是其他整数,则程序结束运行。

  • 当输入为1时,在计算球体体积之前,打印Please enter the radius:,然后读入球体半径,完成计算;
  • 当输入为2时,在计算圆柱体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算;
  • 当输入为3时,在计算圆锥体体积之前,打印Please enter the radius and the height:,然后读入底圆半径和高,完成计算。

计算结果在一行内输出,保留小数点后两位。

输入样例:

1
2
3
2.4 3
0

输出样例:

1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius:
33.51
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:
Please enter the radius and the height:
18.10
1-Ball
2-Cylinder
3-Cone
other-Exit
Please enter your command:

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 M

注:Π(音:派)的数字只限定在6位小数的话计算值是错的。。

 1 #include <stdio.h>
 2 
 3 void face();  //函数声明1,每次的界面输出
 4 
 5 struct volume{
 6     int num;
 7     double r;
 8     double h;
 9     double ver;//体积
10 };
11 
12 int main()
13 {
14     double x = 3.1415926535;
15     struct volume v[9999];
16     
17     int i = 0;
18     while(1){
19         scanf("%d", &v[i].num);
20         if(v[i].num == 1){
21             scanf("%lf", &v[i].r);
22         }else if(v[i].num == 2){
23             scanf("%lf %lf", &v[i].r, &v[i].h);
24         }else if(v[i].num == 3){
25             scanf("%lf %lf", &v[i].r, &v[i].h);
26         }else{
27             break;
28         }
29         i++;
30     }
31     i = 0;
32     while(1){
33         face();
34         if(v[i].num == 1){
35             printf("Please enter the radius:\n");
36             v[i].ver = 4 * x * v[i].r * v[i].r * v[i].r / 3;
37         }else if(v[i].num == 2){
38             printf("Please enter the radius and the height:\n");
39             v[i].ver = x * v[i].r * v[i].r * v[i].h;
40         }else if(v[i].num == 3){
41             printf("Please enter the radius and the height:\n");
42             v[i].ver = x * v[i].r * v[i].r * v[i].h / 3;
43         }else{
44             break;
45         }
46         printf("%.2f\n", v[i].ver);
47         i++;
48     }
49     
50     return 0;
51 }
52 
53 void face()   //函数定义1
54 {
55     printf("1-Ball\n");
56     printf("2-Cylinder\n");
57     printf("3-Cone\n");
58     printf("other-Exit\n");
59     printf("Please enter your command:\n");
60 }

 

posted on 2022-12-23 15:15  青空栀浅fd  阅读(281)  评论(0)    收藏  举报