用C语言编时钟程序要求时钟的指针不一

用C语言编时钟程序要求时钟的指针不一,第1张

/*开发环境:turbo c 2.0模拟时钟转动程序代码*/

#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(1)

circle(300,240,200)

circle(300,240,205)

circle(300,240,5)

for(i=0i<60i++) /*划钟点上的短线*/

{

if(i%5==0)

l=15

else

l=5

x1=200*sin(i*6*pi/180)+300

y1=200*cos(i*6*pi/180)+240

x2=(200-l)*sin(i*6*pi/180)+300

y2=(200-l)*cos(i*6*pi/180)+240

line(x1,y1,x2,y2)

}

}

main()

{

int x,y,i,k=1

int gdriver=9,gmode=2

unsigned char h,m,s

int o,p,q

float n

struct time t[1]

struct date d[1]

initgraph(&gdriver,&gmode,"c:\\tc")

initgraph(&gdriver,&gmode,"c:\\tc")

for(i=0i<=6i++)

{

settextstyle(TRIPLEX_FONT,HORIZ_DIR,i) /*控制输出字符的字体,方向,大小*/

cleardevice()

settextjustify(1,1) /*在指定坐标上输出字符串*/

outtextxy(300,80,"12")

outtextxy(300,390,"6")

outtextxy(140,230,"9")

outtextxy(460,230,"3")

outtextxy(380,100,"1")

outtextxy(220,100,"11")

outtextxy(430,160,"2")

outtextxy(430,310,"4")

outtextxy(380,370,"5")

outtextxy(220,370,"7")

outtextxy(160,160,"10")

outtextxy(160,310,"8")

}

init()

setwritemode(1)/*设置画线的输出模式*/

if(k!=0)

{

getdate(d) /*获得系统日期手猛桐函数*/

o=d[0].da_year

p=d[0].da_mon

q=d[0].da_day

gettime(t) /*获得系统时间函数*/

h=t[0].ti_hour

m=t[0].ti_min

s=t[0].ti_sec

}

setcolor(7)/*设置时针毕坦颜色*/

n=(float)h+(float)m/60

d(150,n,30) /*画出时针*/

setcolor(14) /*设置分针颜色*/

d(170,m,6)/*画出分针*/

setcolor(4) /*设置秒针颜色*/

d(190,s,6) /*画出秒针*/

while(!kbhit()) /*控制程序按下任意键退出*/

{

while(t[0].ti_sec==s)

gettime(t)

gotoxy(44,18) /*使光标移动到指定坐标*/

printf("\b\b\b\b\b\b\b\b\b") /*退格,使表示时间的字符串不断变化*/

sound(400) /*按给定的频率打开PC扬声器*/

delay(70) /*中断程序的执行,时间为70毫秒*/

sound(200)

delay(30)

nosound()/*按给定的频率关闭PC扬声器*/

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()

}

if(s<10)/*用字符的形式输出时间*/

{ if(m<10)

printf("%u:0%u:0%u",h,m,s)

else

printf("%u:%u:0%u",h,m,s)

}

else

{ if(m<10)

printf("%u:0%u:%u",h,m,s)

else

printf("%u:%u:%u",h,m,s)

}

gotoxy(34,19)/*在指定坐标上输出日期*/

printf("%d年%d月%d日",o,p,q)

printf("\b\b\b\b\b\b\b\b\b")

}

getch()

closegraph()

}

#include"graphics.h"

#define PI 3.1416

#include"math.h"

#include"dos.h"

main()

{

int x0=320,y0=240,r0=150

void init_sceen()

void sec()

init_sceen(x0,y0,r0)

sec()

closegraph()

}

void init_sceen(int x0,int y0,int r0)/********************************************/

{

int i,x,y,graphdriver,graphmode

char s[10]

float alpha,a0=90

graphdriver=DETECT

initgraph(&graphdriver,&graphmode,"")

setbkcolor(3)

setcolor(2)

circle(x0,y0,r0)

circle(x0,y0,r0+30)

setfillstyle(SOLID_FILL,10)

floodfill(x0-r0-10,y0,2)

/*please input the time*/

for(i=12i>中燃=1i--)

{

alpha=(a0+30*(11-i)*PI/180)

x=x0+cos(alpha)*r0-16

y=y0-sin(alpha)*r0

sprintf(s,"%2d",i)

setcolor(4)

settextstyle(0,0,2)

outtextxy(x,y,s)

}

/*input second*/

for(i=60i>=1i--)

{

alpha=(a0+6*(60-i)*PI/180)

x=x0+cos(alpha)*(r0-20)

y=y0-sin(alpha)*(r0-20)

setcolor(14)

if(i%5==0)

circle(x,y,5)

else circle(x,y,2)

floodfill(x,y,14)

}

setlinestyle(0,0,3)

}

void sec(void) /******************************************************************/

{

int x,y,i,j,k,xj,yj,xk,yk,xi,yi,x0=320,y0=240,r0=150

union REGS r

unsigned char *shijie=""

unsigned char *daa=""

struct time tim

struct date dat

float alphai,alphak,alphaj,a0=90

xi=x0yi=y0xj=x0yj=y0xk=x0yk=y0

do

{

/*intput the time*/

x=38y=12

gettime(&tim)

sprintf(shijie,"%02d:%02d:%02d",tim.ti_hour,tim.ti_min,tim.ti_sec)

setfillstyle(SOLID_FILL,0)

bar(245,190,375,210)

setcolor(15)

outtextxy(245,190,shijie)

/卖猛虚*input the date*/ /*****************************************************/

getdate(&dat)

sprintf(daa,"%02d--%02d--%02d",dat.da_year,dat.da_mon,dat.da_day)

/*setfillstyle(SOLID_FILL,3)*/

bar(225,290,395,310)

setcolor(RED)

outtextxy(225,290,daa)

x=190y=430

setcolor(RED)

outtextxy(x-26,y,"Designed by GuoLiuTa0")

setcolor(LIGHTRED)

outtextxy(x+76,y0+20,"NBA GAME"知念)

setlinestyle(0,0,3)

k=tim.ti_hour

j=tim.ti_min

i=tim.ti_sec

alphak=(a0+30*(12-k)-j*5/60.*6)*PI/180

alphaj=(a0-6*j)*PI/180

/*write second hand*/

alphai=(a0+6*(60-i))*PI/180

x=x0+cos(alphai)*(r0-32)

y=y0-sin(alphai)*(r0-32)

setcolor(BLACK)

line(x0,y0,xi,yi)

setcolor(YELLOW)

line(x0,y0,x,y)

xi=x

yi=y

/*write minute hand*/

x=x0+cos(alphaj)*(r0-60)

y=y0-sin(alphaj)*(r0-60)

setcolor(BLACK)

line(x0,y0,xj,yj)

setcolor(BLUE)

line(x0,y0,x,y)

xj=x

yj=y

/*write hour hand*/

x=x0+cos(alphak)*(r0-99)

y=y0-sin(alphak)*(r0-99)

setcolor(BLACK)

line(x0,y0,xk,yk)

setcolor(RED)

line(x0,y0,x,y)

xk=x

yk=y

delay(10000)

}

while(!kbhit())

}

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=0i<iNumi++)

{

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=0iAngle<360iAngle+=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:2i<3i++)

{

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#等。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12452108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存