easyx,c++,针式钟表

要求

在屏幕上显示一个图形时钟,时间与系统时间一致,且要随着时间
的走动准确的走动。能模拟机械钟表行走,还要准确地
利用数字显示日期和时间,在屏幕上显示一个活动时钟
按任意键时程序退出。标注设计人为你自己

代码

#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <math.h>

#define PI 3.1415926

int main()
{
    initgraph(400, 500); // 创建绘图窗口

    cleardevice(); // 清空窗口

    while (!kbhit()) // 当没有按下键盘按键时循环
    {
        cleardevice(); // 清空窗口

        time_t t = time(0); // 获取当前时间
        tm* now = localtime(&t); // 转换为本地时间

        int hour = now->tm_hour; // 获取当前小时
        int minute = now->tm_min; // 获取当前分钟
        int second = now->tm_sec; // 获取当前秒

        // 画表盘
        setlinecolor(BLACK);
        setlinestyle(PS_SOLID, 2);
        circle(200, 200, 180);

        // 设置填充颜色
        setfillcolor(BLUE);
        fillcircle(200, 200, 180);		// 填充圆

        // 标注钟点
        for (int i = 1; i <= 12; i++)
        {
            int x = 200 + 150 * sin(PI * i * 30 / 180);
            int y = 200 - 150 * cos(PI * i * 30 / 180);
            TCHAR strNum[3];
            _stprintf_s(strNum, _T("%d"), i);
            outtextxy(x, y, strNum);
        }

        // 显示当前时间信息
        TCHAR strDate[50];
        _stprintf_s(strDate, _T("Date: %04d-%02d-%02d"), now->tm_year + 1900, now->tm_mon + 1, now->tm_mday);
        outtextxy(150, 250, strDate);

        TCHAR strTime[50];
        _stprintf_s(strTime, _T("Time: %02d:%02d:%02d"), now->tm_hour, now->tm_min, now->tm_sec);
        outtextxy(155, 280, strTime);

        // 画时针
        setlinecolor(GREEN);
        setlinestyle(PS_SOLID, 5);
        line(200, 200, 200 + 60 * sin(PI * (hour * 30 + minute / 2) / 180), 200 - 60 * cos(PI * (hour * 30 + minute / 2) / 180));

        // 画分针        
        setlinecolor(YELLOW);
        setlinestyle(PS_SOLID, 4);
        line(200, 200, 200 + 100 * sin(PI * minute * 6 / 180), 200 - 100 * cos(PI * minute * 6 / 180));

        // 画秒针
        setlinecolor(RED);
        setlinestyle(PS_SOLID, 2);
        line(200, 200, 200 + 150 * sin(PI * second * 6 / 180), 200 - 150 * cos(PI * second * 6 / 180));


        //写名
        outtextxy(150, 400, L"设计人:***");
        Sleep(1000); // 延时1秒
    }

    closegraph(); // 关闭绘图窗口

    return 0;
}

运行结果

在这里插入图片描述

注意事项

1.需要去官网下载easyx(一个绘图库)(有人说只能只支持vc,本人vs2022,可以用)
在这里插入图片描述
在这里插入图片描述
2.记得把设计人改一下噢~

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值