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