体积计算总是产生0
Volume calculations always resulting in 0
本文关键字:计算 更新时间:2023-10-16
我是C语言编程的新手。我在计算圆锥内截锥的体积,我发现程序总是返回0。
该程序包含其他功能中的功能。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int AreaCirculo ( float radio ){
if ( radio <= 0 ){
throw 1;
}
int aux = M_PI * pow (radio,2) ;
return aux ;
}
int VolumenCono ( float r, float h ){
if ( h <= 0 ){
throw 2 ;
}
int aux = (((1/3 )* AreaCirculo (r)) * h );
return aux;
}
int RadioMenor (float R, float H, float h){
if ( H < h){
throw 3;
}
if ( R <= 0){
throw 4;
}
if ( h <= 0){ // No hay ConoTruncado
throw 5;
}
if ( H <=0 ){
throw 6;
}
int aux = (( R * h) / H ) ;
return aux;
}
int VolumenConoTruncado ( float R, float H, float h){
int aux2 = VolumenCono (R,H) - VolumenCono (( RadioMenor (R,H,h)),h);
return aux2;
}
int main (){
float R,H,h;
printf ( " THIS PROGRAM CALCULATE THE VOLUME OF A TRUNCATED CONE IN A CONE.n");
printf ( " Enter the Radio : ");
scanf ("%f",&R);
printf ( " Enter the tallest: ");
scanf ("%f",&H);
printf ( " Enter the lower height : ");
scanf ("%f",&h);
try {
int auxi = VolumenConoTruncado (R,H,h);
printf ( " El Volumen del cono truncado es es %i.n",auxi);
}
catch ( int CodigoError){
switch (CodigoError){
case 1 : printf ( " ERROR : El radio no puede ser menor o igual a cero . n"); break;
case 2 : printf ( " ERROR : La altura no puede ser menor o igual a cero . n"); break;
case 3 : printf ( "La altura menor no puede ser mayor a la mayor.n"); break;
case 4 : printf ( "El radio no puede ser menor o igual cero "); break;
case 5 : printf ( "La altura del cono truncado no puede ser menor a cero"); break;
case 6 : printf ( "La altura no puede ser menor o igual a cero "); break;
}
}
system ("pause");
return 0;
}
我认为这是因为您混合了float和int,如下行所示:
int aux = (( R * h) / H )
注意整数截断分数:
int x = 1.0/2.0; // x = 0;
此处:
int aux = (((1/3 )* AreaCirculo (r)) * h );
1/3
将计算为0
,因此aux
也将始终为零。
更改为:
int aux = (((1.0/3) * AreaCirculo(r)) * h);
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 计算缩放多边形的比例,得到给定的多边形面积
- 在C++中如何在没有pow的情况下进行基础计算
- 计算平均值,不包括上次得分
- 如何计算多映射中重复对的数量