本文目錄一覽:
c程序時鐘代碼誰有
#include reg52.h
#define uint unsigned int
sbit P3_0=P3^0;
sbit K1=P3^2;
sbit K2=P3^3;
sbit K3=P3^4;
sbit K4=P3^5;
uint count,min,hour,i,j=0;
uint code tab1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uint code tab2[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
uint code tab3[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0x00,0xff,0x00,0xff,
0xfe,0xfb,0xef,0xbf,0xfd,0xf7,0xdf,0x7f,0x7e,0x3c,0x18,0x00,0x81,
0xc3,0xe7,0xff,0xe7,0xdb,0xbd,0x7e,0xff,0x7e,0xbd,0xdb,0xe7,0xff,
0x00,0xff,0x00,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x80,
0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0x00,0xff,0x00,0xff,0xfe,0xfc,
0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,
0xff,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,0x00,0xff,0x00,0xff};
void adjust(void)
{
if(!K3) //分調整
{
for(i=0;i20000;i++);min++;
if(min==60)min=0;
}
if(!K4) //時調整
{
for(i=0;i20000;i++);hour++;
if(hour==24)hour=0;
}
}
void display(void)
{
P0=tab1[min%10];P2=0xf7;for(i=0;i5;i++);P2=0xff;//分個位顯示
P0=tab1[min/10];P2=0xfb;for(i=0;i5;i++);P2=0xff;//分十位顯示
P0=tab2[hour%10];P2=0xfd;for(i=0;i5;i++);P2=0xff;//時個位顯示
P0=tab1[hour/10];P2=0xfe;for(i=0;i5;i++);P2=0xff;//時十位顯示
}
void ring(void)
{
if(hour/10==0(hour%10=8hour%10=9))P3_0=0;//早上7:00到晚上7:00自動整點報時,其中13、14點不報時
if(hour/10==1(hour%10=0hour%10=2))P3_0=0;
if(hour/10==1(hour%10=5hour%10=9))P3_0=0;
}
void update(void)
{
if(count==1200)
{
count=0;min++;
if(min==60)
{
min=0;hour++;
if(hour==24)hour=0;
ring();
}
}
}
void main(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
EA=1;
ET0=1;
while(1)
{
if(count==120)P3_0=1;//報時六秒後自動關閉蜂鳴器
adjust();
display();
}
}
void timer0_rupt(void) interrupt 1 // 定時器0中斷
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count%10==0)
{
P1=tab3[j];
j++;
if(j99)j=0;
}
update();
}
用C語言做數字時鐘每走一秒響一次,求大神告訴源代碼
「響一次」需要牽涉到圖形編程中的音樂播放問題,需要自己下載圖形編程相關庫文件,具體實現請自己在TODO里添加播放音樂的代碼
數字時鐘的實現很簡單,運用time.h相關函數即可
#includestdio.h
#includestdlib.h
#includetime.h
time_t oldt=-1;
struct tm *p;
bool Printdate()
{
time_t t=time(NULL);
if(t!=oldt)
{
oldt=t;
p=localtime(t);
system(“cls”);
printf(“%d/%d/%d 周”,1900+p-tm_year,1+p-tm_mon,p-tm_mday,p-tm_hour,p-tm_min,p-tm_sec);
switch(p-tm_wday)
{
case 1:printf(“一”);break;
case 2:printf(“二”);break;
case 3:printf(“三”);break;
case 4:printf(“四”);break;
case 5:printf(“五”);break;
case 6:printf(“六”);break;
case 7:printf(“日”);break;
}
printf(” %d:%02d:%02d “, p-tm_hour, p-tm_min, p-tm_sec);
return 1;
}
return 0;
}
main()
{
while(1)
if(Printdate())
{
/*************TODO*************/
/**For example: printf(“\a”);**/
}
return 0;
}
c語言怎麼樣編寫一個時鐘程序
c語言時鐘程序代碼如下:
#includewindows.h
#includemath.h
#define ID_TIMER 1//計時器ID
#define TWOPI (2*3.14159)
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT(“Clock”);
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(wndclass))
{
MessageBox(NULL,TEXT(“This program requires Windows
T”),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT(“Analog Clock”),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(msg,NULL,0,0))
{
TranslateMessage(msg);
DispatchMessage(msg);
}
return msg.wParam;
}
void Setsotropic(HDC hdc,int cxClient,int cyClient)
{
SetMapMode(hdc,MM_ISOTROPIC);
SetWindowExtEx(hdc,1000,1000,NULL);
SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
}
void RotatePoint(POINT pt[],int iNum,int iAngle)
{
int i;
POINT ptTemp;
for(i=0;iiNum;i++)
{
ptTemp.x=(int)(pt[i].x*cos(TWOPI*iAngle/360)+pt[i].y*sin(TWOPI*iAngle/360));
ptTemp.y=(int)(pt[i].y*cos(TWOPI*iAngle/360)+pt[i].x*sin(TWOPI*iAngle/360));
pt[i]=ptTemp;
}
}
void DrawClock(HDC hdc)
{
int iAngle;
POINT pt[3];
for(iAngle=0;iAngle360;iAngle+=6)
{
pt[0].x=0;
pt[0].y=900;
RotatePoint(pt,1,iAngle);
pt[2].x=pt[2].y=iAngle%5?33:100;
pt[0].x-=pt[2].x/2;
pt[0].y-=pt[2].y/2;
pt[1].x=pt[0].x+pt[2].x;
pt[1].y=pt[0].y+pt[2].y;
SelectObject(hdc,GetStockObject(BLACK_BRUSH));
Ellipse(hdc,pt[0].x,pt[0].y,pt[1].x,pt[1].y );
}
}
void DrawHands(HDC hdc,SYSTEMTIME *pst,BOOL fChange)
{
static POINT pt[3][5]={0,-150,100,0,0,600,-100,0,0,-150, 0,-200,50,0,0,800,-50,0,0,-200, 0,0,0,0,0,0,0,0,0,800 };
int i,iAngle[3];
POINT ptTemp[3][5];
iAngle[0]=(pst-wHour*30)%360+pst-wMinute/2;
iAngle[1]=pst-wMinute*6;
iAngle[2]=pst-wSecond*6;
memcpy(ptTemp,pt,sizeof(pt));
for(i=fChange?0:2;i3;i++)
{
RotatePoint(ptTemp[i],5,iAngle[i]);
Polyline(hdc,ptTemp[i],5);
}
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int cxClient,cyClient;
static SYSTEMTIME stPrevious;
BOOL fChange;
HDC hdc;
PAINTSTRUCT ps;
SYSTEMTIME st;
switch(message)
{
case WM_CREATE:
SetTimer(hwnd,ID_TIMER,1000,NULL);
GetLocalTime(st);
stPrevious=st;
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_TIMER:
GetLocalTime(st);
fChange=st.wHour!=stPrevious.wHour||st.wMinute!=stPrevious.wMinute;
hdc=GetDC(hwnd);
Setsotropic(hdc,cxClient,cyClient);
SelectObject(hdc,GetStockObject(WHITE_PEN));
DrawHands(hdc,stPrevious,fChange);
SelectObject(hdc,GetStockObject(BLACK_PEN));
DrawHands(hdc,st,TRUE);
stPrevious=st;
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,ps);
Setsotropic(hdc,cxClient,cyClient);
DrawClock(hdc);
DrawHands(hdc,stPrevious,TRUE);
EndPaint(hwnd,ps);
return 0;
case WM_DESTROY:
KillTimer(hwnd,ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
C語言是一門通用計算機編程語言,應用廣泛。C語言的設計目標是提供一種能以簡易的方式編譯、處理低級存儲器、產生少量的機器碼以及不需要任何運行環境支持便能運行的編程語言。儘管C語言提供了許多低級處理的功能,但仍然保持著良好跨平台的特性,以一個標準規格寫出的C語言程序可在許多電腦平台上進行編譯,甚至包含一些嵌入式處理器(單片機或稱MCU)以及超級電腦等作業平台。
C語言是由UNIX的研製者丹尼斯·里奇(Dennis Ritchie)於1970年 由 肯·湯普遜(Ken Thompson)所研製出的B語言的基礎上發展和完善起來的。目前,C語言編譯器普遍存在於各種不同的操作系統中,例如UNIX、MS-DOS、Microsoft Windows及Linux等。C語言的設計影響了許多後來的編程語言,例如C++、Objective-C、Java、C#等。
C語言如何獲取電腦實時時間
需要利用C語言的時間函數time和localtime,具體說明如下:
一、函數介面介紹:
1、time函數。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結構體,一般為長整型。
這個函數會獲取當前時間,並返回。 如果參數__timer非空,會存儲相同值到__timer指向的內存中。
time函數返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒。
由於是秒作為單位的,所以這並不是習慣上的時間,要轉為習慣上的年月日時間形式就需要另外一個函數了。
2、localtime函數。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個結構體,包含了年月日時分秒等信息。
這種結構是適合用來輸出的。
二、參考代碼:
#include stdio.h
#include time.h
int main ()
{
time_t t;
struct tm * lt;
time (t);//獲取Unix時間戳。
lt = localtime (t);//轉為時間結構。
printf ( “%d/%d/%d %d:%d:%d\n”,lt-tm_year+1900, lt-tm_mon, lt-tm_mday, lt-tm_hour, lt-tm_min, lt-tm_sec);//輸出結果
return 0;
}
注意事項:
struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt-tm_year+1900。
c語言時鐘代碼
#includegraphics.h /* 引入graphic.h */
#includemath.h /* 引入math.h */
#includedos.h /* 引入dos.h */
#define pi 3.1415926 /*定義pi=3.14159*/
#define X(a,b,c) x=a*cos(b*c*pi/180-pi/2)+300;
#define Y(a,b,c) y=a*sin(b*c*pi/180-pi/2)+240;
#define d(a,b,c) X(a,b,c);Y(a,b,c);line(300,240,x,y) /*定義……*/
void init() /*初始化程序*/
{int i,l,x1,x2,y1,y2; /*定義……*/
setbkcolor(1); /*設置顏色*/
circle(300,240,200); /*作園*/
circle(300,240,205);
circle(300,240,5);
for(i=0;i60;i++) /*循環(算時間)*/
{if(i%5==0) l=15;
else l=5;
x1=200*cos(i*6*pi/180)+300;
y1=200*sin(i*6*pi/180)+240;
x2=(200-l)*cos(i*6*pi/180)+300;
y2=(200-l)*sin(i*6*pi/180)+240;
line(x1,y1,x2,y2);
}
}
main()
{
int x,y;
int gd=VGA,gm=2;
unsigned char h,m,s; /*定義*/
struct time t[1];
initgraph(gd,gm,”d:\\tc”);
init();
setwritemode(1);
gettime(t);
h=t[0].ti_hour;
m=t[0].ti_min;
s=t[0].ti_sec; /*定義時分秒*/
setcolor(7); /*設置顏色*/
d(150,h,30);
setcolor(14);
d(170,m,6);
setcolor(4);
d(190,s,6);
while(!kbhit()) /*獲取鍵盤相應*/
{while(t[0].ti_sec==s)
gettime(t); /*C語言中得到時間的函數*/
sound(400); /*計算時間……*/
delay(70);
sound(200);
delay(30);
nosound();
setcolor(4);
d(190,s,6);
s=t[0].ti_sec;
d(190,s,6);
if (t[0].ti_min!=m)
{
setcolor(14);
d(170,m,6);
m=t[0].ti_min;
d(170,m,6);
}
if (t[0].ti_hour!=h)
{ setcolor(7);
d(150,h,30);
h=t[0].ti_hour;
d(150,h,30);
sound(1000);
delay(240);
nosound();
delay(140);
sound(2000);
delay(240);
nosound();
}
}
getch(); /*設置空格後退出*/
closegraph();
}
具體的。。就是套用用幾個函數算時間。。
不要對這種很長的東西害怕,其實大部分都是在畫這個鐘~
加油哦~
時鐘程序(C語言)怎麼寫
具體代碼如下:
#includegraphics.h
#includemath.h
#includedos.h
#define PI 3.1415926
//屏幕中心的坐標(640X480模式下)
#define mid_x 320
#define mid_y 240
int main()
{ int graphdriver=DETECT,graphmode;
int end_x,end_y;
struct time curtime;
float th_hour,th_min,th_sec;
initgraph(graphdriver,graphmode,”C:\\TC2″); //初始化VGA屏幕模式
setbkcolor(BLACK); //使用黑色的背景色
while(!kbhit(0)) //若有鍵盤輸入,則跳出,即是結束程序
{ setcolor(GREEN); //把畫筆設為綠色
circle(mid_x,mid_y,180); //鐘的外圓
circle(mid_x,mid_y,150); //鐘的內圓
circle(mid_x,mid_y,1); //畫出鐘的圓心
gettime(curtime); //取得系統當前時間
th_sec=(float)curtime.ti_sec*0.1047197551; //把秒針的角度化為弧度,為以後繪製時方便,下同
th_min=(float)curtime.ti_min*0.1047197551+th_sec/60.0; //分針的弧度
th_hour=(float)curtime.ti_hour*0.5235987755+th_min/12.0; //時度的弧度,注意整時是12等分的,所時乘的是3.14/180*5
//計算出時針的尾的坐標(時針長70)
end_x=mid_x+70*sin(th_hour);
end_y=mid_y-70*cos(th_hour);
setcolor(RED);
line(mid_x,mid_y,end_x,end_y); //用紅色線畫出時針
//計算出分針坐標(分針長110)
end_x=mid_x+110*sin(th_min);
end_y=mid_y-110*cos(th_min);
setcolor(RED);
line(mid_x,mid_y,end_x,end_y); //用紅色畫出分針
end_x=mid_x+140*sin(th_sec);
end_y=mid_y-140*cos(th_sec);
setcolor(RED);
line(mid_x,mid_y,end_x,end_y); //同上,畫出秒針,長為140
//畫出鐘盤上的刻度,刻度長20
line(140,240,160,240); //9點對應的大刻度
line(320,60,320,80); //12點對應的大刻度
line(500,240,480,240); //3點的刻度
line(320,420,320,400); //6點的刻度
line(410,395.7,400,378.4); //5點
line(475.7,330,458.4,320); //4點
line(475.7,150,458.4,160); //2點
line(410,84.3,400,101.6); //1點
line(230,84.3,240,101.6); //11點
line(164.3,150,181.6,160); //10點
line(164.3,330,181.6,320); //8點
line(230,395.7,240,378.4); //7點
sleep(BLUE); //這裡應該是打錯,停止一秒,應為sleep(1000)
cleardevice(); //清除屏幕上的顯示
}
closegraph(); //關閉VGA屏幕,即返迴文本方式
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/256427.html