当前位置:网站首页>Date class and time class definitions (operator overload application)
Date class and time class definitions (operator overload application)
2022-07-24 15:04:00 【It's Xiaoguang a~】
List of articles
Date class and time class definitions ( Operator overload application )
Date class definition ( Operator overload application ):
class Data // At first, it was only used to indicate the date of publication , Later, it was found that both borrowing and return dates were available
{
int year;
int month;
int day;
public:
Data(){
};
Data(int year1,int month1,int day1):year(year1),month(month1),day(day1){
};
friend ostream&operator<<(ostream&os,const Data&d);
friend istream&operator>>(istream&is,Data&d);
bool operator<(const Data &d)const // Date comparison function
{
return year!=d.year?year<d.year:month!=d.month?month<d.month:day<d.day;}
int operator-(Data d) // The operation of the date , You can directly subtract two months by month , You can also operate a little more accurately as follows ( It can also be used to judge the normal and leap years for more accurate operation )
{
if(year<d.year||(year==d.year&&month<d.month)||(year==d.year&&month==d.month&&day<d.day))
return -1; // There is no way to subtract the date ( When is the later date )
int m[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31}; // Press the subscript ( Corresponding to the month ) A filling , The first number is not counted , For February of each year, press 28 Tianji ( Do not consider the leap year )
int count=0;
int a=d.year,b=d.month,c=d.day;
while(1)
{
if(this->year==a&&this->month==b&&this->day==c) break;
if(c==m[b]){
c=1;
if(b==12)
{
b=1;
a++;
}
else b++;
}
else c++;
count++;
}
return count;
}
Data operator+ (int x)
{
int m[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31}; // Press the subscript ( Corresponding to the month ) A filling , The first number is not counted , For February of each year, press 28 Tianji ( Do not consider the leap year )
while(x--)
{
if(day==m[month])
{
day=1;
if(month==12)
{
year++;
month=1;
}
else month++;
}
else day++;
}
return *this;
}
};
istream&operator>>(istream&is,Data&d) // First reload the input and output of the publication date
{
while(1)
{
is>>d.year>>d.month>>d.day;
if(d.year<1900||d.year>2019||d.month<1||d.month>12||d.day<1||d.day>31); // Here, it can be optimized to judge leap years or ordinary years, and then judge
else break;
}
return is;
}
ostream&operator<<(ostream&os,const Data&d)
{
//os<<"year:"<<d.year<<" "<<"month:"<<d.month<<" "<<"day:"<<d.day;
// In order to unify input and output , Change to the following output form
os<<d.year<<" "<<d.month<<" "<<d.day;
return os;
}
Time class definition ( Operator overload application ):
class Time
{
int year,month,day,hour,min;
string s;
public:
Time() {
};
~Time() {
};
Time(int year1,int month1 ,int day1,int hour1,int min1):year(year1),month(month1),day(day1),hour(hour1),min(min1) {
};
friend istream& operator>>(istream&,Time&t);
friend ostream& operator<< (ostream&,const Time&t);
int operator- (Time t) // Overload operator "-" Define... Within a class , Out of class definitions due to year,month Waiting is private , Need to use get Function fetch data , There is no need to
{
// Just define it directly , There is no need to use references , At first, I felt the same , Using references , Later, it was found that the error could not be adjusted , It took a long time to find out , After using the reference, the value after the minus sign will become the value before the minus sign , The two values are the same
if((year<t.year)||(year==t.year&&month<t.month)||(year==t.year&&month==t.month&&day<t.day)||(year==t.year&&month==t.month&&day==t.day&&hour<t.hour)||year==t.year&&month==t.month&&day==t.day&&hour==t.hour&&min<t.min)
return -1;
int countyear=0;
int countmonth=0;
int countday=0;
int counthour=0;
int countmin=0;
int m[13] = {
0,31,28,31,30,31,30,31,31,30,31,30,31}; // Press the subscript ( Corresponding to the month ) A filling , The first number is not counted , For February of each year, press 28 Tianji ( Do not consider the leap year )
while(1)
{
if(this->year==t.year&&this->month==t.month&&this->day==t.day&&hour==t.hour&&min==t.min) break;
if(t.min==60)
{
t.hour++;
counthour++;
t.min=0;
if(t.hour==24)
{
t.day++;
countday++;
t.hour=0;
if(t.day>m[t.month]) // The number of days at the end of the above date exists , If write "==", Then skip directly , Unrealistic
{
t.month++;
countmonth++;
t.day=1;
if(t.month>12) //12 The moon exists
{
t.year++;
countyear++;
t.month=1;
}
}
}
}
else {
t.min++;countmin++;}
}
//cout<<countyear<<" "<<countmonth<<" "<<countday<<" "<<counthour<<" "<<countmin<<endl;
return countmin;
}
//Time operator+ (int i); // utilize "-" Operation can replace "+"
// For the comparison of time , I didn't plan to use it at first , However, the later login and the view of the user's travel ticket need to be used
bool operator< (const Time&t)const
{
return year!=t.year?year<t.year:month!=t.month?month<t.month:day!=t.day?day<t.day:hour!=t.hour?hour<t.hour:min<t.min;
}
bool operator== (const Time&t)const
{
return year==t.year&&month==t.month&&day==t.day&&hour==t.hour&&min==t.min;
}
};
istream&operator>>(istream&in,Time&t)
{
while(1) // It's good or bad to use circulation , When the program is running, the user can input the time again , But when the time in the file is wrong , The program will loop all the time
{
//in>>t.year>>t.s>>t.month>>t.s>>t.day>>t.s>>t.hour>>t.s>>t.min;
in>>t.year>>t.month>>t.day>>t.hour>>t.min;
if(t.year>2000&&t.year<=2019&&t.month>=1&&t.month<=12&&t.day>=1&&t.day<=31&&t.hour>=0&&t.hour<=23&&t.min>=0&&t.min<60) // Limit time , It should also be noted that outdated tickets cannot be bought ( Query is also not available , This can be requested in login time )
break;
else cout<<"Time error, Please try again :"<<endl;
}
return in;
}
ostream&operator<<(ostream&out,const Time&t)
{
out<<t.year<<" "<<t.month<<" "<<t.day<<" "<<t.hour<<" "<<t.min;
return out;
}
边栏推荐
- Moving the mouse into select options will trigger the mouseleave event processing scheme
- Huawei camera capability
- 股票开户之后就可以购买6%的理财产品了?
- JS native - array sorting to find out the characters with the largest number of strings
- Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布
- The spiral matrix of the force buckle rotates together (you can understand it)
- The server switches between different CONDA environments and views various user processes
- Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
- “00后”来了!数睿数据迎来新生代「无代码」生力军
- String application - calculate the longest true prefix of a string
猜你喜欢

Kotlin类与继承

spark学习笔记(三)——sparkcore基础知识

深度学习中的学习率调整策略(1)

zabbix管理员忘记登录密码

使用 Fiddler Hook 报错:502 Fiddler - Connection Failed

多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题

Similarities and differences between nor flash and NAND flash

Overall testing framework for performance testing

【USENIX ATC'22】支持异构GPU集群的超大规模模型的高效的分布式训练框架Whale

Rest style
随机推荐
Self join usage of SQL
Mysql库的操作
Not configured in app.json (uni releases wechat applet)
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
Strongly connected component
Bibliometrix: dig out the one worth reading from thousands of papers!
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022
Various searches (⊙▽⊙) consolidate the chapter of promotion
Attributeerror: module 'distutils' has no attribute' version error resolution
Leetcode · daily question · 1184. distance between bus stops · simulation
zabbix管理员忘记登录密码
Machine learning practice notes
Rest style
Route planning method for UAV in unknown environment based on improved SAS algorithm
The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic
CSDN garbage has no bottom line!
onBlur和onChange冲突解决方法
异或程序
Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration
Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出