当前位置:首页 » 编程语言 » c语言时钟

c语言时钟

发布时间: 2022-01-29 02:31:03

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++典型模块与项目实战大全,里面有很多好玩的项目

热点内容
朋友圈视频压缩 发布:2024-05-07 08:33:01 浏览:870
MAC账户登录如何进入忘记密码 发布:2024-05-07 08:14:52 浏览:671
相机存储卡要求格式化 发布:2024-05-07 08:08:44 浏览:16
十一代思域哪个配置划算 发布:2024-05-07 07:59:12 浏览:352
鸿蒙系统和安卓系统哪个大 发布:2024-05-07 07:46:37 浏览:623
安卓平台用什么虚拟机 发布:2024-05-07 07:44:14 浏览:247
ta栅格算法 发布:2024-05-07 07:03:23 浏览:802
符号源码 发布:2024-05-07 06:26:09 浏览:707
玩hypixel服务器ip地址要什么版本 发布:2024-05-07 06:22:50 浏览:62
代码为什么要编译 发布:2024-05-07 06:22:48 浏览:495