C ++程序查找圆锥体的体积,球体的体积,八边形的面积以及两点之间的距离

时间:2018-10-26 22:21:33

标签: c++

这是我多次重写的代码,只是无法正常工作。应该很简单,就是不得到感谢。 C ++

对于不同的公式,将需要执行一些不同的函数计算,这些计算要么找到值的平方根,要么找到加到幂的数字,或者两者兼而有之。

必须使用cmath库函数“ pow”和/或“ sqrt”进行计算。与此同时,您将使用acos(-1)进行估算。此值必须使用常量变量保存。

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

#define PI 3.1415926 //PI
using namespace std;
int option;
char countine = 'y';
double coneVolume;
double sphereVolume;
double octagonArea;
double pointDistance;

//Calculations functions
void cone(double r, double h)
    {
        cout <<"Volume = " << (1/3) * PI * pow ( r, 2 )  * h;
    }
void sphere (double r)
    {
        cout << "Volume + " << (4/3) * pow ( r, 3 ) * PI;
    }
void octagon (double s)
    {
        //cout << "Area = " << 2 (1 + sqrt (2))- pow ( s, 2);
    }
void point (double x1, double x2, double y1, double y2)
    {
        //cout << "distance = " << ((sqrt pow((x2-x1),2) + pow((y2-y1),2);
    }
int main ()
//The menu choices will be the following four calculations:
    //Find the volume of a cone
    //Find the volume of a sphere
    //Find the area of octagon
    //Find the distance between two points
{
char  countinue = 'y';
while (countinue = 'y')

    cout << "select program to run " <<endl;
    // volume of cone
    cout << "1.Find the volume of a cone "<<endl;
    // volume of sphere
    cout << "2.Find the volume of a sphere" <<endl;
    // area of octagon
    cout << "3.Find the Area of a octagon " << endl;
    // distance of two points 
    cout << "4.Find the distance between two points " << endl;
    cout << " Enter option ? ";
    cin >> option;

     switch (option)

    {
        // volume of cone
        case 1:
 //using namespace std;
            option = true;
            double r,h;
            cout<<"\nEnter cone radius: ";
            cin>>r;
            cout<<"\nEnter cone height: ";
            cin>>h;
            cone = (r, h);
            cout<<"\nThe volume of the cone is: "<<setprecision(2) 
 <<fixed<<cone<<endl<<endl;
            break;
            //volume sphere
        case 2:
            option = true;
            double r;
            cout<<"\nEnter sphere radius: ";
            cin>>r;
            sphere = (r);
            cout<<"\nThe volume of the sphere is: "<<setprecision(2) 
<<fixed<<sphere<<endl<<endl;
            break;
            //area octagon
        case 3:
            option = true;
            double octagon;
            cout<<"\nEnter the length of one side: ";
            cin>>s;

            octagon = octagonArea (sideLength);
            cout<<"\nThe area of the octagon is: "<<setprecision(2) 
 <<fixed<<octagon<<endl<<endl;
            break;
            //distance
        case 4:
            option = true;
            double distance;
            cout<<"\nEnter the first x and y coordinates: ";
            cin>>x1>>y1;
            cout<<"\nEnter the second x and y coordinates: ";
            cin>>x2>>y2;

            distance = pointDistance (x1, x2, y1, y2);

            cout<<"\nThe distance between points ("<<x1<<", "<<y1<<") and (" 
  <<x2<<", "<<y2<<") is: "<<setprecision(4)<<fixed<<distance<<endl<<endl;
            break;
    }

  return 0;
}

1 个答案:

答案 0 :(得分:0)

以上评论当然是正确的。

此外,我发现了其他问题,例如:

  • 您未按要求使用acos(-1)
  • 某些变量未声明
  • 您同时在函数和main中显示了结果
  • 总的来说,您期望函数返回结果,但是函数被声明为void ...
  • 您忘记询问用户是否要继续

这是我获得的代码,似乎可以正常工作。请不要简单地复制它,而是尝试理解修改。

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

static double const PI = acos(-1);
using namespace std;

//Calculations functions
double cone(double r, double h)
    {
        return (1.0/3.0) * PI * pow ( r, 2)  * h;
    }
double sphere (double r)
    {
        return (4.0/3.0) * pow ( r, 3 ) * PI;
    }
double octagon (double s)
    {
        return 2 * (1 + sqrt (2)) * pow (s, 2);
    }
double pointDistance (double x1, double x2, double y1, double y2)
    {
        return sqrt(pow((x2-x1),2) + pow((y2-y1),2));
    }
int main ()
//The menu choices will be the following four calculations:
    //Find the volume of a cone
    //Find the volume of a sphere
    //Find the area of octagon
    //Find the distance between two points
{
    char  countine = 'y';
    while (countine == 'y')  {
        int option;
        cout << "select program to run " <<endl;
        // volume of cone
        cout << "1.Find the volume of a cone "<<endl;
        // volume of sphere
        cout << "2.Find the volume of a sphere" <<endl;
        // area of octagon
        cout << "3.Find the Area of a octagon " << endl;
        // distance of two points 
        cout << "4.Find the distance between two points " << endl;
        cout << " Enter option ? ";
        cin >> option;

        switch (option)
        {
            // volume of cone
            case 1:
            {
                double r,h;
                cout<<"\nEnter cone radius: ";
                cin>>r;
                cout<<"\nEnter cone height: ";
                cin>>h;
                double volume = cone(r, h);
                cout<<"\nThe volume of the cone is: "<<setprecision(2) <<fixed<<volume<<endl<<endl;
            }
                break;
                //volume sphere
            case 2:
            {
                double r;
                cout<<"\nEnter sphere radius: ";
                cin>>r;
                double volume = sphere(r);
                cout<<"\nThe volume of the sphere is: "<<setprecision(2) <<fixed << volume << endl << endl;
            }
                break;
                //area octagon
            case 3:
            {
                cout<<"\nEnter the length of one side: ";
                double s;
                cin>>s;
                double area = octagon (s);
                cout<<"\nThe area of the octagon is: "<< setprecision(2) <<fixed << area << endl << endl;
            }
                break;
                //distance
            case 4:
            {
                double x1, y1, x2, y2;
                cout<<"\nEnter the first x and y coordinates: ";
                cin>>x1>>y1;
                cout<<"\nEnter the second x and y coordinates: ";
                cin>>x2>>y2;
                double distance = pointDistance (x1, x2, y1, y2);

                cout<<"\nThe distance between points ("<<x1<<", "<<y1<<") and (" <<x2<<", "<<y2<<") is: "
                <<setprecision(4) << fixed << distance <<endl<<endl;
            }
                break;
        }
        cout << "Do you want to continue?";
        cin >> countine;
    }
  return 0;
}