如何使用在C++中具有多个参数的if语句来调用多个函数

How do i call multiple functions using if statements that have multiple parameters in C++

本文关键字:if 语句 函数 调用 参数 何使用 C++      更新时间:2023-10-16

这个程序为每个条件运行所有函数,而它应该为每个条件只运行一个函数。为什么?我应该编写计算球体、圆柱体和圆锥体的体积和表面积的函数:我不知道是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;
}

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;
}