【问题标题】:How do i call multiple functions using if statements that have multiple parameters in C++如何在 C++ 中使用具有多个参数的 if 语句调用多个函数
【发布时间】:2013-02-23 09:21:08
【问题描述】:

这个程序为每个条件运行所有函数,而它应该只为每个条件运行一个函数。为什么?我应该编写计算球体、圆柱体和圆锥体的体积和表面积的函数:我不知道是 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 选择计算 (1) 体积 (2) 表面积:1 输入圆柱半径:5.5 输入圆柱高度:4.2 圆柱体积为399.139

选择形状 (1) 球体 (2) 圆柱体 (3) 圆锥体 (q) 退出:2 选择计算 (1) 体积 (2) 表面积:2 输入圆柱半径:5.5 输入圆柱高度:4.2 圆柱体表面积为335.208

选择形状 (1) 球体 (2) 圆柱体 (3) 圆锥体 (q) 退出:3 选择计算 (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;
}

【问题讨论】:

  • 如果您提供真实代码并统一格式化,将会很有帮助。
  • 这个格式我也试过了,还是不行
  • 什么是“这种格式”?格式化可以自动化,但如果这不是一个选项,您需要手动进行。

标签: c++ function if-statement


【解决方案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;
}

【讨论】:

  • 我同意你的回答,当表达式计算为 false0null 时,条件为假。在所有其他情况下,它对 C 语言的评估结果为 true。所以即使5 被评估为真或任何负数。但我猜你提到这个!0 == 1 评估为真,!0 == 2 评估为假。我会在你的答案中编辑这个。
猜你喜欢
  • 1970-01-01
  • 2018-07-30
  • 2014-05-28
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
相关资源
最近更新 更多