网站的逻辑结构专门做搞笑视频的网站
2026/4/18 11:37:43 网站建设 项目流程
网站的逻辑结构,专门做搞笑视频的网站,重庆公共资源交易中心网,长沙手机模板建站前言#xff1a; 这篇博客主要会介绍一下Date类的实现#xff0c;需要运用到前面学习的一些C的知识。同时#xff0c;也可以通过这个小练习来检验一下自己的学习成果#xff0c;我会先把.h文件放在前言后面#xff0c;大家可以自己先去实现一下试试#xff0c;再来看看博…前言 这篇博客主要会介绍一下Date类的实现需要运用到前面学习的一些C的知识。同时也可以通过这个小练习来检验一下自己的学习成果我会先把.h文件放在前言后面大家可以自己先去实现一下试试再来看看博主的文章。Date.h代码语言javascriptAI代码解释#pragma once class Date { public: // 获取某年某月的天数 int GetMonthDay(int year, int month); // 全缺省的构造函数 Date(int year 1900, int month 1, int day 1); // 拷贝构造函数 // d2(d1)--this //Date(const Date d); // 赋值运算符重载--其实也可以不写 // d2 d3 - d2.operator(d2, d3) Date operator(const Date d); // 析构函数 /*~Date();*/ // 日期天数 Date operator(int day); // 日期天数 Date operator(int day); // 日期-天数 Date operator-(int day); // 日期-天数 Date operator-(int day); // 前置 Date operator(); // 后置 Date operator(int); // 后置-- Date operator--(int); // 前置-- Date operator--(); // 运算符重载 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); // 日期-日期 返回天数 int operator-(const Date d); //打印 void Print(); private: int _year; int _month; int _day; };注意博客中像打印析构拷贝构造赋值运算符重载这些之前实现过很多次的就不会再出现了大家可以自己去实现一下(当然Date类的析构拷贝赋值运算符重载都是可以不写的)最后也可以直接在代码总结里面看哈 。目录一.Date类和的运算符重载实现与复用1.1 1.2 1.3 复用复用复用对比图​编辑test.cpp:二.Date类-和-的运算符重载实现与复用2.1 -2.2 -test.cpp三.Date类前置--与后置--的运算符重载实现3.1 前置3.2 后置test.cpp3.3 前置--3.4 后置--test.cpp四.Date类比较符号的运算符重载实现4.1 4.2 4.3 4.4 4.5 4.6 !test.cpp五.Date类日期-日期的重载实现日期-日期test.cpp:六.代码总览Date.hDate.cpptest.cpp补充判断日期是否合法流插入流提取一.Date类和的运算符重载实现与复用1.1 --我们在实现日期天数之间还是需要先实现一个获取每月天数的函数的这个可以直接在.h里面写比较简单这里就直接展示给大家看了。代码语言javascriptAI代码解释// 获取某年某月的天数 int GetMonthDay(int year, int month) { assert(month 0 month 13); static int MonthDayArray[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; } else { return MonthDayArray[month]; } }在有了这个函数之后我们就可以直接实现的重载了注意分文件写的时候.cpp里面需要带上类域代码语言javascriptAI代码解释//日期天数 Date Date::operator(int day) { _day day; while (_day GetMonthDay(_year, _month)) { _day - GetMonthDay(_year, _month); _month; if (_month 13) { _year; _month 1; } } //this出了作用域还在所以可以用引用返回减少拷贝 return *this; }思路--因为是所以原来的也会改变不需要先拷贝一份。我们直接让当前日期的天数加上给的天数然后进入循环直到日期合规为止。在循环里每次减掉当月的天数再month。当月份达到13时就加年然后把月置为1。就这样当循环结束时返回*this(可以使用引用返回)1.2 --有了之后实现也是很简单的我们先来看看代码吧代码语言javascriptAI代码解释// 日期天数 Date Date::operator(int day) { //加天数需要原来的不改变 Date tmp(*this); //剩余逻辑和类似 tmp._day day; while (tmp._day GetMonthDay(tmp._year, tmp._month)) { tmp._day - GetMonthDay(tmp._year, tmp._month); tmp._month; if (tmp._month 13) { tmp._year; tmp._month 1; } } return tmp; }思路--我们直接多加一个对象tmp先把*this拷贝给它用它去就行了最后返回这个tmp就行(注意tmp出了作用域会销毁所以不能使用传引用返回)1.3 复用--写完之后会发现上面的代码重复度很高那我们是不是可以直接复用呢那是复用好还是复用好呢我们接着往下看吧复用代码语言javascriptAI代码解释//复用 Date Date::operator(int day) { Date tmp(*this); tmp day; return tmp; }复用代码语言javascriptAI代码解释//复用 Date Date::operator(int day) { *this *this day; return *this; }对比图--最后我们可以得出用拷贝多的复用拷贝少的更好也就是复用 那么后续的-和-也是一样的道理-复用-会更好。test.cpp:代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h void test() { // 日期天数 Date d1(2025, 8, 3); Date d2d1100; //d1.operator(100); d1.Print(); d2.Print(); // 日期天数 Date d3(2025, 8, 3); Date d4 d3 100; //d3.operator(100); d3.Print(); d4.Print(); } int main() { test(); return 0; }--测试完成打印出来的结果都符合预期退出码为0二.Date类-和-的运算符重载实现与复用2.1 ---我们先来看下-实现的代码再来了解思路代码语言javascriptAI代码解释// 日期-天数 Date Date::operator-(int day) { _day - day; while (_day 0) { _month--; if (_month 0) { _month 12; --_year; } _day GetMonthDay(_year, _month); } return *this; }思路-和一样原来的也会改变所以不需要额外的对象然后最后也是直接返回*this就行可以使用引用返回减少了拷贝。中间的实现思路就是先用当前日期天数减去给的天数如果0就进入循环先--month(如果为0了就令它变为12之后--year)然后再用天数当前月份天数。直到循环结束返回*this。2.2 ---有了上面的经验之后我们直接选择复用-就行了代码语言javascriptAI代码解释// 日期-天数 //直接复用 Date Date::operator-(int day) { //-的话原来的不改变 Date tmp(*this); tmp - day; return tmp; }思路依旧是利用了一个对象tmp先拷贝*this再直接用tmp-并返回就行了test.cpp代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h void test() { // 日期-天数 Date d1(2025, 8, 3); Date d2 d1 - 100; //d1.operator-(100); d1.Print(); d2.Print(); // 日期-天数 Date d3(2025, 8, 3); Date d4 d3 - 100; //d3.operator-(100); d3.Print(); d4.Print(); } int main() { test(); return 0; }--测试完成打印出来的结果都符合预期退出码为0三.Date类前置--与后置--的运算符重载实现--关于前置和后置的区别我们在C语言中就讲过前置的是先运算后赋值后置是先赋值后运算。所以它们的区别主要在于需要返回值的时候。那么在运算符重载中为了区分前置和后置我们选择了给后置加上一个形参这个形参甚至可以什么都不给这也是上一篇中没讲的一个运算符成重载的特点3.1 前置--有了前面的一些实现这里直接复用就行了很简单直接看注释和代码就行代码语言javascriptAI代码解释// 前置 Date Date::operator() { //前置先后赋值,所以返回值是运算之后的 *this 1; return *this; }3.2 后置--有了前面的一些实现这里直接复用就行了很简单直接看注释和代码就行代码语言javascriptAI代码解释// 后置 Date Date::operator(int) { //后置先赋值后所以返回值是运算之前的。 //我们先把原来的保存下来 Date tmp(*this); *this 1; return tmp; }test.cpp代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h void test() { // 前置 Date d1(2025, 8, 3); Date ret1 d1; d1.Print();//2025,8,4 ret1.Print();//2025,8,4 //后置 Date d2(2025, 8, 3); Date ret2 d2; d1.Print();//2025,8,4 ret2.Print();//2025,8,3 } int main() { test(); return 0; }--测试完成打印结果符合预期退出码为03.3 前置----有了前面的一些实现这里直接复用就行了很简单直接看注释和代码就行代码语言javascriptAI代码解释// 前置-- Date Date::operator--() { //前置--先--后赋值,所以返回值是运算之后的 *this - 1; return *this; }3.4 后置----有了前面的一些实现这里直接复用就行了很简单直接看注释和代码就行代码语言javascriptAI代码解释// 后置-- Date Date::operator--(int) { //后置--先赋值后--所以返回值是运算之前的。 //我们先把原来的保存下来 Date tmp(*this); *this - 1; return tmp; }test.cpp代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h void test() { //前置-- Date d1(2025, 8, 3); Date ret1--d1; d1.Print();//2025,8,2 ret1.Print();//2025,8,2 //后置-- Date d2(2025, 8, 3); Date ret2d2--; d2.Print();//2025,8,2 ret2.Print();//2025,8,3 } int main() { test(); return 0; }--测试完成打印结果符合预期退出码为0四.Date类比较符号的运算符重载实现--我们的比较类符号有很多。但我们只需要完整的实现其中几个剩下的通过复用和逻辑取反就可以了。4.1 代码语言javascriptAI代码解释// 运算符重载 bool Date::operator(const Date d) { return (_year d._year) (_month d._month) (_day d._day); }--这里的逻辑比较简单年月日都相等这个日期就是相等的了4.2 代码语言javascriptAI代码解释// 运算符重载 bool Date::operator(const Date 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; // } // else { // return false; // } //} //else { // return false; //} //简化 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; }--其实就是先判断年看谁大如果相等再判断月份。月份如果能比出来就直接返回true,不能就继续判断天数这里直接返回表达式结果就行。最后如果这些判断都不满足了就返回false4.3 --通过复用和就能实现这个了两者任意满足其中一个就行代码语言javascriptAI代码解释// 运算符重载 bool Date::operator (const Date d) { return (*this d) || (*this d); }4.4 --小于不就是的结果逻辑取反嘛代码语言javascriptAI代码解释// 运算符重载 bool Date::operator (const Date d) { return !(*this d);//的逻辑取反 }4.5 -- 就是的逻辑取反代码语言javascriptAI代码解释// 运算符重载 bool Date::operator (const Date d) { return !(*this d);//的逻辑取反 }4.6 !-- !就是的逻辑取反代码语言javascriptAI代码解释// !运算符重载 bool Date::operator ! (const Date d) { return !(*this d);//的逻辑取反 }test.cpp代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h void test() { Date d1(2025, 8, 3); Date d2(2025, 8, 5); Date d3(2025, 8, 3); // 运算符重载 cout (d1 d3) \n;//1 cout (d1 d2) \n\n;//0 //运算符重载 cout (d1 d3) \n;//0 cout (d1 d2) \n;//0 cout (d2 d1) \n\n;//1 // 运算符重载 cout (d1 d3) \n;//1, cout (d1 d2) \n;//0, cout (d2 d1) \n\n;//1, //运算符重载 cout (d1 d3) \n;//0 cout (d1 d2) \n;//1 cout (d2 d1) \n\n;//0 // 运算符重载 cout (d1 d3) \n;//1, cout (d1 d2) \n;//1, cout (d2 d1) \n\n;//0, //! 运算符重载 cout (d1 ! d3) \n;//0 cout (d1 ! d2) \n\n;//1 } int main() { test(); return 0; }--测试完成打印结果都符合预期(有点多)退出码为0--1表示true0表示false就不用多说了吧五.Date类日期-日期的重载实现--这个日期减日期我们可以直接全部换算成天数来求但是这样来很麻烦。所以还是使用下面这个方法会好点还是先看代码。日期-日期代码语言javascriptAI代码解释// 日期-日期 返回天数 int Date::operator-(const Date d) { Date max *this; Date min d; int ans 1;//符号 int n 0; if (*this d) { max d; min *this; ans -1; } while (min ! max) { min; n; } return n * ans; }思路--先假设*this为较大值d为较小值。此时的符号就是1(后面直接乘就是正数)但是我们需要进行判断一下如果假设错了就直接在if语句里改正过来再令符号标识为-1(后面乘就会为负数)。后续操作就是只要小的不等于大的就一直minn也一直。直到相等这时的n就是他们两之间相差的天数然后乘以符号就是我们想要的结果了test.cpp:代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h void test() { // 日期-日期 返回天数 Date d1(2025, 8, 3); Date d2(2025, 8, 5); int days1 d1 - d2;//-2 int days2 d2 - d1;//2 cout days1 \n days2 \n; } int main() { test(); return 0; }--测试完成打印结果符合预期退出码为0六.代码总览Date.h代码语言javascriptAI代码解释#pragma once #includeiostream #includeassert.h using namespace std; class Date { public: // 获取某年某月的天数 int GetMonthDay(int year, int month) { assert(month 0 month 13); static int MonthDayArray[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; } else { return MonthDayArray[month]; } } // 全缺省的构造函数 Date(int year 1900, int month 1, int day 1); // 拷贝构造函数 // d2(d1)--this //Date(const Date d); // 赋值运算符重载--其实也可以不写 // d2 d3 - d2.operator(d2, d3) Date operator(const Date d) { { if (this ! d) { _year d._year; _month d._month; _day d._day; } return *this; } } // 析构函数 /*~Date();*/ // 日期天数 Date operator(int day); // 日期天数 Date operator(int day); // 日期-天数 Date operator-(int day); // 日期-天数 Date operator-(int day); // 前置 Date operator(); // 后置 Date operator(int); // 后置-- Date operator--(int); // 前置-- Date operator--(); // 运算符重载 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); // 日期-日期 返回天数 int operator-(const Date d); //打印 void Print(); private: int _year; int _month; int _day; };Date.cpp代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h // 全缺省的构造函数 Date::Date(int year, int month, int day) { _year year; _month month; _day day; } //// 拷贝构造函数--可以不写 //// d2(d1)--this //Date::Date(const Date d) //{ // _year d._year; // _month d._month; // _day d._day; //} //日期天数 Date Date::operator(int day) { _day day; while (_day GetMonthDay(_year, _month)) { _day - GetMonthDay(_year, _month); _month; if (_month 13) { _year; _month 1; } } //this出了作用域还在所以可以用引用返回减少拷贝 return *this; } // //// 日期天数 //Date Date::operator(int day) //{ // //加天数需要原来的不改变 // Date tmp(*this); // //剩余逻辑和类似 // tmp._day day; // while (tmp._day GetMonthDay(tmp._year, tmp._month)) // { // tmp._day - GetMonthDay(tmp._year, tmp._month); // tmp._month; // if (tmp._month 13) // { // tmp._year; // tmp._month 1; // } // } // return tmp; //} //我们上面实现的和可以互相复用 //复用 Date Date::operator(int day) { Date tmp(*this); tmp day; return tmp; } ////复用 //Date Date::operator(int day) //{ // *this *this day; // return *this; //} //最后我们可以得出用拷贝多的复用拷贝少的更好也就是复用 // 日期-天数 Date Date::operator-(int day) { _day - day; while (_day 0) { _month--; if (_month 0) { _month 12; --_year; } _day GetMonthDay(_year, _month); } return *this; } // 日期-天数 //直接复用 Date Date::operator-(int day) { //-的话原来的不改变 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; } // 运算符重载 bool Date::operator(const Date d) { return (_year d._year) (_month d._month) (_day d._day); } // 运算符重载 bool Date::operator(const Date 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; // } // else { // return false; // } //} //else { // return false; //} //简化 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) { return (*this d) || (*this d); } // 运算符重载 bool Date::operator (const Date d) { return !(*this d);//的逻辑取反 } // 运算符重载 bool Date::operator (const Date d) { return !(*this d);//的逻辑取反 } // !运算符重载 bool Date::operator ! (const Date d) { return !(*this d);//的逻辑取反 } // 日期-日期 返回天数 int Date::operator-(const Date d) { Date max *this; Date min d; int ans 1;//符号 int n 0; if (*this d) { max d; min *this; ans -1; } while (min ! max) { min; n; } return n * ans; } //打印 void Date::Print() { cout _year - _month - _day endl; } //// 析构函数--可以不写 //Date::~Date() //{ // _year 0; // _month 0; // _day 0; //}test.cpp代码语言javascriptAI代码解释#define _CRT_SECURE_NO_WARNINGS 1 #includeDate.h void test() { //// 日期天数 //Date d1(2025, 8, 3); //Date d2d1100; ////d1.operator(100); //d1.Print(); //d2.Print(); //// 日期天数 //Date d3(2025, 8, 3); //Date d4 d3 100; ////d3.operator(100); //d3.Print(); //d4.Print(); // /////////////////////////////////////// //// 日期-天数 //Date d1(2025, 8, 3); //Date d2 d1 - 100; ////d1.operator-(100); //d1.Print(); //d2.Print(); //// 日期-天数 //Date d3(2025, 8, 3); //Date d4 d3 - 100; ////d3.operator-(100); //d3.Print(); //d4.Print(); //////////////////////////////////////////// //// 前置 //Date d1(2025, 8, 3); //Date ret1 d1; //d1.Print();//2025,8,4 //ret1.Print();//2025,8,4 ////后置 //Date d2(2025, 8, 3); //Date ret2 d2; //d1.Print();//2025,8,4 //ret2.Print();//2025,8,3 ///////////////////////////////////////////// ////前置-- //Date d1(2025, 8, 3); //Date ret1--d1; //d1.Print();//2025,8,2 //ret1.Print();//2025,8,2 ////后置-- //Date d2(2025, 8, 3); //Date ret2d2--; //d2.Print();//2025,8,2 //ret2.Print();//2025,8,3 /////////////////////////////////////////////// //Date d1(2025, 8, 3); //Date d2(2025, 8, 5); //Date d3(2025, 8, 3); //// 运算符重载 //cout (d1 d3) \n;//1 //cout (d1 d2) \n\n;//0 // ////运算符重载 //cout (d1 d3) \n;//0 //cout (d1 d2) \n;//0 //cout (d2 d1) \n\n;//1 // //// 运算符重载 //cout (d1 d3) \n;//1, //cout (d1 d2) \n;//0, //cout (d2 d1) \n\n;//1, // ////运算符重载 //cout (d1 d3) \n;//0 //cout (d1 d2) \n;//1 //cout (d2 d1) \n\n;//0 //// 运算符重载 //cout (d1 d3) \n;//1, //cout (d1 d2) \n;//1, //cout (d2 d1) \n\n;//0, ////! 运算符重载 //cout (d1 ! d3) \n;//0 //cout (d1 ! d2) \n\n;//1 ///////////////////////////////////////////////// // 日期-日期 返回天数 Date d1(2025, 8, 3); Date d2(2025, 8, 5); int days1 d1 - d2;//-2 int days2 d2 - d1;//2 cout days1 \n days2 \n; } int main() { test(); return 0; }完整源代码cpp-exclusive-warehouse: 【CPP知识学习仓库】 - Gitee.com补充判断日期是否合法--在构造和流提取都用的上流插入流提取测试https://www.dongchedi.com/article/7597928520563606078https://www.dongchedi.com/article/7597922326298378814https://www.dongchedi.com/article/7597926762722837016https://www.dongchedi.com/article/7597929338554057240https://www.dongchedi.com/article/7597921994151428633https://www.dongchedi.com/article/7597929797910020632https://www.dongchedi.com/article/7597928366092091928https://www.dongchedi.com/article/7597929590161900056https://www.dongchedi.com/article/7597927909500224024https://www.dongchedi.com/article/7597930186495050302https://www.dongchedi.com/article/7597927708630303256https://www.dongchedi.com/article/7597928039523353150https://www.dongchedi.com/article/7597926803315474969https://www.dongchedi.com/article/7597927168760676889https://www.dongchedi.com/article/7597922707208421912https://www.dongchedi.com/article/7597928484878795289https://www.dongchedi.com/article/7597919362842247704https://www.dongchedi.com/article/7597922002967560766https://www.dongchedi.com/article/7597921013909078590https://www.dongchedi.com/article/7597919426217771582https://www.dongchedi.com/article/7597919793479565886https://www.dongchedi.com/article/7597923501185450521https://www.dongchedi.com/article/7597922479633596990https://www.dongchedi.com/article/7597917398364602942https://www.dongchedi.com/article/7597919122625839678https://www.dongchedi.com/article/7597920141354992190https://www.dongchedi.com/article/7597918012628566553https://www.dongchedi.com/article/7597913022207885849https://www.dongchedi.com/article/7597917338478166552https://www.dongchedi.com/article/7597916391366656537https://www.dongchedi.com/article/7597917833053553214https://www.dongchedi.com/article/7597918098691686974https://www.dongchedi.com/article/7597917894638502425https://www.dongchedi.com/article/7597918523683832382https://www.dongchedi.com/article/7597911679200363070https://www.dongchedi.com/article/7597913077954495001

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询