温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用面向对象思想实现时钟C++描述

发布时间:2020-06-27 16:07:22 来源:网络 阅读:610 作者:liam2199 栏目:编程语言

用面向对象思想实现时钟C++描述的实例代码:

# include <iostream>
# include <time.h>
# include <iomanip>
# include <windows.h>
//# include <unistd.h>

using namespace std;

//初始化的数据来自系统,以后的逻辑运算及显示自实现
class Clock
{
public:
	Clock()
	{
		time_t  t = time(NULL);
		struct tm ti = *localtime(&t);

		hour = ti.tm_hour;
		min = ti.tm_min;
		sec = ti.tm_sec;
	}

	void run()
	{
		while (1)
		{
			system("cls");
			show(); //完成显示
			tick();//数据更新
		}
	}

private:
	void show()
	{
		//system("cls");
		cout << setw(2) << setfill('0') << hour << ":";
		cout << setw(2) << setfill('0') << min << ":";
		cout << setw(2) << setfill('0') << sec << ":";
	}
	void tick()
	{
		Sleep(1);
		if (++sec == 60)
		{
			sec = 0;
			min += 1;
			if (++min == 60)
			{
				min = 0;
				hour += 1;
				if (++hour == 24)
				{
					hour = 0;
				}
			}
		}
	}
	int hour = 0;
	int min = 0;
	int sec = 0;
};

int main(void)
{
	Clock c;
	c.run();

	cout << " Hello World " << endl;
	return 0;
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI