当前位置:网站首页>Pta:6-30 time addition

Pta:6-30 time addition

2022-06-23 04:38:00 Sy_ Faker

Design a time class , When used to save 、 branch 、 Seconds and other private data members , By overloading operators “+” Realization 2 The sum of times . requirement : (1) The time range of hours is limited to greater than or equal to 0;(2) The time range of minutes is 0-59 branch ;(3) The time range of seconds is 0-59 second .

#include <iostream>
using namespace std;
class Time {
    
private:
 int hours,minutes, seconds;
public:
 Time(int h=0, int m=0, int s=0);
 Time operator + (Time &);
 void DispTime();
};

/*  Please fill in the answer here  */

int main() {
    
 Time tm1(8,75,50),tm2(0,6,16), tm3;
 tm3=tm1+tm2;
 tm3.DispTime();
 return 0;
}

Output :
Here is the corresponding output . for example :

9h:22m:6s
Code up :

Time::Time(int h,int m,int s)
{
    
	hours=h;
	minutes=m;
	seconds=s;
}
void Time::DispTime()
{
    
	cout<<hours<<"h:"<<minutes<<"m:"<<seconds<<"s"<<endl;
}
Time Time::operator +(Time & k)
{
    
	Time temp;
	temp.minutes=this->minutes+k.minutes;
	temp.seconds=this->seconds+k.seconds;
	temp.hours=this->hours+k.hours;
	if(temp.seconds>59)
	{
    
		temp.seconds-=60;
		temp.minutes++;
	}
	if(temp.minutes>59)
	{
    
		temp.minutes-=60;
		temp.hours++;
	}
	return temp;
}
原网站

版权声明
本文为[Sy_ Faker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222259539176.html