add solution of lab01

This commit is contained in:
2025-01-20 02:18:25 -05:00
parent 0dd22a2682
commit 8b451ff045
5 changed files with 141 additions and 11 deletions

61
labs/classes/time.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "time.h"
#include <iostream>
#include <iomanip>
//init hour, minute, second to 0
Time::Time() {
hour = 0;
minute = 0;
second = 0;
}
Time::Time(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const {
return second;
}
void Time::setHour(int h) {
hour = h;
}
void Time::setMinute(int m) {
minute = m;
}
void Time::setSecond(int s) {
second = s;
}
void Time::PrintAMPM() const {
int hh = hour;
std::string ampm = "am";
if (hh == 0) {
hh = 12;
} else if (hh == 12) {
ampm = "pm";
} else if (hh > 12) {
hh -= 12;
ampm = "pm";
}
std::cout << hh << ":";
if (minute < 10) {
std::cout << "0" << minute << ":";
} else {
std::cout << minute << ":";
}
std::cout << std::setfill('0') << std::setw(2) << second;
std::cout << " " << ampm << std::endl;
}