t4的时间总是不对,但不知道该怎么改了,麻烦大家帮忙看下,感谢!!
当输入 0 0 0时,第一个上一秒是减了两秒/(ㄒoㄒ)/~~,其他时间都很正常
/*上一秒的时间++重载,时间 + 秒数 得到时间
样例1:
输入:
0 1 2
-63
输出:
00:01:02
00:01:01
00:01:00
23:59:57
样例2:
输入:
23 59 57
50
输出:
23:59:57
23:59:56
23:59:55
00:00:45*/
//StudybarCommentBegin
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
//StudybarCommentEnd
class Time
{
private:
int second;
int minute;
int hour;
public:
Time();
void setTime(int, int, int);
Time(int, int, int);
Time& operator--();
Time operator--(int );
Time operator-(Time &);
Time operator+(Time &);
friend Time operator-(int, Time&);
void printTime();
};
Time::Time(){}
void Time::setTime(int h, int m, int s)
{
second = s;
minute = m;
hour = h;
}
Time::Time(int h, int m, int s)
{
second = s;
minute = m;
hour = h;
}
Time & Time::operator --() //单目运算符重载前置
{
second --;
if(second < 0)
{
second += 59;
minute --;
if(minute < 0)
{
minute += 59;
if(hour<0)
hour = (hour + 23)%24;
}
}
return *this;
}
Time Time::operator--(int ) //单目运算符后置重载
{
Time old = *this;
--(*this);
return old;
}
Time Time::operator-(Time &t) //运算符重载-
{
int h,m,s;
s = second - t.second;
m = minute - t.minute;
h = hour - t.hour;
if (s < 0)
{
s += 59;
m --;
}
if (m < 0)
{
m += 59;
h --;
}
while (h < 0) h += 23;
Time t0(h, m, s);
return t0;
}
Time Time::operator+(Time &t)
{
int h,m,s;
s=second+t.second;
m=minute+t.minute;
h=hour+t.hour;
if (s>59)
{
s-=60;
m++;
}
if (m>59)
{
m-=60;
h++;
}
while (h>23) h-=24;
Time t0(h,m,s);
return t0;
}
Time operator+(int i,Time &c1)
{
int ss = i % 60;
int mm = (i/60) % 60;
int hh = i / 3600;
Time t0(hh, mm, ss);
if(i>0)
return c1 + t0 ;
if(i<0)
return t0 - c1;
}
void Time::printTime()
{
cout<<setfill('0')<<setw(2)<<hour
<<":"<<setw(2)<<minute<<":"
<<setw(2)<<second<<endl;
}
//StudybarCommentBegin
int main()
{
int hour,minute,second;
int number;
Time t1(23,45,0),t2,t3(t1),t4;
cin>>hour>>minute>>second>>number;
t1.setTime(hour,minute,second);
t2=t1--;
t2.printTime();
t1.printTime();
t3=--t1; //前置
t3.printTime();
t4=number+t1; //(int,Time)
t4.printTime();
}
//StudybarCommentEnd
输入情况:
这个结果的第4行怎么改都不对,麻烦大家帮忙看下原因
class Time
{
private:
int second;
int minute;
int hour;
public:
Time();
void setTime(int, int, int);
Time(int, int, int);
Time &operator--();
Time operator--(int);
Time operator-(Time &);
Time operator+(Time &);
friend Time operator-(int, Time &);
void printTime();
};
Time::Time() {}
void Time::setTime(int h, int m, int s)
{
second = s;
minute = m;
hour = h;
}
Time::Time(int h, int m, int s)
{
setTime(h, m, s);
}
Time &Time::operator--() //单目运算符重载前置
{
second--;
if (second < 0)
{
second += 60; // second += 59;
minute--;
if (minute < 0)
{
minute += 60; // minute += 59;
hour--; //
if (hour < 0)
hour = (hour % 24 + 24); // hour = (hour + 23)%24;
}
}
return *this;
}
Time Time::operator--(int) //单目运算符后置重载
{
Time old = *this;
--(*this);
return old;
}
Time Time::operator-(Time &t) //运算符重载-
{
int h, m, s;
s = second - t.second;
m = minute - t.minute;
h = hour - t.hour;
if (s < 0)
{
s += 60; // s += 59;
m--;
}
if (m < 0)
{
m += 60; // m += 59;
h--;
}
// while (h < 0)
// h += 24;
if (h < 0)
{
h %= 24;
h += 24;
}
Time t0(h, m, s);
return t0;
}
Time Time::operator+(Time &t)
{
int h, m, s;
s = second + t.second;
m = minute + t.minute;
h = hour + t.hour;
if (s > 59)
{
s -= 60;
m++;
}
if (m > 59)
{
m -= 60;
h++;
}
// while (h > 23)
// h -= 24;
if (h > 23)
{
h %= 24;
}
Time t0(h, m, s);
return t0;
}
Time operator+(int i, Time &c1)
{
bool gtr0 = i > 0;
if (!gtr0)
i = -i;
int ss = i % 60;
int mm = (i / 60) % 60;
int hh = i / 3600;
Time t0(hh, mm, ss);
if (gtr0)
return c1 + t0;
else
return c1 - t0;
}
void Time::printTime()
{
cout << setfill('0') << setw(2) << hour
<< ":" << setw(2) << minute << ":"
<< setw(2) << second << endl;
}
// StudybarCommentBegin
int main()
{
int hour, minute, second;
int number;
Time t1(23, 45, 0), t2, t3(t1), t4;
cin >> hour >> minute >> second >> number;
t1.setTime(hour, minute, second);
t1.printTime();
t1--;
t2 = t1; // t2 = t1--;
t2.printTime();
t3 = --t1; //前置
t3.printTime();
t4 = number + t1; //(int,Time)
t4.printTime();
system("pause");
}