1. 程式人生 > >定義日期類Date,並重載運算子實現幾種操作

定義日期類Date,並重載運算子實現幾種操作

題目要求這樣:定義一日期Date類,過載++,--,-和^ 運算,分別完成求日期+1,-1, 兩個日期相減之間天數,以及取對應日期的星期幾的操作,並編寫主函式進行測試。

程式碼:

Date類

</pre><pre name="code" class="cpp">//
//  Date.h
//  Date
//
//  Created by KevinLee on 15/10/13.
//  Copyright © 2015年 KevinLee. All rights reserved.
//

#include <iostream>
#include <math.h>
using namespace std;
static int dom1[12]={31,28,31,30,31,30,31,31,30,31,30,31};//Day of Month 平年
static int dom2[12]={31,29,31,30,31,30,31,31,30,31,30,31};//Day of Month 閏年
bool isLeap(int year){//判斷是否是閏年
    return (year%4==0&&year%100!=0)||year%400==0?true:false;
}
class Date{
private:
    int year,month,day;
public:
    Date(int y,int m,int d){
        year=y;month=m;day=d;
        cout<<"New Date:["<<y<<"-"<<m<<"-"<<d<<"]"<<endl;
        if(year<1900){
            cout<<"The year cannot be smaller than 1900!"<<endl;//年份不能小於1900
            year=1900;
        }
        if(month<0||month>12) {
            cout<<"Wrong month!"<<endl;//月份輸入不正確
            month=1;
        }
        if((isLeap(year)&&day>dom2[month-1])||day<1||(isLeap(year)==false&&day>dom1[month-1])){
            cout<<"Wrong day!"<<endl;//天數輸入不正確
                day=1;
            }
    }
    Date(string){
        year=1900;month=1;day=1;
    }
    Date(){};
    Date operator++(int);//日期加1
    Date operator--(int);//日期減1
    int operator-(Date);//求兩個日期之間的間隔天數
    string operator^(int);//求日期之後多少天是星期幾,0代表當天
    int countDays();//計算一天在一年中的第幾天
    void show(){
        cout<<year<<"-"<<month<<"-"<<day<<endl;
    }
};

int Date::countDays(){
    int days=0;
    if (isLeap(year)) {
        for (int i=0; i<month-1; i++) {
            days+=dom2[i];
        }
        days+=day;
    }
    else{
        for (int i=0; i<month-1; i++) {
            days+=dom1[i];
        }
        days+=day;
    }
    return days;
}

Date Date::operator++(int i){
    if (isLeap(year)) {
        if (day==dom2[month-1]) {
            day=1;
            month++;
            if (month>12) {
                year++;
                month=1;
            }
        }
        else{
            day++;
        }
    }
    else{
        if (day==dom1[month-1]) {
            day=1;
            month++;
            if (month>12) {
                year++;
                month=1;
            }
        }
        else{
            day++;
        }
    }
    return *this;
}
Date Date::operator--(int i){
    if (isLeap(year)) {
        if (day==1) {
            month--;
            if (month<1) {
                year--;
                month=12;
            }
            day=dom2[month-1];
        }
        else{
            day--;
        }
    }
    else{
        if (day==1) {
            month--;
            if (month<1) {
                year--;
                month=12;
            }
            day=dom1[month-1];
        }
        else{
            day--;
        }
    }
    return *this;
}

int Date::operator-(Date other){//利用成員函式過載
    int subDay=0;
    if(this->year==other.year){
        subDay+=abs(this->countDays()-other.countDays());
    }
    else{
        if(this->year<other.year){//交換兩個相減的日期,保證前者在後者之後
            Date temp;
            temp=*this;
            *this=other;
            other=temp;
        }
        if(this->year-other.year>2){
            for(int i=1;i<this->year-other.year;i++){
                subDay+=365;
                if(isLeap(other.year+i)){
                    subDay+=1;
                }
            }
        }
        subDay+=365-other.countDays()+this->countDays();
        if(isLeap(other.year)) subDay++;
    }
    return subDay;
}
string Date::operator^(int i){
    string str[7]={"Mon","Tue","Wed","Thur","Fri","Sat","Sun"};
    Date date("19000101");//已知1900年1月1日為星期一
    int flag;
    int day=*this-date+i;
    flag=day%7;
    return str[flag];
}

測試主函式:

//
//  main.cpp
//  Date
//
//  Created by KevinLee on 15/10/13.
//  Copyright © 2015年 KevinLee. All rights reserved.
//

#include <iostream>
#include "Date.h"
using namespace std;
int main(int argc, const char * argv[]) {
    Date date(2012,2,29),date2(2013,3,1),date3(2015,10,14);
    (date++).show();
    (date--).show();
    date.show();
    date2.show();
    date3.show();
    cout<<date-date2<<endl;//求兩個日期間隔天數
    string str=date3^0;//^符號接天數
    cout<<str<<endl;
    return 0;
}