c語言時鍾
A. 用c語言 編寫 一個時鍾程序
/*全屏幕模擬時鍾的c源程序*/
#include<graphics.h>
#include<math.h>
#include<dos.h>
#define
pi
3.1415926
#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(0);
circle(300,240,200);
circle(300,240,205);
circle(300,240,5);
for(i=0;i<60;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())
/*kbhit檢查當前按下的健*/
{while(t[0].ti_sec==s)
gettime(t);
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();
/*關閉圖形系統*/
}
B. c語言怎麼樣編寫一個時鍾程序
c語言時鍾程序代碼如下:
#include<windows.h>
#include<math.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;i<iNum;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;iAngle<360;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;i<3;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語言中的時鍾問題
#include"stdio.h"
#include"graphics.h"
#include"math.h"
#include"time.h"
#include"dos.h"
int point_sec,point_min,point_hour;
int x,y,z;
int array_sec[60][2];
int array_min[60][2];
int array_hour[60][2];
int a[60][2];
void zhongmian()
{
int n,m,p;
x=320;
y=240;
z=140;
point_sec=132;
point_min=110;
point_hour=90;
setfillstyle(1,2);
setlinestyle(0,0,3);
ellipse(x,y,0,180,220,150);
setlinestyle(0,0,3);
ellipse(x,y,180,360,220,150);
ellipse(x,y,90,270,210,150);
ellipse(x,y,90,270,200,150);
ellipse(x,y,90,270,190,150);
ellipse(x,y,90,270,180,150);
ellipse(x,y,90,270,170,150);
ellipse(x,y,90,270,160,150);
ellipse(x,y,90,270,150,150);
ellipse(x,y,270,90,150,150);
ellipse(x,y,270,90,160,150);
ellipse(x,y,270,90,170,150);
ellipse(x,y,270,90,180,150);
ellipse(x,y,270,90,190,150);
ellipse(x,y,270,90,200,150);
ellipse(x,y,270,90,210,150);
setcolor(3);
ellipse(x,80,25,155,72,72);
ellipse(x,80,40,140,55,55);
setcolor(RED);
circle(x,y,148);
floodfill(165,240,RED);
floodfill(145,240,RED);
floodfill(125,240,RED);
floodfill(105,240,RED);
floodfill(475,240,RED);
floodfill(495,240,RED);
floodfill(515,240,RED);
floodfill(535,240,RED);
setlinestyle(0,0,3);
line(135,320,90,420);
line(505,320,550,420);
line(90,420,550,420);
arc(x,y,26,154,200);
setcolor(YELLOW);
arc(x,y,32,147,192);
setcolor(YELLOW);
setfillstyle(1,1);
floodfill(250,400,RED);
putpixel(320,100,YELLOW);
fillellipse(320,100,5,5);
putpixel(390,119,YELLOW);
fillellipse(390,119,5,5);
putpixel(441,170,YELLOW);
fillellipse(441,170,5,5);
putpixel(460,240,YELLOW);
fillellipse(460,240,5,5);
putpixel(441,310,YELLOW);
fillellipse(441,310,5,5);
putpixel(390,361,YELLOW);
fillellipse(390,361,5,5);
putpixel(320,380,YELLOW);
fillellipse(320,380,5,5);
putpixel(250,361,YELLOW);
fillellipse(250,361,5,5);;
putpixel(199,310,YELLOW);
fillellipse(199,310,5,5);
putpixel(180,240,YELLOW);
fillellipse(180,240,5,5);;
putpixel(199,170,YELLOW);
fillellipse(199,170,5,5);;
putpixel(250,119,YELLOW);
fillellipse(250,119,5,5);;
for(p=6,n=0,m=0;n<15;p=p+6,n++,m++){
a[m][0]=(int)(x+z*sin(2*3.14*p/360));
a[m][1]=(int)(y-z*cos(2*3.14*p/360));
array_sec[m][0]=(int)(x+point_sec*sin(2*3.14*p/360));
array_sec[m][1]=(int)(y-point_sec*cos(2*3.14*p/360));
array_min[m][0]=(int)(x+point_min*sin(2*3.14*p/360));
array_min[m][1]=(int)(y-point_min*cos(2*3.14*p/360));
array_hour[m][0]=(int)(x+point_hour*sin(2*3.14*p/360));
array_hour[m][1]=(int)(y-point_hour*cos(2*3.14*p/360));
}
for(p=6,n=0;n<15;p=p+6,n++,m++){
a[m][0]=(int)(x+z*cos(2*3.14*p/360));
a[m][1]=(int)(y+z*sin(2*3.14*p/360));
array_sec[m][0]=(int)(x+point_sec*cos(2*3.14*p/360));
array_sec[m][1]=(int)(y+point_sec*sin(2*3.14*p/360));
array_min[m][0]=(int)(x+point_min*cos(2*3.14*p/360));
array_min[m][1]=(int)(y+point_min*sin(2*3.14*p/360));
array_hour[m][0]=(int)(x+point_hour*cos(2*3.14*p/360));
array_hour[m][1]=(int)(y+point_hour*sin(2*3.14*p/360));
}
for(p=6,n=0;n<15;p=p+6,n++,m++){
a[m][0]=(int)(x-z*sin(2*3.14*p/360));
a[m][1]=(int)(y+z*cos(2*3.14*p/360));
array_sec[m][0]=(int)(x-point_sec*sin(2*3.14*p/360));
array_sec[m][1]=(int)(y+point_sec*cos(2*3.14*p/360));
array_min[m][0]=(int)(x-point_min*sin(2*3.14*p/360));
array_min[m][1]=(int)(y+point_min*cos(2*3.14*p/360));
array_hour[m][0]=(int)(x-point_hour*sin(2*3.14*p/360));
array_hour[m][1]=(int)(y+point_hour*cos(2*3.14*p/360));
}
for(p=6,n=0;n<15;p=p+6,n++,m++){
a[m][0]=(int)(x-z*cos(2*3.14*p/360));
a[m][1]=(int)(y-z*sin(2*3.14*p/360));
array_sec[m][0]=(int)(x-point_sec*cos(2*3.14*p/360));
array_sec[m][1]=(int)(y-point_sec*sin(2*3.14*p/360));
array_min[m][0]=(int)(x-point_min*cos(2*3.14*p/360));
array_min[m][1]=(int)(y-point_min*sin(2*3.14*p/360));
array_hour[m][0]=(int)(x-point_hour*cos(2*3.14*p/360));
array_hour[m][1]=(int)(y-point_hour*sin(2*3.14*p/360));
}
for(n=0;n<60;n++)
putpixel(a[n][0],a[n][1],YELLOW);
}
void zhuandong()
{
struct tm * p;
time_t l;
int n,m,i,q,b,c,sec,min,hour;
n=61;
m=61;
i=61;
b=0;
c=0;
while(1){
l=time(NULL);
p=localtime(&l);
sec=p->tm_sec;
min=p->tm_min;
hour=p->tm_hour;
gotoxy(29,12);
if(n==sec){
setcolor(0);
setlinestyle(0,0,1);
line(x,y,array_sec[(sec+58)%60][0],array_sec[(sec+58)%60][1]);
setlinestyle(0,0,2);
line(x,y,array_min[(min+58)%60][0],array_min[(min+58)%60][1]);
setlinestyle(0,0,3);
line(x,y,array_hour[(hour%12*5+min/12+58)%60][0],array_hour[(hour%12*5+min/12+58)%60][1]);
setcolor(YELLOW);
setlinestyle(0,0,3);
line(x,y,array_hour[(hour%12*5+min/12+59)%60][0],array_hour[(hour%12*5+min/12+59)%60][1]);
setlinestyle(0,0,2);
line(x,y,array_min[(min+59)%60][0],array_min[(min+59)%60][1]);
printf("%s",asctime(p));
sound(50);
delay(100);
nosound();
setcolor(YELLOW);
setlinestyle(0,0,1);
line(320,240,array_sec[(sec+59)%60][0],array_sec[(sec+59)%60][1]);
}
n=sec+1;
if(n==60)n=0;
if(b==0)
{m=min;b=1;}
if(min==m){
setcolor(0);
setcolor(YELLOW);
setlinestyle(0,0,3);
line(x,y,array_hour[(hour%12*5+min/12+59)%60][0],array_hour[(hour%12*5+min/12+59)%60][1]);
setlinestyle(0,0,2);
line(x,y,array_min[(min+59)%60][0],array_min[(min+59)%60][1]);
}
m=min+1;
if(m==60)m=0;
if(c==0)
{i=min;c=1;}
if(i==min){
setcolor(0);
setcolor(YELLOW);
setlinestyle(0,0,3);
line(x,y,array_hour[(hour%12*5+min/12+59)%60][0],array_hour[(hour%12*5+min/12+59)%60][1]);
}
i=min+1;
if(i==60)i=0;
if(kbhit())exit(1);
}
}
main()
{
int gdriver,mode;
gdriver=VGA;
mode=VGAHI;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&mode,"");
setbkcolor(0);
setcolor(RED);
zhongmian();
zhuandong();
closegraph();
}
D. C語言時鍾
運行的話,可以用快捷鍵windows鍵+R
E. C語言怎樣編時鍾那個東西,程序
讀取系統時間,將其轉換為你想表現的樣子,static控制項上 自繪表盤 或者 電子時鍾LED顯示 等等。然後設置個定時器 每秒更新一次
F. c語言中怎麼設置計時器
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
clock_t start = clock();
//do some process here
clock_t end = (clock() - start)/CLOCKS_PER_SEC;
cout<<"time comsumption is "<<end<<endl;
}
(6)c語言時鍾擴展閱讀
使用linux的系統設置計時器
#include <sys/time.h>
int main()
{
timeval starttime,endtime;
gettimeofday(&starttime,0);
//do some process here
gettimeofday(&endtime,0);
double timeuse = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - startime.tv_usec;
timeuse /=1000;//除以1000則進行毫秒計時,如果除以1000000則進行秒級別計時,如果除以1則進行微妙級別計時
}
timeval的結構如下:
strut timeval
{
long tv_sec; /* 秒數 */
long tv_usec; /* 微秒數 */
};
G. c語言時鍾代碼
#include<graphics.h> /* 引入graphic.h */
#include<math.h> /* 引入math.h */
#include<dos.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;i<60;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();
}
具體的。。就是套用用幾個函數算時間。。
不要對這種很長的東西害怕,其實大部分都是在畫這個鍾~
加油哦~
H. c語言的時鍾程序
#include<graphics.h>
#include<math.h>
#include<dos.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;
}
I. (100分)c語言寫時鍾
不相信你.
點(sin x),( cos x) (0<x<360) 在直角坐標系中能構成一個圓.
line(x1,y1,x2,y2) point(x,y) 是常用的畫線和點的函數,自己做吧
J. 怎樣用c語言編寫時鍾(轉的那種)
純c怕是寫不出來,會用到其他東西吧,如win32界面編程、mfc,openCV,dx。
如果樓主是為了完成作業才弄什麼時鍾,下面的就不用看了
要是為了興趣,幹嘛去弄時鍾,弄鍵盤鉤子、視頻監控,遠程攝像頭多好玩。
最近在看這書:Visual+C++典型模塊與項目實戰大全,裡面有很多好玩的項目