-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.cpp
More file actions
125 lines (112 loc) · 3.15 KB
/
Clock.cpp
File metadata and controls
125 lines (112 loc) · 3.15 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
// Clock class for different clock types
#include <iostream>
#include <iomanip>
#include <string>
#include "Clock.h"
using namespace std;
// Clock constructor
Clock::Clock() {
hours = 0;
minutes = 0;
seconds = 0;
clockType = "null";
timeStatus = "AM";
}
// Mutators
void Clock::setType(string type) {
clockType = type;
}
// Accessors
string Clock::getType() {
return clockType;
}
// Print time based on clockType
void Clock::printTime() {
if (clockType == "24") {
cout << setw(2) << setfill('0') << hours << ':';
cout << setw(2) << setfill('0') << minutes << ':';
cout << setw(2) << setfill('0') << seconds;
}
else if (clockType == "12") {
cout << setw(2) << setfill('0') << hours << ':';
cout << setw(2) << setfill('0') << minutes << ':';
cout << setw(2) << setfill('0') << seconds << " " << timeStatus;
}
}
// Changes the time of a clock object by incrementing the selected field
void Clock::setTime(int option) {
switch (option) {
// Add hour
case 1:
// 24-hour clock
if (clockType == "24") {
hours++;
// Wrap hour at 24 and set zero
if (hours == 24) {
hours = 0;
}
}
// 12-hour clock
else if (clockType == "12") {
hours++;
// Wrap hour at 12 and set zero
if (hours == 13) {
hours = 1;
}
// Each time hour is 12, alternate timeStatus
if (hours == 12) {
if (timeStatus == "AM") {
timeStatus = "PM";
}
else {
timeStatus = "AM";
}
}
}
break;
// Add minute
case 2:
minutes++;
// Wrap minute at 60 and add hour
if (minutes == 60) {
minutes = 0;
setTime(1);
}
break;
// Add second
case 3:
seconds++;
// Wrap second at 60 and add minute
if (seconds == 60) {
seconds = 0;
setTime(2);
}
break;
}
}
// Displays 12 and 24 hour clocks to the user
void ShowClocks(Clock clock12, Clock clock24) {
// Print upper border
cout << setw(26) << setfill('*') << "";
cout << setw(5) << setfill(' ')<< "";
cout << setw(26) << setfill('*') << "";
cout << endl;
// Print clock titles
cout << "*" << " 12-Hour Clock " << "*";
cout << setw(5) << setfill(' ') << "";
cout << "*" << " 24-Hour Clock " << "*";
cout << endl;
// Print 12-Hour clock time
cout << "* ";
clock12.printTime();
cout << " * ";
// Print 24-Hour clock time
cout << "* ";
clock24.printTime();
cout << " *" << endl;
// Print lower border
cout << setw(26) << setfill('*') << "";
cout << setw(5) << setfill(' ')<< "";
cout << setw(26) << setfill('*') << "";
cout << endl;
}