C语言程序设计题:模拟时钟转动的程序

C语言程序设计题:模拟时钟转动的程序,第1张

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

#include"graphicsh"

#include"mathh"

#include"dosh"

#define pi 31415926

#define X(a,b,c) x=acos(bcpi/180-pi/2)+300

#define Y(a,b,c) y=asin(bcpi/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=200sin(i6pi/180)+300;

y1=200cos(i6pi/180)+240;

x2=(200-l)sin(i6pi/180)+300;

y2=(200-l)cos(i6pi/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=0;i<=6;i++)

{

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

}

TODRVB100就表示将年、月、日、时、分、秒、空、星期读取到从VB100开始的8个字节中。当你要用到“秒”实时数据,就直接从VB15中取值就是了。但注意一点,读出来的值是以BCD码表示的数据,需要整数数据必须用指令BCDI转换

简单的说,有几种时钟,可部分参见clock_gettime函数描述。(1)挂钟,返回的是系统内核里面的一个变量。这个变量是从主板上的CMOS芯片里读出的。CMOS芯片是独立供电的,在电脑断电后也有小电池供电,就像电子表一样计时。(2)CPU的计时,是CPU有一个寄存器不断自动累加,比如2GHz的CPU每05ns就+1。(3)可编程时钟,有低精度的和高精度的两种,其实都是主板上的芯片,受到CPU的控制。

这个描述非常简化,系统在其中做了很多事情,其实linux时钟系统非常复杂,详情请参阅有关书籍。

Hand类的代码:

Public MustInherit Class Hand

Protected gp As GraphicsPath = New GraphicsPath()

Protected gpBase As GraphicsPath = Nothing

Protected midX As Integer = 150 ‘默认的窗体

Protected midY As Integer = 150 ‘中心位置

‘构造器,得到窗体中心位置

Public Sub New(ByVal theForm As Form1)

midX = (theFormClientRectangleLeft + theFormClientRectangleRight) / 2

midY = (theFormClientRectangleTop + theFormClientRectangleBottom) / 2

End Sub

MustOverride Sub Transform(ByVal d As DateTime)

‘绘制指针路径

Overridable Sub Draw(ByVal g As Graphics)

Dim aPen As Pen = New Pen(BrushesBlack, 4F)

gDrawPath(aPen, gp)

gFillPath(BrushesBlack, gp)

aPenDispose()

End Sub

‘使用矩阵实现路径(gp)的旋转

Public Sub Rotate(ByVal angle As Double)

gp = CType(gpBaseClone(), GraphicsPath)

Dim mTransform As Matrix = New Matrix()

mTransformRotateAt(CType(angle,Single),NewPointF(midX,midY))

gpTransform(mTransform)

End Sub

End Class

为了节省篇幅,上面的代码省略了引入命名空间的语句。

下面是分针(MinuteHand)类的定义:

Public Class MinuteHand

Inherits Hand

‘构造器,生成绘制分针的路径(gp)

Public Sub New(ByVal myForm As Form1)

MyBaseNew(myForm)

gpAddLine(midX, midY, midX, 45)

gpAddLine(midX, 45, midX - 3, 50)

gpAddLine(midX - 3, 50, midX + 3, 50)

gpAddLine(midX + 3, 50, midX, 45)

gpBase = CType(gpClone(), GraphicsPath)

End Sub

‘Transform方法取得系统当前时间,并旋转时钟指针。

Public Overrides Sub Transform(ByVal d As DateTime)

Dim minuteTime As Double = (CDbl(dMinute) + CDbl(dSecond / 60))

Dim angle As Double = (CDbl(minuteTime) / 60) 360

Rotate(angle)

End Sub

End Class

对所有的指针旋转的方法都是相同的,因此在基类中实现。由于时针和秒针的实现与分针相似,所不同者,只在于构造器中绘制的指针路径不同和Transform方法中转动的角度不同,在这里就不在赘述了。

另外还需要提一下的是画时钟表面的代码,时钟表面用ClockFace类来实现。这个类首先画一个圆代表时钟,然后画上米老鼠的图案,最后在相应的位置画上数字1~12代表12个小时。

Public Sub Draw(ByVal g As Graphics)

DrawClockFace(g)

DrawImage(g)

DrawNumbers(g)

DrawPin(g)

End Sub

下面是ClockFace类的属性:

Private ClockRectangle As Rectangle

Private ClockFont As Font = New Font("Arial", 12)

Private midPoint As Point

Private ClockImage As Bitmap

Private Const IMAGEX As Integer = 50

Private Const IMAGEY As Integer = 50

DrawClockFace方法用来画时钟表面:

Private Sub DrawClockFace(ByVal g As Graphics)

gFillEllipse(BrushesWhite, ClockRectangleLeft + 10, ClockRectangleTop + 10, ClockRectangleWidth - 20, ClockRectangleHeight - 20)

gDrawEllipse(PensBlack, ClockRectangleLeft + 10, ClockRectangleTop + 10, ClockRectangleWidth - 20, ClockRectangleHeight - 20)

End Sub

然后用Graphics对象的DrawImage方法画出米老鼠的:

Private Sub DrawImage(ByVal g As Graphics)

Dim nWidth As Integer = ClockImageWidth

Dim nHeight As Integer = ClockImageHeight

Dim destRect As Rectangle = New Rectangle(midPointX - IMAGEX / 2, midPointY - IMAGEY / 2, IMAGEX, IMAGEY)

gDrawImage(ClockImage, destRect)

End Sub

数字在时钟上的位置是用sin和cos函数计算的:

Private Sub DrawNumbers(ByVal g As Graphics)

Dim count As Integer = 1

Dim a As Double

For a = 0 To 2 MathPI Step 2 MathPI / 12

Dim x As Double = (ClockRectangleWidth - 70) / 2 MathCos(a - MathPI / 3) + (ClockRectangleWidth - 70) / 2 + 25

Dim y As Double = (ClockRectangleWidth - 70) / 2 MathSin(a - MathPI / 3) + (ClockRectangleWidth - 70) / 2 + 20

gDrawString(ConvertToString(count), ClockFont, BrushesBlack, CType(x, Single), CType(y, Single), New StringFormat())

count += 1

Next

End Sub

最后是窗体文件(Form1vb):

Public Class Form1

Inherits SystemWindowsFormsForm

Private MyMinuteHand As MinuteHand

Private MyHourHand As HourHand

Private MySecondHand As SecondHand

Private TheClockFace As ClockFace

Private FirstTick As Boolean = False

‘在窗体的OnPaint事件中取得Graphics对象

Protected Overrides Sub OnPaint(ByVal e As SystemWindowsFormsPaintEventArgs)

If (FirstTick = False) Then Exit Sub

Dim g As Graphics = eGraphics

TheClockFaceDraw(g)

MyHourHandDraw(g)

MyMinuteHandDraw(g)

MySecondHandDraw(g)

TheClockFaceDrawPin(g)

End Sub

‘计时器事件

Private Sub Timer1_Tick(ByVal sender As SystemObject, ByVal e As SystemEventArgs) Handles Timer1Tick

MySecondHandTransform(DateTimeNow)

MyHourHandTransform(DateTimeNow)

MyMinuteHandTransform(DateTimeNow)

FirstTick = True

Invalidate()

以上就是关于C语言程序设计题:模拟时钟转动的程序全部的内容,包括:C语言程序设计题:模拟时钟转动的程序、请问高手PLC编程的实时时钟怎么用、编程中的时钟时间是如何得到等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/zz/10100677.html

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

发表评论

登录后才能评论

评论列表(0条)

保存