当前位置:网站首页>Pta:6-71 clock simulation

Pta:6-71 clock simulation

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

One Time class , Data members sometimes 、 branch 、 second . Request analog stopwatch , One second at a time , full 60 Second carry , The second starts counting from zero . full 60 Fractional carry , Minutes are counted from zero . When the output 、 The value of minutes and seconds .( Use overload ++ Operator implementation )

Time class definition :
class MyTime
Sample test procedure :

/*  Please fill in the answer here  */

int main()
{
    
	MyTime t1,t2(23,59,59),t3;
	cin>>t3;
	++t1;
	cout<<t1<<endl;
	++t2;
	cout<<t2<<endl;
	++t3;
	cout<<t3<<endl;
	return 0;
}

sample input :
12 35 59
sample output :
0:0:1
0:0:0
12:36:0

The main contents are : Constructors 、 heavy load ++ And input / output operators 、this The pointer

#include<iostream>
using namespace std;
class MyTime
{
    
	int hour;
	int mintue;
	int second;
	public:
		MyTime()
		{
    
			hour=0;
			mintue=0;
			second=0;
		}
		MyTime(int a,int b,int c)
		{
    
			hour=a;
			mintue=b;
			second=c;
		}
		MyTime operator ++()
		{
    
			this->second++;
			if(this->second>=60)
			{
    
				this->second=0;
				this->mintue++;
			}
			if(this->mintue>=60)
			{
    
				this->mintue=0;
				this->hour++;
			}
			if(this->hour>=24)
			{
    
				this->hour=0;
			}
		}
		friend istream& operator >>(istream&in,MyTime &t)
		{
    
			in>>t.hour>>t.mintue>>t.second;
			return in;
		}
		friend ostream& operator <<(ostream&os,MyTime &t)
		{
    
			os<<t.hour<<":"<<t.mintue<<":"<<t.second;
		}
};

原网站

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