gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:33:27 25 4
gpt4 key购买 nike

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

说明:

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

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

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

使用这些等式:

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

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

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

  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是地球距离

关于c++ - 在 cmath 库中使用函数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42867105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com