诚求一个C语言程序:模拟时钟转动程序

诚求一个C语言程序:模拟时钟转动程序,第1张

#include <stdioh>

#include <stdlibh>

#include <timeh>

//#include <dosh>

#include <conioh>

int main()

{

struct tm curtime;

time_t t;

clock_t start;

double th_hour,th_min,th_sec;

do

{

printf("

");

printf(" This is a simple clock program

");

printf("

");

printf("

");

printf("The current time is:

");

t=time(0);

curtime=gmtime(&t); /得到当前系统时间/

if((double)curtime->tm_hour<=12) /午前的处理/

{

printf("AM ");

if((double)curtime->tm_hour<10) printf("0"); /十点之前在小时数前加零/

printf("%0f:",(double)curtime->tm_hour);

}

else /午后的处理/

{

printf("PM ");

if((double)curtime->tm_hour-12<10) printf("0");

printf("%0f:",(double)curtime->tm_hour-12);

}

if((double)curtime->tm_min<10) printf("0");

printf("%0f:",(double)curtime->tm_min);

if((double)curtime->tm_sec<10) printf("0");

printf("%0f",(double)curtime->tm_sec);

printf("

");

start=clock();

while(clock()-start<1000);

// delay(1000); /延时一秒后刷新/

system("cls");

}while(!kbhit()); / 按任一键退出 /

return 0;

}

////////////////////////////////////////////

// 程序名称:钟表模拟程序(表针形式)

// 编译环境:Visual C++ 60,EasyX 2011惊蛰版

// 程序编写:BestAns <BestAns@qqcom>

// 最后更新:2010-10-30

//

#include

 

<graphicsh>

#include

 

<conioh>

#include

 

<mathh>

#define

PI

31415926536

void

 

DrawHand

(

int

 hour

,

 

int

 minute

,

 

int

 second

)

{

double

 a_hour

,

 a_min

,

 a_sec

;

// 时、分、秒针的弧度值

int

 x_hour

,

 y_hour

,

 x_min

,

 y_min

,

 x_sec

,

 y_sec

;

// 时、分、秒针的末端位置

// 计算时、分、秒针的弧度值

a_sec 

=

 second 

 

2

 

 PI 

/

 

60

;

a_min 

=

 minute 

 

2

 

 PI 

/

 

60

 

+

 a_sec 

/

 

60

;

a_hour

=

 hour 

 

2

 

 PI 

/

 

12

 

+

 a_min 

/

 

12

;

// 计算时、分、秒针的末端位置

x_sec 

=

 

int

(

120

 

 

sin

(

a_sec

));

y_sec 

=

 

int

(

120

 

 

cos

(

a_sec

));

x_min 

=

 

int

(

100

 

 

sin

(

a_min

));

y_min 

=

 

int

(

100

 

 

cos

(

a_min

));

x_hour

=

 

int

(

70

 

 

sin

(

a_hour

));

y_hour

=

 

int

(

70

 

 

cos

(

a_hour

));

// 画时针

setlinestyle

(

PS_SOLID

,

 NULL

,

 

10

);

setcolor

(

WHITE

);

line

(

320

 

+

 x_hour

,

 

240

 

-

 y_hour

,

 

320

 

-

 x_hour 

/

 

7

,

 

240

 

+

 y_hour 

/

 

7

);

// 画分针

setlinestyle

(

PS_SOLID

,

 NULL

,

 

6

);

setcolor

(

LIGHTGRAY

);

line

(

320

 

+

 x_min

,

 

240

 

-

 y_min

,

 

320

 

-

 x_min 

/

 

5

,

 

240

 

+

 y_min 

/

 

5

);

// 画秒针

setlinestyle

(

PS_SOLID

,

 NULL

,

 

2

);

setcolor

(

RED

);

line

(

320

 

+

 x_sec

,

 

240

 

-

 y_sec

,

 

320

 

-

 x_sec 

/

 

3

,

 

240

 

+

 y_sec 

/

 

3

);

}

void

 

DrawDial

()

{

// 绘制一个简单的表盘

circle

(

320

,

 

240

,

 

2

);

circle

(

320

,

 

240

,

 

60

);

circle

(

320

,

 

240

,

 

160

);

outtextxy

(

296

,

 

310

,

 

"BestAns"

);

// 绘制刻度

int

 x

,

 y

;

for

 

(

int

 i

=

0

;

 i

<

60

;

 i

++)

{

=

 

320

 

+

 

int

(

145

 

 

sin

(

PI 

 

2

 

 i 

/

 

60

));

=

 

240

 

+

 

int

(

145

 

 

cos

(

PI 

 

2

 

 i 

/

 

60

));

if

 

(

%

 

15

 

==

 

0

)

bar

(

-

 

5

,

 y 

-

 

5

,

 x 

+

 

5

,

 y 

+

 

5

);

else

 

if

 

(

%

 

5

 

==

 

0

)

circle

(

x

,

 y

,

 

3

);

else

putpixel

(

x

,

 y

,

 WHITE

);

}

}

void

 

main

()

{

initgraph

(

640

,

 

480

);

// 初始化 640 x 480 的绘图窗口

DrawDial

();

// 绘制表盘

setwritemode

(

R2_XORPEN

);

// 设置 XOR 绘图模式

// 绘制表针

SYSTEMTIME

 

ti

;

// 定义变量保存当前时间

while

(!

kbhit

())

// 按任意键退出钟表程序

{

GetLocalTime

(&

ti

);

// 获取当前时间

DrawHand

(

ti

wHour

,

 ti

wMinute

,

 ti

wSecond

);

// 画表针

Sleep

(

1000

);

// 延时 1 秒

DrawHand

(

ti

wHour

,

 ti

wMinute

,

 ti

wSecond

);

// 擦表针(擦表针和画表针的过程是一样的)

}

closegraph

();

// 关闭绘图窗口

}

其了就是用了easyx

单片机程序是基于硬件基础上开发的,你至少添加注释和硬件环境说明,不然大家都不知道怎么帮你。

你直接给出代码,也不清楚需要指正什么,这里也没有设置时间/闹钟的代码,发文前应该清楚表达要问的内容。

1)假设你的晶振是12MHz,那一个机器周期是1MHz,定时器的最长定时周期是(65535/10^6)你想要1/6秒触发一次中断就必须结合软件计时,为了尽量精确:硬件计时40000次,软件计时25次那么定时器的设这应该是TH1=(65535-40000)%256;TL1=(65535-40000)%256,(注意计数器大部分是加法计数)

2)按键监听没有给出思路,也没有处理按键抖动,我只能分析程序:

有一个外部中断触发时间设置,P34设置小时、P35设置分钟,但是调整时间后没有进行显示,你至少要让数码管显示结果才能知道怎么调整。

下面是简易时钟的实现:

简易时钟的界面只有数字和指针。这里肯定要用到Timer控件啦,每秒钟刷新界面,这样的话就要求双缓冲。

第一步:添加用户控件,命名为myClock。

第二步:构造函数实例化控件和做双缓冲处理。

public myClock()

{

InitializeComponent();

thisSetStyle(ControlStylesAllPaintingInWmPaint, true);

thisSetStyle(ControlStylesDoubleBuffer, true);

thisSetStyle(ControlStylesResizeRedraw, true);

thisSetStyle(ControlStylesSelectable, true);

thisSetStyle(ControlStylesSupportsTransparentBackColor, true);

thisSetStyle(ControlStylesUserPaint, true);

myTimer = new Timer();

myTimerInterval = 1000;

myTimerEnabled = true;

myTimerTick += new EventHandler(myTimer_Tick);

}

第三步:Paint重绘事件

private void myClock_Paint(object sender, PaintEventArgs e)

{

Graphics gra = eGraphics;//创建一个画板

//

//表盘上的指针

using (SolidBrush sb = new SolidBrush(ColorBlue))

{

Font f = new Font("宋体", 18);

graDrawString("1", f, sb, 195, 20);

graDrawString("2", f, sb, 235, 60);

graDrawString("3", f, sb, 250, 123);

graDrawString("4", f, sb, 235, 183);

graDrawString("5", f, sb, 195, 225);

graDrawString("6", f, sb, 125, 245);

graDrawString("7", f, sb, 55, 225);

graDrawString("8", f, sb, 15, 183);

graDrawString("9", f, sb, 0, 123);

graDrawString("10", f, sb, 15, 60);

graDrawString("11", f, sb, 55, 20);

graDrawString("12", f, sb, 125, 0);

}

//表盘的框架:黑色,宽度是3

using (Pen p = new Pen(ColorBlack, 3))

{

pDashStyle = DashStyleSolid;

graDrawEllipse(p, 0, 0, 270, 270);

}

//表盘上的三个指针i,j,k;

using (Pen p1 = new Pen(ColorRed))

{

using (Pen p2 = new Pen(ColorBlue))

{

using (Pen p3 = new Pen(ColorYellow))

{

graDrawPie(p3, 20, 20, 230, 230, k, 01f);//秒

graDrawPie(p2, 30, 30, 210, 210, j, 01f);//分

graDrawPie(p1, 40, 40, 190, 190, i, 01f);//小时

}

}

}

//指针旋转的代码

if (i < 360)

{

i += 6;

if (i == 270)

{

if (j <= 360)

{

j += 6;

if (j == 270)

{

if (k <= 360)

{

k += 30;

}

else

{

k = 30;

}

}

}

else

{

j = 6;

}

}

}

else

{

i = 6;

}

}

贴出所有代码:

//控件名:myClock

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemDrawing;

using SystemData;

using SystemText;

using SystemWindowsForms;

using SystemDrawingDrawing2D;

namespace myControl

{

public partial class myClock : UserControl

{

public myClock()

{

InitializeComponent();

thisSetStyle(ControlStylesAllPaintingInWmPaint, true);

thisSetStyle(ControlStylesDoubleBuffer, true);

thisSetStyle(ControlStylesResizeRedraw, true);

thisSetStyle(ControlStylesSelectable, true);

thisSetStyle(ControlStylesSupportsTransparentBackColor, true);

thisSetStyle(ControlStylesUserPaint, true);

myTimer = new Timer();

myTimerInterval = 1000;

myTimerEnabled = true;

myTimerTick += new EventHandler(myTimer_Tick);

}

private Timer myTimer;

//定义三个变量

float i = 270, j = 270, k = 270;

private void myTimer_Tick(object sender, EventArgs e)

{

thisInvalidate();

}

private void myClock_Paint(object sender, PaintEventArgs e)

{

Graphics gra = eGraphics;//创建一个画板

//

//表盘上的指针

using (SolidBrush sb = new SolidBrush(ColorBlue))

{

Font f = new Font("宋体", 18);

graDrawString("1", f, sb, 195, 20);

graDrawString("2", f, sb, 235, 60);

graDrawString("3", f, sb, 250, 123);

graDrawString("4", f, sb, 235, 183);

graDrawString("5", f, sb, 195, 225);

graDrawString("6", f, sb, 125, 245);

graDrawString("7", f, sb, 55, 225);

graDrawString("8", f, sb, 15, 183);

graDrawString("9", f, sb, 0, 123);

graDrawString("10", f, sb, 15, 60);

graDrawString("11", f, sb, 55, 20);

graDrawString("12", f, sb, 125, 0);

}

//表盘的框架:黑色,宽度是3

using (Pen p = new Pen(ColorBlack, 3))

{

pDashStyle = DashStyleSolid;

graDrawEllipse(p, 0, 0, 270, 270);

}

//表盘上的三个指针i,j,k;

using (Pen p1 = new Pen(ColorRed))

{

using (Pen p2 = new Pen(ColorBlue))

{

using (Pen p3 = new Pen(ColorYellow))

{

graDrawPie(p3, 20, 20, 230, 230, k, 01f);//秒

graDrawPie(p2, 30, 30, 210, 210, j, 01f);//分

graDrawPie(p1, 40, 40, 190, 190, i, 01f);//小时

}

}

}

//指针旋转的代码

if (i < 360)

{

i += 6;

if (i == 270)

{

if (j <= 360)

{

j += 6;

if (j == 270)

{

if (k <= 360)

{

k += 30;

}

else

{

k = 30;

}

}

}

else

{

j = 6;

}

}

}

else

{

i = 6;

}

}

}

}

#include<graphicsh>

#include<mathh>

#include<dosh>

#define PI 31415926

#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 lsin(th);

y1=y-lcos(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)curtimeti_hour<=12) /午前的处理/

{printf("AM ");

if((float)curtimeti_hour<10) printf("0"); /十点之前在小时数前加零/

printf("%0f:",(float)curtimeti_hour);

}

else /午后的处理/

{printf("PM ");

if((float)curtimeti_hour-12<10) printf("0");

printf("%0f:",(float)curtimeti_hour-12);

}

if((float)curtimeti_min<10) printf("0");

printf("%0f:",(float)curtimeti_min);

if((float)curtimeti_sec<10) printf("0");

printf("%0f",(float)curtimeti_sec);

/以下三行计算表针转动角度,以竖直向上为起点,顺时针为正/

th_sec=(float)curtimeti_sec01047197551; /2π/60=01047197551/

th_min=(float)curtimeti_min01047197551 th_sec/600;

th_hour=(float)curtimeti_hour0523598775 th_min/120; / 2π/12=05235987755 /

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语言程序:模拟时钟转动程序全部的内容,包括:诚求一个C语言程序:模拟时钟转动程序、跪求C语言编写的时钟(能修改时钟时间,且时钟是指针式的 只编写修改时间的部分也行)、51单片机 c语言写的电子时钟程序 请帮忙指正!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/zz/9869530.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存