C語(yǔ)言實(shí)現(xiàn)基于控制臺(tái)的電子時(shí)鐘
使用c語(yǔ)言制作一個(gè)控制臺(tái)的電子時(shí)鐘,供大家參考,具體內(nèi)容如下
學(xué)習(xí)了c語(yǔ)言基本語(yǔ)法后,在學(xué)習(xí)了time.h的庫(kù)文件,讓我產(chǎn)生了想制作一款電子時(shí)鐘的念頭,那好就開(kāi)始動(dòng)手操作吧。
使用到下面這些技術(shù):
首先必須先導(dǎo)入庫(kù)
/***************** 實(shí)時(shí)數(shù)字時(shí)鐘(和計(jì)算機(jī)系統(tǒng)時(shí)間關(guān)聯(lián)) **************
#include <time.h> ?-- 必須的時(shí)間函數(shù)頭文件
time_t -- 時(shí)間類(lèi)型(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ù),通過(guò)gotoxy顯示出來(lái)?
?? ?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個(gè)位置 分別是小時(shí)的個(gè)位十位,分鐘的個(gè)位十位和秒鐘的個(gè)位十位?
int x=8;
?? ??? ?if(wei=='1'){
?? ??? ??? ?draw_numb(x*1,shu);?? ?//這里調(diào)用了 draw_numb函數(shù)吧x*1是橫坐標(biāo)(也表示第幾個(gè)位置數(shù)),shu是要顯示的數(shù)據(jù)調(diào)過(guò)去?
?? ??? ?}?? ?//x*1表示第一個(gè)位置?
?? ??? ?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í)間類(lèi)型變量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)行)

是不是滿(mǎn)滿(mǎn)的成就感! 好了今天就分享到這了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++集體數(shù)據(jù)交換實(shí)現(xiàn)示例講解
這篇文章主要介紹了C++集體數(shù)據(jù)交換實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-11-11
淺談C++中對(duì)象的復(fù)制與對(duì)象之間的相互賦值
這篇文章主要介紹了淺談C++中對(duì)象的復(fù)制與對(duì)象之間的相互賦值,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
利用c++寫(xiě)一個(gè)簡(jiǎn)單的推箱子小游戲
推箱子想必是很多人童年時(shí)期的經(jīng)典游戲,我們依舊能記得抱個(gè)老人機(jī)娛樂(lè)的場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于如何利用c++寫(xiě)一個(gè)簡(jiǎn)單的推箱子小游戲的相關(guān)資料,需要的朋友可以參考下2021-09-09
C語(yǔ)言中不定參數(shù)?...?的語(yǔ)法以及函數(shù)封裝
不定參數(shù)是指函數(shù)可以接收不確定個(gè)數(shù)的參數(shù),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中不定參數(shù)?...?的語(yǔ)法以及函數(shù)封裝的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
C語(yǔ)言 90后懷舊游戲超級(jí)瑪麗的實(shí)現(xiàn)流程
90后最風(fēng)靡的游戲是什么?第一個(gè)聯(lián)想到的肯定是插卡游戲機(jī)或者VCD加光盤(pán)運(yùn)行在電視機(jī)上的超級(jí)瑪麗了,它的經(jīng)典絕對(duì)可以排在第一位,長(zhǎng)大后的我們今天來(lái)用C語(yǔ)言重溫一下2021-11-11
C++實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
c++調(diào)用實(shí)現(xiàn)yolov5轉(zhuǎn)onnx介紹
大家好,本篇文章主要講的是c++調(diào)用實(shí)現(xiàn)yolov5轉(zhuǎn)onnx介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2021-12-12

