鱼C论坛

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

[技术交流] 简单计时器

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

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

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

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

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

输出结果如图:

图一

图一

图二

图二

C++
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <memory>
  5. #include <string>
  6. #include <stdexcept>
  7. #include <chrono>
  8. #include <thread>

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

  21. using std::string;
  22. class Time {
  23. public:
  24.         int h, m, s;
  25.         string clock; // 显示时间格式 hh:mm:ss
  26.         Time(int, int, int);
  27.         Time operator ++ (int);
  28.         bool operator != (const Time&) const;
  29. };

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

  34. Time Time::operator ++ (int) {
  35.         ++s;
  36.         if (s >= 60) {
  37.                 ++m;
  38.                 s -= 60;
  39.         }
  40.         if (m >= 60)
  41.         {
  42.                 ++h;
  43.                 m -= 60;
  44.         }
  45.         string format = "%02d:%02d:%02d";
  46.         clock = stringFormat(format, h, m, s);
  47.         return Time(h, m, s);
  48. }

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

  52. using std::map, std::vector;
  53. using std::cout, std::endl;
  54. namespace Digits {

  55.         map<char, vector<string>> digits = {
  56.                 {'0', { "#####", "#   #", "#   #", "#   #", "#####" } },
  57.                 {'1', { "  #  ", " ##  ", "  #  ", "  #  ", " ### " } },
  58.                 {'2', { "#####", "    #", "#####", "#    ", "#####" } },
  59.                 {'3', { "#####", "    #", "#####", "    #", "#####" } },
  60.                 {'4', { "#   #", "#   #", "#####", "    #", "    #" } },
  61.                 {'5', { "#####", "#    ", "#####", "    #", "#####" } },
  62.                 {'6', { "#####", "#    ", "#####", "#   #", "#####" } },
  63.                 {'7', { "#####", "    #", "    #", "    #", "    #" } },
  64.                 {'8', { "#####", "#   #", "#####", "#   #", "#####" } },
  65.                 {'9', { "#####", "#   #", "#####", "    #", "#####" } },
  66.                 {':', { "     ", " []  ", "     ", " []  ", "     " } },
  67.         };

  68.         void display(Time T) {
  69.                 for (int i = 0; i < 5; i++) {
  70.                         for (const char& c : T.clock) {
  71.                                 cout << digits[c][i] << "   ";
  72.                         }
  73.                         cout << endl;
  74.                 }
  75.         }
  76. }

  77. void countDown(Time end) {
  78.         Time start(0, 0, 0);

  79.         using namespace Digits;
  80.         while (start != end) {
  81.                 system("cls");
  82.                 display(start);
  83.                 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  84.                 start++;
  85.         }
  86.         system("cls");
  87.         display(start);
  88. }

  89. int main(void) {
  90.         Time T(0, 1, 13); // 计时
  91.         countDown(T);
  92.         return 0;
  93. }
复制代码

评分

参与人数 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-5-1 10:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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