Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Time.cpp
- include "Time.h"
- #include<iostream>
- #include<iomanip>
- using namespace std;
- int Time::gethour() const
- {
- return hour;
- }
- int Time::getSec() const
- {
- return sec;
- }
- //Time.h
- #pragma once
- #ifndef _Time_h
- #define _Time_h
- class Time
- {
- protected:
- int hour;
- int min;
- int sec;
- public:
- Time()
- {
- //hour = 0;
- //min = 0;
- //sec = 0;
- }
- Time(int h, int m, int s);
- int gethour() const;
- int getMin() const
- {
- return min;
- }
- int getSec() const;
- };
- #endif
- //Miltime.h
- #pragma once
- #include<iostream>
- #include<string.h>
- #include<cstdlib>
- using namespace std;
- #ifndef _MilTime_h
- #define _MilTime_h
- #include "Time.h"
- class MilTime : public Time
- {
- private:
- int milHours;
- int milSeconds;
- public:
- MilTime(string hours, int seconds)
- {
- int length = strlen(hours.c_str());
- int numdigits = log10(atoi(hours.c_str())) + 1;
- int numzeroes = length - numdigits;
- milHours = atoi(hours.c_str());
- milSeconds = seconds;
- hour = (milHours / 100);
- if (numzeroes == 2) {
- hour = 0;
- }
- min = milHours % 100;
- sec = milSeconds;
- }
- bool setTime(int milhours, int milseconds);
- int getHour();
- int getStandHr();
- };
- #endif
- //Miltime.cpp
- #include<iostream>
- #include<iomanip>
- #include "Time.h"
- #include "MilTime.h"
- using namespace std;
- bool MilTime::setTime(int mhours, int mseconds)
- {
- bool ret = true;
- if ((mhours <= 2359 && mhours >= 0) && (sec < 60 && sec >= 0 ))
- {
- milHours = mhours;
- milSeconds = mseconds;
- hour = (milHours / 100);
- min = milHours % 100;
- sec = milSeconds;
- }
- else
- {
- ret = false;
- }
- return ret;
- }
- int MilTime::getHour()
- {
- //if (hour == 0) { return 12; }
- // return hour;
- //}
- return (milHours);
- }
- int MilTime::getStandHr()
- {
- if (hour == 0) { return 12; }
- return hour % 12;
- }
- //Main.cpp
- #include "Miltime.h"
- #include "Time.h"
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- MilTime m1("0012", 3);
- bool isValid;
- int mSeconds;
- int mHours;
- cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
- m1.setTime(1145, 5);
- cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
- m1.setTime(1419, 34);
- cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
- cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
- cin >> mHours;
- cin >> mSeconds;
- isValid = m1.setTime(mHours, mSeconds);
- while (!isValid)
- {
- cout << "Sorry, that wasn't a valid time. Try again.";
- cout << endl << "Enter a number of military hours in the format #### followed by a space and then a number of seconds: ";
- cin >> mHours >> mSeconds;
- isValid = m1.setTime(mHours, mSeconds);
- }
- cout << "A military time of " << m1.getHour() << ":" << m1.getSec() << " = " << m1.getStandHr() << ":" << m1.getMin() << ":" << m1.getSec() << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement