如何使用在C ++中有多个参数的if语句调用多个函数 [英] How do i call multiple functions using if statements that have multiple parameters in C++

查看:152
本文介绍了如何使用在C ++中有多个参数的if语句调用多个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序运行每个条件的所有函数,当它应该只为每个条件运行一个函数。为什么?我应该编写函数,计算球体,圆柱体和圆锥的体积和表面积:我不知道是否是if语句是乱七八糟的,还是函数本身。
此程序的理想输出如下:



选择形状(1)球体(2)圆柱体1
选择计算(1)体积(2)表面积:1
输入球体半径:5.5
球体积为696.91



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



选择形状(1)球体(2)圆柱体$ b选择计算(1)体积(2)表面积:1
输入圆柱体半径:5.5
输入圆柱体高度:4.2
圆柱体积为399.139

b
$ b

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



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



选择形状(1)球体$ b选择计算(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<< 选择形状(1)球体(2)圆柱体(3)圆锥体(q)退出:;
cin>> shapeCall;
cout<< 选择一个计算(1)体积(2)表面积:;
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<< 输入高度:;
cin>> entHeight;
cout<< cylinder_volume(entRadius,entHeight)< endl
}
if(shapeCall == 2)
{
if(compCall == 2)
cout< Enter Radius:;
cin>> entRadius;
cout<< 输入高度:;
cin>> entHeight;
cout<< cylinder_surface_area(entRadius,entHeight)<< endl
}
if(shapeCall == 3)
{
if(compCall == 1)
cout< Enter Radius:;
cin>> entRadius;
cout<< 输入高度:;
cin>> entHeight;
cout<< cone_volume(entRadius,entHeight)< endl
}
if(shapeCall ==)
{
if(compCall == 2)
cout< Enter Radius:;
cin>> entRadius;
cout<< 输入高度:;
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)
语句;

如果 expression code> 1 或 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
}


This program runs all of the functions for each condition, when it should only run one function for each condition. Why? i am supposed to Write functions that compute the volume and surface area of a sphere, circular cylinder, and a circular cone: I can't figure out if it is the if statements that are messing up,or the functions themselves. An ideal output for this program would be as follows:

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 1 Select a Computation (1) volume (2) surface area: 1 Enter radius of sphere: 5.5 Volume of sphere is 696.91

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 1 Select a Computation (1) volume (2) surface area: 2 Enter radius of sphere: 5.5 Surface area of sphere is 380.133

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 2 Select a Computation (1) volume (2) surface area: 1 Enter radius of cylinder: 5.5 Enter height of cylinder: 4.2 Volume of cylinder is 399.139

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 2 Select a Computation (1) volume (2) surface area: 2 Enter radius of cylinder: 5.5 Enter height of cylinder: 4.2 Surface area of cylinder is 335.208

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 3 Select a Computation (1) volume (2) surface area: 1 Enter radius of cone: 5.5 Enter height of cone: 4.2 Volume of cone is 133.046

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: 3 Select a Computation (1) volume (2) surface area: 2 Enter radius of cone: 5.5 Enter height of cone: 4.2 Surface area of cone is 214.607

Select a Shape (1) sphere (2) cylinder (3) cone (q) quit: q

Good bye!

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

解决方案

An if statement expects to be in the following format

if (expression)
    statement;

In the event that expression evaluates to 1, or true, statement is executed. In every other event, it is not.

The reason why it appears as if every condition evaluates to true, is because you change the way you format your if statements throughout your code. You seem to use the following format for your conditionals very often.

if ( compCall == 1)
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << sphere_volume (entRadius) << endl;

Consider that the if statement will be associated only with the directly following statement. Thus, that code is equivalent to

if ( compCall == 1)
{
    cout << "Enter Radius: ";
}
cin >> entRadius;
cout << sphere_volume (entRadius) << endl;

The problem you're having arises from not properly enclosing your conditionals in curly braces. It may still be considered valid code, but it can produce very unexpected results if used incorrectly. The following code would, in this situation, produce the results you expect, as the statements associated with the condition are properly enclosed in curly braces.

if ( compCall == 1)
{
   cout << "Enter Radius: ";
   cin >> entRadius;
   cout << sphere_volume (entRadius) << endl;
}

这篇关于如何使用在C ++中有多个参数的if语句调用多个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆