Problem F: 时间类的加、减法赋值运算
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 3806 Solved: 2214
[ Submit][ Status][ Web Board]
Description
Input
Output
Sample Input
Sample Output
HINT
Append Code
#include<iostream>
#include <iomanip>
using namespace std;
class Time
{
private:
int h,m,s;
int k;
public:
Time():h(0),m(0),s(0){}
Time(int a,int b,int c):h(a),m(b),s(c){}
Time(const Time &t):h(t.h),m(t.m),s(t.s){}
~Time(){}
int hour(){return h;}
int minute(){return m;}
int second(){return s;}
void hour(int a){h = a;}
void minute(int b){m = b;}
void second(int c){s = c;}
Time& setTime(int a,int b,int c){h = a;m = b;s = c;return *this;}
Time& setTime(const Time&t){h = t.h;m = t.m;s = t.s;return *this;}
Time& getTime(){return *this;}///
Time& inputTime(){cin>>h>>m>>s;return *this;}
void showTime()
{
if(h*3600+m*60+s>=24*3600||m>59||s>59||h<0||s<0||m<0)
cout<<"Time error"<<endl;
else cout<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<setfill('0')<<m<<":"<<setw(2)<<setfill('0')<<s<<endl;
}
Time& operator +=(int t)
{
if(h*3600 + m*60 + s >= 24*3600|| m>59||s>59||h<0||s<0||m<0)
return *this;
else
{
k = (h*3600 + m*60 + s+t)%(24*3600);
h = k/3600;
m = (k -h*3600)/60;
s = k-h*3600-m*60;
return *this;
}
}
Time& operator -=(int t)
{
if(h*3600 + m*60 + s >= 24*3600|| m>59||s>59||h<0||s<0||m<0)
return *this;
else
{
k = (h*3600 + m*60 + s-t+24*3600)%(24*3600);
h = k/3600;
m = (k -h*3600)/60;
s = k-h*3600-m*60;
return *this;
}
}
};
int main()
{
int cases;
cin>>cases;
for(int i = 1; i <= cases; ++i)
{
Time t;
t.inputTime();
Time tt(t);
int num;
cin>>num;
t += num;
t.showTime();
tt -= num;
tt.showTime();
}
}
1493




被折叠的 条评论
为什么被折叠?



