C語言實現(xiàn)基于控制臺的電子時鐘
更新時間:2022年05月02日 11:13:16 作者:碼來的小朋友
這篇文章主要為大家詳細介紹了C語言實現(xiàn)基于控制臺的電子時鐘,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
使用c語言制作一個控制臺的電子時鐘,供大家參考,具體內(nèi)容如下
學(xué)習(xí)了c語言基本語法后,在學(xué)習(xí)了time.h的庫文件,讓我產(chǎn)生了想制作一款電子時鐘的念頭,那好就開始動手操作吧。
使用到下面這些技術(shù):
首先必須先導(dǎo)入庫
/***************** 實時數(shù)字時鐘(和計算機系統(tǒng)時間關(guān)聯(lián)) **************
#include <time.h> ?-- 必須的時間函數(shù)頭文件
time_t -- 時間類型(time.h 定義)
struct tm -- 時間結(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); -- 獲取時間,以秒計,從1970年1月一日起算,存于rawtime
?? ??? ??? ??? ?-- 獲取到當前的秒數(shù),參數(shù)為0則函數(shù)返回值即為結(jié)果
localtime(&rawtime); -- 轉(zhuǎn)為當?shù)貢r間,tm 時間結(jié)構(gòu)
system("cls");--命令行清屏獲取坐標的代碼如下
#include <windows.h>
void gotoxy(int x,int y)?? ?//光標定位函數(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) ? //光標定位函數(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個位置 分別是小時的個位十位,分鐘的個位十位和秒鐘的個位十位?
int x=8;
?? ??? ?if(wei=='1'){
?? ??? ??? ?draw_numb(x*1,shu);?? ?//這里調(diào)用了 draw_numb函數(shù)吧x*1是橫坐標(也表示第幾個位置數(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; ? ? //時間類型變量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);?? ?//獲取到當前的秒數(shù),參數(shù)為0則函數(shù)返回值即為結(jié)果
? ? ? ? curtime=localtime(&t);?? ?//得到當前系統(tǒng)時間/
? ? ? ? if((double)curtime->tm_hour<=12) ?//午前的處理/
?? ??? ?{?? ?gotoxy(5,3);
?? ??? ??? ?
? ? ? ? ? ? printf("AM ");
? ? ? ? ? ? //if((double)curtime->tm_hour<10) draws('1',0); ? //十點之前在小時數(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); ?//等待延時1000ms
? ? ? ? system("cls");
? ? }while(!kbhit());?? ?//按任一鍵退出 ??
? ? return 0;}最后運行截圖(完美運行)

是不是滿滿的成就感! 好了今天就分享到這了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
QT quick-Popup彈出窗口自定義的實現(xiàn)
本文主要介紹了QT quick-Popup彈出窗口自定義的實現(xiàn),文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
VC++實現(xiàn)View內(nèi)容保存為圖片的方法
這篇文章主要介紹了VC++實現(xiàn)View內(nèi)容保存為圖片的方法,涉及VC++中Bitmap類的save方法相關(guān)使用技巧,需要的朋友可以參考下2016-08-08
詳談C++何時需要定義賦值/復(fù)制構(gòu)造函數(shù)
下面小編就為大家?guī)硪黄斦凜++何時需要定義賦值/復(fù)制構(gòu)造函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01

