文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-06-30 12:03

关注

这篇文章主要介绍“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语言怎么实现基于控制台的电子时钟

到此,关于“C语言怎么实现基于控制台的电子时钟”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯