此程序针对每个条件运行所有函数,而且每个条件只应运行一个函数。为什么?我应该编写函数来计算球体,圆柱体和圆锥体的体积和表面积:我无法弄清楚它是如果混乱起来的if语句,还是函数本身。 该程序的理想输出如下:如何使用在C++中具有多个参数的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 汽缸是399.139
选择形状(1)球(2)筒(3)锥形(q)中退出:2 选择一个计算(1)体积(2)表面积:2 输入气缸的半径:5.5 输入气缸的高度:气缸的4.2 表面积是335.208
选择形状(1)球(2)筒(3)锥形(q)中退出:3 选择一个小样utation(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;
}
如果您提供了真实的代码并对其进行一致格式化,这将会很有帮助。 – 2013-02-23 09:41:45
我也试过这种格式,它也不起作用 – 2013-02-23 10:00:36
什么是“这种格式”?格式化可以自动化,但如果这不是一个选项,则需要手动完成。 – 2013-02-25 06:41:22