该程序运行每个条件的所有函数,它应该只为每个条件运行一个函数。为什么?我应该编写计算球体,圆柱体和圆锥体的体积和表面积的函数:我无法弄清楚它是if语句是搞乱还是函数本身。 该计划的理想输出如下:
选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:1 选择计算(1)体积(2)表面积:1 输入球体半径:5.5 球体积为696.91
选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:1 选择计算(1)体积(2)表面积:2 输入球体半径:5.5 球体的表面积为380.133
选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:2 选择计算(1)体积(2)表面积:1 输入圆柱半径:5.5 输入气缸高度:4.2 圆柱体积为399.139
选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:2 选择计算(1)体积(2)表面积:2 输入圆柱半径:5.5 输入气缸高度:4.2 圆筒表面积为335.208
选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:3 选择计算(1)体积(2)表面积:1 输入圆锥半径:5.5 输入圆锥高度:4.2 锥体积为133.046
选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:3 选择计算(1)体积(2)表面积:2 输入圆锥半径:5.5 输入圆锥高度:4.2 锥的表面积是214.607
选择一个Shape(1)球体(2)圆柱体(3)圆锥体(q)退出:q
再见!
#include <iostream>
#include <math.h>
using namespace std;
double sphere_volume(double radius);
double sphere_surface_area(double radius);
double cylinder_volume(double radius, double height);
double cylinder_surface_area(double radius, double height);
double cone_volume(double radius, double height);
double cone_surface_area(double radius, double height);
int main()
{
double entHeight;
double entRadius;
char shapeCall;
char compCall;
cout << "Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: ";
cin >> shapeCall;
cout << "Select a Computation (1) volume (2) surface area: ";
cin >> compCall;
if ( shapeCall == 1 )
{
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
}
if ( shapeCall == 1 )
{
if ( compCall == 2)
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_surface_area (entRadius) << endl;
}
if ( shapeCall == 2 )
{
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cylinder_volume (entRadius, entHeight) << endl;
}
if (shapeCall == 2 )
{
if ( compCall == 2)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cylinder_surface_area (entRadius, entHeight) << endl;
}
if (shapeCall == 3 )
{
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cone_volume (entRadius, entHeight) << endl;
}
if (shapeCall == )
{
if ( compCall == 2)
cout << "Enter Radius: ";
cin >> entRadius;
cout << "Enter Height: ";
cin >> entHeight;
cout << cone_surface_area (entRadius, entHeight) << endl;
}
return 0;
}
double sphere_volume(double radius)
{
double sphereVolume;
sphereVolume = 3.14 * pow(radius, 3) * 4/3;
return sphereVolume;
}
double sphere_surface_area(double radius)
{
double sphereSurfArea = 4 * 3.14 * pow( radius, 2 );
return sphereSurfArea;
}
double cylinder_volume(double radius, double height)
{
double cylinderVolume = 3.14 * pow (radius, 2) * height;
return cylinderVolume;
}
double cylinder_surface_area(double radius, double height)
{
double cylinderSurfArea = 2 * 3.14 * pow (radius, 2) + 2 * 3.14 * radius * height;
return cylinderSurfArea;
}
double cone_volume(double radius, double height)
{
double coneVolume;
coneVolume = (1/3) * 3.14 * pow (radius, 2) * height;
return coneVolume;
}
double cone_surface_area(double radius, double height)
{
double coneSurfArea = 3.14 * pow (radius, 2) + 3.14 * sqrt(pow (radius, 2) + pow (height, 2));
return coneSurfArea;
}
答案 0 :(得分:1)
if语句需要采用以下格式
if (expression)
statement;
如果expression
评估为非零数字,或true
,则执行statement
。在其他所有事件中,事实并非如此。
出现的原因好像每个条件的计算结果为true,这是因为您在整个代码中更改了if语句的格式。您似乎经常使用以下格式为您的条件。
if ( compCall == 1)
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
考虑if语句只与直接跟随的语句相关联。因此,该代码等同于
if ( compCall == 1)
{
cout << "Enter Radius: ";
}
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
你所遇到的问题是因为没有正确地将你的条件括在花括号中。它可能仍被视为有效代码,但如果使用不当,可能会产生非常意外的结果。在这种情况下,以下代码将生成您期望的结果,因为与条件关联的语句正确地用大括号括起来。
if ( compCall == 1)
{
cout << "Enter Radius: ";
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;
}