c语言钟表
⑴ c语言编写钟表的问题
#include<graphics.h>
#include<math.h>
#include<dos.h>
#define PI 3.1415926
#define x0 320 /*定义钟表中心坐标*/
#define y0 240
void DrawClock(int x,int y,int color) /*画表盘*/
{ int r=150; /*表盘的半径*/
float th;
setcolor(color);
circle(x,y,r);
circle(x,y,2);
}
void DrawHand(int x,int y,float th,int l,int color)
{
int x1,y1;
x1=x l*sin(th);
y1=y-l*cos(th);
setcolor(color);
line(x,y,x1,y1);
}
void main()
{int gdriver=DETECT,gmode;
struct time curtime;
float th_hour,th_min,th_sec;
initgraph(&gdriver,&gmode,"");
setbkcolor(0);
while(! kbhit())
{
DrawClock(x0,y0,14);
gettime(&curtime); /*得到当前系统时间*/
gotoxy(35,20); /*定位输出位置*/
if((float)curtime.ti_hour<=12) /*午前的处理*/
{printf("AM ");
if((float)curtime.ti_hour<10) printf("0"); /*十点之前在小时数前加零*/
printf("%.0f:",(float)curtime.ti_hour);
}
else /*午后的处理*/
{printf("PM ");
if((float)curtime.ti_hour-12<10) printf("0");
printf("%.0f:",(float)curtime.ti_hour-12);
}
if((float)curtime.ti_min<10) printf("0");
printf("%.0f:",(float)curtime.ti_min);
if((float)curtime.ti_sec<10) printf("0");
printf("%.0f",(float)curtime.ti_sec);
/*以下三行计算表针转动角度,以竖直向上为起点,顺时针为正*/
th_sec=(float)curtime.ti_sec*0.1047197551; /*2π/60=0.1047197551*/
th_min=(float)curtime.ti_min*0.1047197551 th_sec/60.0;
th_hour=(float)curtime.ti_hour*0.523598775 th_min/12.0; /* 2π/12=0.5235987755 */
DrawHand(x0,y0,th_hour,70,2); /*画时针*/
DrawHand(x0,y0,th_min,110,3); /*分针*/
DrawHand(x0,y0,th_sec,140,12); /*秒针*/
sleep(1); /*延时一秒后刷新*/
cleardevice();
}
closegraph();
}
能正常运行,我测试过
来自网络转载
⑵ 用C语言做数字时钟每走一秒响一次,求大神告诉源代码
“响一次”需要牵涉到图形编程中的音乐播放问题,需要自己下载图形编程相关库文件,具体实现请自己在TODO里添加播放音乐的代码
数字时钟的实现很简单,运用time.h相关函数即可
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
time_toldt=-1;
structtm*p;
boolPrintdate()
{
time_tt=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)
{
case1:printf("一");break;
case2:printf("二");break;
case3:printf("三");break;
case4:printf("四");break;
case5:printf("五");break;
case6:printf("六");break;
case7:printf("日");break;
}
printf("%d:%02d:%02d",p->tm_hour,p->tm_min,p->tm_sec);
return1;
}
return0;
}
main()
{
while(1)
if(Printdate())
{
/*************TODO*************/
/**Forexample:printf("a");**/
}
return0;
}
⑶ c语言 时钟模拟
#include<math.h>
#include<dos.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#define PI 3.141592653589793
int h,m,s,i,l,mon,y,d;
struct time t;
struct date data;
draw()
{
gettime(&t); //取得时间信息到t
s=t.ti_sec; //秒
h=t.ti_hour; //时
m=t.ti_min; //分
getdate(&data); //取得日期信息到data
y=data.da_year; //年
mon=data.da_mon; //月
d=data.da_day; //日
//画出钟的外圆(即是轮廓)
setcolor(11);
circle(300,200,152);
setcolor(3);
circle(300,200,157);
//画出60个分钟刻度
for(i=0;i<60;i+=1)
{
if(i%5==0) l=140;
else l=145;
line(300+150*sin(i*PI/30),200-150*cos(i*PI/30),
300+l*sin(i*PI/30),200-l*cos(i*PI/30));
}
//画秒针
setcolor(19);
line(300,200,300+140*sin(s*PI/30),200-140*cos(s*PI/30));
//画分针
setcolor(3);
line(300,200,300+110*sin(m*PI/30),200-110*cos(m*PI/30));
//画时针
setcolor(11);
line(300,200,300+90*sin(((float)h+(float)m/60)*PI/6),200-90*cos(((float)h+(float)m/60)*PI/6));
//标注钟盘上的"3"、"6"、"9"、"12"
settextstyle(3,0,2);
outtextxy(430,190,"3");
outtextxy(295,320,"6");
outtextxy(160,190,"9");
outtextxy(293,60,"12");
}
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,""); //初始化屏幕模式
setbkcolor(8);
while(!kbhit()) //若有键盘输入则跳出(结束程序)
{
draw(); //绘制钟
settextstyle(3,0,5);
setcolor(9);
outtextxy(60,170,"my clock");
gotoxy(35,17);
//打印出数字形式的时间(hh:mm:ss)
if(h<10) printf("0");printf("%d:",h);
if(m<10) printf("0");printf("%d:",m);
if(s<10) printf("0");printf("%d",s);
gotoxy(33,18);
printf("%d:",y);
//打印出日期(mm:dd)
if(mon<10) printf("0");printf("%d:",mon);
if(d<10) printf("0");printf("%d",d);
sound(200); //让喇叭以200HZ叫一声
delay(70); //延时0.07秒,即是声音延续0.07秒
nosound(); //停止声音
sleep(1); //停止一秒
cleardevice(); //清屏
}
}
⑷ 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语言怎么编
#include<graphics.h>
#include<conio.h>
#include<math.h>
voidDraw(inthour,intminute,intsecond)
{
doublea_hour,a_min,a_sec;//时、分、秒针的弧度值
intx_hour,y_hour,x_min,y_min,x_sec,y_sec;//时、分、秒针的末端位置
intx_hour1,y_hour1,x_min1,y_min1,x_sec1,y_sec1;
//计算时、分、秒针的弧度值
a_sec=second*2*PI/60;
a_min=minute*2*PI/60;
a_hour=hour*2*PI/12+a_min/12;;
//计算时、分、秒针的首末端位置
x_sec=320+(int)(120*sin(a_sec));
y_sec=240-(int)(120*cos(a_sec));
x_min=320+(int)(100*sin(a_min));
y_min=240-(int)(100*cos(a_min));
x_hour=320+(int)(70*sin(a_hour));
y_hour=240-(int)(70*cos(a_hour));
x_sec1=320-(int)(15*sin(a_sec));
y_sec1=240+(int)(15*cos(a_sec));
x_min1=320-(int)(10*sin(a_min));
y_min1=240+(int)(10*cos(a_min));
x_hour1=320-(int)(5*sin(a_hour));
y_hour1=240+(int)(5*cos(a_hour));//画时针
setlinestyle(PS_SOLID,NULL,7);
setcolor(WHITE);
line(x_hour1,y_hour1,x_hour,y_hour);
//画分针
setlinestyle(PS_SOLID,NULL,4);
setcolor(LIGHTGRAY);
line(x_min1,y_min1,x_min,y_min);
//画秒针
setlinestyle(PS_SOLID,NULL,2);
setcolor(RED);
line(x_sec1,y_sec1,x_sec,y_sec);
}
voidmain()
{
initgraph(640,480);//初始化640x480的绘图窗口
//绘制一个简单的表盘
circle(320,240,2);
circle(320,240,60);
circle(320,240,160);
outtextxy(296,330,"竹斌");
intx,y;
for(inti=0;i<12;i++)
{
x=320+(int)(140*sin(30*i*2*PI/360));
y=240-(int)(140*cos(30*i*2*PI/360));
switch(i)
{
case0:outtextxy(x-5,y-5,"12");break;
case1:outtextxy(x-5,y-5,"1");break;
case2:outtextxy(x-5,y-5,"2");break;
case3:outtextxy(x-5,y-5,"3");break;
case4:outtextxy(x-5,y-5,"4");break;
case5:outtextxy(x-5,y-5,"5");break;
case6:outtextxy(x-5,y-5,"6");break;
case7:outtextxy(x-5,y-5,"7");break;
case8:outtextxy(x-5,y-5,"8");break;
case9:outtextxy(x-5,y-5,"9");break;
case10:outtextxy(x-5,y-5,"10");break;
case11:outtextxy(x-5,y-5,"11");break;
}
}
//设置XOR绘图模式
setwritemode(R2_XORPEN);//设置XOR绘图模式
//画刻度
inta,b,a1,b1,n=0;
for(n=0;n<60;n++)
{
a=320+(int)(160*sin(n*2*PI/60));
b=240-(int)(160*cos(n*2*PI/60));
a1=320+(int)(150*sin(n*2*PI/60));
b1=240-(int)(150*cos(n*2*PI/60));
if(n%5==0)
setlinestyle(PS_SOLID,NULL,5);
else
setlinestyle(PS_SOLID,NULL,1);
line(a1,b1,a,b);
}
//绘制表针
SYSTEMTIMEti;//定义变量保存当前时间
while(!kbhit())//按任意键退出钟表程序
{
GetLocalTime(&ti);//获取当前时间
Draw(ti.wHour,ti.wMinute,ti.wSecond);//画表针
Sleep(1000);//延时1秒
Draw(ti.wHour,ti.wMinute,ti.wSecond);//擦表针(擦表针和画表针的过程是一样的)
}
closegraph();//关闭绘图窗口
}
⑹ 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();
}
具体的。。就是套用用几个函数算时间。。
不要对这种很长的东西害怕,其实大部分都是在画这个钟~
加油哦~