用C++制作钟表的源程序代码
制作一个电子时钟C语言版

制作一个电子时钟C语言版平常我们常见的电子时钟,能够显示当下的时分秒信息,并且能够随时变化递增,记录着时间的流逝。
今天,我要用c语言来制作这样一款电子时钟,看时间这把杀猪刀如何“一刀一刀”溜掉。
我会一步一步引导大家了解制作过程。
首先,我们可以想到,需要获取系统的时间函数,并且显示在cmd控制台中。
time.h是c/c++日期和时间头文件,用于获取系统日期和时间等要求。
首先给出提取显示当前时间的1.0版本电子时钟。
#include<stdio.h>#include<time.h>#include<string.h>int main(){char str[50];//定义字符串用于存储时间time_t t;struct tm *nt;//tm是time.h中定义的一个结构体t=time(NULL);nt=localtime(&t);//获取当前时间strftime(str,50,"%Y-% m-%d %H:%M:%S ",nt);//strftime是根据当前计算机区域设置格式化本地时间日期printf("现在时刻:%s\n",str);//打印现在时刻getch();return 0;}运行的结果如下:可以看出,的确做到了显示当前时间,但是很明显这不是电子时钟,电子时钟要不断运行变换时间,而不是让时间凝滞。
那么,我们怎么实现真正的电子时钟呢?我们一起思考:如果要实现时间变化,需要不断重新获取当前时间,并且将之前的显示清除掉,然后再打印显示当前时间,这样循环往复。
所以,需要增加一个while(1)死循环,同时要增加一个清屏函数,清除原来的显示。
清屏函数常用的就是system("cls")。
下面是改进后的2.0版代码。
#include<stdio.h>#include<time.h>#include<string.h>int main(){char str[50];time_t t;struct tm *nt;while(1){system("cls");//清屏函数t=time(NULL);nt=localtime(&t);strftime(str,50,"%Y-%m-%d %H:%M:%S “,nt);printf("现在时刻:%s\n",str);}getch();return 0;}然后运行程序,就可以实现电子时钟了,时间不断刷新,秒数递增,时间随之递增。
c#窗体电子时钟源代码

int sec = DateTime.Now.Second;
//将时,分,秒连成一个字符串
TimeInString = (hour < 10) ? "0" + hour.ToString() : hour.ToString();
TimeInString += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString());
TimeInString += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString());
return TimeInString;
}
public string GetDate()
{
String TimeInString = "";
int year = DateTime.Now.Year;
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
int month = DateTime.Now.Month;
int day = DateTime.Now.Day;
C语言课程设计 时钟代码

/*电子时钟源代码*/#include<graphics.h>#include<stdio.h>#include<math.h>#include<dos.h>#define PI 3.1415926 /*定义常量*/#define UP 0x4800 /*上移↑键:修改时间*/#define DOWN 0x5000 /*下移↓键:修改时间*/#define ESC 0x11b /*ESC键:退出系统*/#define TAB 0xf09 /*TAB键:移动光标*//*函数声明*/int keyhandle(int,int); /*键盘按键判断,并调用相关函数处理*/int timeupchange(int); /*处理上移按键*/int timedownchange(int); /*处理下移按键*/int digithour(double); /*将double型的小时数转换成int型*/int digitmin(double); /*将double型的分钟数转换成int型*/int digitsec(double); /*将double型的秒钟数转换成int型*/void digitclock(int,int,int ); /*在指定位置显示时钟或分钟或秒钟数*/void drawcursor(int); /*绘制一个光标*/ void clearcursor(int);/*消除前一个光标*/void clockhandle(); /*时钟处理*/ double h,m,s; /*全局变量:小时,分,秒*/double x,x1,x2,y,y1,y2; /*全局变量:坐标值*/struct time t[1];/*定义一个time结构类型的数组*/main(){int driver, mode=0,i,j;driver=DETECT; /*自动检测显示设备*/initgraph(&driver, &mode, "");/*初始化图形系统*/setlinestyle(0,0,3); /*设置当前画线宽度和类型:设置三点宽实线*/ setbkcolor(0);/*用调色板设置当前背景颜色*/setcolor(9); /*设置当前画线颜色*/ line(82,430,558,430);line(70,62,70,418);line(82,50,558,50);line(570,62,570,418);line(70,62,570,62);line(76,56,297,56);line(340,56,564,56); /*画主体框架的边直线*//*arc(int x, int y, int stangle, int endangle, int radius)*/arc(82,62,90,180,12);arc(558,62,0,90,12);setlinestyle(0,0,3);arc(82,418,180,279,12);setlinestyle(0,0,3);arc(558,418,270,360,12); /*画主体框架的边角弧线*/setcolor(15);outtextxy(300,53,"CLOCK"); /*显示标题*/setcolor(7);rectangle(342,72,560,360); /*画一个矩形,作为时钟的框架*/setwritemode(0); /*规定画线的方式。
经典时钟C语言程序代码

学号:37
指导老师:王欣欣
设计思想及目的:用C语言编写时钟代码,然后下载到51单片机上,用1602液晶屏实现年、月、日、星期、时、分、秒的可调简易时钟。
//***********************************************************************************************************************************
{
yue--;
if(yue==0)
yue=12;
write1(0x80+6);
nyr(6,yue);
}
if(keynum==6)
{
nian--;
if(nian==-1)
week++;
if(week==21)
week=0;
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(keynum==1)
if(keynum==1)
{
TR0=0;
write1(0x80+0x40+9);
write1(0x0f);
}
if(keynum==2)
write1(0x80+0x40+6);
if(keynum==3)
write1(0x80+0x40+3);
}
nyr(6,yue);
}
nyr(9,ri);
write1(0x80+12);
C语言实现电子时钟程序

C语⾔实现电⼦时钟程序本⽂实例为⼤家分享了C语⾔实现电⼦时钟程序的具体代码,供⼤家参考,具体内容如下Qt ⾥⾯运⾏#include<windows.h>#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <time.h>typedef struct{int x;int y;}Point;time_t now;struct tm *pt, t1, t2;int printpoint(Point p){Point p1;p1.x = p.x + 2; p1.y = p.y + 4;gotoxy(p1.x, p1.y); printf("%c%c", 2, 2);gotoxy(p1.x, p1.y + 1); printf("%c%c", 2, 2);p1.y += 4;gotoxy(p1.x, p1.y); printf("%c%c", 2, 2);gotoxy(p1.x, p1.y + 1); printf("%c%c", 2, 2);return 0;}int print0(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0 || i == 12)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2);elseprintf("%c%4s%c", 2, " ", 2);}return 0;}int print1(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);printf("%5s%c", " ", 2);}return 0;}int print2(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0 || i == 6 || i == 12)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2);else if (i>0 && i<6)printf("%5s%c", " ", 2);elseprintf("%c", 2);}return 0;}int print3(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0 || i == 6 || i == 12)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2);elseprintf("%5s%c", " ", 2);}return 0;}int print4(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i<6) printf("%c%4s%c", 2, " ", 2);else if (i == 6)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2);else printf("%5s%c", " ", 2);}return 0;}int print5(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0 || i == 6 || i == 12)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2);else if (i>0 && i<6)printf("%c", 2);elseprintf("%5s%c", " ", 2);}return 0;}int print6(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0 || i == 6 || i == 12)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2);else if (i>0 && i<6)printf("%c", 2);elseprintf("%c%4s%c", 2, " ", 2);}return 0;}int print7(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0) printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2); else printf("%5s%c", " ", 2);}return 0;}int print8(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0 || i == 6 || i == 12)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2);else printf("%c%4s%c", 2, " ", 2);}return 0;}int print9(Point p){int i = 0;for (; i<13; i++){gotoxy(p.x + 1, p.y + i);if (i == 0 || i == 6 || i == 12)printf("%c%c%c%c%c%c", 2, 2, 2, 2, 2, 2); else if (i>0 && i<6)printf("%c%4s%c", 2, " ", 2);elseprintf("%5s%c", " ", 2);}return 0;}int clear(Point p){int i = 0;for (; i<13; i++)gotoxy(p.x, p.y + i); printf("%16s", " ");return 0;}int printtime(Point p, int n){int a, b;Point pp;a = n / 10,b = n % 10;pp.x = p.x + 8, pp.y = p.y;switch (a){case 0: print0(p); break;case 1: print1(p); break;case 2: print2(p); break;case 3: print3(p); break;case 4: print4(p); break;case 5: print5(p); break;}switch (b){case 0: print0(pp); break;case 1: print1(pp); break;case 2: print2(pp); break;case 3: print3(pp); break;case 4: print4(pp); break;case 5: print5(pp); break;case 6: print6(pp); break;case 7: print7(pp); break;case 8: print8(pp); break;case 9: print9(pp); break;}return 0;}int main(){Point phour, pmin, psec, point1, point2;phour.x = 9, pmin.x = 32, psec.x = 55;phour.y = pmin.y = psec.y = 7;point1.x = 25, point2.x = 49;point1.y = point2.y = 7;clrscr();textbackground(YELLOW); /* 设置背景颜⾊*/ textcolor(RED); /* 设置数字显⽰颜⾊*/now = time(0);pt = localtime(&now);t1 = *pt;printtime(phour, t1.tm_hour);printpoint(point1);printtime(pmin, t1.tm_min);printpoint(point2);printtime(psec, t1.tm_sec);while (1){now = time(0);pt = localtime(&now);t2 = *pt;if (t2.tm_sec != t1.tm_sec){t1 = t2;clrscr();printtime(phour, t1.tm_hour);printpoint(point1);printtime(pmin, t1.tm_min);printpoint(point2);printtime(psec, t1.tm_sec);}if (bioskey(1) == 0) continue;else exit(0);}return 0;}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
时钟c语言代码

}
1*/ { second = 0; minute++; } if (minute == 60)/*若minute值为60,表示已过1小时,则 hour值加1*/ { minute = 0; hour++; } if (hour == 24) /*若hour值为24,则hour的值从0开始计时*/ { hour = 0; } } /* 函数功能:时、分、秒时间的显示 函数参数:无 函数返回值:无 */ void Display(void) /*用回车符'\r'不换行,控制时、分、秒显示的位置 */ { printf("%2d:%2d:%2d\r", hour, minute, second); } /* 函数功能:模拟延迟1秒的时间 函数参数:无 函数返回值:无 */ void Delay(void) { 00; t++) { /*循环体为空语句的循环,起延时作用*/ ; }
int main(void) { long i; hour = minute = second = 0; /*hour,minute,second赋初值0*/ for (i=0; i<100000; i++) /*利用循环结构,控制时钟运行的时间*/ { Update(); /*时钟更新*/ Display(); /*时间显示*/ Delay(); /*模拟延时1秒*/ } return 0; } /* 函数功能:时、分、秒时间的更新 函数参数:无 函数返回值:无 */ void Update(void) { second++; if (second == 60)/*若second值为60,表示已过1分钟,则 minute值加
7用C#编的模拟时钟(附源代码)

使用C#模拟时钟表的一种简单制作方法PointF center=new PointF(s_pinlen+3,s_pinlen+3);//中心点位置SolidBrush sb=new SolidBrush(Color.Black);//时钟圆心的刷子除上述变量声明外,时钟表功能编写子方法,方法名为:AngleToPos和myClock,方法AngleToPos是根据角度和百分比计算出一个点的坐标函数,代码如下:PointF AngleToPos(int angle,float percent){PointF pos=new PointF();double radian=angle*Math.PI/180;pos.Y=center.Y-s_pinlen*percent*(float)Math.Sin(radian);pos.X=center.X+s_pinlen*percent*(float)Math.Cos(radian);return pos;}方法myClock主要是绘制时钟表的中心位置、秒针、分针及时针的结束位置。
代码如下: Pen pDisk=new Pen(Color.Orange,3);//时钟背景的笔Pen pScale=new Pen(Color.Coral);//刻度的笔Graphics myGraphics=pictureBox1.CreateGraphics();myGraphics.Clear(Color.White);Pen myPen=new Pen(Color.Black,2);Point CPoint=new Point(s_pinlen,s_pinlen);Point SPoint=new Point((int)(CPoint.X+(Math.Sin(6*s*Math.PI/180))* s_pinlen),(int)(CPoint.Y-(Math.Cos(6*s*Math.PI/180))*s_pinlen));Point MPoint=new Point((int)(CPoint.X+(Math.Sin(6*m*Math.PI/180))* m_pinlen),(int)(CPoint.Y-(Math.Cos(6*m*Math.PI/180))*m_pinlen));Point HPoint=new Point((int)(CPoint.X+(Math.Sin(((30*h)+(m/2))*Ma th.PI/180))*h_pinlen),(int)(CPoint.Y-(Math.Cos(((30*h)+(m/2))*Mat h.PI/180))*h_pinlen));myGraphics.FillEllipse(sb,center.X-8,center.Y-7,14,14);myGraphics.DrawLine(myPen,CPoint,SPoint);myPen=new Pen(Color.Blue,4);。
c语言机械时钟的实现

c语言机械时钟的实现#include#include#include#define PI 3.1415926536void 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++){x = 320 + int(145 * sin(PI * 2 * i / 60));y = 240 + int(145 * cos(PI * 2 * i / 60));if (i % 15 == 0)bar(x - 5, y - 5, x + 5, y + 5);else if (i % 5 == 0)circle(x, y, 3);elseputpixel(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(); // 关闭绘图窗口}。