This repository was archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdigital_clock.cpp
More file actions
233 lines (199 loc) · 4.41 KB
/
digital_clock.cpp
File metadata and controls
233 lines (199 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include<iostream>
#include<conio.h>
#include<dos.h>
#include <windows.h>
#include <time.h>
using namespace std;
char d0[5][3] = { 178,178,178,
178,' ',178,
178,' ',178,
178,' ',178,
178,178,178 };
char d1[5][3] = { ' ',' ',178,
' ',' ',178,
' ',' ',178,
' ',' ',178,
' ',' ',178 };
char d2[5][3] = { 178,178,178,
' ',' ',178,
178,178,178,
178,' ',' ',
178,178,178 };
char d3[5][3] = { 178,178,178,
' ',' ',178,
178,178,178,
' ',' ',178,
178,178,178 };
char d4[5][3] = { 178,' ',178,
178,' ',178,
178,178,178,
' ',' ',178,
' ',' ',178 };
char d5[5][3] = { 178,178,178,
178,' ',' ',
178,178,178,
' ',' ',178,
178,178,178 };
char d6[5][3] = { 178,178,178,
178,' ',' ',
178,178,178,
178,' ',178,
178,178,178 };
char d7[5][3] = { 178,178,178,
' ',' ',178,
' ',' ',178,
' ',' ',178,
' ',' ',178 };
char d8[5][3] = { 178,178,178,
178,' ',178,
178,178,178,
178,' ',178,
178,178,178 };
char d9[5][3] = { 178,178,178,
178,' ',178,
178,178,178,
' ',' ',178,
' ',' ',178 };
char sep[5][3] = { ' ',' ',' ',
' ',178,' ',
' ',' ',' ',
' ',178,' ',
' ',' ',' ' };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoxy(int x, int y){
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);
}
void setcursor(bool visible, DWORD size) {
if(size == 0)
size = 20;
CONSOLE_CURSOR_INFO lpCursor;
lpCursor.bVisible = visible;
lpCursor.dwSize = size;
SetConsoleCursorInfo(console,&lpCursor);
}
void printDigit(int no, int x, int y ){
for(int i=0; i<5; i++){
for(int j=0; j<3; j++){
switch(no){
case 0:
gotoxy(x+j, y+i); cout<<d0[i][j]; break;
case 1:
gotoxy(x+j, y+i); cout<<d1[i][j]; break;
case 2:
gotoxy(x+j, y+i); cout<<d2[i][j]; break;
case 3:
gotoxy(x+j, y+i); cout<<d3[i][j]; break;
case 4:
gotoxy(x+j, y+i); cout<<d4[i][j]; break;
case 5:
gotoxy(x+j, y+i); cout<<d5[i][j]; break;
case 6:
gotoxy(x+j, y+i); cout<<d6[i][j]; break;
case 7:
gotoxy(x+j, y+i); cout<<d7[i][j]; break;
case 8:
gotoxy(x+j, y+i); cout<<d8[i][j]; break;
case 9:
gotoxy(x+j, y+i); cout<<d9[i][j]; break;
case 10:
gotoxy(x+j, y+i); cout<<sep[i][j]; break;
}
}
}
}
void drawBorder(int x=0, int y=0){
char v = 186; // vertical
char h = 205; // horizontal
char tr = 187;
char br = 188;
char tl = 201;
char bl = 200;
int width = 50;
int height = 11;
gotoxy((width/2)+x-11, y); cout<<"C U R R E N T T I M E";
for( int i=1; i<=height; i++ ){
for( int j=1; j<=width; j++ ){
gotoxy(j+x,i+y);
if( i==1 && j==1 ) cout<< tl;
else if( i==height && j==1 ) cout<< bl;
else if( i==1 && j==width ) cout<< tr;
else if( i==height && j==width ) cout<< br;
else if( i==1 || i==height ) cout<< h;
else if( j==1 || j==width ) cout<< v;
}
}
}
int main() {
system("cls");
setcursor(0,0);
srand((unsigned) time(NULL));
time_t now = time(0);
// convert now to string form
char* dt = ctime(&now);
// convert now to tm struct for UTC
tm *lotm = localtime(&now);
dt = asctime(lotm);
int hour = lotm->tm_hour;
int min = lotm->tm_min;
int sec = lotm->tm_sec;
int gap = 5;
int posX;
int posY = 8;
while(1){
system("cls");
drawBorder(9,4);
posX = 15;
if(kbhit()){
char ch = getch();
if(ch==27){
break;
}
}
if( hour < 10 ){
printDigit(0, posX, posY);
printDigit(hour, posX+=gap, posY);
}
else{
printDigit(hour/10, posX, posY);
printDigit(hour%10, posX+=gap, posY);
}
printDigit(10, posX+=gap, posY);
if( min < 10 ){
printDigit(0, posX+=gap, posY);
printDigit(min, posX+=gap, posY);
}
else{
printDigit(min/10, posX+=gap, posY);
printDigit(min%10, posX+=gap, posY);
}
printDigit(10, posX+=gap, posY);
if( sec < 10 ){
printDigit(0, posX+=gap, posY);
printDigit(sec, posX+=gap, posY);
}
else{
printDigit(sec/10, posX+=gap, posY);
printDigit(sec%10, posX+=gap, posY);
}
//50 millisecond processing time is deducted, you can change it...
Sleep(950);
sec++;
if( sec>=60 ){
sec = 0;
min++;
}
if( min>=60 ){
min = 0;
hour++;
}
if( hour>=24 ){
sec = 0;
min = 0;
hour = 0;
}
}
return 0;
}