C++ 编程练习1
来源:互联网 发布:淘宝网dotacoko 编辑:程序博客网 时间:2024/04/28 05:26
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;}
- 学习c编程练习1
- C语言编程(练习1:函数 )
- C和指针编程练习---6.18.1
- c编程练习
- C编程小练习
- C语言编程练习
- C语言编程小练习
- C语言编程练习(二)
- C和指针-编程练习
- C语言编程练习一
- C语言编程练习二
- C语言编程练习随笔
- [C练习]编程实现strstr
- C语言函数编程练习
- C++primer Plus 第六版 编程练习3.7.1
- C语言编程(练习1:循环,三大循环结构 )
- C语言编程(练习1:分支和跳转 )
- C语言编程(练习1:字符输入和输出 )
- Xmanager 连接red-hat 5.3 与 6.3配置方法
- ICE Manual(Documentation for Ice 3.5)---Ice Architecture--异常信息
- IOS开发(7)之UINavigationController导航
- phpcms在不同的页面应用不同的css的方法
- LeetCode: Letter Combinations of a Phone Number
- C++ 编程练习1
- python OS
- VC下获取各个驱动器路径,分类及其获取其信息的资料整理
- asp.net 2.0 下的表单验证Cookieless属性
- IEEE1588硬件支持、linux下PTP协议
- 整理常见Android.mk写法
- 对tinyxml操作的常用接口封装——CUDxml
- linux core dump 知识整理
- MessageBox添加帮助按钮