这篇文章主要介绍“C语言怎么实现基于控制台的电子时钟”,在日常操作中,相信很多人在C语言怎么实现基于控制台的电子时钟问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言怎么实现基于控制台的电子时钟”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
使用到下面这些技术:
首先必须先导入库
/***************** 实时数字时钟(和计算机系统时间关联) ************** #include <time.h> -- 必须的时间函数头文件 time_t -- 时间类型(time.h 定义) struct tm -- 时间结构,time.h 定义如下:(依需求选用) int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; time(&rawtime); -- 获取时间,以秒计,从1970年1月一日起算,存于rawtime -- 获取到当前的秒数,参数为0则函数返回值即为结果 localtime(&rawtime); -- 转为当地时间,tm 时间结构 system("cls");--命令行清屏
获取坐标的代码如下
#include <windows.h> void gotoxy(int x,int y) //光标定位函数,需要包含windos.h头文件 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); }
源代码:
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <conio.h> #include <windows.h> void gotoxy(int x,int y) //光标定位函数,需要包含windos.h头文件 { COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); } void dians(){ int x=8; gotoxy(x*3,8); printf(" **"); gotoxy(x*3,9); printf(" **"); gotoxy(x*3,11); printf(" **"); gotoxy(x*3,12); printf(" **"); gotoxy(x*6,8); printf(" **"); gotoxy(x*6,9); printf(" **"); gotoxy(x*6,11); printf(" **"); gotoxy(x*6,12); printf(" **"); } void draw_numb(int x,int shu){ //判断0-9的数据,通过gotoxy显示出来 if(shu==0){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf("* *"); gotoxy(x,8); printf("* *"); gotoxy(x,9); printf("* *"); gotoxy(x,10); printf("* *"); gotoxy(x,11); printf("* *"); gotoxy(x,12); printf("* *"); gotoxy(x,13); printf("* *"); gotoxy(x,14); printf("*****"); } if(shu==1){ gotoxy(x,6); printf(" * "); gotoxy(x,7); printf(" * "); gotoxy(x,8); printf(" * "); gotoxy(x,9); printf(" * "); gotoxy(x,10); printf(" * "); gotoxy(x,11); printf(" * "); gotoxy(x,12); printf(" * "); gotoxy(x,13); printf(" * "); gotoxy(x,14); printf(" * "); } if(shu==2){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf(" *"); gotoxy(x,8); printf(" *"); gotoxy(x,9); printf(" *"); gotoxy(x,10); printf("*****"); gotoxy(x,11); printf("* "); gotoxy(x,12); printf("* "); gotoxy(x,13); printf("* "); gotoxy(x,14); printf("*****"); } if(shu==3){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf(" *"); gotoxy(x,8); printf(" *"); gotoxy(x,9); printf(" *"); gotoxy(x,10); printf("*****"); gotoxy(x,11); printf(" *"); gotoxy(x,12); printf(" *"); gotoxy(x,13); printf(" *"); gotoxy(x,14); printf("*****"); } if(shu==4){ gotoxy(x,6); printf("* *"); gotoxy(x,7); printf("* *"); gotoxy(x,8); printf("* *"); gotoxy(x,9); printf("* *"); gotoxy(x,10); printf("*****"); gotoxy(x,11); printf(" *"); gotoxy(x,12); printf(" *"); gotoxy(x,13); printf(" *"); gotoxy(x,14); printf(" *"); } if(shu==5){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf("* "); gotoxy(x,8); printf("* "); gotoxy(x,9); printf("* "); gotoxy(x,10); printf("*****"); gotoxy(x,11); printf(" *"); gotoxy(x,12); printf(" *"); gotoxy(x,13); printf(" *"); gotoxy(x,14); printf("*****"); } if(shu==6){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf("* "); gotoxy(x,8); printf("* "); gotoxy(x,9); printf("* "); gotoxy(x,10); printf("*****"); gotoxy(x,11); printf("* *"); gotoxy(x,12); printf("* *"); gotoxy(x,13); printf("* *"); gotoxy(x,14); printf("*****"); } if(shu==7){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf(" *"); gotoxy(x,8); printf(" *"); gotoxy(x,9); printf(" *"); gotoxy(x,10); printf(" *"); gotoxy(x,11); printf(" *"); gotoxy(x,12); printf(" *"); gotoxy(x,13); printf(" *"); gotoxy(x,14); printf(" *"); } if(shu==8){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf("* *"); gotoxy(x,8); printf("* *"); gotoxy(x,9); printf("* *"); gotoxy(x,10); printf("*****"); gotoxy(x,11); printf("* *"); gotoxy(x,12); printf("* *"); gotoxy(x,13); printf("* *"); gotoxy(x,14); printf("*****"); } if(shu==9){ gotoxy(x,6); printf("*****"); gotoxy(x,7); printf("* *"); gotoxy(x,8); printf("* *"); gotoxy(x,9); printf("* *"); gotoxy(x,10); printf("*****"); gotoxy(x,11); printf(" *"); gotoxy(x,12); printf(" *"); gotoxy(x,13); printf(" *"); gotoxy(x,14); printf("*****"); } } void draws(char wei,int shu){//这里定义了6个位置 分别是小时的个位十位,分钟的个位十位和秒钟的个位十位 int x=8; if(wei=='1'){ draw_numb(x*1,shu); //这里调用了 draw_numb函数吧x*1是横坐标(也表示第几个位置数),shu是要显示的数据调过去 } //x*1表示第一个位置 if(wei=='2'){ draw_numb(x*2,shu); } if(wei=='3'){ draw_numb(x*4,shu); } if(wei=='4'){ draw_numb(x*5,shu); } if(wei=='5'){ draw_numb(x*7,shu); } if(wei=='6'){ draw_numb(x*8,shu); } } int main() { system("color 1b"); struct tm *curtime; //结构tm,结构指针curtime,time.h中定义 time_t t; //时间类型变量t,time.h中定义 clock_t start; //结构clock_t,结构变量start,time.h中定义 double th_hour,th_min,th_sec; do { dians(); t=time(0); //获取到当前的秒数,参数为0则函数返回值即为结果 curtime=localtime(&t); //得到当前系统时间/ if((double)curtime->tm_hour<=12) //午前的处理/ { gotoxy(5,3); printf("AM "); //if((double)curtime->tm_hour<10) draws('1',0); //十点之前在小时数前加零 draws('1',((int)curtime->tm_hour)/10); draws('2',((int)((double)curtime->tm_hour))%10); } else //午后的处理 { gotoxy(5,3); printf("PM "); //if((double)curtime->tm_hour-12<10) draws('1',0);//输入0 draws('1',(int)curtime->tm_hour/10); draws('2',((int)((double)curtime->tm_hour))%10); } if((double)curtime->tm_min<10) draws('3',0); draws('3',(int)curtime->tm_min/10); draws('4',(int)curtime->tm_min%10); if((double)curtime->tm_sec<10) draws('5',0); draws('5',(int)curtime->tm_sec/10); draws('6',(int)curtime->tm_sec%10); start=clock(); while(clock()-start<500); //等待延时1000ms system("cls"); }while(!kbhit()); //按任一键退出 return 0;}
最后运行截图(完美运行)
到此,关于“C语言怎么实现基于控制台的电子时钟”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。