前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++实战项目:日期计算器的实现

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

作者头像
用户11290664
发布2024-10-12 11:57:23
530
发布2024-10-12 11:57:23
举报
文章被收录于专栏:学习

一.日期类功能

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

例如:

二.运算符重载函数

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

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

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

1如何在类中定义方法

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

2分文件操作

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

Date.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"


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


}

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

三.具体方法实现

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

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

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
bool Date::operator==(const Date& d) {

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

}

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

2 复用简化代码

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

代码语言:javascript
复制
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日期±天数的实现

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

代码语言:javascript
复制
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;

}

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

代码语言:javascript
复制
Date Date::operator+(int n) {


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


}

4测试

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

test.cpp

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

四源代码

Date.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.日期类功能
  • 二.运算符重载函数
  • 1如何在类中定义方法
  • 2分文件操作
  • 三.具体方法实现
  • 1 日期类的逻辑判断操作符
  • 2 复用简化代码
  • 3日期±天数的实现
  • 4测试
  • 四源代码
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档