课本例题啦,细节稍微有点不同。
友情提示,此题验证一直错误的,建议看一下PI的精度哦(别问我是怎么知道的,一个小时的代价。。。。。)
#include<stdio.h>
#define PI 3.1415926535
void cal(int sel);
double v_ball (void);
double v_cylinder (void);
double v_cone (void);
int main (void)
{
int sel;
while (1) {
printf("1-Ball\n");
printf("2-Cylinder\n");
printf("3-Cone\n");
printf("other-Exit\n");
printf("Please enter your command:\n");
scanf("%d", &sel);
if (sel <1 || sel >3)
break;
else
cal(sel);
}
return 0;
}
void cal (int sel)
{
switch (sel) {
case 1:
printf("%.2f\n", v_ball());
break;
case 2:
printf("%.2f\n", v_cylinder());
break;
case 3:
printf("%.2f\n", v_cone());
break;
}
}
double v_ball (void)
{
double r;
printf("Please enter the radius:\n");
scanf("%lf", &r);
return (4.0/3.0*PI*r*r*r);
}
double v_cylinder(void)
{
double r,h;
printf("Please enter the radius and the height:\n");
scanf("%lf %lf", &r, &h);
return (PI*r*r*h);
}
double v_cone (void)
{
double r,h;
printf("Please enter the radius and the height:\n");
scanf("%lf %lf", &r, &h);
return (1/3.0*PI*r*r*h);
}