このプログラムは、条件ごとに 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;
}