-5

该计划的预期结果如下:

选择形状 (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 && compCall == 1)
    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << sphere_volume (entRadius) << endl; 

    }
    if ( shapeCall == 1 && compCall == 2)

    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << sphere_surface_area (entRadius) << endl;
    }
    if ( shapeCall == 2 && compCall == 1)
    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << "Enter Height: ";
            cin >> entHeight;
            cout << cylinder_volume (entRadius, entHeight) << endl;
    }
    if (shapeCall == 2 && compCall == 2)
    {
            cout << "Enter Radius: ";
            cin >> entRadius;
            cout << "Enter Height: ";
            cin >> entHeight;
            cout << cylinder_surface_area (entRadius, entHeight) << endl;
    }
    if (shapeCall == 3  && compCall == 1)
    {
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << "Enter Height: ";
        cin >> entHeight;
        cout << cone_volume (entRadius, entHeight) << endl;
    }
    if (shapeCall == 3 && compCall == 2)
    { 
        cout << "Enter Radius: ";
        cin >> entRadius;
        cout << "Enter Height: ";
        cin >> entHeight;
        cout << cone_surface_area (entRadius, entHeight) << endl;
    }
system ("pause");
return 0;
}


double sphere_Volume(double radius) 
  { 
   return 4.0 / 3.0 * 3.14159 * pow( radius, 3 ); 
  }
double cylinder_volume(double radius, double height)
  { 
  return 3.14159 * pow(radius, 2) * height;
  }
double cone_volume(double radius, double height)
  {
  return 1.0 / 3.0 * 3.14159 * pow(radius, 2) * height;
  }
double sphere_surface_area(double radius)
  {
  return 4.0 * 3.14159 * pow(radius, 2);
  }
double cylinder_surface_area(double radius, double height)
  {
  return (2.0 * 3.14159 * pow(radius, 2)) + (2.0 * 3.14159 * radius * height);
  }
double cone_surface_area(double radius, double height)
  {
  return (3.14159 * pow(radius, 2)) + (3.14159 * radius * sqrt(pow(radius, 2) +  pow(height, 2)));
  }
4

1 回答 1

0

有两个主要问题。您声明sphere_volume

double sphere_volume(double radius);

但你定义sphere_Volume

double sphere_Volume(double radius) 
{ 
    return 4.0 / 3.0 * 3.14159 * pow( radius, 3 ); 
}

由于您使用sphere_volumein main,您可能应该将定义更改为sphere_volume.

另一个问题是您实际上没有循环。您的main函数将运行一次,但随后它会命中return 0;并且程序将终止。

添加循环的一种方法是在以第一个开头cout并在以 之前结束之后system("pause")包含代码while(shapeCall != 'q')

于 2013-02-23T11:53:16.927 回答