鱼C论坛

 找回密码
 立即注册
查看: 1773|回复: 4

[技术交流] 简单计时器

[复制链接]
发表于 2022-4-24 17:39:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 傻眼貓咪 于 2022-4-24 17:42 编辑

因为无聊,所以又乱写代码了。因为花费时间不长(20 分钟吧),随便写的,不是很优,见笑了

输出结果如图:

图一

图一

图二

图二

C++
#include <iostream>
#include <map>
#include <vector>
#include <memory>
#include <string>
#include <stdexcept>
#include <chrono>
#include <thread>

template<typename ... Args>
std::string stringFormat(const std::string& format, Args ... args)
{
        int n = std::snprintf(nullptr, 0, format.c_str(), args ...) + 1;
        if (n <= 0) {
                throw std::runtime_error("Error during formatting."); // 异常处理
        }
        auto size = static_cast<size_t> (n);
        std::unique_ptr<char[]> buf(new char[size]); // 智能指针实现,防止内存泄漏
        std::snprintf(buf.get(), size, format.c_str(), args ...);
        return std::string(buf.get(), buf.get() + size - 1);
}

using std::string;
class Time {
public:
        int h, m, s;
        string clock; // 显示时间格式 hh:mm:ss
        Time(int, int, int);
        Time operator ++ (int);
        bool operator != (const Time&) const;
};

Time::Time(int hour, int minute, int second) : h(hour), m(minute), s(second) {
        string format = "%02d:%02d:%02d";
        clock = stringFormat(format, h, m, s);
}

Time Time::operator ++ (int) {
        ++s;
        if (s >= 60) {
                ++m;
                s -= 60;
        }
        if (m >= 60)
        {
                ++h;
                m -= 60;
        }
        string format = "%02d:%02d:%02d";
        clock = stringFormat(format, h, m, s);
        return Time(h, m, s);
}

bool Time::operator != (const Time &T)const {
        return (h != T.h) || (m != T.m) || (s != T.s);
}

using std::map, std::vector;
using std::cout, std::endl;
namespace Digits {

        map<char, vector<string>> digits = {
                {'0', { "#####", "#   #", "#   #", "#   #", "#####" } },
                {'1', { "  #  ", " ##  ", "  #  ", "  #  ", " ### " } },
                {'2', { "#####", "    #", "#####", "#    ", "#####" } },
                {'3', { "#####", "    #", "#####", "    #", "#####" } },
                {'4', { "#   #", "#   #", "#####", "    #", "    #" } },
                {'5', { "#####", "#    ", "#####", "    #", "#####" } },
                {'6', { "#####", "#    ", "#####", "#   #", "#####" } },
                {'7', { "#####", "    #", "    #", "    #", "    #" } },
                {'8', { "#####", "#   #", "#####", "#   #", "#####" } },
                {'9', { "#####", "#   #", "#####", "    #", "#####" } },
                {':', { "     ", " []  ", "     ", " []  ", "     " } },
        };

        void display(Time T) {
                for (int i = 0; i < 5; i++) {
                        for (const char& c : T.clock) {
                                cout << digits[c][i] << "   ";
                        }
                        cout << endl;
                }
        }
}

void countDown(Time end) {
        Time start(0, 0, 0);

        using namespace Digits;
        while (start != end) {
                system("cls");
                display(start);
                std::this_thread::sleep_for(std::chrono::milliseconds(1000));
                start++;
        }
        system("cls");
        display(start);
}

int main(void) {
        Time T(0, 1, 13); // 计时
        countDown(T);
        return 0;
}

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +5 收起 理由
jackz007 + 5 + 5 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-24 18:20:58 | 显示全部楼层
牛!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-25 18:37:49 | 显示全部楼层
666  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-24 17:15:41 | 显示全部楼层
这是面向对象与面向过程相结合的吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2023-9-29 11:12:00 | 显示全部楼层
牛!有Python的代码吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 01:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表