Advertisement
bwukki

Clock (CS162)

May 9th, 2018
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. //Main.cpp
  2.  
  3. #include <iostream>
  4. #include "Clock.h"
  5.  
  6. using namespace std;
  7.  
  8. void test1() {
  9.     Clock x{4320};
  10.     Clock y{4321};
  11.     cout << "x: " << x << endl;
  12.     cout << "y: " << y << endl;
  13.     cout << "x difference y " << x.difference(y) << endl;
  14.     cout << "y difference x " << y.difference(x) << endl;
  15.     Clock j{2,1,1};
  16.     cout << "j " << j << endl;
  17.     cout << "x difference j " << x.difference(j) << endl;
  18.     cout << j.getStdTime() << endl;
  19. }
  20.  
  21. int main()
  22. {
  23.     test1();
  24.     return 0;
  25. }
  26.  
  27.  
  28. //Clock.h
  29.  
  30. #ifndef CLOCK_H
  31. #define CLOCK_H
  32. #include <sstream>
  33. #include <iostream>
  34. #include <stdexcept>
  35.  
  36.  
  37. class Clock {
  38. public:
  39.     Clock(); //default constructor
  40.     Clock(const int seconds);
  41.     Clock(const int hours, const int minutes, const int seconds);
  42.     int convertHours(const int&) const;
  43.     int convertMinutes(const int&) const;
  44.     int getSeconds() const;
  45.     int getMinutes() const;
  46.     int getHours() const;
  47.     int getRemainSeconds() const;
  48.     Clock difference(const Clock&) const;
  49.     void setSeconds(const int in);
  50.     std::string getStdTime() const;              //returns clock as a string in 12hr format
  51.  
  52. private:
  53.     int seconds{};
  54.     const static int secondsPerDay{86400};
  55.     const static int secondsPerHour{3600};
  56.     const static int secondsPerMinute{60};
  57.     const static int minutesPerHour{60};
  58.     const static int hoursPerDay{24};
  59.     int checkValidHours(const int&) const;
  60.     int checkValidMinutes(const int&) const;
  61.     int checkValidSeconds(const int&) const;
  62.     bool checkValidLongSeconds(const int&) const;
  63.     void argumentError();
  64.     int getSecond() const;                      //helper function only
  65. };
  66.  
  67. std::ostream& operator<<(std::ostream&,const Clock);
  68.  
  69.  
  70. #endif // CLOCK_H
  71.  
  72. //Clock.cpp
  73.  
  74. #include "Clock.h"
  75.  
  76. //default constructor
  77. Clock::Clock():seconds{0} {}
  78.  
  79. //constructor that takes in a seconds input
  80. Clock::Clock(const int in) {
  81.     seconds = checkValidSeconds(in);
  82. }
  83.  
  84. //constructor that takes in hours/mins/seconds (military time)
  85. Clock::Clock(const int inHours, const int inMinutes, const int inSeconds ) {
  86.     seconds = convertHours(checkValidHours(inHours)) + convertMinutes(checkValidMinutes(inMinutes)) + checkValidSeconds(inSeconds);
  87. }
  88.  
  89. void Clock::argumentError() {  //private
  90.     throw std::invalid_argument("Invalid input");
  91. }
  92.  
  93. int Clock::convertHours(const int& in) const {
  94.     return in*secondsPerHour;
  95. }
  96. int Clock::convertMinutes(const int& in) const {
  97.     return in*secondsPerMinute;
  98. }
  99.  
  100. int Clock::checkValidHours(const int& in) const {
  101.     return in % hoursPerDay;
  102. }
  103.  
  104. int Clock::checkValidMinutes(const int& in) const { //private
  105.     return in % minutesPerHour;
  106. }
  107.  
  108. int Clock::checkValidSeconds(const int& in) const { //private
  109.     return in % secondsPerDay;
  110. }
  111.  
  112. bool Clock::checkValidLongSeconds(const int& in) const { //private
  113.     return in < secondsPerDay && in >= 0;
  114. }
  115.  
  116.  
  117. int Clock::getSecond() const {  //private
  118.     return seconds;
  119. }
  120.  
  121. void Clock::setSeconds(const int in) { //look at
  122.     if(checkValidLongSeconds(in)) {seconds = in;}
  123.     else {argumentError();}
  124. }
  125.  
  126. int Clock::getRemainSeconds() const {
  127.     return secondsPerDay-seconds;
  128. }
  129.  
  130. int Clock::getMinutes() const {
  131.     return (getSecond()-getHours()*secondsPerHour)/secondsPerMinute;
  132. }
  133. int Clock::getHours() const {
  134.     return getSecond()/secondsPerHour;
  135. }
  136.  
  137. int Clock::getSeconds() const {
  138.     return getSecond()-getHours()*secondsPerHour-getMinutes()*secondsPerMinute;
  139. }
  140.  
  141. std::ostream& operator<<(std::ostream& oin, Clock in) {
  142.     oin << in.getHours() << ":" << in.getMinutes() << ":" << in.getSeconds();
  143.     return oin;
  144. }
  145.  
  146. std::string Clock::getStdTime() const {
  147.     std::ostringstream oout{};
  148.     if (getHours() != 12) {oout << getHours()%12 << ":" << getMinutes() << ":" << getSeconds();}
  149.     else {oout << getHours() << ":" << getMinutes() << ":" << getSeconds(); }
  150.     return oout.str();
  151. }
  152.  
  153. Clock Clock::difference(const Clock& in) const {
  154.     Clock result{};
  155.     if(this->seconds > in.seconds) {
  156.         result.setSeconds(this->seconds-in.seconds);
  157.     }
  158.     else {
  159.         result.setSeconds(in.seconds-this->seconds);
  160.     }
  161.     return result;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement