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

  • 本文关键字:问题 函数 cmath c++ cmath
  • 更新时间 :
  • 英文 :


嘿伙计们(和女孩),所以我正在做我的家庭作业,除了我不知道如何使用 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 是地球距离

最新更新