在 cmath 库中使用函数时遇到问题

Trouble with using function in cmath library

嘿伙计们(和姑娘们),我正在做我的家庭作业,我大部分都完成了,除了我不知道如何使用 cmath 库中的函数之一将两个方程组合成一。我将复制并粘贴作业说明,然后是我的代码和我遇到困难的部分。粗体和斜体部分是我难以理解的部分。

说明:

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

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

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

使用这些等式:

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

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

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

  1. 旅行时间(小时)= 旅行距离(英里)/费率(英里/小时)

提示:想一想如何使用 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;
    }

鉴于以上评论,您可能正在寻找:

isgreater(distSun, 93) ? (distSun - 93) : (93 - distSun)

或者,如果你真的想调整老师:

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

所有假设93是地球距离