将 double 转换为 unsigned long C++
Cast double to unsigned long C++
我正在编写一个家庭作业程序,到目前为止,除了行星 "Neptune" 之外,所有内容都可以正确打印出来。其他所有选择都可以正常工作并按预期打印出数字,但 Neptune 打印出正确的数字但为负数。我被告知要将 double 转换为 long,但我相信我已经这样做了,而且没有任何区别。任何帮助或指导将不胜感激。我将附上我的代码,以便你们可以看一下。 Another thing is that when Neptune is chosen 'h', the choices are supposed to be 'h' then weight is 160 and speed is 595. Also, the numbers that are supposed to print for travel time in hours are 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;
}
感谢大家的提前帮助,如果您需要更多信息,请告诉我。谢谢。
问题出在您的变量 distFromPlanets
上,它解析为 -2.7e+9,如 distEarthToSun = 9.3e+7
和 newSunDist = 2.8e+9
。因此 9.3e+7 - 2.8e+9
约等于 -2.8e+9
。我不是天文学家,但如果您将 if 语句更改为 if(selection <= 'a' || selection > 'h')
,则海王星的计算将解析为 2.8e+9 - 9.3+7 ~ +2.7e+9
。再一次,我不是天文学家,所以我不确定这是否是你应该计算的。
我正在编写一个家庭作业程序,到目前为止,除了行星 "Neptune" 之外,所有内容都可以正确打印出来。其他所有选择都可以正常工作并按预期打印出数字,但 Neptune 打印出正确的数字但为负数。我被告知要将 double 转换为 long,但我相信我已经这样做了,而且没有任何区别。任何帮助或指导将不胜感激。我将附上我的代码,以便你们可以看一下。 Another thing is that when Neptune is chosen 'h', the choices are supposed to be 'h' then weight is 160 and speed is 595. Also, the numbers that are supposed to print for travel time in hours are 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;
}
感谢大家的提前帮助,如果您需要更多信息,请告诉我。谢谢。
问题出在您的变量 distFromPlanets
上,它解析为 -2.7e+9,如 distEarthToSun = 9.3e+7
和 newSunDist = 2.8e+9
。因此 9.3e+7 - 2.8e+9
约等于 -2.8e+9
。我不是天文学家,但如果您将 if 语句更改为 if(selection <= 'a' || selection > 'h')
,则海王星的计算将解析为 2.8e+9 - 9.3+7 ~ +2.7e+9
。再一次,我不是天文学家,所以我不确定这是否是你应该计算的。