我正在编写作业程序,到目前为止,除了“海王星”行星以外,其他所有东西都可以正确打印。其他所有选择都可以正常工作并按预期方式打印出数字,但Neptune可以打印正确的数字但为负数。有人告诉我要把双打放长一点,但是我相信我已经做到了,而且没有任何区别。任何帮助或指导,将不胜感激。我将附上我的代码,以便你们看看。另一件事是,当海王星被选择为“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;
}

感谢您提前提供的帮助,如果您需要更多信息,请告诉我。谢谢。

最佳答案

问题出现在您的变量distFromPlanets,它解析为-2.7e + 9,分别是distEarthToSun = 9.3e+7newSunDist = 2.8e+9。因此,9.3e+7 - 2.8e+9大约等于-2.8e+9。我不是天文学家,但是如果您将if语句更改为if(selection <= 'a' || selection > 'h'),则Neptune的计算将解析为2.8e+9 - 9.3+7 ~ +2.7e+9。同样,我不是天文学家,所以我不确定这是否是您应该计算的。

关于c++ - 将double转换为无符号长C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29054099/

10-11 21:04