实验10-1 圆形体体积计算器 (20 分)2021年2月

这是一个使用C语言编写的程序,实现了计算球体、圆柱体和圆锥体体积的功能。用户通过输入命令选择计算类型,程序会根据输入的半径和高度(对于圆柱和圆锥)给出相应体积的计算结果。程序使用了常量π的近似值,并在计算球体体积时应用了公式(4/3)*π*r^3,圆柱体体积为π*r^2*h,圆锥体体积为1/3*π*r^2*h。
摘要由CSDN通过智能技术生成

实验10-1 圆形体体积计算器 (20 分)
本题要求实现一个常用圆形体体积的计算器。
直接上答案:

#include<stdio.h>
#define pi 3.14159265358//pi精确度要够高,不然第二个测试点过不去
//也可写成使pi=4*atan(1.0)或者pi=2*acos(-1.0);

int main(){
   // double pi =atan(1)*4;
	int command;
	double r,h,result;
	while(1){
	
		printf("1-Ball\n2-Cylinder\n3-Cone\nother-Exit\nPlease enter your command:\n");
		scanf("%d",&command);
		switch(command){
			case 1 :{
				printf("Please enter the radius:\n");
				scanf("%lf",&r);
				result=pi*r*r*r*4/3;
				break;
			}
			case 2 :{
				printf("Please enter the radius and the height:\n");
				scanf("%lf%lf",&r,&h);
				result=pi*r*r*h;
				break;
			} 
			case 3 :{
				printf("Please enter the radius and the height:\n");
				scanf("%lf%lf",&r,&h);
				result=pi*r*r*h/3;
				break;
			}
			default : return 0;
		}
		printf("%.2f\n",result);
	}
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值