C++ 編程練習1

1. 編寫日曆打印程序,要求實現如下的main函數:

    class Calendar;

    int main() {

        Calendar c0;

        Calendar c1(2013);

        Calendar c2(2013, 4);

        Calendar c3(2013, 4, 23);

 

        cout<<c0<<c1<<c2<<c3;

        c0 = c3;

        cout<<c0;

    }


[Accepted 2013/0504]

#ifndef _CALENDAR_H_

#define _CALENDAR_H_

#include <iostream>

using namespace std;



class Year{

public:

	Year(int y = -1){

		year = y;

	}

	~Year(){}

	void print(ostream& out)const ;

protected:

	int year;

	bool is_leap_year()const ;

	int cal_1st_day()const ;

};



class Month: public Year{

public:

	Month(int y = -1, int m = -1): Year(y){

		month = m;

	}

	~Month() {}

	void print(ostream& out)const ;

protected:

	int month;

	int cal_1st_day()const ;

};



class Day: public Month{

private:

	int day;

	int calculate()const ;



public:

	Day(int y = -1, int m = -1, int d = -1): Month(y, m) {

		day = d;

	}

	~Day() {}

	void print(ostream& out)const ;

};



class Calendar{

private:

	int year;

	int month;

	int day;

public:

	Calendar(int y = 1, int m = -1, int d = -1) {

		year = y;

		month = m;

		day = d;

	}

	void print(ostream& out)const {

		if(month == -1 && day == -1){

			Year a(year);

			a.print(out);

		}

		if(month != -1 && day == -1){

			Month a(year, month);

			a.print(out);

		}

		if(month != -1 && day != -1){

			Day a(year, month , day);

			a.print(out);

		}

	}

	Calendar& operator=(const Calendar& src){

		year = src.year;

		month = src.month;

		day = src.day;

		return (*this);

	}

};



#endif

..............................


#include "calendar.h"

bool Year::is_leap_year()const {
	if (year % 400 == 0 || year % 100 != 0 && year % 4 == 0)
		return true;
	else return false;
}
int Year::cal_1st_day()const {
	int num_leap_year = 0;
	int first_day = 0;

	num_leap_year = (year - 1) / 4 + (year - 1) / 400 - (year - 1) / 100;
	first_day = ( year + num_leap_year) % 7;
	return first_day;
}

void Day::print(ostream& out)const {
	const char* days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thurday", "Friday", "Saturday"};
	int day_of_week = 0;
	day_of_week = calculate();
	out<<year<<"."<<month<<"."<<day<<" is "<<days[day_of_week]<<endl;
}

void Month::print(ostream& out)const {
	int first_day_of_month = 0;
	int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31};
	const char* days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thurday", "Friday", "Saturday"};

	if(is_leap_year())  months[2] = 29;
	first_day_of_month = cal_1st_day();
	out<<month<<endl;
	
	for(int i = 0; i < 7; i++) { 
	    out.width(10);
		out<<days[i];
	}
	out<<endl;
	for(int i = 0; i < first_day_of_month; i++) { 
	   out.width(10);
	   out<<" ";
	}
	for(int i = 1; i < months[month] + 1; i++)
	{
		out.width(10);
		out<<i;
		if((first_day_of_month + i) % 7 == 0)  out<<endl;
	}
}

void Year::print(std::ostream& out)const {
	out<<"The calendar of "<<year<<endl;

	for(int i = 1; i < 13; i++)
	{
		Month month(year, i);
		month.print(out);
		cout<<endl;
	}
}

int Month::cal_1st_day()const {
	int first_day_of_year = 0;
	int first_day_of_month = 0;
	int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31};

	first_day_of_year = Year::cal_1st_day();
	if(is_leap_year())   months[2] = 29;

	first_day_of_month = first_day_of_year;
	for(int i = 1; i < month; i++)
	{
		first_day_of_month += months[i] % 7;
	}
	return first_day_of_month % 7;
}

int Day::calculate()const {
	int first_day_of_month = 0;

	first_day_of_month = Month::cal_1st_day();
	return (first_day_of_month + day - 1) %7;
}



..............................


#include "calendar.h"

ostream& operator<<(ostream& out, const Calendar& obj){
	obj.print(out);
	return out;
}

int main()
{
    Calendar c0;
    //Calendar c0(1);
    Calendar c1(2013);
    Calendar c2(2013, 5);
    Calendar c3(2013, 5, 4);

    cout<<c0<<c1<<c2<<c3;
	c1 = c2 = c3;
	cout<<c1<<c2<<c3;

	c3 = c0;        
    c0 = c3; 
        cout<<c0;

	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章