C语言 圆形体体积计算器
圓形體體積計算器
題目描述:
本題要求實現一個常用圓形體體積的計算器。計算公式如下: 球體體積 V= ? ?4/3πr ?3 ?? ,其中r是球體半徑。 圓柱體體積 V=πr ?2 ??h,其中r是底圓半徑,h是高。 圓錐體體積 V= ?1/3πr 2 ?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 "math.h" int lifter() {int n;double v1,v2,v3,r,h,p=3.1415;while(1)//判斷指令是1或2或3 {scanf("%d",&n);//輸入指令 printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");if(n==1)//球體體積{scanf("%lf",&r);printf("Please enter the radius:\n");v1=4.0/3*p*pow(r,3);printf("%.2lf\n",v1);}else if(n==2)//圓柱體體積{scanf("%lf%lf",&r,&h);printf("Please enter the radius and the height:\n");v2=p*pow(r,2)*h;printf("%.2lf\n",v2);}else if(n==3)//圓錐體體積{scanf("%lf%lf",&r,&h);printf("Please enter the radius and the height:\n");v3=1.0/3*p*pow(r,2)*h;printf("%.2lf\n",v3);}else//其他整數結束程序 return 0;} } int main() {lifter();return 0; }運行結果如下圖:
總結
以上是生活随笔為你收集整理的C语言 圆形体体积计算器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php配置教程ecstore,ecsto
- 下一篇: 关于wemall,你知道多少?