2

このプログラムは、条件ごとに 1 つの関数のみを実行する必要がある場合に、条件ごとにすべての関数を実行します。なんで?私は、球、円柱、および円錐の体積と表面積を計算する関数を書くことになっています: それが混乱しているのは 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;
}
4

1 に答える 1

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;
}
于 2013-02-23T09:43:20.987 に答える