已生成时钟小程序链接:https://download.csdn.net/download/qq_31293403/86513976
#define _stprintf
#define _stprintf_s
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<tchar.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define PI 3.14159
#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 b(a,b,c) X(a,b,c);Y(a,b,c);
void Draw(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; // 时、分、秒针的末端位置
int x4, y4;
// 计算时、分、秒针的弧度值
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 = 320 + (int)(120 * sin(a_sec));
y_sec = 240 - (int)(120 * cos(a_sec));
x_min = 320 + (int)(100 * sin(a_min));
y_min = 240 - (int)(100 * cos(a_min));
x_hour = 320 + (int)(70 * sin(a_hour));
y_hour = 240 - (int)(70 * cos(a_hour));
// 画时针
setlinestyle(SOLID_FILL,2,0,10);
setcolor(WHITE);
line(320, 240, x_hour, y_hour);
// 画分针
setlinestyle(SOLID_FILL,2,0,6);
setcolor(LIGHTGRAY);
line(320, 240, x_min, y_min);
// 画秒针
setlinestyle(SOLID_FILL,2,0,2);
setcolor(RED);
line(320, 240, x_sec, y_sec);
/*画十二时辰*/
TCHAR s1[12];//分配用来保存十二时辰字符的指针数组
x4 = 175 * sin(0.5 * PI / 180) + 316;
y4 = 175 * -cos(0.5 * PI / 180) + 230;
setcolor(LIGHTCYAN);
_stprintf_s(s1, _T("子"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(30 * PI / 180) + 316;
y4 = 175 * -cos(30 * PI / 180) + 230;
_stprintf_s(s1, _T("丑"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(60 * PI / 180) + 316;
y4 = 175 * -cos(60 * PI / 180) + 230;
_stprintf_s(s1, _T("寅"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(90 * PI / 180) + 316;
y4 = 175 * -cos(90 * PI / 180) + 230;
_stprintf_s(s1, _T("卯"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(120 * PI / 180) + 316;
y4 = 175 * -cos(120 * PI / 180) + 230;
_stprintf_s(s1, _T("辰"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(150 * PI / 180) + 316;
y4 = 175 * -cos(150 * PI / 180) + 230;
_stprintf_s(s1, _T("巳"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(180 * PI / 180) + 316;
y4 = 175 * -cos(180 * PI / 180) + 230;
_stprintf_s(s1, _T("午"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(210 * PI / 180) + 316;
y4 = 175 * -cos(210 * PI / 180) + 230;
_stprintf_s(s1, _T("未"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(240 * PI / 180) + 316;
y4 = 175 * -cos(240 * PI / 180) + 230;
_stprintf_s(s1, _T("申"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(270 * PI / 180) + 316;
y4 = 175 * -cos(270 * PI / 180) + 230;
_stprintf_s(s1, _T("酉"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(300 * PI / 180) + 316;
y4 = 175 * -cos(300 * PI / 180) + 230;
_stprintf_s(s1, _T("戌"));
outtextxy(x4, y4, s1);
x4 = 175 * sin(330 * PI / 180) + 316;
y4 = 175 * -cos(330 * PI / 180) + 230;
_stprintf_s(s1, _T("亥"));
outtextxy(x4, y4, s1);
/*绘制八卦字符*/
TCHAR s2[8];
x4 = 202 * sin(0.5 * PI / 180) + 316;
y4 = 202 * -cos(0.5 * PI / 180) + 230;
_stprintf_s(s2, _T("坎"));
outtextxy(x4, y4, s2);
x4 = 202 * sin(45 * PI / 180) + 316;
y4 = 202 * -cos(45 * PI / 180) + 230;
_stprintf_s(s2, _T("艮"));
outtextxy(x4, y4, s2);
x4 = 202 * sin(90 * PI / 180) + 316;
y4 = 202 * -cos(90 * PI / 180) + 230;
_stprintf_s(s2, _T("震"));
outtextxy(x4, y4, s2);
x4 = 202 * sin(135 * PI / 180) + 316;
y4 = 202 * -cos(135 * PI / 180) + 230;
_stprintf_s(s2, _T("巽"));
outtextxy(x4, y4, s2);
x4 = 202 * sin(180 * PI / 180) + 316;
y4 = 202 * -cos(180 * PI / 180) + 230;
_stprintf_s(s2, _T("离"));
outtextxy(x4, y4, s2);
x4 = 202 * sin(225 * PI / 180) + 316;
y4 = 202 * -cos(225 * PI / 180) + 230;
_stprintf_s(s2, _T("坤"));
outtextxy(x4, y4, s2);
x4 = 202 * sin(270 * PI / 180) + 316;
y4 = 202 * -cos(270 * PI / 180) + 230;
_stprintf_s(s2, _T("兑"));
outtextxy(x4, y4, s2);
x4 = 202 * sin(315 * PI / 180) + 316;
y4 = 202 * -cos(315 * PI / 180) + 230;
_stprintf_s(s2, _T("乾"));
outtextxy(x4, y4, s2);
}
int main()
{
int i, j, k, l, m, x1, x2, y1, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7;
initgraph(650, 500, 0);
/*绘制罗盘*/
setcolor(LIGHTGREEN);
circle(320, 240, 4);//指针中心点
circle(320, 240, 160);//数字时钟圆
circle(320, 240, 190);//十二时辰圆
circle(320, 240, 220);//八卦圆
for (i = 0; i < 60; i++) /*绘数字钟刻度*/
{
if (i % 5 == 0) {
l = 15;
}
else {
l = 5;
}
x1 = 160 * sin(i * 6 * PI / 180) + 320;
y1 = 160 * cos(i * 6 * PI / 180) + 240;
x2 = (160 - l)*sin(i * 6 * PI / 180) + 320;
y2 = (160 - l)*cos(i * 6 * PI / 180) + 240;
line(x1, y1, x2, y2);
}
for (j = 0; j <= 12; j++)/*绘钟表数字*/
{
x3 = 136 * sin(j * 30 * PI / 180) + 316;
y3 = 136 * -cos(j * 30 * PI / 180) + 230;
setcolor(LIGHTCYAN);
TCHAR s[12];
_stprintf(s, _T("%d"), j);
outtextxy(x3, y3, s);
}
for (k = 0; k < 12; k++)/*绘十二时辰刻度*/
{
l = 30;
x4 = 190 * sin(k * 30 * PI / 180+6) + 320;
y4 = 190 * cos(k * 30 * PI / 180+6) + 240;
x5 = (190-l) * sin(k * 30 * PI / 180+6) + 320;
y5 = (190-l) * cos(k * 30 * PI / 180+6) + 240;
line(x4, y4, x5, y5);
}
for (m = 0; m < 9; m++)/*绘制八卦刻度*/
{
x6 = 220 * sin(m * 45 * PI / 180-2) + 320;
y6 = 220 * cos(m * 45 * PI / 180-2) + 240;
x7 = (220 - l)*sin(m * 45 * PI / 180-2) + 320;
y7 = (220 - l)*cos(m * 45 * PI / 180-2) + 240;
line(x6, y6, x7, y7);
}
setwritemode(R2_XORPEN); // 设置 XOR 绘图模式
// 绘制表针
SYSTEMTIME ti; // 定义变量保存当前时间
while (!_kbhit())//按任意键退出
{
GetLocalTime(&ti);
Draw(ti.wHour, ti.wMinute, ti.wSecond); // 画表针
Sleep(1000); // 延时 1 秒
Draw(ti.wHour, ti.wMinute, ti.wSecond); // 擦表针
}
closegraph();
}
此程序默认编译IDE(VS2017)——使用之前请将字符集模式设置为“宽字符字集”,否则会乱码!