C++ 在cmath库中使用函数时出现问题

C++ 在cmath库中使用函数时出现问题,c++,cmath,C++,Cmath,大家好,我正在做我的家庭作业,大部分时间我都做完了,只是我不知道如何使用cmath库中的一个函数将两个方程组合成一个方程。我将复制并粘贴作业说明,然后是我的代码和我遇到困难的部分。粗体和斜体部分正是我所困惑的 说明: #include <iostream> #include <iomanip> #include <string> #include <cmath> using namespace std;

大家好,我正在做我的家庭作业,大部分时间我都做完了,只是我不知道如何使用cmath库中的一个函数将两个方程组合成一个方程。我将复制并粘贴作业说明,然后是我的代码和我遇到困难的部分。粗体和斜体部分正是我所困惑的

说明:

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    using namespace std;

    int main()
    {
string planetName;
char userSelection;
double weightEarth, weightNewPlanet, numSpeed, surfGrav, distSun, numHours, numDays, numYears, distanceBetweenPlanets = 0;

cout << "Welcome to INTERPLANETARY TRAVEL PROGRAM!" << endl
     << "This program enables you to find out your travel time to the planet" << endl
     << "you want to travel to as well as your weight on that planet." << endl
     << "Please enjoy the program and find the perfect planet for you!" << endl << endl << endl
     << "INTERPLANETARY TRAVEL MENU" << endl
     << "--------------------------" << endl
     << "a) Mercury" << endl
     << "b) Venus" << endl
     << "c) Earth" << endl
     << "d) Mars" << endl
     << "e) Jupiter" << endl
     << "f) Saturn" << endl
     << "g) Uranus" << endl
     << "h) Neptune" << endl
     << "q) quit" << endl << endl
     << "Select a planet to travel to or q to quit the program: " << endl;
cin >> userSelection;

if (userSelection >= 'a' && userSelection <= 'h')
{
    cout << "Please enter your weight (in lbs): " << endl;
    cin >> weightEarth;
    cout << "Please enter the speed (in mile per hour) that you would like to travel at: " << endl << endl;
    cin >> numSpeed;

    if (userSelection == 'a')
    {
        planetName = "Mercury";
        distSun = 36;
        surfGrav = 0.27;
    }

    else if (userSelection == 'b')
    {
        planetName = "Venus";
        distSun = 67;
        surfGrav = 0.86;
    }

    else if (userSelection == 'c')
    {
        planetName = "Earth";
        distSun = 93;
        surfGrav = 1.00;
    }

    else if (userSelection == 'd')
    {
        planetName = "Mars";
        distSun = 141;
        surfGrav = 0.37;
    }

    else if (userSelection == 'e')
    {
        planetName = "Jupiter";
        distSun = 483;
        surfGrav = 2.64;
    }

    else if (userSelection == 'f')
    {
        planetName = "Saturn";
        distSun = 886;
        surfGrav = 1.17;
    }

    else if (userSelection == 'g')
    {
        planetName = "Uranus";
        distSun = 1782;
        surfGrav = 0.92;
    }

    else if (userSelection == 'h')
    {
        planetName = "Neptune"; 
        distSun = 2793;
        surfGrav = 1.44;
    }

    distanceBetweenPlanets = std::abs(93 - distSun);

    /*if (userSelection <= 'b')
    {
        distanceBetweenPlanets = 93 - distSun;
    }

    else if (userSelection > 'b')
    {
        distanceBetweenPlanets = distSun - 93;
    }*/

    weightNewPlanet = weightEarth * surfGrav;
    numHours = (distanceBetweenPlanets / numSpeed) * 1000000;
    numDays = (numHours / 24);
    numYears = (numDays / 365);


    cout << "INTERPLANETARY TRAVEL:  Earth to " << planetName << endl
        << "--------------------------------------------------" << endl
        << "Your weight on " << planetName << ":      " << fixed << setprecision(2) << weightNewPlanet << " lbs" << endl << endl
        << "Your travel time to " << planetName << ":" << endl
        << "    In Hours: " << fixed << setprecision(0) << numHours << " hours" << endl
        << "    In Days : " << numDays << " days" << endl
        << "    In Years : " << fixed << setprecision(2) << numYears << " years" << endl << endl;
}

else if (userSelection == 'q')
    {
        cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
    }

else
    {
        cout << "You have entered an invalid selection." << endl;
    }

//system("PAUSE");
return 0;
    }
如果用户选择a-h,程序应要求用户输入他们的体重(礼貌地询问)和他们希望行驶的速度(以英里/小时为单位)。现在,您已经从用户那里获得了所需的所有数据:他们希望旅行到的星球、他们的重量(在地球上以磅为单位)以及他们希望旅行的速度(以英里/小时为单位)

使用用户输入的数据和下一页的表格计算用户在他们选择的星球上的体重以及从地球出发的旅行时间

注:该表显示了每颗行星与太阳的距离。此外,我们在技术上计算两颗行星轨道之间的距离

使用以下方程式:

1.新行星上的重量=地球上的重量*新行星的表面重力

2.行星之间的距离(如果地球离太阳更远)=地球到太阳的距离-新行星到太阳的距离

3.行星之间的距离(如果新行星离太阳更远)=新行星到太阳的距离-地球到太阳的距离

  • 旅行时间(小时)=旅行距离(英里)/速度(英里/小时)
  • 提示:考虑如何使用cmath库中的一个数学函数将#2和#3合并到一个计算中

    代码:

        #include <iostream>
        #include <iomanip>
        #include <string>
        #include <cmath>
        using namespace std;
    
        int main()
        {
    string planetName;
    char userSelection;
    double weightEarth, weightNewPlanet, numSpeed, surfGrav, distSun, numHours, numDays, numYears, distanceBetweenPlanets = 0;
    
    cout << "Welcome to INTERPLANETARY TRAVEL PROGRAM!" << endl
         << "This program enables you to find out your travel time to the planet" << endl
         << "you want to travel to as well as your weight on that planet." << endl
         << "Please enjoy the program and find the perfect planet for you!" << endl << endl << endl
         << "INTERPLANETARY TRAVEL MENU" << endl
         << "--------------------------" << endl
         << "a) Mercury" << endl
         << "b) Venus" << endl
         << "c) Earth" << endl
         << "d) Mars" << endl
         << "e) Jupiter" << endl
         << "f) Saturn" << endl
         << "g) Uranus" << endl
         << "h) Neptune" << endl
         << "q) quit" << endl << endl
         << "Select a planet to travel to or q to quit the program: " << endl;
    cin >> userSelection;
    
    if (userSelection >= 'a' && userSelection <= 'h')
    {
        cout << "Please enter your weight (in lbs): " << endl;
        cin >> weightEarth;
        cout << "Please enter the speed (in mile per hour) that you would like to travel at: " << endl << endl;
        cin >> numSpeed;
    
        if (userSelection == 'a')
        {
            planetName = "Mercury";
            distSun = 36;
            surfGrav = 0.27;
        }
    
        else if (userSelection == 'b')
        {
            planetName = "Venus";
            distSun = 67;
            surfGrav = 0.86;
        }
    
        else if (userSelection == 'c')
        {
            planetName = "Earth";
            distSun = 93;
            surfGrav = 1.00;
        }
    
        else if (userSelection == 'd')
        {
            planetName = "Mars";
            distSun = 141;
            surfGrav = 0.37;
        }
    
        else if (userSelection == 'e')
        {
            planetName = "Jupiter";
            distSun = 483;
            surfGrav = 2.64;
        }
    
        else if (userSelection == 'f')
        {
            planetName = "Saturn";
            distSun = 886;
            surfGrav = 1.17;
        }
    
        else if (userSelection == 'g')
        {
            planetName = "Uranus";
            distSun = 1782;
            surfGrav = 0.92;
        }
    
        else if (userSelection == 'h')
        {
            planetName = "Neptune"; 
            distSun = 2793;
            surfGrav = 1.44;
        }
    
        distanceBetweenPlanets = std::abs(93 - distSun);
    
        /*if (userSelection <= 'b')
        {
            distanceBetweenPlanets = 93 - distSun;
        }
    
        else if (userSelection > 'b')
        {
            distanceBetweenPlanets = distSun - 93;
        }*/
    
        weightNewPlanet = weightEarth * surfGrav;
        numHours = (distanceBetweenPlanets / numSpeed) * 1000000;
        numDays = (numHours / 24);
        numYears = (numDays / 365);
    
    
        cout << "INTERPLANETARY TRAVEL:  Earth to " << planetName << endl
            << "--------------------------------------------------" << endl
            << "Your weight on " << planetName << ":      " << fixed << setprecision(2) << weightNewPlanet << " lbs" << endl << endl
            << "Your travel time to " << planetName << ":" << endl
            << "    In Hours: " << fixed << setprecision(0) << numHours << " hours" << endl
            << "    In Days : " << numDays << " days" << endl
            << "    In Years : " << fixed << setprecision(2) << numYears << " years" << endl << endl;
    }
    
    else if (userSelection == 'q')
        {
            cout << "You have chosen to quit the program. Thank you for using the program!" << endl;
        }
    
    else
        {
            cout << "You have entered an invalid selection." << endl;
        }
    
    //system("PAUSE");
    return 0;
        }
    
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    字符串planetName;
    字符用户选择;
    双倍重量地球,重量新行星,numSpeed,surfGrav,distSun,numHours,numDays,numYears,板间距=0;
    
    cout鉴于上述评论,您可能正在寻找:

    isgreater(distSun, 93) ? (distSun - 93) : (93 - distSun)
    
    或者,如果你真的想调整老师:

    pow( -1, isgreater( distSun, 93 ) ) * (93 - distSun)
    

    所有假设93是地球距离

    几个点-首先,在阅读它时,距离似乎在错误的单位中(您是否缺少一个重要的因子10?)还有,我会质疑按照指示计算距离的逻辑,因为它似乎假设行星排列。也就是说,暗示可能只是一条直线,使用类似isgreater(x,y)的东西嗨,约翰,关于距离,它们实际上是以百万计的,但是我的教授不想让我们写出每一个的全部内容,例如水星是36000000(她也不希望在最终显示中使用逗号).我算出了公式,只需要正确设置输出格式,我认为我应该很好。我会让你不断更新。为什么不
    std::abs(a-b)
    ?其中
    a
    b
    是两个距离,顺序并不重要。@sharyex-好的观点,但不是std:,来自cmath lib:)@John,
    std:
    并不是相互排斥的。事实上,
    要求在
    std
    中声明事物。嘿@John,我最后做了以下事情,因为这是w我们已经了解到了:<代码>(用户选择)B和&用户选择听起来不错-让老师知道你一直在关注:(编辑:但是我会放弃和不必要的使用资源,‘Q’是另外处理的)放弃了<代码> &用户选择,你可能想考虑Sharyex的评论和使用Abess()。…它很优雅,hit是死亡中心的提示,那么也许你没有注意到-它在cmath图书馆里,他说要“思考”上面的内容