c++控制台时钟显示时间

在旧电脑上刷了ubuntu的服务器版本,开着的时候旧电脑(yoga)的屏幕就处于了无用的状态。为了更好的利用这个屏幕,简单使用c++写了一个显示时间的代码。

由于这台yoga的屏幕是可以360翻转的,所以给时间的显示也增加了一点翻转功能。

  1 #include <iostream>
  2 #include <ctime>
  3 #include <chrono>
  4 #include <thread>
  5 #include <string>
  6 using namespace std;
  7 const int MATRIX_WIDTH = 17;
  8 const int MATRIX_HEIGHT = 14;
  9 #ifdef _WIN32
 10     #define CLEAR_SCREEN "cls"
 11 #else
 12     #define CLEAR_SCREEN "clear"
 13 #endif
 14 void clear_screen() {
 15     std::system(CLEAR_SCREEN);
 16 }
 17 // 定义数字0到9的点阵表示
 18 const char digits[10][MATRIX_HEIGHT][MATRIX_WIDTH] = {
 19     {
 20 
 21         "    .XEEEEb     ",
 22         "   MEEEUXEEE8   ",
 23         "  NEE.    OEEC  ",
 24         " ,EEj      EEE  ",
 25         " HEE       jEE1 ",
 26         " EEH        EEZ ",
 27         ".EEZ        EEM ",
 28         " EEZ        EEM ",
 29         " EEN       .EEG ",
 30         " LEE       3EE: ",
 31         " .EEc      EEE  ",
 32         "  MEE7    NEE.  ",
 33         "   bEEEEEEEE.   ",
 34         "     DEEEL7     "
 35 
 36     },
 37     {
 38 
 39         "      ,:LHL     ",
 40         "    jNEEEEE     ",
 41         "     EY'MEE     ",
 42         "        HEE     ",
 43         "        NEE     ",
 44         "        KEE     ",
 45         "        KEE     ",
 46         "        KEE     ",
 47         "        KEE     ",
 48         "        KEE     ",
 49         "        KEE     ",
 50         "        EEE     ",
 51         "        EEE     ",
 52         "        CGD     "
 53 
 54     },
 55     {
 56 
 57         "   :LEEEEEG     ",
 58         "  EEEEHMEEEEU   ",
 59         "  OC      LEEc  ",
 60         "           EEE  ",
 61         "           NEE  ",
 62         "          :KEE  ",
 63         "          KEE   ",
 64         "        :KEE    ",
 65         "       bKEE     ",
 66         "     .KEE       ",
 67         "    bKEE        ",
 68         "  jEEE          ",
 69         " NEEEEEEEEEEEE  ",
 70         " 3GD3DOGGGGGUX  "
 71 
 72     },
 73     {
 74 
 75         "   .CNEEEEE8    ",
 76         "   EEEELLEEEEc  ",
 77         "   :'      EEE  ",
 78         "           cEE: ",
 79         "           EEE  ",
 80         "     .::jZEEG   ",
 81         "     EEEEEEC    ",
 82         "        'jNEEZ  ",
 83         "           7EEM ",
 84         "            KEE ",
 85         "            KEE ",
 86         "   C       EEE1 ",
 87         "  bEEEEEEEEEE7  ",
 88         "   :DHEEEN8.    "
 89 
 90     },
 91     {
 92 
 93         "         bMNj   ",
 94         "        NEEEU   ",
 95         "       EEGEE3   ",
 96         "      EEU EEJ   ",
 97         "     EEM  EEJ   ",
 98         "    EEU   EEJ   ",
 99         "  .EEc    EEC   ",
100         " :EE      EE7   ",
101         "jEEN738ODDEEM3b ",
102         "LEEEEEEEEEEEEEE ",
103         "          EE1   ",
104         "          EEC   ",
105         "          EEd   ",
106         "          bUd   "
107 
108     },
109     {
110 
111         "    NHKKEEEEEX  ",
112         "   7EEEEEEEEEK  ",
113         "   8EN          ",
114         "   NEC          ",
115         "   EE           ",
116         "  .EEEEEENC     ",
117         "  :X3DGMEEEEU   ",
118         "          MEEU  ",
119         "           EEE  ",
120         "           XEE  ",
121         "           EEE  ",
122         "  j      :EEE   ",
123         " JEEEEEEEEEN    ",
124         "  7GNEEEMc      "
125 
126     },
127     {
128 
129         "       1LEEE1   ",
130         "    :EEEEEEN,   ",
131         "   MEEM.        ",
132         "  EEE           ",
133         " LEE   ..       ",
134         " EE77EEEEEEL    ",
135         "3EEEED.'.GEEE.  ",
136         "LEEb       EEE  ",
137         "MEE        8EE, ",
138         "8EE        cEE: ",
139         " EEX       EEE  ",
140         " CEEG     LEEj  ",
141         "  jEEEEEEEEE7   ",
142         "    7LEEEX:     "
143 
144     },
145     {
146 
147         " KEEEEEEEKNMHH  ",
148         " EEEEEEEEEEEEE  ",
149         "          :EE.  ",
150         "          EEJ   ",
151         "         EEK    ",
152         "        NEE     ",
153         "       CEE.     ",
154         "      .EE8      ",
155         "      EEE       ",
156         "     NEE        ",
157         "    3EE:        ",
158         "   .EEU         ",
159         "  .EEE          ",
160         "  1XG           ",
161 
162     },
163     {
164 
165         "    8EEEEEL.    ",
166         "  OEEEGC8EEEM   ",
167         " 1EEj     :EEO  ",
168         " EEE       EEE  ",
169         " DEEj     :EE7  ",
170         "  UEENj  bEE7   ",
171         "    EEEEEEE     ",
172         "  DEEL:.8EEEM   ",
173         " EEE      ,EEE  ",
174         "7EE1       jEE1 ",
175         "cEEc       7EEj ",
176         " EEE:     .EEE  ",
177         "  KEEEEHEEEEL   ",
178         "    JHEEEM1     "
179 
180     },
181     {
182 
183         "    cEEEEEO     ",
184         "  1EEELOLEEE3   ",
185         " 1EE3     DEEc  ",
186         " EEN       KEE  ",
187         ",EE1       jEE  ",
188         ".EEX       :EE. ",
189         " EEEj     :EEE  ",
190         "  NEEENMEEEHEE  ",
191         "   .bEEEEC XEE  ",
192         "          :EE:  ",
193         "         CEEG   ",
194         "       1EEEJ    ",
195         "  8EEEEEEX      ",
196         "  COLIN'        "
197 
198     }};
199 
200 string digitst[MATRIX_HEIGHT] = {
201     "##################################     #############################################",
202     "##################################     #############################################",
203     "##################################  #  #############################################",
204     "################################## ### #############################################",
205     "##################################  #  #############################################",
206     "##################################     #############################################",
207     "##################################     #############################################",
208     "##################################     #############################################",
209     "##################################  #  #############################################",
210     "################################## ### #############################################",
211     "##################################  #  #############################################",
212     "##################################     #############################################",
213     "##################################     #############################################",
214     "##################################     #############################################"};
215 
216 int ascii_to_int(char a)
217 {
218     int res = int(a) - 48;
219     if (res > 9 || res < 0)
220         return 0;
221     return res;
222 }
223 
224 void display_clock(const string &c)
225 {
226 
227     for (int row = 0; row < MATRIX_HEIGHT; row++)
228     {
229         for (int col = 0; col < MATRIX_WIDTH; col++)
230         {
231             digitst[row][col] = digits[ascii_to_int(c[0])][row][col];
232         }
233     }
234     for (int row = 0; row < MATRIX_HEIGHT; row++)
235     {
236         for (int col = 0; col < MATRIX_WIDTH; col++)
237         {
238             digitst[row][col + MATRIX_WIDTH] = digits[ascii_to_int(c[1])][row][col];
239         }
240     }
241     for (int row = 0; row < MATRIX_HEIGHT; row++)
242     {
243         for (int col = 0; col < MATRIX_WIDTH; col++)
244         {
245             digitst[row][col + MATRIX_WIDTH * 2 + 5] = digits[ascii_to_int(c[2])][row][col];
246         }
247     }
248     for (int row = 0; row < MATRIX_HEIGHT; row++)
249     {
250         for (int col = 0; col < MATRIX_WIDTH; col++)
251         {
252             digitst[row][col + MATRIX_WIDTH * 3 + 5] = digits[ascii_to_int(c[3])][row][col];
253         }
254     }
255 
256     for (int i = 0; i < MATRIX_HEIGHT; i++)
257     {
258         for (int j = 0; j < MATRIX_WIDTH * 4 + 5; j++)
259         {
260             cout << digitst[i][j];
261         }
262         cout << endl;
263     }
264 }
265 
266 void display_clock_turn180(const string &c)
267 {
268     for (int row = 0; row < MATRIX_HEIGHT; row++)
269     {
270         for (int col = 0; col < MATRIX_WIDTH; col++)
271         {
272             digitst[row][col + MATRIX_WIDTH * 3 + 5] = digits[ascii_to_int(c[0])][MATRIX_HEIGHT - 1 - row][MATRIX_WIDTH - 1 - col];
273         }
274     }
275     for (int row = 0; row < MATRIX_HEIGHT; row++)
276     {
277         for (int col = 0; col < MATRIX_WIDTH; col++)
278         {
279             digitst[row][col + MATRIX_WIDTH * 2 + 5] = digits[ascii_to_int(c[1])][MATRIX_HEIGHT - 1 - row][MATRIX_WIDTH - 1 - col];
280         }
281     }
282     for (int row = 0; row < MATRIX_HEIGHT; row++)
283     {
284         for (int col = 0; col < MATRIX_WIDTH; col++)
285         {
286             digitst[row][col + MATRIX_WIDTH] = digits[ascii_to_int(c[2])][MATRIX_HEIGHT - 1 - row][MATRIX_WIDTH - 1 - col];
287         }
288     }
289     for (int row = 0; row < MATRIX_HEIGHT; row++)
290     {
291         for (int col = 0; col < MATRIX_WIDTH; col++)
292         {
293             digitst[row][col] = digits[ascii_to_int(c[3])][MATRIX_HEIGHT - 1 - row][MATRIX_WIDTH - 1 - col];
294         }
295     }
296 
297     for (int i = 0; i < MATRIX_HEIGHT; i++)
298     {
299         for (int j = 0; j < MATRIX_WIDTH * 4 + 5; j++)
300         {
301             cout << digitst[i][j];
302         }
303         cout << endl;
304     }
305 }
306 
307 void while_display_clock()
308 {
309     while (true)
310     {
311         clear_screen();
312         std::time_t currentTime = std::time(nullptr);
313         std::tm *localTime = std::localtime(&currentTime);
314         string sh = to_string(localTime->tm_hour);
315         string sm = to_string(localTime->tm_min);
316         if (sh.size() == 1)
317         {
318             sh = '0' + sh;
319         }
320         if (sm.size() == 1)
321         {
322             sm = '0' + sm;
323         }
324 
325         display_clock(sh + sm);
326         std::this_thread::sleep_for(std::chrono::seconds(1));
327     }
328 }
329 
330 void while_display_clock_turn180()
331 {
332     while (true)
333     {
334         clear_screen();
335         std::time_t currentTime = std::time(nullptr);
336         std::tm *localTime = std::localtime(&currentTime);
337         string sh = to_string(localTime->tm_hour);
338         string sm = to_string(localTime->tm_min);
339         if (sh.size() == 1)
340         {
341             sh = '0' + sh;
342         }
343         if (sm.size() == 1)
344         {
345             sm = '0' + sm;
346         }
347 
348         display_clock_turn180(sh + sm);
349         std::this_thread::sleep_for(std::chrono::seconds(1));
350     }
351 }
352 int main()
353 {
354     while_display_clock_turn180();
355     return 0;
356 }

最终效果如下

 

posted @ 2023-08-02 21:20  duxingmengshou  阅读(118)  评论(0编辑  收藏  举报