C++ 将双精度转换为无符号长C++;
我正在做一个家庭作业,到目前为止,除了“海王星”外,所有的东西都打印出来了。其他的选择都很好,可以打印出它们应该是的数字,但是海王星打印出正确的数字,但是作为负片。有人告诉我把双冠王投给长传,但我相信我投了,这没有什么区别。任何帮助或指导都将不胜感激。我会附上我的代码,你们可以看看。另一件事是当海王星被选为“h”时,选择应该是“h”,那么重量是160,速度是595。此外,以小时为单位打印的旅行时间数字为4537815天189076年518.02年。小时和天数的计算结果是正确的,除了最后一个数字需要在我的代码中四舍五入一,年份的计算结果是正确的,除了它是负数C++ 将双精度转换为无符号长C++;,c++,casting,double,long-integer,unsigned,C++,Casting,Double,Long Integer,Unsigned,我正在做一个家庭作业,到目前为止,除了“海王星”外,所有的东西都打印出来了。其他的选择都很好,可以打印出它们应该是的数字,但是海王星打印出正确的数字,但是作为负片。有人告诉我把双冠王投给长传,但我相信我投了,这没有什么区别。任何帮助或指导都将不胜感激。我会附上我的代码,你们可以看看。另一件事是当海王星被选为“h”时,选择应该是“h”,那么重量是160,速度是595。此外,以小时为单位打印的旅行时间数字为4537815天189076年518.02年。小时和天数的计算结果是正确的,除了最后一个数字需
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main()
{
char selection;
double weight = 0.0;
double speed = 0.0;
double surfGrav;
double distEarthToSun = 93 * 1000000;
double distfromSun = 0.0;
double newSunDist;
double newWeight;
double distFromPlanets;
// double travelTime;
double travelTimeHours;
double travelTimeDays;
double travelTimeYears;
string planetName;
cout << "Welcome to INTERPLANETARY TRAVEL PROGRAM!\n";
cout << "This program enables you to find out your travel time to the planet\n"
<< "you want to travel to as well as your weight on that planet.\n";
cout << "Please enjoy the program and find the perfect planet for you!\n";
cout << '\n';
cout << '\n';
cout << "INTERPLANETARY TRAVEL MENU\n";
cout << "--------------------------\n";
cout << "a) Mercury\n";
cout << "b) Venus\n";
cout << "c) Earth\n";
cout << "d) Mars\n";
cout << "e) Jupiter\n";
cout << "f) Saturn\n";
cout << "g) Uranus\n";
cout << "h) Neptune\n";
cout << "q) quit\n";
cout << '\n';
cout << "Select a planet to travel to or q to quit the program: \n";
cin >> selection;
if (selection >= 'a' && selection <= 'h')
{
cout << "Please enter your weight (in lbs): \n";
cin >> weight;
cout << "Please enter the speed (in mile per hour) that you would like to travel at: \n";
cin >> speed;
}
if (selection == 'a')
{
planetName = "Mercury";
surfGrav = 0.27;
distfromSun = 36;
}
else if (selection == 'b')
{
planetName = "Venus";
surfGrav = 0.86;
distfromSun = 67;
}
else if (selection == 'c')
{
planetName = "Earth";
surfGrav = 1.00;
distfromSun = 93;
}
else if (selection == 'd')
{
planetName = "Mars";
surfGrav = 0.37;
distfromSun = 141;
}
else if (selection == 'e')
{
planetName = "Jupiter";
surfGrav = 2.64;
distfromSun = 483;
}
else if (selection == 'f')
{
planetName = "Saturn";
surfGrav = 1.17;
distfromSun = 886;
}
else if (selection == 'g')
{
planetName = "Uranus";
surfGrav = 0.92;
distfromSun = 1782;
}
else if (selection == 'h')
{
planetName = "Neptune";
surfGrav = 1.44;
distfromSun = 2793;
}
else if (selection == 'q')
{
cout << "You have chosen to quit the program. Thank you for using the program!\n";
return 0;
}
else
{
cout << "You have entered an invalid selection.\n";
return 0;
}
newWeight = weight * surfGrav;
newSunDist = distfromSun * 1000000;
unsigned long l1 = newSunDist;
if (selection <= 'a' || selection >= 'h')
{
distFromPlanets = distEarthToSun - l1;
}
else
{
distFromPlanets = l1 - distEarthToSun;
}
travelTimeHours = distFromPlanets/speed;
travelTimeDays = travelTimeHours/24.0;
travelTimeYears = travelTimeDays/365.0;
cout <<'\n';
cout << "INTERPLANETARY TRAVEL: Earth to " << planetName << '\n';
cout << "--------------------------------------------------\n";
cout << fixed << setprecision(2) << "Your weight on " << planetName << ": " << newWeight << " lbs\n";
cout << '\n';
cout << "Your travel time to " << planetName << ":\n";
cout << fixed << setprecision(2) << " " << "In Hours: " << (long int)(travelTimeHours + 0.5) << " hours\n";
cout << fixed << setprecision(2) << " " << "In Days : " << (long int)(travelTimeDays + 0.5) << " days\n";
cout << fixed << setprecision(2) << " " << "In Years : " << travelTimeYears << " years\n";
cout << '\n';
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符选择;
双倍重量=0.0;
双速=0.0;
双曲面曲面;
双distEarthToSun=93*1000000;
与太阳的双距离=0.0;
双新闻;
双倍新重量;
双距行星;
//双倍旅行时间;
双倍旅行时间;
双倍旅行天数;
双旅行时间年;
字符串planetName;
问题出在你的变量distFromPlanets
上,它分解为-2.7e+9,因为distEarthToSun=9.3e+7
和newsnud=2.8e+9
。因此9.3e+7-2.8e+9
大约等于-2.8e+9
。我不是天文学家,但如果你把if语句改为if>(选择“h”)
海王星的计算结果将解析为2.8e+9-9.3+7+2.7e+9
。再说一遍,我不是天文学家,所以我不确定这是否是你应该计算的。伙计,检查你的返回键,我想它按了两下。在travelTimeYears=travelTimeDays/365.0;
上设置一个断点。然后跨过那条线。然后将鼠标悬停在travelTimeYears
和travelTimeDays
上,告诉我们它们是什么。为什么代码中的空格要加倍?这并不能让代码更清晰,反而让代码读起来很痛苦。