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

温州专业网站建设seo推广方法集合

温州专业网站建设,seo推广方法集合,seo网站建设,政府门户网站建设背景意义《C初阶之类和对象》【经典案例:日期类】 前言:------------代码实现------------设计指南程序代码头文件:Data.h源文件:Data.cppTest.cpp 运行结果: ------------核心操作------------操作一:获取某年的某…

《C++初阶之类和对象》【经典案例:日期类】

  • 前言:
  • ------------代码实现------------
  • 设计指南
  • 程序代码
    • 头文件:Data.h
    • 源文件:
      • Data.cpp
      • Test.cpp
    • 运行结果:
  • ------------核心操作------------
    • 操作一:获取某年的某月的天数
    • 操作二:判断一个日期是否合法
    • 操作三:运算符+=的重载函数(日期 += 天数)
    • 操作四:运算符-=的重载函数(日期 -= 天数)
    • 操作五:运算符-的重载函数(日期 - 日期)

在这里插入图片描述

往期《C++初阶》回顾:

/------------ 入门基础 ------------/
【C++的前世今生】
【命名空间 + 输入&输出 + 缺省参数 + 函数重载】
【普通引用 + 常量引用 + 内联函数 + nullptr】
/------------ 类和对象 ------------/
【类 + 类域 + 访问限定符 + 对象的大小 + this指针】
【类的六大默认成员函数】
【初始化列表 + 自定义类型转换 + static成员】
【友元 + 内部类 + 匿名对象】

在这里插入图片描述

前言:

(✧∇✧)ノ hi~ ,小伙伴们早上好呀!(๑˃̵ᴗ˂̵)و 昨天终于考完试啦~🎉 今天就要收拾行李回家咯 🧳在踏上归途之前,博主决定为大家带来类和对象专题的最后一节精华内容:【经典案例:日期类】的实现!✨
这个案例将串联我们之前学过的所有重要概念,绝对是检验学习成果的完美实践!💪

经过前面关于 C++ 类和对象的系统学习 📚✨,我们已经逐步掌握了:

  • 类的定义 💡
  • 对象的创建与使用 🔄
  • 成员变量和成员函数的设计 💻

这些知识就像搭建高楼的基石 🏢⛏️,为我们接下来的实践操作奠定了坚实的基础 💪!现在,是时候将这些理论运用到实际中,亲手实现一个"日期类"啦~ (๑•̀ㅂ•́)و✧

------------代码实现------------

设计指南

我们要实现的日期类(Date)应该具备以下功能:

  • 存储年、月、日信息
  • 支持日期的合法性验证
  • 实现日期的基本运算(如:加减天数)
  • 支持日期的比较操作
  • 提供多种格式的日期输出

程序代码

头文件:Data.h

#pragma once//任务1:包含需要使用的头文件
#include <iostream>
#include <assert.h>
using namespace std;//任务2:实现日期类
class Date
{/*------------------------------第四部分:使用友元函数进行运算符重载------------------------------*/friend ostream& operator<<(ostream& out, const Date& d);  //输出流重载friend istream& operator>>(istream& in, Date& d);         //输入流重载
public:/*------------------------------第一部分:定义默认函数------------------------------*//*--------------实现:默认构造函数--------------*/Date(int year,int month,int day)  //全缺省的默认构造函数:_year(year),_month(month),_day(day){}/*------------------------------第二部分:Date类的成员函数------------------------------*//*--------------实现:“打印日期”的成员函数--------------*/void Pirnt(){cout << _year << "/" << _month << "/" << _day << endl;}/*--------------实现:“获取某年的某月的天数”的成员函数--------------*/int GetMonthDay(int year, int month) //注意:该成员函数使用频繁,所以选择类内实现,自动为内联函数{//1.使用断言保证传入的日期的合法性assert(month >= 1 && month <= 12);//2.定义一个静态数组存储一年的每月的天数static int MonthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//3.判断年是闰年还是平年//1)能被4整除且不能被100整除的年正好是闰年//2)能被400整除的年也是闰年//特殊的情况 (二月的天数)if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 29;}//正常的情况else{return MonthDayArray[month];}}/*--------------声明:“判断一个日期是否合法”的成员函数--------------*/bool checkDate();/*------------------------------第三部分:使用成员函数进行运算符重载------------------------------*//*--------------声明使用成员函数重载的“比较运算符”--------------*/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;int operator-(const Date& d)const;/*--------------声明使用成员函数重载的“自增/自减运算符”--------------*/Date& operator++();    //前置++Date operator++(int);  //后置++Date& operator--();    //前置--Date operator--(int);  //后置--
private:int _year;int _month;int _day;
};//任务3:声明流运算符重载
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,  Date& d);

源文件:

Data.cpp

#include "Date.h"
/*---------------------------------实现:“检查一个日期是否合法”---------------------------------*/
bool Date::checkDate()
{if (_month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)){return false;}else{return true;}
}/*---------------------------------“比较”运算符的重载函数的实现---------------------------------*//*---------------------1.实现:运算符<的重载函数---------------------*/
bool Date::operator<(const Date& d)const
{//1.比较年份if (_year < d._year){return true;}//2.年份相同比较月份else if (_year == d._year){if (_month < d._month){return true;}//3.年月相同比较日else if (_month == d._month){return _day < d._day;}}return false;
}/*---------------------2.实现:运算符<=的重载函数---------------------*/
bool Date::operator<=(const Date& d)const
{return *this < d || *this == d;
}/*---------------------3.实现:运算符>的重载函数---------------------*/bool Date::operator>(const Date& d)const
{return !(*this <= d);
}/*---------------------4.实现:运算符>=的重载函数---------------------*/bool Date::operator>=(const Date& d)const
{return !(*this < d);
}/*---------------------5.实现:运算符==的重载函数---------------------*/
bool Date::operator==(const Date& d)const
{只要两个日期的:年、月、日中有一个不相等,这两个日期就不相等//if (_year != d._year || _month != d._month || _day != d._day)//{//	return false;//}return _year == d._year && _month == d._month && _day == d._day;
}/*---------------------6.实现:运算符!=的重载函数---------------------*/
bool Date::operator!=(const Date& d)const
{return !(*this == d);
}/*---------------------------------“加减”运算符的重载函数的实现---------------------------------*/
/*---------------------1.实现:运算符+=的重载函数(日期 += 天数)---------------------*/
Date& Date::operator+=(int day)
{//1.判断要加上的天数是不是负数 if (day < 0){return *this -= (-day);}//2.直接将天数加在日期的天数上_day += day;//3.使用while循环修正日期的天数while (_day > GetMonthDay(_year, _month)){//3.1:直接将不合法的天数减去当前日期的这一月应有的天数_day -= GetMonthDay(_year, _month);//3.2:调整月份_month++;//3.3:修正月份if (_month > 12){_year++;_month = 1;}}return *this;
}/*---------------------2.实现:运算符+的重载函数(日期 + 天数)---------------------*/
Date Date::operator+(int day)const
{Date tmp = *this;tmp += day;return tmp;
}/*---------------------3.实现:运算符-=的重载函数(日期 -= 天数)---------------------*/
Date& Date::operator-=(int day)
{//1.判断要减去的天数是不是负数if (day < 0){return *this += (-day);}//2.直接将天数减去日期的天数_day -= day;//3.使用while循环修正日期的天数while (_day <= 0){//3.1:调整月份_month--;//3.2:修正月份if(_month<0){_year--;_month = 12;}//3.3:直接将不合法的天数加上当前日期的上一月应有的天数_day += GetMonthDay(_year, _month);}return *this;
}/*---------------------4.实现:运算符-的重载函数(日期 - 天数)---------------------*/
Date Date::operator-(int day)const
{Date tmp = *this;tmp -= day;return tmp;
}/*---------------------5.实现:运算符-的重载函数(日期 - 日期)---------------------*/
int Date::operator-(const Date& d) const
{//1.使用假设法:假设调用者的日期更大,被调者d的日期更小Date max = *this;Date min = d;//2.调整max和min的日期int flag = 1;  //flag的设置很关键:日期之差没有负值,可以使用flag进行调整if (*this < d){max = d;min = *this;flag = -1; }//3.定义变量记录两个日期之间的天数差值int n = 0;while (min != max){++min;++n;}return n * flag; 
}/*---------------------------------“自增/自减”运算符的重载函数的实现---------------------------------*//*---------------------1.实现:运算符前置++的重载函数---------------------*/
Date& Date::operator++()
{*this += 1;return *this;
}/*---------------------2.实现:运算符后置++的重载函数---------------------*/
Date Date::operator++(int)
{Date tmp = *this;*this += 1;return tmp;
}/*---------------------3.实现:运算符前置--的重载函数---------------------*/
Date& Date::operator--()
{*this -= 1;return *this;
}/*---------------------4.实现:运算符后置--的重载函数---------------------*/
Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}/*---------------------------------“流运算符”运算符的重载函数的实现---------------------------------*/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;if (!d.checkDate()){cout << "输出的日期不合法!!!";}return in;
}

Test.cpp

#include "Date.h"void printTestResult(const char* testName, bool passed) 
{cout << testName <<"\t" << "[" << (passed ? "成功" : "失败") << "]" << endl;
}int main() 
{cout << "===== 日期类测试 =====" << endl;/*----------------测试1: 日期合法性----------------*/Date valid(2024, 5, 15);   //合法日期Date invalid(2024, 2, 30); //不合法日期printTestResult("日期合法性检查", valid.checkDate() && !invalid.checkDate());/*----------------测试2: 比较运算符----------------*/Date d1(2024, 5, 15);Date d2(2024, 5, 16);bool cmpResult = (d1 < d2) && (d1 <= d2) && !(d1 > d2) && !(d1 >= d2);printTestResult("比较运算符", cmpResult);/*----------------测试3: 加法运算----------------*/Date d3 = d1 + 30;Date expectedAdd(2024, 6, 14);printTestResult("日期+天数", d3 == expectedAdd);/*----------------测试4: 减法运算----------------*/Date d4 = d1 - 25;Date expectedSub(2024, 4, 20);printTestResult("日期-天数", d4 == expectedSub);Date d5(2024, 4, 15);int sub = d1 - d5;printTestResult("日期-日期", sub == 30);/*----------------测试5: 自增运算符----------------*/Date d6 = d1;Date postInc = d6++; //注意这里的细节:先进行后置++的测试,再进行前置++的测试,这样输出的结果会更直观一点Date preInc = ++d6;printTestResult("自增运算符", (postInc == Date(2024, 5, 15)) && (preInc == Date(2024, 5, 17)));/*----------------测试6: 流输出----------------*/cout << "流输出测试: " << d1;/*----------------测试7: 流输入(手动验证)----------------*/cout << "请输入日期 (YYYY MM DD): ";Date input(2025, 5, 15);cin >> input;cout << "输入结果: " << input;return 0;
}

运行结果:

在这里插入图片描述

------------核心操作------------

操作一:获取某年的某月的天数

/*--------------实现:“获取某年的某月的天数”的成员函数--------------*/
int GetMonthDay(int year, int month) //注意:该成员函数使用频繁,所以选择类内实现,自动为内联函数
{//1.使用断言保证传入的日期的合法性assert(month >= 1 && month <= 12);//2.定义一个静态数组存储一年的每月的天数static int MonthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//3.判断年是闰年还是平年//1)能被4整除且不能被100整除的年正好是闰年//2)能被400整除的年也是闰年//特殊的情况 (二月的天数)if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 29;}//正常的情况else{return MonthDayArray[month];}
}

在这里插入图片描述

操作二:判断一个日期是否合法

/*---------------------------------实现:“检查一个日期是否合法”---------------------------------*/
bool Date::checkDate()
{if (_month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)){return false;}else{return true;}
}

在这里插入图片描述

操作三:运算符+=的重载函数(日期 += 天数)

/*---------------------1.实现:运算符+=的重载函数(日期 += 天数)---------------------*/
Date& Date::operator+=(int day)
{//1.判断要加上的天数是不是负数 if (day < 0){return *this -= (-day);}//2.直接将天数加在日期的天数上_day += day;//3.使用while循环修正日期的天数while (_day > GetMonthDay(_year, _month)){//3.1:直接将不合法的天数减去当前日期的这一月应有的天数_day -= GetMonthDay(_year, _month);//3.2:调整月份_month++;//3.3:修正月份if (_month > 12){_year++;_month = 1;}}return *this;
}

在这里插入图片描述

操作四:运算符-=的重载函数(日期 -= 天数)

/*---------------------3.实现:运算符-=的重载函数(日期 -= 天数)---------------------*/
Date& Date::operator-=(int day)
{//1.判断要减去的天数是不是负数if (day < 0){return *this += (-day);}//2.直接将天数减去日期的天数_day -= day;//3.使用while循环修正日期的天数while (_day <= 0){//3.1:调整月份_month--;//3.2:修正月份if(_month<0){_year--;_month = 12;}//3.3:直接将不合法的天数加上当前日期的上一月应有的天数_day += GetMonthDay(_year, _month);}return *this;
}

在这里插入图片描述

操作五:运算符-的重载函数(日期 - 日期)

/*---------------------5.实现:运算符-的重载函数(日期 - 日期)---------------------*/
int Date::operator-(const Date& d) const
{//1.使用假设法:假设调用者的日期更大,被调者d的日期更小Date max = *this;Date min = d;//2.调整max和min的日期int flag = 1;  //flag的设置很关键:日期之差没有负值,可以使用flag进行调整if (*this < d){max = d;min = *this;flag = -1; }//3.定义变量记录两个日期之间的天数差值int n = 0;while (min != max){++min;++n;}return n * flag; 
}

在这里插入图片描述
在这里插入图片描述

http://www.dtcms.com/wzjs/129732.html

相关文章:

  • 深圳市建设网站营销培训方案
  • 国外商品网站百度关键词排名价格
  • 让做网站策划没经验怎么办网店如何引流与推广
  • 网站建设合同标准版搜索引擎营销方案例子
  • 萧山网站优化seo模板建站
  • wordpress法律主题关键词优化教程
  • 朝阳区北京网站建设快速建站教程
  • 国外优秀论文网站百度开户需要什么条件
  • 上海建设网站制作国产长尾关键词拘挖掘
  • 滨湖区知名做网站价格清远今日头条新闻
  • 做网站最大的公司友情链接获取的途径有哪些
  • seo推广岗位职责四川seo优化
  • 福州网站建设招商网络软营销
  • 替代 wordpress基本seo技术在线咨询
  • 怎样做网站收录seo工程师是什么职业
  • 株洲市哪里有做公司官方网站沧州网站seo公司
  • 平台建网站恢复正常百度
  • 网站做研究生毕业论文代写文章质量高的平台
  • 体育网站建设的必要性市场seo是什么意思
  • 网站账户上的余额分录怎么做做seo需要哪些知识
  • 网站开发能从事那些职业网站seo视频狼雨seo教程
  • 南昌网站开发公司电话seo网站建设公司
  • 昆明网络推广昆明网站建设昆明昆明什么是sem和seo
  • 织梦网站广告公众号代运营
  • jtbc网站开发教程seo流量软件
  • 夏天做哪些网站致富百度竞价恶意点击软件
  • 网站开发项目实训总结百度网址提交入口平台
  • 提升学历一般多少钱蜗牛精灵seo
  • 福田祥菱官网青岛seo排名公司
  • 美国cloudflare服务器网站快速优化排名app