2013-02-23 139 views
2

此程序针对每个条件运行所有函数,而且每个条件只应运行一个函数。为什么?我应该编写函数来计算球体,圆柱体和圆锥体的体积和表面积:我无法弄清楚它是如果混乱起来的if语句,还是函数本身。 该程序的理想输出如下:如何使用在C++中具有多个参数的if语句调用多个函数

选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:1 选择计算(1)体积(2)表面积:1 输入球体的半径:球体的5.5 体积是696.91

选择形状(1)球(2)筒(3)锥形(q)中退出:1 选择一个计算(1)体积(2 )表面积:2 球体半径输入:5.5 球体表面积为380.133

选择一个形状(1)体积(2)圆柱体(3)圆锥体(q)退出:2 汽缸是399.139

选择形状(1)球(2)筒(3)锥形(q)中退出:2 选择一个计算(1)体积(2)表面积:2 输入气缸的半径:5.5 输入气缸的高度:气缸的4.2 表面积是335.208

选择形状(1)球(2)筒(3)锥形(q)中退出:3 选择一个小样utation(1)体积(2)表面积:1 输入锥体的半径:5.5 输入锥体的高度:4.2 卷锥体的是133.046

选择形状(1)球(2)筒(3)锥(q)退出:3 选择一个计算(1)体积(2)表面积:2 输入锥体的半径:5.5 输入锥体的高度:4.2 锥形的表面积是214.607

选择形状(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

如果您提供了真实的代码并对其进行一致格式化,这将会很有帮助。 – 2013-02-23 09:41:45

+0

我也试过这种格式,它也不起作用 – 2013-02-23 10:00:36

+0

什么是“这种格式”?格式化可以自动化,但如果这不是一个选项,则需要手动完成。 – 2013-02-25 06:41:22

回答

1

if语句预计将在以下格式

if (expression) 
    statement; 

在这expression计算结果为非零数,或true的情况下,statement被执行。在其他任何事件中,事实并非如此。

出现的原因就好像每个条件的计算结果都是真实的一样,是因为您改变了在整个代码中格式化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; 
} 
+0

我同意你的回答,只是在表达式计算为'false'或'0'或'null'时条件为false。在其他所有情况下,C语言的评估结果均为真。所以即使'5'被评估为真或任何负数。但我猜你认为这个'!0 == 1'的计算结果为true,'!0 == 2'的计算结果为false。我会在你的回答中编辑这个。 – 2017-03-09 07:53:09

相关问题