Time difference
Description
defines a class Time, which contains three attributes: hour, minute, and second. Define its constructor Time(int, int, int) to initialize the hour, minute, and second respectively. Overloaded subtraction operator, used to find the number of seconds (non-negative integer) between two times.
Input
has 2 lines. Each line represents 1 time, including three values of hour, minute, and second. The input is a legal 24-hour time. See the example for
Output
.
Sample Input
12 10 10
10 20 20
Sample Output
Deference is 6590 seconds.
Title given code
int main()
{
int a, b, c;
cin>>a>>b>>c;
Time t1(a, b, c);
cin>>a>>b>>c;
Time t2(a, b, c);
cout<<"Deference is "<<(t2 - t1)<<" seconds."<<endl;
return 0;
}
code:
#include<iostream>
#include<math.h>
using namespace std;
class Time{
int hh;//小时
int mm;//分钟
int ss;//秒
public:
Time(int h,int m,int s){
//构造函数
hh=h;
mm=m;
ss=s;
}
friend int operator -(const Time &t1,const Time &t2){
int time1=t1.hh*3600+t1.mm*60+t1.ss;
int time2=t2.hh*3600+t2.mm*60+t2.ss;
return abs(time1-time2);//注意返回非负整数
}
};
int main()
{
int a, b, c;
cin>>a>>b>>c;
Time t1(a, b, c);
cin>>a>>b>>c;
Time t2(a, b, c);
cout<<"Deference is "<<(t2 - t1)<<" seconds."<<endl;
return 0;
}