简体   繁体   English

如何修复if语句的输出

[英]How to fix output of if statements

I'm making a program that reads in a date in the format MM DD YYYY and outputs what day of the week the date is. 我正在编写一个程序,该程序以MM DD YYYY格式读取日期,并输出该日期是星期几。 Here's the actual problem (I know it's long but it might help fix my problem): 这是实际的问题(我知道这很长,但这可能有助于解决我的问题):

Write a program that determines the day of the week for a given date. 编写确定给定日期的星期几的程序。 You can invent your own complex algorithm that takes into account the special leap year rules, and changes in calendars, but this is a case where it makes sense to look for things that are familiar. 您可以发明自己的复杂算法,其中要考虑到特殊的leap年规则和日历的更改,但是在这种情况下,寻找熟悉的事物才有意义。 Who else might need to compute values from dates over a wide span of time? 还有谁可能需要根据很宽的时间范围内的日期计算值? Historians work with dates, but generally don't compute from them. 历史学家使用日期进行工作,但通常不根据日期进行计算。 Astronomers, however, need to know the difference in time between orbital events in the solar system that span hundreds of years. 然而,天文学家需要了解跨越数百年的太阳系轨道事件之间的时间差。 Consulting an astronomy text, you will find that there is a standard way of representing a date, called the Julian Day Number (JDN). 查阅天文文本,您会发现有一种表示日期的标准方式,称为朱利安天数(JDN)。 This value is the number of days that have elapsed since January 1, 4713 BC Given the JDN for a date, there is a simple formula that tells the day of the week: 此值是自公元前4713年1月1日以来经过的天数。给定一个日期的JDN,有一个简单的公式可以告诉星期几:

DayOfWeek = (JDN + 1) % 7 DayOfWeek =(JDN + 1)%7

The result is in the range of 0 to 6, with 0 representing Sunday. 结果范围是0到6,其中0代表星期日。

The only remaining problem is how to compute the JDN, which is not so simple. 剩下的唯一问题是如何计算JDN,这不是那么简单。 The algorithm computes several intermediate results that are added together to give the JDN. 该算法计算几个中间结果,这些结果相加在一起即可得到JDN。 We look at the computation of each of these three intermediate values in turn. 我们依次看一下这三个中间值的计算。

If the date comes from the Gregorian calendar (later than October 15, 1582), then compute intRes1 with the following formula; 如果日期来自公历(晚于1582年10月15日),则使用以下公式计算intRes1; otherwise, let intRes1 be zero. 否则,让intRes1为零。

intRes1 = 2 – year / 100 + year / 400 (integer division) intRes1 = 2 –年/ 100 +年/ 400(整数除法)

The second intermediate result is computed as follows: 第二个中间结果的计算如下:

intRes2 = static_cast(365.25 * Year) intRes2 = static_cast(365.25 *年)

We compute the third intermediate value with this formula: 我们使用以下公式计算第三个中间值:

intRes3 = static_cast(30.6001 * (month + 1)) intRes3 = static_cast(30.6001 *(月+1))

Finally, the JDN is computed this way: 最后,JDN的计算方式如下:

JDN = intRes1 + intRes2 + intRes3 + day + 1720994.5 JDN = intRes1 + intRes2 + intRes3 +天+ 1720994.5

Your program should make appropriate use of value-returning functions in solving this problem. 您的程序应适当使用返回值的函数来解决此问题。 These formulas require nine significant digits; 这些公式需要九位有效数字; you may have to use the integer type long and the floating-point type double. 您可能必须使用整数类型long和浮点类型double。 Your program should prompt the user appropriately for input of the date; 您的程序应适当提示用户输入日期; it should also properly label the output. 它也应该正确标记输出。 Use proper coding style with comments to document the algorithm as needed. 根据需要使用适当的编码样式以及注释来记录算法。

Your program is to read from a file. 您的程序是从文件读取。 The file consists of dates in the format: mm dd yyyy 该文件包含以下格式的日期: mm dd yyyy

I've tried changing the if (DayOfWeek = ) statements to if else statements with the final being else but it only outputs "Monday" no matter the date. 我尝试将if(DayOfWeek =)语句更改为if else语句,最后将else语句更改为其他语句,但无论日期如何,它仅输出“ Monday”。

int main() {
    int JDN = 0;
    int DayOfWeek = 0;
    int res1 = 0;
    int res2 = 0;
    int res3 = 0;
    int day = 0;
    int month = 0;
    int year = 0;
    string final;
    ifstream inData;
    string file;
    cout << "Enter File Name: ";
    cin >> file;
    try {
        inData.open(file.c_str());
    }
    catch (int e) {
        return -1;
    }
    if (inData.is_open()) {
        inData >> month;
        inData >> day;
        inData >> year;
        if (month > 10 && day > 15 && year > 1582) {
            res1 = 2 - year / 100 + year / 400;
        }
        else {
            res1 = 0;
        }
        res2 = static_cast<int>(365.25 * year);
        res3 = static_cast<int>(30.6001 * (month + 1));
        JDN = res1 + res2 + res3 + day + 1720994.5;
        DayOfWeek = (JDN + 1) % 7;
        if (DayOfWeek = 0) {
            cout << "Sunday";
        }
        if (DayOfWeek = 1) {
            cout << "Monday";
        }
        if (DayOfWeek = 2) {
            cout << "Tuesday";
        }
        if (DayOfWeek = 3) {
            cout << "Wednesday";
        }
        if (DayOfWeek = 4) {
            cout << "Thursday";
        }
        if (DayOfWeek = 5) {
            cout << "Friday";
        }
        if (DayOfWeek = 6) {
            cout << "Saturday";
        }
    }
    else {
        cout << "File Not Found";
    }
    return 0;
}

The program outputs every day of the week on a single line when it should only output one. 程序在一周中的每一天只应输出一行,因此每天只输出一行。 What is my problem? 我怎么了

Comparison is "=="; 比较是“ ==“; "=" is assignment. “ =”是作业。

You should have been getting compiler warnings, check the output more carefully. 您应该已经收到了编译器警告,请更仔细地检查输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM