当前位置:网站首页>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;
}
边栏推荐
- Which securities company is good at opening an account with flush? Excuse me, is it safe to open an account with mobile phone or stock?
- Leetcode · daily question · 1184. distance between bus stops · simulation
- kali简洁转换语言方法(图解)
- Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
- TypeError: Cannot read property ‘make‘ of undefined
- Simple encapsulation of wechat applet wx.request
- REST风格
- Extjs4 instance address and Chinese document address
- 在哪家证券公司开户最好最安全 如何开户炒股票
- PIP source switching
猜你喜欢

TypeError: Cannot read property ‘make‘ of undefined

VSCode如何调试Nodejs

Mysql库的操作

spark:获取日志中每个时间段的访问量(入门级-简单实现)

Class loading mechanism and parental delegation mechanism

Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection

【MATLAB】MATLAB画图系列二 1.元胞与数组转化 2.属性元胞 3.删除nan值 4.合并多fig为同一fig 5.合并多fig至同一axes

文件操作详解

Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial

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
随机推荐
Detailed explanation of address bus, data bus and control bus
Rasa 3.x learning series -rasa fallbackclassifier source code learning notes
华为相机能力
Attributeerror: module 'distutils' has no attribute' version error resolution
Spark: get the access volume of each time period in the log (entry level - simple implementation)
DS graph - minimum spanning tree
Leetcode · daily question · 1184. distance between bus stops · simulation
VSCode如何调试Nodejs
Jmmert aggregation test report
Grpc middleware implements grpc call retry
Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
Preparation of mobile end test cases
【USENIX ATC'22】支持异构GPU集群的超大规模模型的高效的分布式训练框架Whale
Video game design report template and resources over the years
基于ABP实现DDD--实体创建和更新
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
Machine learning practice notes
The spiral matrix of the force buckle rotates together (you can understand it)
Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals
在哪家证券公司开户最好最安全 如何开户炒股票