實驗10-1 圓形體體積計算器 (20分)


本題要求實現一個常用圓形體體積的計算器。計算公式如下:

  • 球體體積 V=34​​πr3​​,其中r是球體半徑。
  • 圓柱體體積 V=πr2​​h,其中r是底圓半徑,h是高。
  • 圓錐體體積 V=31​​πr2​​h,其中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:

代碼:
#include <stdio.h>
#include <string.h>
#include <math.h>
#define PI acos(-1)
int main() {
    int d,flag = 0;
    double r,h;
    printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
    while(scanf("%d",&d)) {
        switch(d) {
            case 1:
                printf("Please enter the radius:\n");
                scanf("%lf",&r);
                printf("%.2f\n",PI * 4 / 3 * r * r * r);
                break;
            case 2:
                printf("Please enter the radius and the height:\n");
                scanf("%lf%lf",&r,&h);
                printf("%.2f\n",PI * r * r * h);
                break;
            case 3:
                printf("Please enter the radius and the height:\n");
                scanf("%lf%lf",&r,&h);
                printf("%.2f\n",PI * r * r * h / 3);
                break;
            default: flag = 1;
        }
        if(flag) break;
        printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM