最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C语言实现动态万象时钟操作步骤及源码
时间:2026-08-15 12:00:50 编辑:袖梨 来源:一聚教程网
C语言实现动态万象时钟操作步骤及源码需要先看清适用场景和关键步骤,避免只记结论却忽略实际限制。
一、项目介绍
这是一个用C语言绘制的动态万象时钟。
编译环境:Visual Studio2019
第三方库:Easyx2022 注意需要提前安装easyX,如没有基础可以先了解easyX图形编程
二、运行截图

三、部分代码
1.引入图形头文件
#include <graphics.h>
2.定义窗口大小
#define Width 550#define Height 550
3.设定圆的结构
struct TimeCircle{int fors;// 每一圈分成的份数int R;// 圈的半径double NextTime;// 上一个时刻时间double Radian;// 累加的弧度};int main(){initgraph(Width, Height);SYSTEMTIME ti;TimeCircle TC[7];TCHAR str[25];for (int i = 0; i < 7; i++){TC[i].R = (i + 1) * 35;TC[i].Radian = 0;TC[i].NextTime = 0;switch (i){case 0:TC[i].fors = 1; break;// 年case 1:TC[i].fors = 12; break;// 月case 2:TC[i].fors = 30; break;// 日case 3:TC[i].fors = 7; break;// 周case 4:TC[i].fors = 24; break;// 时case 5:TC[i].fors = 60; break;// 分case 6:TC[i].fors = 60; break;// 秒}}BeginBatchDraw();while (true){GetLocalTime(&ti);TC[2].fors = monthdasy(ti.wYear, ti.wMonth);for (int j = 0; j < 7; j++){if (TC[5].NextTime != ti.wMinute){TC[5].NextTime = ti.wMinute;TC[5].Radian = 0;}else{TC[5].Radian = TC[5].Radian + (2 * PI / TC[5].fors - TC[5].Radian) / 10;}if (TC[6].NextTime != ti.wSecond){TC[6].NextTime = ti.wSecond;TC[6].Radian = 0;}else{TC[6].Radian = TC[6].Radian + (2 * PI / TC[6].fors - TC[6].Radian) / 10;}for (int i = 0; i < TC[j].fors; i++){switch (j){case 0: _stprintf_s(str, _T("%d年"), ti.wYear); break;case 1: _stprintf_s(str, _T("%d月"), (i + ti.wMonth) % TC[j].fors ? (i + ti.wMonth) % TC[j].fors : TC[j].fors); break;case 2: _stprintf_s(str, _T("%d号"), (i + ti.wDay) % TC[j].fors ? (i + ti.wDay) % TC[j].fors : i + ti.wDay); break;case 3: str[0] = L"周"[0]; str[1] = L"日一二三四五六"[(i + ti.wDayOfWeek) % 7]; str[2] = L""[0]; break;case 4: _stprintf_s(str, _T("%d时"), (i + ti.wHour) % TC[j].fors); break;case 5: _stprintf_s(str, _T("%d分"), (i + ti.wMinute) % TC[j].fors); break;case 6: _stprintf_s(str, _T("%d秒"), (i + ti.wSecond) % TC[j].fors); break;}DrawCircle(str, i, TC[j].fors, TC[j].R, TC[j].Radian);;}}FlushBatchDraw();cleardevice();}EndBatchDraw();return 0;}四、完整源码
#include <graphics.h>#include <easyx.h> #include <conio.h>#include <math.h>#include <time.h>#include <stdio.h>const double PI = acos(-1.0);#define Width 550#define Height 550// str代表绘制的字符串// variable 代表每次的变量// fors 每次需要循环的次数总数// R 该圈的半径// Radian 累加的弧度void DrawCircle(TCHAR str[25], int variable, int fors, int R, double Radian);// 绘制一圈字符int monthdasy(int y, int m);// 某年某月的天数 y 代表年份 m 代表月份// 一个圈的结构体struct TimeCircle{int fors;// 每一圈分成的份数int R;// 圈的半径double NextTime;// 上一个时刻时间double Radian;// 累加的弧度};int main(){initgraph(Width, Height);SYSTEMTIME ti;TimeCircle TC[7];TCHAR str[25];for (int i = 0; i < 7; i++){TC[i].R = (i + 1) * 35;TC[i].Radian = 0;TC[i].NextTime = 0;switch (i){case 0:TC[i].fors = 1; break;// 年case 1:TC[i].fors = 12; break;// 月case 2:TC[i].fors = 30; break;// 日case 3:TC[i].fors = 7; break;// 周case 4:TC[i].fors = 24; break;// 时case 5:TC[i].fors = 60; break;// 分case 6:TC[i].fors = 60; break;// 秒}}BeginBatchDraw();while (true){GetLocalTime(&ti);TC[2].fors = monthdasy(ti.wYear, ti.wMonth);for (int j = 0; j < 7; j++){if (TC[5].NextTime != ti.wMinute){TC[5].NextTime = ti.wMinute;TC[5].Radian = 0;}else{TC[5].Radian = TC[5].Radian + (2 * PI / TC[5].fors - TC[5].Radian) / 10;}if (TC[6].NextTime != ti.wSecond){TC[6].NextTime = ti.wSecond;TC[6].Radian = 0;}else{TC[6].Radian = TC[6].Radian + (2 * PI / TC[6].fors - TC[6].Radian) / 10;}for (int i = 0; i < TC[j].fors; i++){switch (j){case 0: _stprintf_s(str, _T("%d年"), ti.wYear); break;case 1: _stprintf_s(str, _T("%d月"), (i + ti.wMonth) % TC[j].fors ? (i + ti.wMonth) % TC[j].fors : TC[j].fors); break;case 2: _stprintf_s(str, _T("%d号"), (i + ti.wDay) % TC[j].fors ? (i + ti.wDay) % TC[j].fors : i + ti.wDay); break;case 3: str[0] = L"周"[0]; str[1] = L"日一二三四五六"[(i + ti.wDayOfWeek) % 7]; str[2] = L""[0]; break;case 4: _stprintf_s(str, _T("%d时"), (i + ti.wHour) % TC[j].fors); break;case 5: _stprintf_s(str, _T("%d分"), (i + ti.wMinute) % TC[j].fors); break;case 6: _stprintf_s(str, _T("%d秒"), (i + ti.wSecond) % TC[j].fors); break;}DrawCircle(str, i, TC[j].fors, TC[j].R, TC[j].Radian);;}}FlushBatchDraw();cleardevice();}EndBatchDraw();return 0;}int monthdasy(int y, int m){if (m == 2)return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) ? 29 : 28;elsereturn 31 - (m - 3) % 5 % 2;}void DrawCircle(TCHAR str[25], int variable, int fors, int R, double Radian){settextcolor(variable ? HSLtoRGB((360.f / fors) * variable, 1, 0.5f) : WHITE);double a, x0, y0, w, h, x1, y1;int x, y;settextstyle(16, 0, L"楷体", variable * 3600 / fors, variable * 3600 / fors, 0, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH);a = (fors == 60) ? ((variable + 1) * PI * 2 / fors - Radian) : (variable * PI * 2 / fors);// 计算字符串弧度 aw = textwidth(str);// 计算字符串宽 w、高 hh = textheight(str);x1 = R * cos(a);// 计算输出字符串的左上角位置y1 = R * sin(a);x0 = x1 * cos(-a) - y1 * sin(-a);// 将字符串绕原点顺时针旋转 a 弧度y0 = y1 * cos(-a) + x1 * sin(-a);x0 -= w / 2;// 将字符串向左上偏移 w/2、h/2y0 += h / 2;// 绘图坐标向下为正x = (int)(x0 * cos(a) - y0 * sin(a));// 将字符串绕原点逆时针旋转 a 弧度y = (int)(y0 * cos(a) + x0 * sin(a));outtextxy(int(Width / 2 + x + 0.5), int(Height / 2 - y + 0.5), str);// 绘图坐标向下为正}
相关文章
- 崩铁4.2绯英培养材料详情 08-15
- 拍照搜题网页版入口-夸克AI搜题官方网页版直达 08-15
- 王者荣耀世界元流之子+西施队配队玩法详情 08-15
- 亿方云网页版登录入口-亿方云官网登录入口 08-15
- 朱砂痣久难消你是否能知道歌曲介绍 08-15
- POKI.免费游戏免下载入口-POKI.免费游戏入口多端同步 08-15