嘿伙计们(和女孩们)所以我正在完成我的家庭作业,除了我无法弄清楚如何使用cmath库中的一个函数将两个方程组合成一个之外我大部分都已完成。我将复制并粘贴作业说明,然后是我的代码和我遇到困难的部分。粗体和粗体的部分斜体是我难倒的。
说明
如果用户选择a-h,程序应该要求用户输入他们的体重(礼貌地询问)和他们希望旅行的速度(以英里/小时为单位)。现在您拥有了用户所需的所有数据:他们希望前往的星球,他们的重量(以地球上的磅数为单位)以及他们希望行进的速度(以英里/小时为单位)。
使用数据,用户输入和下一页的表格计算用户在他们选择的星球上的重量以及从地球开始的旅行时间。
注意:该表显示了每颗行星与太阳的距离。此外,我们在技术上计算两个行星的轨道之间的距离。
使用这些等式:
1.新星球上的重量=地球上的重量*新行星的表面重力
2.行星之间的距离(如果地球离太阳更远)=地球到太阳的距离 - 从新行星到太阳的距离
3.行星之间的距离(如果新行星远离太阳)=从新行星到太阳的距离 - 从地球到太阳的距离
提示:考虑一下如何使用cmath库中的一个Math函数来组合#2& #3进行一次计算
CODE:
#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;
}
答案 0 :(得分:2)
鉴于上述评论,您可能正在寻找:
isgreater(distSun, 93) ? (distSun - 93) : (93 - distSun)
或者,如果你真的想调整老师:
pow( -1, isgreater( distSun, 93 ) ) * (93 - distSun)
假设93是地球距离