类的练习:模拟电子秒表

类的练习:模拟电子秒表



题目:

在这里插入图片描述


代码:

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值