Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main.cpp
- #include <iostream>
- #include "Clock.h"
- using namespace std;
- void test1() {
- Clock x{4320};
- Clock y{4321};
- cout << "x: " << x << endl;
- cout << "y: " << y << endl;
- cout << "x difference y " << x.difference(y) << endl;
- cout << "y difference x " << y.difference(x) << endl;
- Clock j{2,1,1};
- cout << "j " << j << endl;
- cout << "x difference j " << x.difference(j) << endl;
- cout << j.getStdTime() << endl;
- }
- int main()
- {
- test1();
- return 0;
- }
- //Clock.h
- #ifndef CLOCK_H
- #define CLOCK_H
- #include <sstream>
- #include <iostream>
- #include <stdexcept>
- class Clock {
- public:
- Clock(); //default constructor
- Clock(const int seconds);
- Clock(const int hours, const int minutes, const int seconds);
- int convertHours(const int&) const;
- int convertMinutes(const int&) const;
- int getSeconds() const;
- int getMinutes() const;
- int getHours() const;
- int getRemainSeconds() const;
- Clock difference(const Clock&) const;
- void setSeconds(const int in);
- std::string getStdTime() const; //returns clock as a string in 12hr format
- private:
- int seconds{};
- const static int secondsPerDay{86400};
- const static int secondsPerHour{3600};
- const static int secondsPerMinute{60};
- const static int minutesPerHour{60};
- const static int hoursPerDay{24};
- int checkValidHours(const int&) const;
- int checkValidMinutes(const int&) const;
- int checkValidSeconds(const int&) const;
- bool checkValidLongSeconds(const int&) const;
- void argumentError();
- int getSecond() const; //helper function only
- };
- std::ostream& operator<<(std::ostream&,const Clock);
- #endif // CLOCK_H
- //Clock.cpp
- #include "Clock.h"
- //default constructor
- Clock::Clock():seconds{0} {}
- //constructor that takes in a seconds input
- Clock::Clock(const int in) {
- seconds = checkValidSeconds(in);
- }
- //constructor that takes in hours/mins/seconds (military time)
- Clock::Clock(const int inHours, const int inMinutes, const int inSeconds ) {
- seconds = convertHours(checkValidHours(inHours)) + convertMinutes(checkValidMinutes(inMinutes)) + checkValidSeconds(inSeconds);
- }
- void Clock::argumentError() { //private
- throw std::invalid_argument("Invalid input");
- }
- int Clock::convertHours(const int& in) const {
- return in*secondsPerHour;
- }
- int Clock::convertMinutes(const int& in) const {
- return in*secondsPerMinute;
- }
- int Clock::checkValidHours(const int& in) const {
- return in % hoursPerDay;
- }
- int Clock::checkValidMinutes(const int& in) const { //private
- return in % minutesPerHour;
- }
- int Clock::checkValidSeconds(const int& in) const { //private
- return in % secondsPerDay;
- }
- bool Clock::checkValidLongSeconds(const int& in) const { //private
- return in < secondsPerDay && in >= 0;
- }
- int Clock::getSecond() const { //private
- return seconds;
- }
- void Clock::setSeconds(const int in) { //look at
- if(checkValidLongSeconds(in)) {seconds = in;}
- else {argumentError();}
- }
- int Clock::getRemainSeconds() const {
- return secondsPerDay-seconds;
- }
- int Clock::getMinutes() const {
- return (getSecond()-getHours()*secondsPerHour)/secondsPerMinute;
- }
- int Clock::getHours() const {
- return getSecond()/secondsPerHour;
- }
- int Clock::getSeconds() const {
- return getSecond()-getHours()*secondsPerHour-getMinutes()*secondsPerMinute;
- }
- std::ostream& operator<<(std::ostream& oin, Clock in) {
- oin << in.getHours() << ":" << in.getMinutes() << ":" << in.getSeconds();
- return oin;
- }
- std::string Clock::getStdTime() const {
- std::ostringstream oout{};
- if (getHours() != 12) {oout << getHours()%12 << ":" << getMinutes() << ":" << getSeconds();}
- else {oout << getHours() << ":" << getMinutes() << ":" << getSeconds(); }
- return oout.str();
- }
- Clock Clock::difference(const Clock& in) const {
- Clock result{};
- if(this->seconds > in.seconds) {
- result.setSeconds(this->seconds-in.seconds);
- }
- else {
- result.setSeconds(in.seconds-this->seconds);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement