Object-oriented implementation of perpetual calendar

Implement a perpetual calendar (solar version) using an object-oriented approach.


  • 【Implementation Tips】:

The crux of this problem is to determine which day of the week the first day of the desired month is. If we want to determine the day of the week on December 1, 2009, we can use the following methods:
(1) Find the day of the year that December 1, 2009 is, and store it in c.
(2) s=year-1+(year-1)/4-(year-1)/100+(year-1)/400 + c;
(3) num=s%7; then num represents the day of the week Days, 0 means Sunday, 1 means Monday, ... the
output format is as follows: (such as December 2009)
write picture description here


C++ Code:

The header file declaration of the class:

#ifndef CIRCLE_H
#define CIRCLE_H

class Date {
private:
    int year, momth;
public:
    Date(int y = 1970, int m = 1);
    void setDate(int y, int m);  //设置日期
    bool isLeapYear();  //判断闰年
    int monthDays();  //该月有多少天
    int dayNumber();  //该天是当年第几天
    int week();  //该天星期几
};

#endif
Implementation of the class:
#include "calendar.h"

Date::Date(int year, int momth) //构造函数
{
    this->year = year;
    this->momth = momth;
}

void Date::setDate(int year, int momth)  //设置日期
{
    this->year = year;
    this->momth = momth;
}

bool Date::isLeapYear() //判断是否为闰年
{
    bool leap = false;
    if ((this->year % 4 == 0 && this->year % 100 != 0) || this->year % 400 == 0) {
        leap = true;
    }

    return leap;
}

int Date::monthDays() //该月有多少天
{
    int d;
    switch (this->momth)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        d = 31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        d = 30;
        break;
    case 2:
        d = this->isLeapYear() ? 29 : 28;
        break;
    default :
        break;
    }
    return d;
}

int Date::dayNumber() //该天是当年第几天
{ 
    int sum = 0;
    int mom[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    mom[2] = this->isLeapYear() ? 29 : 28;
    for (int m = 1; m < this->momth; m++) {
        sum += mom[m];
    }

    return sum;
}

int Date::week() //该天星期几
{
    int year = this->year - 1;
    int s = year + (year / 4) - (year / 100) + (year / 400);
    int num = s + dayNumber() + 1;

    return num % 7;
}
Implementation of the main function:
#include <iostream>
#include "calendar.h"
using namespace std;

void print(Date date)
{
    int cnt = date.week();
    cout << "日\t一\t二\t三\t四\t五\t六" << endl;
    for (int i = 0; i < cnt; i++) {
        cout << " \t";
    }
    for (int i = 1; i <= date.monthDays(); i++) {
        cout << i << "\t";
        cnt++;
        if (cnt % 7 == 0) {
            cout << endl;
        }
    }
    cout << endl;
    system("pause");
}

void scan(int &y, int &m)
{
    system("cls");
    cout << "请输入需查看的年、月:" << endl;
    cin >> y;
    cin >> m;
}

int main()
{
    int year, momth, x = 0;
    scan(year, momth);
    Date date(year, momth);
    print(date);
    while (true) {
        cout << "\n是否需要修改查询的时间?\n1、是\t2、否" << endl;
        cin >> x;
        if (x == 1) {
            scan(year, momth);
            date.setDate(year, momth);
            print(date);
        }
        else {
            break;
        }
    }

    return 0;
}
  • Non-class implementation:
    Code:
#include <iostream>
using namespace std;

bool IsLeap_year(int year) //判断是否为闰年
{
    bool leap = false;
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
        leap = true;
    }

    return leap;
}

int SumDay(int *mom, int momth) //返回此年到这个月前的天数
{
    int sum = 0;
    for (int i = 0; i < momth - 1; i++) {
        sum += mom[i];
    }

    return sum;
}

int CntWeek(int *mom, int momth, int year) //返回此年1月1号所在星期几
{
    int s = year + (year / 4) - (year / 100) + (year / 400);
    int t = s + SumDay(mom, momth) + 1;

    return t % 7;
}

int main()
{
    int year, momth, day;
    int mom[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    while (true) {
        cout << "请输入需查看的年、月(输入0结束程序):" << endl;
        cin >> year;
        if (!year) {
            break;
        }
        cin >> momth;
        if (IsLeap_year(year)) {
            mom[1] = 29;
        }
        else {
            mom[1] = 28;
        }
        int cnt = CntWeek(mom, momth, year - 1);
        cout << "日\t一\t二\t三\t四\t五\t六" << endl;
        for (int i = 0; i < cnt; i++) {
            cout << " \t";
        }
        for (int i = 1; i <= mom[momth - 1]; i++) {
            cout << i << "\t";
            cnt++;
            if (cnt % 7 == 0) {
                cout << endl;
            }
        }
        cout << endl;
        system("pause");
        system("cls");
    }

    return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326658845&siteId=291194637