Date class

Question:

Specify, design, and implement a class called date. Use integers to represent a date’s month, day, and year. Write a member function to increment the date to the next day.
Include friend functions to display a date in both number and word format.

My answer:

date.h

#ifndef MY_DATE_H
#define MY_DATE_H

#define NUMBER_FORMAT 0
#define WORD_FORMAT 1

class date{
public:
    date(){
        month = 1;
        day = 1;
        year = 1900;
        is_leap_year = false;
    };
    date(int m, int d, int y);
    void one_day_passed();
    friend void show_date(const date& a, int format);
private:
    int month, day, year;
    bool is_leap_year;
    bool recheck();
};

#endif

date.cpp

#include "date.h"
#include <assert.h>
#include <iostream>

using namespace std;

date::date(int m, int d, int y){
    assert(m >= 1 && m <= 12);
    assert(d >= 1 && d <= 31);
    // 判断是否是闰年
    if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0){
        is_leap_year = true;
    } else {
        is_leap_year = false;
    }
    month = m;
    day = d;
    year = y;
    assert(recheck());
}

bool date::recheck(){
    if(month == 4 || month == 6 || month == 9 ||month == 11){
        if(day == 31){
            cout << "Error: this month doesn't have 31 days! " << endl;
            return false;
        }
    }
    if(month == 2){
        if((is_leap_year && day > 29) || (!is_leap_year && day > 28)){
            cout << "Error: this month doesn't have 31 days! " << endl;
            return false;
        }
    }
    return true;
}

void date::one_day_passed(){
    if(month == 2){
        if((is_leap_year && day == 29) || (!is_leap_year && day == 28)){
            month += 1;
            day = 1;
        } else {
            day += 1;
        }
    } else if(month == 4 || month == 6 || month == 9 ||month == 11){
        if(day == 30){
            month += 1;
            day = 1;
        } else {
            day += 1;
        }
    } else {
        if(month == 12 && day == 31){
            month = 1;
            day = 1;
            year += 1;
            if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
                is_leap_year = true;
            } else {
                is_leap_year = false;
            }
        } else if(day == 31){
            month += 1;
            day = 1;
        } else {
            day += 1;
        }
    }
}

void show_date(const date& a, int format){
    if(format == NUMBER_FORMAT){
        cout << "date: " << a.year << "." << a.month << "." << a.day << endl;
    } else if(format == WORD_FORMAT){
        cout << "date: " << a.year << " ";
        switch(a.month){
            case 1:
                cout << "January";
                break;
            case 2:
                cout << "February";
                break;
            case 3:
                cout << "March";
                break;
            case 4:
                cout << "April";
                break;
            case 5:
                cout << "May";
                break;
            case 6:
                cout << "June";
                break;
            case 7:
                cout << "July";
                break;
            case 8:
                cout << "August";
                break;
            case 9:
                cout << "September";
                break;
            case 10:
                cout << "October";
                break;
            case 11:
                cout << "November";
                break;
            case 12:
                cout << "December";
                break;
            default:
                cout << "Error in Month";
                break;
        }
        cout << " " << a.day << endl;
    } else {
        cout << "Incorrect format indicated." << endl;
    }
}

main.cpp

#include "date.h"
#include <iostream>
#include <cassert>

using namespace std;

void print_list();

int main(){
    int day, month, year;
    cout << "Initialize Date" << endl;
    cout << "Please input year: " << endl;
    cin >> year;
    cout << "Please input month: " << endl;
    cin >> month;
    cout << "Please input day: " << endl;
    cin >> day;
    date a(month, day, year);
    bool continued = true;
    while(continued){
        int choice;
        int n;// get user input
        print_list();
        cin >> choice;
        switch(choice){
            case 1:
                cout << "Please input the number of days: " << endl;
                cin >> n;
                assert(n > 0);
                while(n > 0){
                    a.one_day_passed();
                    n--;
                }
                break;
            case 2:
                cout << "Please input the type of output: " << endl;
                cout << "1: NUMBER FORMAT" << endl;
                cout << "2: WORD FORMAT" << endl;
                cin >> n;
                if(n == 1){
                    show_date(a, NUMBER_FORMAT);
                } else if(n == 2){
                    show_date(a, WORD_FORMAT);
                } else {
                    cout << "incorrect input, skip" << endl;
                }
                break;
            case 3:
                continued = false;
                break;
            default:
                break;
        }
    }
    return 0;
}

void print_list(){
    cout << "******operations******" << endl;
    cout << "1: n days passed" << endl;
    cout << "2. show date" << endl;
    cout << "3. exit" << endl;
    cout << "**********************" << endl;
}

结果:

Initialize Date
Please input year: 
1998
Please input month: 
2
Please input day: 
28
******operations******
1: n days passed
2. show date
3. exit
**********************
1
Please input the number of days: 
1
******operations******
1: n days passed
2. show date
3. exit
**********************
2
Please input the type of output: 
1: NUMBER FORMAT
2: WORD FORMAT
1
date: 1998.3.1
******operations******
1: n days passed
2. show date
3. exit
**********************
1
Please input the number of days: 
730
******operations******
1: n days passed
2. show date
3. exit
**********************
2
Please input the type of output: 
1: NUMBER FORMAT
2: WORD FORMAT
1
date: 2000.2.29
******operations******
1: n days passed
2. show date
3. exit
**********************
1
Please input the number of days: 
50
******operations******
1: n days passed
2. show date
3. exit
**********************
2
Please input the type of output: 
1: NUMBER FORMAT
2: WORD FORMAT
2
date: 2000 April 19
******operations******
1: n days passed
2. show date
3. exit
**********************
3

遇到的问题:

note: declared here
     friend void show_date(const date& a, int format);

一开始show_date的format参数是默认参数,后来去掉了默认参数,就没有报上述的问题。

Reference:

整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p125

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值