当前位置: 首页 > news >正文

C++初阶的应用-日期管理系统的设计与实现

这个代码我只有一部分需要讲,其他的会在类和对象下讲解!

#include<iostream>
using namespace std;
//定义Date类
class Date
{
public://友元函数friend ostream& operator<<(ostream& out,const Date& d );friend istream& operator>>(istream& in, Date d);//成员函数的定义//判断日期是否合法bool panduan(int year, int month, int day){if (month < 1 || month > 12 || day > GetDay(year, month) || day < 0){return false;}return true;}//初始化列表Date(int year1, int month1, int day1):year(year1), month(month1), day(day1){//int a, b, c;//a = year;//b = month;//c = day;//while(panduan(a, b, c) == false)//{//	cout << "日期输入不合法,请重新输入!" << endl;//	cin >> a >> b >> c;//}//year = a;//month = b;//day = c;}//得到日期int GetDay(int year, int month){int arr[13] = { -1,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 arr[2]+ 1;}return arr[month];}//重载运算符的声明Date& operator-=(int day1);Date operator-(int day1) const;Date& operator+=(int day1);Date operator+(int day1) 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;int operator-(const Date& d) const;bool operator!=(const Date& d) const;Date operator++(int);Date& operator++();
private://成员变量的定义int year = 1 ;int month = 1;int day = 1;
};
//-=运算符的重载
Date& Date::operator-=(int day1)
{if (day1 < 0){return *this += (-day1);}day -= day1;while (day <= 0){//借位--month;if (month == 0){--year;month = 12;}day += GetDay(year, month);}return *this;
}
//-运算符重载
Date Date::operator-(int day1) const
{Date tmp = *this;tmp -= day1;return tmp;
}
//后置++运算符重载
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}
//另一个-运算符重载
int Date::operator-(const Date& d) const
{Date max = *this > d ? *this : d;Date min = *this < d ? *this : d;//Date max = *this;//Date min = d;//if (*this < d)//{//	max = d;//	min = *this;//}int n = 0;//while (max != min)//{//	min += 1;//	++n;//}while (max != min){++min;++n;}return n;
}
//+=运算符重载
Date& Date::operator+=(int day1)
{if (day1 < 0){return *this -= (-day1);}day += day1;while (day > GetDay(year, month)){day -= GetDay(year, month);//月进位++month;if (month == 13){++year;month = 1;}}return *this;
}
//+运算符重载
Date Date::operator+(int day1) const
{Date tmp = *this ;tmp += day1;return tmp;
}
//前置++运算符重载
Date& Date::operator++()
{*this += 1;return *this;
}
//==运算符重载
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 year != d.year|| month != d.month|| day != d.day;
}
//>运算符重载
bool Date::operator>(const Date& d) const
{if (year < d.year){return false;}else if (year == d.year && month < d.month){return false;}else if (year == d.year && month == d.month && day <= d.day){return false;}else{return true;}
}
//>=运算符重载
bool Date::operator>=(const Date& d) const
{return *this > d || *this == d;
}
//<运算符重载 
bool Date::operator<(const Date& d) const
{return !(*this >= d);//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;
}
//<<运算符重载
ostream& operator<<(ostream& out,const Date& d)
{out << d.year << "年" << d.month << "月" << d.day << "日" /*<< endl*/;return out;
}
//>>运算符重载
istream& operator>>(istream& in, Date d)
{in >> d.year >> d.month >> d.day;return in;
}
//打印菜单
void menu()
{int n;while(1){cout << "*****************************************" << endl;cout << "***** 0 日期加天数运算 ******************" << endl;cout << "***** 1 日期减天数运算 ******************" << endl; cout << "***** 2 日期减日期运算 ******************" << endl;cout << "***** 3 退出计算器     ******************" << endl;cout << "*****************************************" << endl;cout << "请选择你要进行的操作:>" << endl;cin >> n;if (n == 0){int year, month, day, _day;cout << "请输入日期:>" << endl;cin >> year >> month >> day;Date d1(year, month, day);if(d1.panduan(year, month, day)){cout << "请输入你想加的天数:>" << endl;cin >> _day;Date d2 = d1 + _day;cout << "该日期过" << _day << "天后的日期为" << d2;}else{cout << "由于你输入的日期不合理,请重新选择操作!" << endl;}}else if (n == 1){int year, month, day, _day;cout << "请输入日期:>" << endl;cin >> year >> month >> day;Date d1(year, month, day);if (d1.panduan(year, month, day)){cout << "请输入你想减的天数:>" << endl;cin >> _day;Date d2 = d1 - _day;//d1 -= _day;cout << "该日期前" << _day << "天的日期为" << d2;//cout << "该日期前" << _day << "天的日期为" << d1;}else{cout << "由于你输入的日期不合理,请重新选择操作!" << endl;}}else if (n == 2){int year, month, day, year1, month1, day1;cout << "请输入日期:>" << endl;cin >> year >> month >> day;Date d1(year, month, day);if (d1.panduan(year, month, day)){cout << "请输入另一个日期:>" << endl;cin >> year1 >> month1 >> day1;Date d2(year1, month1, day1);if (d2.panduan(year1, month1, day1)){int _day = d1 - d2;cout << d1 << "与" << d2 << "相差的天数为" << _day << "天" << endl;}}else{cout << "由于你输入日期的不合理,请重新选择操作!" << endl;}}else if (n == 3){return;}else{cout << "你输入的数字不规范,请重新输入!" << endl;}}
}
int main()
{//Date d1(2025, 5, 1);//Date d2(2025, 4, 18);//int _day = d1 - d2;cout << "欢迎进入日期管理系统" << endl;int n;while(1){cout << "************************" << endl;cout << "***** 0 退出系统 *******" << endl;cout << "***** 1 进入系统 *******" << endl;cout << "************************" << endl;cout << "请选择你的操作:>" << endl;cin >> n;if (n == 0){return 0;}else if (n == 1){menu();break;}else{cout << "输入数字不规范,请重新输入一个数字!" << endl;}}return 0;
}

那些注释的代码有些是能跑通的,有些是会陷入死循环的,所以我就改进了一下,只有两个函数:<<和>>运算符重载有一个注意的点:

我们如果把<<和>>置为成员函数,那么就会写为d1<<cout不符合我们的使用规范,至于返回值是这个的原因,我现在也没弄懂,但是只要会实现就可以了,主要是看懂代码,其他的东西基本上会到类和对象下来进行讲解。所以我没有过多的讲了,千万不要取关了啊!

相关文章:

  • hackmyvm-quick3
  • 运维侠职场日记9:用DeepSeek三天通关详解自动化操作pdf批量提取PDF文字将PDF转Word文档(附上脚本代码)
  • aws(学习笔记第三十九课) iot-core
  • 【人工智能】Agent未来市场与技术潜力分析
  • 【网络原理】TCP协议如何实现可靠传输(确认应答和超时重传机制)
  • C++项目 —— 基于多设计模式下的同步异步日志系统(5)(建造者模式)
  • [操作系统] 信号
  • 2025.04.20
  • 代码随想录训练营第36天 ||1049. 最后一块石头的重量 II 494. 目标和 474. 一和零
  • 无回显RCE
  • 凤凰架构-数据管理与存储
  • 节点流和处理流基本使用
  • 探索 Model Context Protocol (MCP):它如何影响 AI 的表现?
  • Go语言--语法基础4--基本数据类型--浮点数类型
  • Cherry Studio配置MCP服务全流程解析
  • Unity3D仿星露谷物语开发37之浇水动画
  • 色谱图QCPColorMap
  • React Router V7使用详解
  • 用于手部康复设备的TinyML语音分类嵌入式人工智能模块
  • OpenCV 对图像进行阈值处理 cv2.threshold
  • 被指违反代理协议遭南航暂停售票资格, 去哪儿网:今起恢复
  • 探索未来课堂更多可能,“人工智能课堂分析循证实验室”在沪成立
  • 普京宣布临时停火30小时
  • 上海推出平台算法治理合规指引:不得“静默推荐”,算法应用向上向善
  • 新华每日电讯写在浦东开发开放35周年之际:改革开放为笔,绘就崭新传奇
  • 红十字国际委员会加沙地带办公场所再次遭袭