7-165 圆形体体积计算器 (20 分)
生活随笔
收集整理的這篇文章主要介紹了
7-165 圆形体体积计算器 (20 分)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
7-165 圓形體體積計算器 (20 分)
本題要求實現(xiàn)一個常用圓形體體積的計算器。計算公式如下:
- 球體體積?V=34?πr3,其中r是球體半徑。
- 圓柱體體積?V=πr2h,其中r是底圓半徑,h是高。
- 圓錐體體積?V=31?πr2h,其中r是底圓半徑,h是高。
輸入格式:
在每次計算之前,要求輸出如下界面:
1-Ball 2-Cylinder 3-Cone other-Exit Please enter your command:然后從標準輸入讀進一個整數(shù)指令。
輸出格式:
如果讀入的指令是1或2或3,則執(zhí)行相應的體積計算;如果是其他整數(shù),則程序結束運行。
- 當輸入為1時,在計算球體體積之前,打印Please enter the radius:,然后讀入球體半徑,完成計算;
- 當輸入為2時,在計算圓柱體體積之前,打印Please enter the radius and the height:,然后讀入底圓半徑和高,完成計算;
- 當輸入為3時,在計算圓錐體體積之前,打印Please enter the radius and the height:,然后讀入底圓半徑和高,完成計算。
計算結果在一行內(nèi)輸出,保留小數(shù)點后兩位。
輸入樣例:
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<iostream> using namespace std; #define PI 3.1415926535 void print(){printf("1-Ball\n\ 2-Cylinder\n\ 3-Cone\n\ other-Exit\n\ Please enter your command:\n"); } int main(){int num;double r,h;while(1){print();cin>>num;if(num==1){printf("Please enter the radius:\n");cin>>r;printf("%.2lf\n",4/3.0*PI*r*r*r);}else if(num==2){printf("Please enter the radius and the height:\n");cin>>r>>h;printf("%.2lf\n",PI*1.0*r*r*h);}else if(num==3){printf("Please enter the radius and the height:\n");cin>>r>>h;printf("%.2lf\n",1.0/3*PI*r*r*h);}else break;}return 0; }總結
以上是生活随笔為你收集整理的7-165 圆形体体积计算器 (20 分)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机毕业设计SpringBoot选题推
- 下一篇: 蓝牙通信之蓝牙扫描