C语言实现基于控制台的电子时钟

使用c语言制作一个控制台的电子时钟,供大家参考,具体内容如下
学习了c语言基本语法后,在学习了time.h的库文件,让我产生了想制作一款电子时钟的念头,那好就开始动手操作吧。
使用到下面这些技术:
【C语言实现基于控制台的电子时钟】首先必须先导入库

/***************** 实时数字时钟(和计算机系统时间关联) **************#include -- 必须的时间函数头文件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 void gotoxy(int x,int y)//光标定位函数,需要包含windos.h头文件{COORD coord; coord.X=x; coord.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); }

源代码:
#include #include #include #include #include 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); //等待延时1000mssystem("cls"); }while(!kbhit()); //按任一键退出return 0; }

最后运行截图(完美运行)
C语言实现基于控制台的电子时钟
文章图片

是不是满满的成就感! 好了今天就分享到这了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读