Advertisement
wafflecone67

Miltime

Apr 4th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. //Time.cpp
  2. include "Time.h"
  3. #include<iostream>
  4. #include<iomanip>
  5. using namespace std;
  6. int Time::gethour() const
  7. {
  8. return hour;
  9. }
  10. int Time::getSec() const
  11. {
  12. return sec;
  13.  
  14. }
  15.  
  16. //Time.h
  17.  
  18. #pragma once
  19. #ifndef _Time_h
  20. #define _Time_h
  21. class Time
  22.  
  23. {
  24. protected:
  25. int hour;
  26. int min;
  27. int sec;
  28. public:
  29. Time()
  30.  
  31. {
  32.  
  33. //hour = 0;
  34. //min = 0;
  35. //sec = 0;
  36.  
  37. }
  38. Time(int h, int m, int s);
  39. int gethour() const;
  40. int getMin() const
  41.  
  42. {
  43. return min;
  44. }
  45.  
  46. int getSec() const;
  47.  
  48. };
  49.  
  50. #endif
  51. //Miltime.h
  52.  
  53. #pragma once
  54. #include<iostream>
  55. #include<string.h>
  56. #include<cstdlib>
  57. using namespace std;
  58. #ifndef _MilTime_h
  59. #define _MilTime_h
  60. #include "Time.h"
  61. class MilTime : public Time
  62. {
  63. private:
  64. int milHours;
  65. int milSeconds;
  66.  
  67. public:
  68.  
  69. MilTime(string hours, int seconds)
  70. {
  71.  
  72. int length = strlen(hours.c_str());
  73. int numdigits = log10(atoi(hours.c_str())) + 1;
  74. int numzeroes = length - numdigits;
  75. milHours = atoi(hours.c_str());
  76. milSeconds = seconds;
  77.  
  78. hour = (milHours / 100);
  79. if (numzeroes == 2) {
  80. hour = 0;
  81.  
  82. }
  83. min = milHours % 100;
  84. sec = milSeconds;
  85. }
  86. bool setTime(int milhours, int milseconds);
  87. int getHour();
  88. int getStandHr();
  89.  
  90. };
  91.  
  92. #endif
  93.  
  94. //Miltime.cpp
  95.  
  96. #include<iostream>
  97. #include<iomanip>
  98. #include "Time.h"
  99. #include "MilTime.h"
  100. using namespace std;
  101. bool MilTime::setTime(int mhours, int mseconds)
  102.  
  103. {
  104. bool ret = true;
  105. if ((mhours <= 2359 && mhours >= 0) && (sec < 60 && sec >= 0 ))
  106. {
  107. milHours = mhours;
  108. milSeconds = mseconds;
  109. hour = (milHours / 100);
  110. min = milHours % 100;
  111. sec = milSeconds;
  112. }
  113. else
  114. {
  115. ret = false;
  116. }
  117. return ret;
  118. }
  119. int MilTime::getHour()
  120. {
  121. //if (hour == 0) { return 12; }
  122. // return hour;
  123. //}
  124. return (milHours);
  125. }
  126.  
  127. int MilTime::getStandHr()
  128. {
  129. if (hour == 0) { return 12; }
  130. return hour % 12;
  131. }
  132. //Main.cpp
  133.  
  134. #include "Miltime.h"
  135. #include "Time.h"
  136. #include <iostream>
  137. #include <string>
  138. using namespace std;
  139.  
  140. int main()
  141. {
  142. MilTime m1("0012", 3);
  143. bool isValid;
  144. int mSeconds;
  145. int mHours;
  146. cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
  147. m1.setTime(1145, 5);
  148. cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
  149. m1.setTime(1419, 34);
  150. cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
  151.  
  152. cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
  153. cin >> mHours;
  154. cin >> mSeconds;
  155. isValid = m1.setTime(mHours, mSeconds);
  156. while (!isValid)
  157. {
  158. cout << "Sorry, that wasn't a valid time. Try again.";
  159. cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
  160. cin >> mHours >> mSeconds;
  161. isValid = m1.setTime(mHours, mSeconds);
  162. }
  163. cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
  164.  
  165. system("pause");
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement