7-1 圆形体体积计算器 (20 分)

这篇博客主要介绍了C++中如何通过函数进行球体、圆柱体和圆锥体体积的计算。文章强调了在编程时注意浮点数精度问题,如使用3.1415926535来提高π的精度,并提醒读者在处理输入输出时确保类型匹配,避免因类型不一致导致的死循环。此外,还提供了一个简单的用户交互程序示例,演示了如何根据用户输入调用相应函数计算几何体的体积。
摘要由CSDN通过智能技术生成

在这里插入图片描述
输入样例
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>
double Cone(double r,double h);
double Cylinder(double r,double h);
double ball(double r);
void print();
int main(){
	int a,cnt=0;
	double r,h;
	while(1){
		print();
		scanf("%d",&a);
		if(a==1){
			printf("Please enter the radius:\n");
			scanf("%lf",&r);
			ball(r);
		}else if(a==2){
			printf("Please enter the radius and the height:\n");
			scanf("%lf %lf",&r,&h);
			Cylinder(r,h);
		}else if(a==3){
			printf("Please enter the radius and the height:\n");
			scanf("%lf %lf",&r,&h);
			Cone(r,h);
		}else{
			break;
		}
	}
} 
void print(){
	printf("1-Ball\n");
	printf("2-Cylinder\n");
	printf("3-Cone\n");
	printf("other-Exit\n");
	printf("Please enter your command:\n");
}
double ball(double r){
	double v;
	v=4.0/3.0*3.1415926535*r*r*r;
	printf("%.2f\n",v);
}
double Cylinder(double r,double h){
	double v;
	v=3.1415926535*r*r*h;
	printf("%.2f\n",v);
}
double Cone(double r,double h){
	double v;
	v=(3.1415926535*r*r*h)/3.0;
	printf("%.2f\n",v);
}

可以直接主函数操作就行,这里是练习以下调用函数
自己的注意点
pi的精度3.1415926535,否则差4分的测试点,卡了好久
类型和变量一定一定要匹配对应,比如我的定义输入的是int型,运行输入时是double的数字,就出现疯狂死循环,我还在找是不是while出错了

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值