c++动态时钟问题 为什么不能正确显示?
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
#include "dos.h"
using namespace std;
class Clock //定义一个Clock类 里面有时、分、秒
{
public:
Clock(int a = 0, int b = 0, int c = 0)//定义Clock类构造函数
{
Hour = a;
Minute = b;
Second = c;
}
friend Clock operator+(Clock&t1, Clock&t2);//声明运算符+重载函数
void ShowTime();//
private:
int Hour; //时
int Minute;//分
int Second;//秒
};
void Clock::ShowTime()//Clock类函数 输出时分秒
{
cout << Hour << ":" << Minute << ":" << Second << endl;
}
Clock operator+(Clock &t1, Clock&t2)//定义Clock类运算符+重载函数
{
Clock t3;
t3.Second = t1.Second + t2.Second;
if (t3.Second >= 60)//如果秒大于或等于60秒则结果是取除于60的余数
{
t3.Second = t3.Second - 60;
t3.Minute = t1.Minute + t2.Minute + 1;
if (t3.Minute >= 60)//如果分大于60则减去模60
{
t3.Minute = t3.Minute - 60;
t3.Hour = t1.Hour + t2.Hour + 1;
}
if (t3.Hour >= 24)//如果时大于60则减去模24
{
t3.Hour = t3.Hour - 24;
}
}
else//如果秒没有大于或等于60秒
{
t3.Minute = t1.Minute + t2.Minute;
t3.Hour = t1.Hour + t2.Hour + 1;
if (t3.Minute >= 60)//如果分大于60则减去模60
{
t3.Minute = t3.Minute - 60;
t3.Hour = t1.Hour + t2.Hour + 1;
}
if (t3.Hour >= 24)//如果时大于60则减去模24
{
t3.Hour = t3.Hour - 24;
}
}
return t3;
}
void main()
{
Clock t1(8, 30, 6), t2(0, 0, 1), t3;//定义3个Clock类对象
t3 = t1 + t2;
t3.ShowTime();//输出时分秒
Sleep(1000);
system("cls");
while (1)
{
t3 = t3 + t1;
t3.ShowTime();//输出时分秒
Sleep(1000);
system("cls");
}
system("pause");
}