C++趣味编程【时钟】

本文介绍了使用C++编写的简单时钟程序,可通过键盘进行模式切换和时间操作,展示了基本的控制台输入输出和键盘事件处理。

 今天太无聊了没事干

So,索性就用C++编了一个时钟的小程序(还是十分简单)

这里我参考了某位大佬的文章,十分感谢。

代码实现:

 

(小小蒟蒻,代码可能有点繁琐,请谅解)(可能有些小BUG) 

// 左右键切换模式  A,D键选单位  W,S键改数值 
#define _CRT_SECURE_NO_WARNINGS
#define VK_l 0x25	//左 
#define VK_r 0x27   //右 
#define VK_x 0x53	//下
#define VK_u 0x57   //上
#define VK_D 0x44   //D
#define VK_A 0x41   //A  
#define VK_Enter 0x0D  //Enter 
#define VK_s 0x20   //空格
#include<windows.h>
#include<bits/stdc++.h>
using namespace std;
int w=1,s,minn,hour,tway=1,timerj;
bool tonoff;
bool num[16][7][8]={{{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}}  ,  {{0,0,1,0,0},{0,1,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,1,1,1,0}}  ,  {{0,1,1,1,0},{1,0,0,0,1},{0,0,0,0,1},{0,0,0,1,0},{0,0,1,0,0},{0,1,0,0,0},{1,1,1,1,1}}  ,  {{1,1,1,1,1},{0,0,0,1,0},{0,0,1,0,0},{0,0,0,1,0},{0,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}}  ,  {{0,0,0,1,0},{0,0,1,1,0},{0,1,0,1,0},{1,0,0,1,0},{1,1,1,1,1},{0,0,0,1,0},{0,0,0,1,0}}  ,  {{1,1,1,1,1},{1,0,0,0,0},{1,1,1,1,0},{0,0,0,0,1},{0,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}}  ,  {{0,0,1,1,0},{0,1,0,0,0},{1,0,0,0,0},{1,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}}  ,  {{1,1,1,1,1},{0,0,0,0,1},{0,0,0,1,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,1,0,0}}  ,  {{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}}  ,  {{0,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,1},{0,0,0,0,1},{0,0,0,1,0},{0,1,1,0,0}}  ,  {{0,0,0,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,0,0,0},{0,0,1,0,0},{0,0,1,0,0},{0,0,0,0,0}}  ,  {{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1}}  ,  {{0,0,1,0,0},{0,1,0,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{0,1,0,1,0},{0,0,1,0,0}}  ,  {{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{0,1,1,1,0}}  ,  {{1,0,0,0,1},{1,0,0,1,1},{1,0,0,1,1},{1,0,1,0,1},{1,1,0,0,1},{1,1,0,0,1},{1,0,0,0,1}}  ,  {{1,1,1,1,0},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,0,0,0,1},{1,1,1,1,0}}};
void go(int x,int y){		//控制光标位置 
    HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
    COORD c={x,y};
    SetConsoleCursorPosition(hOut,c);
}
void show(int number,int x,int y,int t){
	go(x,y);
	for(int i=0;i<7;i++){
		for(int j=0;j<5;j++){
			x++;
			if(num[number][i][j]==1){
				cout<<"■";
			}
			else{
				cout<<"  ";
			}
		}
		x-=5; y++;
		go(x,y);
		Sleep(t);
	}
}
void winSize(int lines_height,int cols_width){		//控制屏幕 
    char cmd[100];
    sprintf(cmd,"mode con cols=%d lines=%d",lines_height,cols_width);
    system(cmd);
}
void no(){		//隐藏光标 
    CONSOLE_CURSOR_INFO cursor_info={1,0}; 
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}
void square(int x,int y,bool condition){  //方框 
	for(int i=1;i<=26;i+=2){
		go(i+x-1,y);
		if(condition==1) cout<<"■";
		else cout<<"  ";
	}
	for(int i=1;i<=7;i++){
		go(x,y+i);
		if(condition==1) cout<<"■";
		else cout<<"  ";
		go(x+24,y+i);
		if(condition==1) cout<<"■";
		else cout<<"  ";
	}
	for(int i=1;i<=26;i+=2){
		go(i+x-1,y+8);
		if(condition==1) cout<<"■";
		else cout<<"  ";
	}
} 
void timenow(){     //当前时间 
	go(44,8);
	cout<<"当前时间";
	time_t nowtime;
	time(&nowtime);
	tm* p=localtime(&nowtime);
	show(p->tm_hour/10,0,10,0);
	show(p->tm_hour%10,12,10,0);
	show(10,24,10,0);
	show(p->tm_min/10,36,10,0);
	show(p->tm_min%10,48,10,0);
	show(10,60,10,0);
	show(p->tm_sec/10,72,10,0);
	show(p->tm_sec%10,84,10,0);
} 
void timer(){    //计时器 
	if(GetAsyncKeyState(VK_Enter)) square(tway*36-36,9,0),tonoff=1;
	if(tonoff==1){
		time_t nowtime;
		time(&nowtime);
		tm* p=localtime(&nowtime);
		if(timerj!=p->tm_sec){
			s--;
		}
		timerj=p->tm_sec;
		if(s==-1&&minn==0&&hour==0){
			tonoff=0;
			for(int i=1;i<=5;i++){
				Beep(500,500);
				Sleep(1000);
			}
			s=0;
		}
	}

	if(GetAsyncKeyState(VK_s)) tonoff=0,s=0,minn=0,hour=0;
	if(GetAsyncKeyState(VK_u)&&tonoff==0) (tway==3)?(s++):((tway==2)?(minn++):(hour++)),Sleep(100);
    if(GetAsyncKeyState(VK_x)&&tonoff==0) (tway==3)?(s--):((tway==2)?(minn--):(hour--)),Sleep(100);
    if(GetAsyncKeyState(VK_D)&&tonoff==0) square(tway*36-36,9,0),tway++,Sleep(100);
    if(GetAsyncKeyState(VK_A)&&tonoff==0) square(tway*36-36,9,0),tway--,Sleep(100);
    
    if(tway==0) tway=3; 
    if(tway==4) tway=1; 
    if(s==-1&&minn==0&&hour==0) s=59,hour=99,minn=59;
	if(s==-1&&(minn>0||hour>0)) s=59,minn--;
	if(s==60) minn++,s=0;
	if(minn==-1) hour--,minn=59;
	if(minn==60) hour++,minn=0;
	if(hour==100||hour==-1) minn=0,hour=0,s=0;
	if(tonoff==0) square(tway*36-36,9,1);
	
	go(44,8);
	cout<<" 计时器";
	show(hour/10,2,10,0);
	show(hour%10,14,10,0);
	show(10,26,10,0);
	show(minn/10,38,10,0);
	show(minn%10,50,10,0);
	show(10,62,10,0);
	show(s/10,74,10,0);
	show(s%10,86,10,0);
} 
void button(){		//键位判断 
    if(GetAsyncKeyState(VK_l)) system("cls"),w--,Sleep(100);
    if(GetAsyncKeyState(VK_r)) system("cls"),w++,Sleep(100);
    if(w==3) w=1;
    if(w==0) w=2;
}

int main(){
	winSize(98,27);
	no();
	for(int j=18,i=11;i<=15;i++,j+=12){
		show(i,j,10,100);
	}
	Sleep(1000);
	system("cls");
	Sleep(100);
	while(1){
		button();
		if(w==1) timenow();
		if(w==2) timer();
	}
	system("pause>nul");
	return 0;
}

有什么好的意见欢迎评论

程序清单 按以下步骤向视图类(CClockView)添加下列数据成员及成员函数。 (1) 添加表示年、月、日、时、分、秒的变量。 int year; int month; int day; int hour; int minute; int second; (2) 添加秒表的计数变量。 int watch; (3) 添加时钟的画笔及画刷变量。 CPen m_HouPen, m_MinPen, m_SecPen; // 各种针的画笔 CBrush m_MarkBrush; // 表盘标记的画刷 (4) 添加时钟控制变量。 CPoint m_Center; // 表的中心 double m_Radius; // 表的半径 CPoint m_Hour [2], m_OldHour [2]; // 时针当前及前一次位置 CPoint m_Minute [2], m_OldMin [2]; // 分针当前及前一次位置 CPoint m_Second [2], m_OldSec [2]; // 秒针当前及前一次位置 (5) 添加秒表的两个按钮位置变量。 CRect m_WatchStart; CRect m_WatchStop; (6) 添加两个函数,计算时钟各指针位置。 void SetClock (int hour, int minute, int second); CPoint GetPoint (int nLenth, int nValue); (7) 在视图类构造函数中增加初始化语句: CClockView::~CClockView() { //设定时间 year=2010; month=11; day=22; hour=0; minute=0; second=0; //设定画笔画刷 m_HouPen.CreatePen(PS_SOLID,5,RGB(255,0,0));//时针画笔 m_MinPen.CreatePen(PS_SOLID,3,RGB(0,0,250));//分针画笔 m_SecPen.CreatePen(PS_SOLID,1,RGB(0,0,0));//秒针画笔 m_MarkBrush.CreateSolidBrush(RGB(250,250,0)); //设定表芯位置 m_Center.x=222; m_Center.y=222; //设定时钟半径 m_Radius=222; //计算指针位置 SetClock(hour,minute,second); //设定秒表计数器及按钮位置 watch=0; m_WatchStart=CRect(480,310,560,340);//启动按钮 m_WatchStop=CRect(590,310,670,340);//停止按钮 } 编写指针位置函数SetClock和GETpOINT。 首先在ClockView.cpp文件头部下添加下面两行代码,以便进行数学计算。 #define PI 3.14159265258 #include"math.h" 然后添加下列代码: //计算个指针位置的函数 void CClockView::SetClock(int hour,int minute,int second) { hour=hour*5; hour=hour+minute/12; //保存时针原位置 m_OldHour[0]=m_Hour[0]; m_OldHour[1]=m_Hour[1];
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值