我只是想在尽我的能力写一个关于秒表的程序,或许只是我的一厢情愿导致这个表只为我而走,我像LOL中的“时光老头”活在自己的时间里。
#include <iostream>
#include <iomanip>
using namespace std;
class Clock
{
public:
Clock(int hour = 0, int minute = 0, int second = 0 ):m_hour(hour), m_minute(minute),m_second(second)
{
};
friend ostream& operator<<(ostream& out, const Clock& clock);
Clock& operator=(const Clock& clock);
Clock& operator++();
Clock operator++(int);
public:
int m_hour;
int m_minute;
int m_second;
};
ostream& operator<<(ostream& out, Clock &clock)
{
out<<setw(2)<<setfill('0')<<clock.m_hour<<":"<<setw(2)<<setfill('0')
<<clock.m_minute<<":"<<setw(2)<<setfill('0')<<clock.m_second<<'\r';
return out;
}
Clock& Clock::operator=(const Clock& clock)
{
m_hour = clock.m_hour;
m_minute = clock.m_minute;
m_second = clock.m_second;
return *this;
}
Clock& Clock::operator++(){
m_second++; //新增数据成员Second
if (60 == m_second) {
m_second = 0;
m_minute++;
if (60 == m_minute) {
m_minute = 0;
m_hour++;
if (24 == m_hour) {
m_hour = 0;
}
}
}
return *this;
}
Clock Clock::operator++(int) // clock++
{
Clock c = *this;
++(*this); //调用前++ (*this).operator++()
return c;
}
int sleep()
{
for ( int i = 0; i< 1000; i++)
{
for (int j = 0; j < 1000; j++)
{
for( int k = 0; k < 3 ; k++ )
{
int ncount = 0;
}
}
}
return 0;
}
int main()
{
char ch = 'n';
cout<<"是否启动时钟?";
cin>>ch;
if(ch == 'y')
{
Clock c1 ;
for(int count = 0; count < 7200; count ++ )
{
cout<<(++c1);
sleep();
}
}
return 0;
}
从我的时间中,你学到了什么,是不是可以将我的时间拿走供你利用呢?加油吧,骚年!