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

server/2024/10/17 15:58:24/

目录

  • 一.日期类功能
  • 二.运算符重载函数
  • 1如何在类中定义方法
  • 2分文件操作
  • 三.具体方法实现
  • 1 日期类的逻辑判断操作符
  • 2 复用简化代码
  • 3日期+-天数的实现
  • 4测试
  • 四源代码

一.日期类功能

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

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

二.运算符重载函数

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

在这里插入图片描述


http://www.ppmy.cn/server/132037.html

相关文章

【 香格里拉酒店-注册/登录安全分析报告】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 暴力破解密码&#xff0c;造成用户信息泄露短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造成亏损无底洞 …

Oracle-19g数据库的安装

简介 Oracle是一家全球领先的数据库和云解决方案提供商。他们提供了一套完整的技术和产品&#xff0c;包括数据库管理系统、企业级应用程序、人工智能和机器学习工具等。Oracle的数据库管理系统是业界最受欢迎和广泛使用的数据库之一&#xff0c;它可以管理和存储大量结构化和…

Android Studio build失败解决方案

问题背景 使用android studio编译app时遇到在仓库中找不到对应插件的问题 以及Connection refused的问题 Error:Connection refused: no further information android studio 在网上找了各种方法尝试了&#xff0c;都没有用。 解决方案 升级了最新的android studio版本&am…

Eking管理易 Html5Upload 前台任意文件上传漏洞复现

0x01 产品描述&#xff1a; ‌Eking管理易是一款专为广告制品制作企业量身定制的管理软件产品&#xff0c;旨在帮助企业实现规范化、科学化管理&#xff0c;提升运营效率和降低运营成本。‌ 该软件由广州易凯软件技术有限公司开发&#xff0c;基于JAVA企业版技术研发&#xff0…

Windows记事本不显示下划线的解决方法

文章目录 问题解决方法一、进行缩放二、改变字体大小 问题 Windows不显示下划线 解决方法 一、进行缩放 Ctrl 鼠标滚轮 或 Ctrl 放大 二、改变字体大小 编辑——>字体——>增加字体大小 原本字体大小为11&#xff0c;改为12 显示下划线

怎么快速定位bug?怎么编写测试用例?

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 作为一名测试人员如果连常见的系统问题都不知道如何分析&#xff0c;频繁将前端人员问题指派给后端人员&#xff0c;后端人员问题指派给前端人员&#xff0c;那么在…

UE5蓝图学习笔记玩家碰撞触发死亡加一秒黑屏

UE5蓝图学习笔记玩家碰撞触发死亡加一秒黑屏 1.代表检测自身是否到和其他Actor碰撞。 2.判断Actor是否等于Player Pawn 3.摄像机在一秒钟褪色0-1。 4.Delay延时一秒执行。 5.获取当前关卡的名字。 6.重新加载当前的关卡 。 7.获取Get Plyer Pawn。 8.获取玩家相机控制器…

k8s 1.28.2 集群部署 MinIO 分布式集群

文章目录 [toc]MinIO 介绍MinIO 生产硬件要求MinIO 存储要求MinIO 内存要求MinIO 网络要求MinIO 部署架构分布式 MinIO复制的 MinIO 部署 MinIO创建目录节点打标签创建 namespace创建 pv创建 MinIO配置 ingress问题记录通过代理服务器访问 MinIO 的 Object Browser 界面一直显示…