#include <stdio.h>
#include <time.h>
#include"stdlib.h"
#include "windows.h"
void main ()
{
while(1)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( " 07The current date/time is: %s", asctime (timeinfo) );
Sleep(1000);
system("cls");
}
}
『貳』 誰能幫我用c語言編寫桌面鍾表啊!
#include<math.h>
#include<dos.h>
#include<graphics.h>
#define CENTERX 320 /*表盤中心位置*/
#define CENTERY 175
#define CLICK 100 /*喀嗒聲頻率*/
#define CLICKDELAY 30 /*喀嗒聲延時*/
#define HEBEEP 10000 /*高聲頻率*/
#define LOWBEEP 500 /*低聲頻率*/
#define BEEPDELAY 200 /*報時聲延時*/
/*表盤刻度形狀*/
int Mrk_1[8]={-5,-160,5,-160,5,-130,-5,-130, };
int Mrk_2[8]={-5,-160,5,-160,2,-130,-2-130, };
/*時針形狀*/
int HourHand[8]={-3,-100,3,-120,4, 10,-4,10};
/*分針形狀*/
int MiHand[8]={-3,-120,3,-120,4, 10,-4,10};
/*秒針形狀*/
int SecHand[8]={-2,-150,2,-150,3, 10,-3,10};
/*發出喀嗒聲*/
void Click()
{
sound(CLICK);
delay(CLICKDELAY);
nosound();
}
/*高聲報時*/
void HighBeep()
{
sound(HEBEEP);
delay(BEEPDELAY);
nosound;
}
/*低聲報時*/
void LowBeep()
{
sound(LOWBEEP);
}
/*按任意角度畫多邊形*/
void DrawPoly(int *data,int angle,int color)
{
int usedata[8];
float sinang,cosang;
int i;
sinang=sin((float)angle/180*3.14);
cosang=cos((float)angle/180*3.14);
for(i=0;i<8;i+=2)
{
usedata[i] =CENTERX+ cosang*data[i]-sinang*data[i+1]+.5;
usedata[i+1]=CENTERY+sinang*data[i]+cosang*data[i+1]+.5;
}
setfillstyle(SOLID_FILL,color);
fillpoly(4,usedata);
}
/*畫表盤*/
void DrawClock(struct time *cutime)
{
int ang;
float hourrate,minrate,secrate;
setbkcolor(BLUE);
cleardevice();
setcolor(WHITE);
/* 畫刻度*/
for(ang=0;ang<360;ang+=90)
{
DrawPoly(Mrk_1,ang,WHITE);
DrawPoly(Mrk_2,ang+30,WHITE);
DrawPoly(Mrk_2,ang+60,WHITE);
}
secrate=(float)cutime->ti_sec/60;
minrate=((float)cutime->ti_min+secrate)/60;
hourrate=(((float)cutime->ti_hour/12)+minrate)/12;
ang=hourrate*360;
DrawPoly(HourHand,ang,YELLOW);/*畫時針*/
ang=minrate*360;
DrawPoly(MiHand,ang, GREEN);/*畫分針*/
ang=secrate*360;
DrawPoly(SecHand,ang, RED);/*畫秒針*/
}
main()
{
int gdriver=EGA,
gmode=EGAHI;
int curpage;
struct time curtime ,newtime ;
initgraph(&gdriver,&gmode,"c:\\tc");
setbkcolor(BLUE);
cleardevice();
gettime(&curtime);
curpage=0;
DrawClock(&curtime);
while(1)
{
if(kbhit())
break; /*按任意鍵退出*/
gettime(&newtime); /*檢測系統時間*/
if(newtime.ti_sec!=curtime.ti_sec)/*每1秒更新一次時間*/
{
if(curpage==0)
curpage=1;
else
curpage=0;
curtime=newtime;
/*設置繪圖頁*/
setactivepage(curpage);
/*在圖頁上畫表盤*/
DrawClock(&curtime);
/*設置繪圖頁為當前可見頁*/
setvisualpage(curpage);
/*0分0秒高聲報時*/
if(newtime.ti_min==0&&newtime.ti_sec==0)
HighBeep();
/* 59分55至秒時低聲報時*/
else if(newtime.ti_min==59&&
newtime.ti_sec<=59)
LowBeep();/*其他時間只發出喀嗒聲*/
else
Click();
}
}
closegraph();
}