这是 Zeller 程序的代码。除了 1111 年 2 月 31 日、1111 年 6 月 31 日这通常应该是错误的日期之外,一切都运行良好。 [关闭]

Posted

技术标签:

【中文标题】这是 Zeller 程序的代码。除了 1111 年 2 月 31 日、1111 年 6 月 31 日这通常应该是错误的日期之外,一切都运行良好。 [关闭]【英文标题】:Here is the code for the Zeller program. Everything work well except some date as February 31 1111, June 31 1111 which normally should be wrong. [closed] 【发布时间】:2015-03-24 21:36:26 【问题描述】:

我正在用 C++ 编写一个 Zeller 程序,但我一直在寻找一些通常应该是错误的日期,但我却得到了答案。 这是 Zeller 程序的代码。除了 1111 年 2 月 31 日、1111 年 6 月 31 日这通常应该是错误的日期之外,一切都运行良好。有什么帮助吗??

int stringMonthInteger(string strMonth)
        int numMonth;
        if (strMonth == "January")
            numMonth = 13;
        
        else if (strMonth == "February")
            numMonth = 14;
        
        else if (strMonth == "March")
            numMonth = 3;
        
        else if (strMonth == "April")
            numMonth = 4;
        
        else if (strMonth == "May")
            numMonth = 5;
        
        else if (strMonth == "June")
            numMonth = 6;
        
        else if (strMonth == "July")
            numMonth = 7;
        
        else if (strMonth == "August")
            numMonth = 8;
        
        else if (strMonth == "September")
            numMonth = 9;
        
        else if (strMonth == "October")
            numMonth = 10;
        
        else if (strMonth == "November")
            numMonth = 11;
        
        else if (strMonth == "December")
            numMonth = 12;
        
        else
            numMonth = -1;
        return numMonth;
    
    int main() 
        int numMonth;
        string strMonth;
        int day;
        int year = -1;

        // The greater of the program 
        cout << " Welcome to the Zeller Day / Date Calculator! " << endl;
        cout << " ************************************************** " << endl;
        cout << " Please enter a date, in the following format: " << endl;
        cout << " March 17 1976 " << endl;
        cin >> strMonth;  
        cin >> day; 
        cin >> year; 

        // Here the program call our main function
        numMonth = stringMonthInteger(strMonth);

        while (numMonth == -1 || day < 1 || day >31 || year <= 0)
            cout << " The date you entered was not in the correct format. Please try again. " << endl;
            cin.clear();
            cin.ignore(265, '\n');
            cout << " Please enter a date, in the following format: " << endl;
            cout << " December 7 1941 " << endl;
            //day = -1;
            //year = -1;
            cin >> strMonth; 
            cin >> day; 
            cin >> year; 
            numMonth = stringMonthInteger(strMonth);
        
        int dayOFWeek = 0;
        if (numMonth == 13 || (numMonth == 14))
            numMonth = numMonth + 12;
            year = year - 1;
            
        int centuryYear = year % 100; 
        int century = year / 100; 
        dayOFWeek = static_cast<int>(day + ((13 * (numMonth + 1)) / 5) + centuryYear + (centuryYear / 4) + (century / 4) + (5 * (century))) % 7;
        string dayOfWeekStr;
        switch (dayOFWeek)
        
        case 0:
            dayOfWeekStr = "Saturday";
            break;
        case 1:
            dayOfWeekStr = "Sunday";
            break;
        case 2:
            dayOfWeekStr = "Monday";
            break;
        case 3:
            dayOfWeekStr = "Tuesday";
            break;
        case 4:
            dayOfWeekStr = "Wednesday";
            break;
        case 5:
            dayOfWeekStr = "Thursday";
            break;
        case 6:
            dayOfWeekStr = "Friday";
            break;
        default:
            dayOfWeekStr = "wrong";
            break;
        
        cout << " The day is " << dayOfWeekStr << "." << endl;

【问题讨论】:

【参考方案1】:

考虑到while (numMonth == -1 || day &lt; 1 || day &gt;31 || year &lt;= 0) ,您还必须明确提及无效日期的条件,例如 2 月 31 日、6 月 31 日。

【讨论】:

以上是关于这是 Zeller 程序的代码。除了 1111 年 2 月 31 日、1111 年 6 月 31 日这通常应该是错误的日期之外,一切都运行良好。 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Zeller公式计算星期几HDU 6112 今夕何夕

蔡勒(Zeller)公式及其推导:快速将任意日期转换为星期数

第4次作业类测试代码+098+吴超

在 MySQL 上遇到错误代码 1111 我该如何解决?

遇到 SQL 错误 1111 的问题

1111