本題要求實現一個常用圓形體體積的計算器。計算公式如下:
- 球體體積 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:
然后從標准輸入讀進一個整數指令。
輸出格式:
如果讀入的指令是1或2或3,則執行相應的體積計算;如果是其他整數,則程序結束運行。
- 當輸入為1時,在計算球體體積之前,打印
Please enter the radius:
,然后讀入球體半徑,完成計算; - 當輸入為2時,在計算圓柱體體積之前,打印
Please enter the radius and the height:
,然后讀入底圓半徑和高,完成計算; - 當輸入為3時,在計算圓錐體體積之前,打印
Please enter the radius and the height:
,然后讀入底圓半徑和高,完成計算。
計算結果在一行內輸出,保留小數點后兩位。
#include<stdio.h>
#include<math.h>
#define PI 3.1415926535
int main()
{
int n;
double height,radius;
while(1)
{
printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\n"
"Please enter your command:\n");
scanf("%d",&n);
if(n==1)
{
printf("Please enter the radius:\n");
scanf("%lf",&radius);
printf("%.2lf\n",4*PI*pow(radius,3)/3);
}
else if(n==2)
{
printf("Please enter the radius and the height:\n");
scanf("%lf %lf",&radius,&height);
printf("%.2lf\n",PI*radius*radius*height);
}
else if(n==3)
{
printf("Please enter the radius and the height:\n");
scanf("%lf %lf",&radius,&height);
printf("%.2lf\n",PI*radius*radius*height/3);
}
else
{
break;
}
}
}