⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program_4_5.cpp

📁 清华关于C++ 的程序讲义 值得一看 关于算法
💻 CPP
字号:
// Program 4.5: Determine whether user date is valid
#include <iostream>
using namespace std;
int main() {
    enum MonthsOfYear {January = 1, February, March,
         April, May, June, July, August, September,
         October, November, December };
    // prompt and extract date
    cout << "Please supply a date (mm dd yyyy): ";
    int Month;
    int Day;
    int Year;
    cin >> Month >> Day >> Year;
    // compute days in February
    int DaysInFebruary;
    if ( Year % 4 )
        DaysInFebruary = 28;
    else if ( (Year%400) == 0 )
        DaysInFebruary = 29;
    else if ( (Year%100) == 0 )
        DaysInFebruary = 28;
    else
        DaysInFebruary = 29;
    // if month is valid, determine how many days it has
    int DaysInMonth;
    switch (Month) {
        case January: case March: case May: case July:
        case August: case October: case December:
            DaysInMonth = 31;
            break;
        case April: case June: case September:
        case November:
            DaysInMonth = 30;
            break;
        case February:
            DaysInMonth = DaysInFebruary;
            break;
        default:
            cout << "Invalid Month: " << Month << endl;
            return 1;
    }
    // determine whether input day is vlid
    if ((Day < 1) || (Day > DaysInMonth)) {
        cout << "Invalid day of month: " << Day << endl;
        return 1;
    }
    // display result
    cout << Month << "/" << Day << "/" << Year
    << " is a valid date" << endl;
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -