C語言實(shí)現(xiàn)基于控制臺的電子時(shí)鐘
使用c語言制作一個控制臺的電子時(shí)鐘,供大家參考,具體內(nèi)容如下
學(xué)習(xí)了c語言基本語法后,在學(xué)習(xí)了time.h的庫文件,讓我產(chǎn)生了想制作一款電子時(shí)鐘的念頭,那好就開始動手操作吧。
使用到下面這些技術(shù):
首先必須先導(dǎo)入庫
/***************** 實(shí)時(shí)數(shù)字時(shí)鐘(和計(jì)算機(jī)系統(tǒng)時(shí)間關(guān)聯(lián)) ************** #include <time.h> ?-- 必須的時(shí)間函數(shù)頭文件 time_t -- 時(shí)間類型(time.h 定義) struct tm -- 時(shí)間結(jié)構(gòu),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); -- 獲取時(shí)間,以秒計(jì),從1970年1月一日起算,存于rawtime ?? ??? ??? ??? ?-- 獲取到當(dāng)前的秒數(shù),參數(shù)為0則函數(shù)返回值即為結(jié)果 localtime(&rawtime); -- 轉(zhuǎn)為當(dāng)?shù)貢r(shí)間,tm 時(shí)間結(jié)構(gòu) system("cls");--命令行清屏
獲取坐標(biāo)的代碼如下
#include <windows.h> void gotoxy(int x,int y)?? ?//光標(biāo)定位函數(shù),需要包含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) ? //光標(biāo)定位函數(shù),需要包含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的數(shù)據(jù),通過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個位置 分別是小時(shí)的個位十位,分鐘的個位十位和秒鐘的個位十位? int x=8; ?? ??? ?if(wei=='1'){ ?? ??? ??? ?draw_numb(x*1,shu);?? ?//這里調(diào)用了 draw_numb函數(shù)吧x*1是橫坐標(biāo)(也表示第幾個位置數(shù)),shu是要顯示的數(shù)據(jù)調(diào)過去? ?? ??? ?}?? ?//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; ?? ?//結(jié)構(gòu)tm,結(jié)構(gòu)指針curtime,time.h中定義 ? ? time_t t; ? ? //時(shí)間類型變量t,time.h中定義 ? ? clock_t start;?? ?//結(jié)構(gòu)clock_t,結(jié)構(gòu)變量start,time.h中定義 ? ?? ? ? double th_hour,th_min,th_sec; ? ? do ? ? {?? ?dians(); ? ? ? ? t=time(0);?? ?//獲取到當(dāng)前的秒數(shù),參數(shù)為0則函數(shù)返回值即為結(jié)果 ? ? ? ? curtime=localtime(&t);?? ?//得到當(dāng)前系統(tǒng)時(shí)間/ ? ? ? ? if((double)curtime->tm_hour<=12) ?//午前的處理/ ?? ??? ?{?? ?gotoxy(5,3); ?? ??? ??? ? ? ? ? ? ? ? printf("AM "); ? ? ? ? ? ? //if((double)curtime->tm_hour<10) draws('1',0); ? //十點(diǎn)之前在小時(shí)數(shù)前加零 ? ? ? ? ? ? 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); ?//等待延時(shí)1000ms ? ? ? ? system("cls"); ? ? }while(!kbhit());?? ?//按任一鍵退出 ?? ? ? return 0;}
最后運(yùn)行截圖(完美運(yùn)行)
是不是滿滿的成就感! 好了今天就分享到這了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++集體數(shù)據(jù)交換實(shí)現(xiàn)示例講解
這篇文章主要介紹了C++集體數(shù)據(jù)交換實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11C語言中不定參數(shù)?...?的語法以及函數(shù)封裝
不定參數(shù)是指函數(shù)可以接收不確定個數(shù)的參數(shù),下面這篇文章主要給大家介紹了關(guān)于C語言中不定參數(shù)?...?的語法以及函數(shù)封裝的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01C語言 90后懷舊游戲超級瑪麗的實(shí)現(xiàn)流程
90后最風(fēng)靡的游戲是什么?第一個聯(lián)想到的肯定是插卡游戲機(jī)或者VCD加光盤運(yùn)行在電視機(jī)上的超級瑪麗了,它的經(jīng)典絕對可以排在第一位,長大后的我們今天來用C語言重溫一下2021-11-11C++實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11c++調(diào)用實(shí)現(xiàn)yolov5轉(zhuǎn)onnx介紹
大家好,本篇文章主要講的是c++調(diào)用實(shí)現(xiàn)yolov5轉(zhuǎn)onnx介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12