【C++】实验四:Date类

【C++】实验系列主要是为了总结代码和知识点,方便复习。
以下代码都是自己敲出来的,在VS上可以运行。

细节: /*********************************************/
// 第四次实验 Date类.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

#include <iostream>
#include<iomanip>
using namespace std;

class CDate {
private:
	int year;
	int month;
	int day;
	static int n[];
public:
	CDate(int year, int month, int day) : year(year), month(month), day(day) {}
	CDate() {
		year = 2019;
		month = 3;
		day = 27;
	}
	void display() {
		cout << year << "-" << month << "-" << day << endl;
	}
	int Getyear() { return year; }
	int Getmonth() { return month; }
	int Getday() { return day; }
	//判断是否为闰年
	/*********************************************/
	bool leapyear() {                 //返回值类型用bool
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
			return true;
		else
			return false;
	}
	//前置
	CDate operator++() {
		if (leapyear() && month == 2 && day == 28)
			day++;
		if (n[month] == day)
		{
			if (month == 12)
			{
				day = month = 1;
				year++;
			}
			else
			{
				month++;
				year++;
			}
		}
		else
			day++;
		return *this;
	}

	CDate operator=(CDate a) {
		year = a.year;
		month = a.month;
		day = a.day;
	}


	//后置
	CDate operator++(int) {
		CDate a = *this;
		if (leapyear() && month == 2 && day == 28)
			day++;
		if (n[month] == day)
		{
			if (month == 12)
			{
				day = month = 1;
				year++;
			}
			else
			{
				month++;
				year++;
			}
		}
		else
			day++;
		return a;
	}
	//判断是星期几          /*********************************************/
	int whitchday(CDate a, int k) {
		int i, j, m, zhou;
		//Part1:加起始月天数
		if (a.year == year && a.month ==month)
			m = day - a.day;
		else
		{

			if (a.month == 2 && a.day == 29)
				m = 0;
			else
				m = n[a.month] - a.day;
			if (a.leapyear() && a.month <= 2)
				m++;

			//Part2:加中间年的一整月一整月
			for (i = a.year; i < year; i++)
			{
				for (j = a.month + 1; j <= 12; j++)
				{
					m += n[j];
				}
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
					m++;
			}

			///Part3:该年的一整月一整月
			for (j = 1; j < month; j++)
				m += n[j];
			if (leapyear() && month > 2)
				m++;

			//Part4:加结尾月天数
			m += day;
		}	
		m = m + k;
		zhou = m % 7;
		
		if (zhou == 0)
			return 7;
		else
			return(zhou);
	}
	int getn(int a) {
		if (leapyear())
			return n[a] + 1;
		else
			return n[a];

	}
	~CDate() {}
	void print_the_cald(CDate a, int k) {
		int i, j, m;//   i:月    j:天    m:星期几
		CDate b(year,1, 1);
		m = b.whitchday(a, k);
		for (i = 1; i <= 12; i++)//12个月重复12次
		{
			cout<< i<<" month:" << endl;
			cout << "MON    TUE    WED    THU    FRI   SAT    SUN" << endl;
			
			if (m % 7 != 1 && m % 7 != 0)//输出前第一行对齐周几
				cout << std::left << setw(7 * (m % 7 - 1)) << "  ";
			else if (m % 7 == 0)
				cout << std::left << setw(7 * 6) << "  ";
			else
				;

			for (j=1;j<=n[i]; j++,m++)//输出每月的 第1天到第n[i]天
			{
			     if (m%7 == 1&&j!=1)
					cout << endl;
				 cout << std::left << setw(7) <<j;
			}
			cout << endl << endl;
		}
	}

};
int CDate::n[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31 };//非闰年每月总天数

int main()
{
	CDate a(2018, 3, 23);
	CDate b;
	CDate c(2019, 1, 1);
	CDate d(2019, 1, 1);
	a.display();
	b.display();
	(++b).display();
	(b++).display();
	cout << endl;
	d.display();
	c.display();
	cout << b.getn(1) << endl;//输出该月天数
	cout << d.whitchday(c, 2) << endl;//判断周几
	d.print_the_cald(c, 2);//打印该年所有的日历
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值