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

发布于:2024-10-15 ⋅ 阅读:(144) ⋅ 点赞:(0)

一.日期类功能

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

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

二.运算符重载函数

我们在一开始学习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;
}

在这里插入图片描述