c++实战项目:日期计算器的实现

一.日期类功能

我们通过对日期类±整型操作来得到具体多少天后的日期,并在控制台输出。

例如:
在这里插入图片描述

二.运算符重载函数

我们在一开始学习c语言的时候学习过±等基础运算符,但是这些运算符只能对内置类型进行操作如a+b。但是对于内置类型(如我们定义的日期类Date)我们想对他们进行操作就不能用这些操作符了,这样再发明一个新的操作符就太复杂。

这时候我们就可以用运算符重载
定义为

返回值 operator 运算符(形参)
如日期类+天数就可以定义为int operator+(int x, int y)

1如何在类中定义方法

注意:由于c++的封装性,我们定义类中的成员变量是私有的,必需使用我们写的方法才能访问到,进行修改,因此和以前写栈中的方法不同的是,我们为了方便,把类的方法写在类的内部,这样就可以访问其成员变量了.

在这里插入图片描述

2分文件操作

为了简介明了,我们可以像以前写栈,分好几个文件,一个.c文件只写方法,另一个.h文件只写声明和定义类.

Date.h

#pragma once
#include<iostream>
#include<stdbool.h>
#include<assert.h>
using namespace std;
class Date
{
public:
	
	void Print();

private:
	int _year;
	int _month;
	int _day;

};

Date.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"


void Date::Print() {//注意函数名前要指定类域
	cout << _year << " " << _month << " " << _day << endl;


}

注意:
>我们在定义的时候要在函数名和返回值之间加类域(void Date::Print()),但是构造函数没有返回值.所以直接在函数名之前加就行
在这里插入图片描述

三.具体方法实现

1 日期类的逻辑判断操作符

其中要写>,<,>=,<=,!=.
我们先用简单逻辑来判断两个类的大小函数和是否相等

bool Date::operator>(const Date& d) {
	if (_year>d._year) {
		return true;
	}
	if (_year==d._year&&_month >d._month) {
		return true;
	}
	if (_year == d._year && _month == d._month&&_day>d._day) {
		return true;
	}

	return false;
}
bool Date::operator==(const Date& d) {

	return _year == d._year && _month == d._month && _day == d._day;

}

其中加const是为了防止出错为了防止权限的缩小.&是为了防止无限递归.

2 复用简化代码

我们上面写了>和==,我们可以用逻辑取反来简化代码,起到复用的效果.
这时候全部代码为

bool Date::operator>(const Date& d) {
	if (_year>d._year) {
		return true;
	}
	if (_year==d._year&&_month >d._month) {
		return true;
	}
	if (_year == d._year && _month == d._month&&_day>d._day) {
		return true;
	}

	return false;
}
bool Date::operator<(const Date& d) {

	return !(*this >= d);
}
bool Date::operator>=(const Date& d) {

	return *this > d || *this == d;

}
bool Date::operator<=(const Date& d) {
	return !(*this > d);
}
bool Date::operator==(const Date& d) {

	return _year == d._year && _month == d._month && _day == d._day;

}
bool Date::operator!=(const Date& d) {


	return !(*this == d);
}

3日期±天数的实现

我们要写两个方法,一个判断当月的天数进行操作,一个对天数和月份的修改直到到达正常值.

int Date::GetDay(int year, int month) {
	assert(month > 0 && month < 13);
	static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//arr[0]不返回,随机值都行
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0)
		|| (year % 400 == 0))) {

		return 29;
	}
	return arr[month];
}
Date& Date::operator+=(int n) {//n为天数
	_day += n;

	while (_day > GetDay(_year, _month)) {
		_day -= GetDay(_year, _month);
		_month++;
		if (_month == 13) {
			_year++;
			_month = 1;
		}

	}

	return *this;

}

我们也可以进行复用来让+复用+=

Date Date::operator+(int n) {


	Date tem = *this;//拷贝构造不改变*this内容
	tem += n;
	return tem;


}

4测试

最后再写一个主函数进行测试,拿今天进行测试+100天

test.cpp

在这里插入图片描述

在这里插入图片描述

测试成功和网络上的日期计算器一样,成功!!!

四源代码

Date.h

#pragma once
#include<iostream>
#include<stdbool.h>
#include<assert.h>
using namespace std;
class Date
{
public:
	bool operator>(const Date& d);
	bool operator<(const Date& d);
	bool operator>=(const Date& d);
	bool operator<=(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	Date(int a, int b, int c);
	int GetDay(int year, int month);
	Date& operator+=(int n);
	Date operator+(int n);
	Date& operator-=(int n);
	Date operator-(int n);


	
	void Print();

private:
	int _year;
	int _month;
	int _day;

};


Date.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
bool Date::operator>(const Date& d) {
	if (_year>d._year) {
		return true;
	}
	if (_year==d._year&&_month >d._month) {
		return true;
	}
	if (_year == d._year && _month == d._month&&_day>d._day) {
		return true;
	}

	return false;
}
bool Date::operator<(const Date& d) {

	return !(*this >= d);
}
bool Date::operator>=(const Date& d) {

	return *this > d || *this == d;

}
bool Date::operator<=(const Date& d) {
	return !(*this > d);
}
bool Date::operator==(const Date& d) {

	return _year == d._year && _month == d._month && _day == d._day;

}
bool Date::operator!=(const Date& d) {


	return !(*this == d);
}
Date::Date(int a , int b , int c ) {
	_year = a;
	_month = b;
	_day = c;

}
int Date::GetDay(int year, int month) {
	assert(month > 0 && month < 13);
	static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0)
		|| (year % 400 == 0))) {

		return 29;
	}
	return arr[month];
}
Date& Date::operator+=(int n) {
	_day += n;

	while (_day > GetDay(_year, _month)) {
		_day -= GetDay(_year, _month);
		_month++;
		if (_month == 13) {
			_year++;
			_month = 1;
		}

	}

	return *this;

}
Date Date::operator+(int n) {


	Date tem = *this;
	tem += n;
	return tem;


}
Date& Date::operator-=(int n) {
	_day = _day - n;
	while (_day <= 0) {

		_month--;
		if (_month == 0) {

			_month = 12;
			_year--;

		}
		_day += GetDay(_year, _month);



	}
	return *this;



}
Date Date::operator-(int n) {

	Date tem = *this;
	tem -= n;
	return tem;
}

void Date::Print() {
	cout << _year << " " << _month << " " << _day << endl;


}

test.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
int main() {


	Date a(2024,10,11);
	a.Print();
	
	a += 100;//测试+=
	a.Print();
	a -= 100;//测试-=
	a.Print();
	Date b = a + 100;//测试+
	b.Print();
	Date c = b - 100;//测试-
	c.Print();

	bool d = b > c;
	cout << d << endl;
	return 0;
}

在这里插入图片描述

(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值