C++ —— 日期计算器


1. 头文件

#pragma once
#include <iostream>
using namespace std;

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);
	int GetMonthDay();
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d)const;
	bool operator<(const Date& d)const;
	bool operator<=(const Date& d)const;
	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;

	Date& operator+=(int day);
	Date operator+(int day) const;

	Date& operator-=(int day);
	Date operator-(int day) const;

	Date& operator++();
	Date operator++(int);

	Date& operator--();
	Date operator--(int);

	int operator-(const Date& d) const;

	friend ostream& operator<<(ostream& _out, const Date& d);
	friend istream& operator>>(istream& _in, Date& d);
	void Print() const;
	
private:
	int _year;
	int _month;
	int _day;
};


2. 主函数的实现

#include "Date.h"


Date::Date(int year, int month, int day)
	: _year(year)
	, _month(month)
	, _day(day)
{}

int Date::GetMonthDay()
{
	int day[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 day[2] + 1;
	}
	return day[_month];
}

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

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

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

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

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

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

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay())
	{
		_day -= GetMonthDay();
		++_month;
		if (_month > 12)
		{
			_month = 1;
			++_year;
		}
	}
	return *this;
}

Date Date::operator+(int day) const
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 1)
	{
		--_month;
		if (_month < 1)
		{
			_month = 12;
			--_year;
		}
		_day += GetMonthDay();
	}
	return *this;
}

Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}


Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return (n * flag);
}

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

ostream& operator<<(ostream& _out, const Date& d)
{
	_out << d._year << "/" << d._month << "/" << d._day;
	return _out;
}

istream& operator>>(istream& _in, Date& d)
{
	_in >> d._year >> d._month >> d._day;
	return _in;
}


3. 目录文件

#include "Date.h"
int main()
{
	int input = 0;
	do {
		cout << "                                **********  欢迎进入日期计算器  ****************" << endl;
		cout << "                                **********  1.计算两日期差几天  ****************" << endl;
		cout << "                                **********  2.计算某日往后n天   ****************" << endl;
		cout << "                                **********  3.计算某日往前n天   ****************" << endl;
		cout << "                                **********  0.  退出系统       ****************" << endl;
		printf("\n\n\n");
		cout << "请选择:";
		cin >> input;
		if (input == 1)
		{
			Date d1;
			Date d2;
			cout << "请输入第一个日期:";
			cin >> d1;
			cout << "请输入第二个日期:";
			cin >> d2;
			cout << d1 << "和" << d2 << "相差" << (d2 - d1) << "天" << endl;
			printf("\n\n\n");
		}
		else if (input == 2)
		{
			Date d1;
			int day = 0;
			cout << "请输入某日日期:";
			cin >> d1;
			cout << "请输入往后多少天:";
			cin >> day;
			cout << d1 << "往后" << day << "天是" << (d1 + day) << endl;
			printf("\n\n\n");
		}
		else if (input == 3)
		{
			Date d1;
			int day = 0;
			cout << "请输入某日日期:";
			cin >> d1;
			cout << "请输入往前多少天:";
			cin >> day;
			cout << d1 << "往前" << day << "天是" << (d1 - day) << endl;
			printf("\n\n\n");
		}
		else if (input == 0)
		{
			break;
		}
		else
		{
			cout << "请输入正确的选项" << endl;
			printf("\n\n\n");
		}

	} while (input);
}


有需要自取 gitee 链接  :日期计算器——有趣的中国人的gitee

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/480832.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

opencv函数使用查找

opencv官方文档地址&#xff1a;https://docs.opencv.org/4.x/index.html 先选对应的版本opencv-python 以这个函数为例子 model cv2.face.LBPHFaceRecognizer.create() 点开后找face类的LBP里面就有create函数的用法

vue 安装脚手架报错 certificate has expired

vue 安装脚手架的时候报错&#xff0c;报错信息如下&#xff1a; 错误信息&#xff1a;npm ERR! request to https://registry.npm.taobao.org/vue%2fcli failed, reason: certificate has expired 翻译&#xff1a;npm ERR&#xff01;请求到https://registry.npm.taobao.org…

python的街道办管理系统flask-django-php-nodejs

随着世界经济信息化、全球化的到来和互联网的飞速发展&#xff0c;推动了各行业的改革。若想达到安全&#xff0c;快捷的目的&#xff0c;就需要拥有信息化的组织和管理模式&#xff0c;建立一套合理、动态的、交互友好的、高效的街道办管理系统。当前的信息管理存在工作效率低…

python的BBS论坛系统flask-django-nodejs-php

为了更好地发挥本系统的技术优势&#xff0c;根据BBS论坛系统的需求&#xff0c;本文尝试以B/S架构设计模式中的django/flask框架&#xff0c;python语言为基础&#xff0c;通过必要的编码处理、BBS论坛系统整体框架、功能服务多样化和有效性的高级经验和技术实现方法&#xff…

XS2186,八通道,兼容IEEE802.3at/af,以太网供电PSE控制器

XS2186是一个八通道、供电设备&#xff08;PSE&#xff09;电源 控制器&#xff0c;设计用于IEEE 802.3at/af兼容PSE。器 件提供用电设备&#xff08;PD&#xff09;检测、分级、限流以及负载 断开检测。器件支持全自动工作、软件编程和外挂 eeprom。器件还支持最新二事件分级。…

4核16G服务器租用优惠价格,26.52元1个月,半年149元

阿里云4核16G服务器优惠价格26.52元1个月、79.56元3个月、149.00元半年&#xff0c;配置为阿里云服务器ECS经济型e实例ecs.e-c1m4.xlarge&#xff0c;4核16G、按固定带宽 10Mbs、100GB ESSD Entry系统盘&#xff0c;活动链接 aliyunfuwuqi.com/go/aliyun 活动链接打开如下图&a…

C语言基础知识复习(考研)

&#xff08;1&#xff09;C语言文件操作 1 什么是文件 文件有不同的类型&#xff0c;在程序设计中&#xff0c;主要用到两种文件&#xff1a; (1)程序文件。包括源程序文件(后缀为.c)、目标文件(后缀为.obj)、可执行这种文件的内容是程序代码。 (2)数据文件。文件的内容不是…

win10下自由切换多版本JDK操作

1.在window 系统变量 path路径追加%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 2.下载多版本jdk zip文件解压到到C:\Program Files\Java\目录下 3.定义切换Java版本的bat文件,内容如下 @echo off @echo -------------------welcome to use Java version switch service------------…

短剧分销cps推广很简单,短剧分销授权平台怎么搭建?

一、短剧分销是什么&#xff1f; 短剧分销&#xff0c;也被称为短剧CPS&#xff0c;是一种通过推广短剧并获得收益的方式。分销方会提供短剧片单&#xff0c;内容创作者可以根据这些片单剪辑成短视频内容进行分发。这样既能避免版权问题&#xff0c;又可以利用优质内容吸引用户…

vue3怎么使用reactive赋值

使用ref赋值&#xff1a; const list ref([]) const getList async () > {const res await axios.get(/list)list.value res.data } 如何使用reactive来替换呢&#xff1f; //const list ref([]) const list reactive([]) const getList async () > {const res…

LabVIEW焓差试验室流量计现场自动校准系统

LabVIEW焓差试验室流量计现场自动校准系统 在现代工业和科研领域&#xff0c;流量计的准确性对于保证生产过程的质量和效率非常重要。开发了一种基于LabVIEW的焓差试验室流量计现场自动校准系统&#xff0c;通过提高流量计校准的准确性和效率。 在空调器空气焓值法能效测量装…

3.22号arm

作业 实验现象&#xff0c;主函数每一秒循环字符m&#xff0c;当按下按键1&#xff0c;则串口输出1&#xff0c;按下按键2&#xff0c;串口输出2&#xff0c;按下按键3&#xff0c;串口输出3 main.c #include "uart4.h"#include "led.h"#include "k…

LeetCode每日一题——x 的平方根

x 的平方根OJ链接&#xff1a;69. x 的平方根 - 力扣&#xff08;LeetCode&#xff09; 题目&#xff1a; 思路&#xff1a; 乍一看题目只需要算一个数的平方根&#xff0c;根据我们之前学的C语言我们能很快的想到使用sqrt&#xff0c;pow这类的<math.h>库函数&#xf…

【python】Anaconda安装后打不开jupyter notebook(网页不自动跳出)

文章目录 一、遇到的问题&#xff1a;jupyter notebook网页不自动跳出&#xff08;一&#xff09;输入jupyter notebook命令&#xff08;二&#xff09;手动打开网页 二、解决办法&#xff1a;指定浏览器&#xff08;一&#xff09;找文件 jupyter_notebook_config.py&#xff…

备考ICA----Istio实验7---故障注入 Fault Injection 实验

备考ICA----Istio实验7—故障注入 Fault Injection 实验 Istio 的故障注入用于模拟应用程序中的故障现象&#xff0c;以测试应用程序的故障恢复能力。故障注入有两种: 1.delay延迟注入 2.abort中止注入 1. 环境准备 kubectl apply -f istio/samples/bookinfo/platform/kube/…

聚酰亚胺PI材料难于粘接,用什么胶水粘接?那么让我们先一步步的从认识它开始(七): 聚酰亚胺PI薄膜的厚度

聚酰亚胺PI薄膜的厚度 聚酰亚胺&#xff08;PI&#xff09;薄膜的厚度可以根据具体的应用需求而有所不同&#xff0c;通常可以在几个微米&#xff08;μm&#xff09;到几十微米之间。下面是一些常见的聚酰亚胺PI薄膜的厚度范围及其应用&#xff1a; 1.超薄膜&#xff1a; 聚酰…

练习实践-进程回收01-找到并清理僵尸进程

参考来源&#xff1a; https://blog.csdn.net/qq_36528114/article/details/71076110 https://blog.51cto.com/u_12083623/2363384 极客时间-性能优化实战-CPU性能篇 进程回收中的孤儿和僵尸进程的特点 演示环境&#xff1a; 操作系统&#xff1a;Ubuntu18.04 查询工具&#x…

【超全详解】Maven工程配置与常见问题解决指南

Maven工程 目录 Maven工程一、如何检查Maven工程是否配置正确&#xff1f;1、检查路径2、检查基本配置3、其他配置 二、Maven的基本操作基本操作install和package的区别 三、获取别人的Maven工程之后如何修改&#xff1f;四、如何正确写好配置文件&#xff1f;1.寻找配置资源2.…

eclipse中使用PlantUML plugin查看对象关系

一.背景 公司安排的带徒弟任务&#xff0c;给徒弟讲了如何设计对象。他们的思维里面都是单表增删改查&#xff0c;我的脑海都是一个个对象&#xff0c;他们相互关系、各有特色本事。稳定的结构既能满足外部功能需求&#xff0c;又能在需求变更时以最小代价响应。最大程度的记录…
最新文章