当前位置:网站首页>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;
}
边栏推荐
- Leetcode · daily question · 1184. distance between bus stops · simulation
- spark:指定日期输出相应日期的日志(入门级-简单实现)
- The first n rows sorted after dataframe grouping nlargest argmax idmax tail!!!!
- LeetCode高频题56. 合并区间,将重叠的区间合并为一个区间,包含所有区间
- Attributeerror: module 'distutils' has no attribute' version error resolution
- 循环结构practice
- String application - calculate the longest true prefix of a string
- Clear all spaces in the string
- LeetCode·每日一题·1184.公交站间的距离·模拟
- The spiral matrix of the force buckle rotates together (you can understand it)
猜你喜欢

Detailed explanation of address bus, data bus and control bus

Maotai ice cream "bucked the trend" and became popular, but its cross-border meaning was not "selling ice cream"

REST风格
![[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes](/img/4d/b0ba599a732d1390c5eeb1aea6e83c.png)
[matlab] matlab drawing Series II 1. Cell and array conversion 2. Attribute cell 3. delete Nan value 4. Merge multiple figs into the same Fig 5. Merge multiple figs into the same axes

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

kali简洁转换语言方法(图解)

(零九)Flask有手就行——Cookie和Session

Explain the edge cloud in simple terms | 2. architecture

Here comes the problem! Unplug the network cable for a few seconds and plug it back in. Does the original TCP connection still exist?

AG. DS binary tree -- hierarchical traversal
随机推荐
Performance test - analyze requirements
Research Summary / programming FAQs
DDD based on ABP -- Entity creation and update
Similarities and differences between nor flash and NAND flash
mysql
Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
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?
使用 Fiddler Hook 报错:502 Fiddler - Connection Failed
(09) flask is OK if it has hands - cookies and sessions
Learning rate adjustment strategy in deep learning (1)
DS graph - minimum spanning tree
Problem handling of repeated restart during Siemens botu installation
“00后”来了!数睿数据迎来新生代「无代码」生力军
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
Kotlin class and inheritance
spark学习笔记(三)——sparkcore基础知识
新手第一次怎么买股票 哪家证券公司开户最好最安全
Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
How do novices buy stocks for the first time? Which securities company is the best and safest to open an account