类的练习:模拟电子秒表
题目:
代码:
#include<iostream>
using namespace std;
#include<iomanip>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<windows.h> //不能写成cwindows,已经试过了
class Clock
{
public:
//Clock(int newh=0,int newm=0,int mews=0); 用17行函数和18行函数的结果是一样的,17行是构造函数而18行是普通接口
Clock();
~Clock();
void init(int newh,int newm,int news);
void run();
private:
int h,m,s;
};
/*
Clock::Clock(int newh,int newm,int news)
{
h=newh;
m=newm;
s=news;
}
*/
Clock::Clock()
{
}
Clock::~Clock()
{
}
void Clock::init(int newh,int newm,int news)
{
h=newh;
m=newm;
s=news;
}
void Clock::run()
{
while(1)
{
system("cls");
cout << setw(2) << setfill('0') << h << ':';
cout << setw(2) << setfill('0') << m << ':';
cout << setw(2) << setfill('0') << s ;
Sleep(1000); //以0.001秒为单位
++s;
if( s==60 )
{
s=0;
++m;
if( m==60 )
{
m=0;
++h;
if( h==23 )
{
h=0;
}
}
}
}
}
int main()
{
//ios_base::sync_with_stdio(0); 这道题关闭了流同步就没法用,不用在意这行
Clock c;
c.init(23,59,55);
c.run();
system("pause");
return 0;
}