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

📄 rili-1.5.cpp

📁 请输入一个日期
💻 CPP
字号:
#include <iostream>
#include <iomanip>
using namespace std;

class calendarType
{
public:
    calendarType(int Year = 1500, int Month = 1);
    void setYear(int Year);
    void setMonth(int Month);
    int getYear();
    int getMonth();
    int getDays(int Year, int Month);
    bool IsLeapYear(int Year);
    void disCalendar(int Year, int Month);
private:
    int year;
    int month;
    int days;
};

int main()
{
    int year, month;
    cout << "请输入年份和月份:";
    cout << flush;
    cin >> year >> month;
    calendarType Cal(year, month);
    Cal.disCalendar(year, month);
    getchar(); getchar();
    return 0;
}

calendarType::calendarType(int Year, int Month)
{
    if (Year < 1500 || Year > 3000)
        year = 1500;
    else
        year = Year;
    if (Month > 12 || Month < 1)
        month = 1;
    else
        month = Month;
    getDays(year, month);
}

void calendarType::setYear(int Year)
{
    if (Year >= 1500 && Year <= 3000)
        year = Year;
}

void calendarType::setMonth(int Month)
{
    if (Month >=1 && Month <= 12)
        month = Month;
}

inline int calendarType::getYear()
{
    return year;
}

inline int calendarType::getMonth()
{
    return month;
}

int calendarType::getDays(int Year, int Month)
{
    switch(Month) {
    case 1: case 3: case 5:
    case 7: case 8: case 10:
    case 12:
        days = 31;
        break;
    case 4: case 6: case 9:
    case 11:
        days = 30;
        break;
    case 2:
        if (IsLeapYear(Year))
            days = 29;
        else
            days = 28;
    }
    return days;
}

bool calendarType::IsLeapYear(int Year)
{
    if ((Year % 4 == 0 && Year % 100 != 0)
		|| Year % 400 == 0)
        return true;
    else
        return false;
}

void calendarType::disCalendar(int Year, int Month)
{
    int totalDays = 0;
    for (int i = 1500; i < Year; i++)
    {
        if (IsLeapYear(i))
            totalDays += 366;
        else
            totalDays += 365;
    }
    for (int k = 1; k < Month; k++)
    {
        totalDays += getDays(Year, k);
    }
    totalDays++;
    int week = totalDays % 7;
    cout << setw(30) << Year << "年" << Month << "月";
    cout << endl;
    cout << setw(8) << "Sun";
    cout << setw(8) << "Mon";
    cout << setw(8) << "Tue";
    cout << setw(8) << "Wed";
    cout << setw(8) << "Thu";
    cout << setw(8) << "Fri";
    cout << setw(8) << "Sat";
    cout << endl;
    week++;
    if (week > 7) week = 1;
    for (int j = 1; j < week; j++)
        cout << setw(8) << "";
    getDays(Year, Month);
    for (int a = 1; a <= days; a++)
    {
        if (week > 7)
		{
            cout << endl;
            week = 1;
        }
        cout << setw(8) << a;
        week++;
    }
}

⌨️ 快捷键说明

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