C++趣味编程【时钟2代】

时钟第2代他来了

更新内容:

  1. 加入了秒表功能
  2. 修复了已知问题

 下面是代码环节

// 左右键切换模式  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,watchj,ws_,wminn,whour;
bool tonoff,wonoff;
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,1,0,0,1},{1,1,0,0,1},{1,0,1,0,1},{1,0,0,1,1},{1,0,0,1,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 watch(){     //秒表 
	if(w==3)if(GetAsyncKeyState(VK_Enter)) (wonoff==0)?(wonoff=1):(wonoff=0),wonoff=1;
	if(w==3)if(GetAsyncKeyState(VK_s)) wonoff=0,wminn=0,whour=0,ws_=0,Sleep(500);
	if(s==60&&minn==59&&hour==99) s=59,wonoff=0;
	if(wonoff==1){
		time_t nowtime;
		time(&nowtime);
		tm* p=localtime(&nowtime);
		if(watchj!=p->tm_sec){
			ws_++;
		}
		watchj=p->tm_sec;
	}
	
	
	if(s==60&&wminn==59&&whour==99) ws_=59;
	if(s==60) wminn++,ws_=0;
	if(minn==60) whour++,wminn=0;
	
	if(w==3){
		go(44,8);
		cout<<"  秒表";
		show(whour/10,2,10,0);
		show(whour%10,14,10,0);
		show(10,26,10,0);
		show(wminn/10,38,10,0);
		show(wminn%10,50,10,0);
		show(10,62,10,0);
		show(ws_/10,74,10,0);
		show(ws_%10,86,10,0);
	}
	
} 
void timer(){    //计时器 
	if(w==2) 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;
			MessageBox(0,"TIME OUT!","TIMER",MB_ICONINFORMATION);
			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(w==2){
		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==4) w=1;
    if(w==0) w=3;
}

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();
		timer();
		watch();
	}
	system("pause>nul");
	return 0;
}

有什么好的意见欢迎评论

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值