Question
hi, I just need the read function( std::istream& read(std::istream& istr); ) thx Time Module The time module is designed to: read and write time values.
hi, I just need the read function(
std::istream& read(std::istream& istr);
)
thx
Time Module
The time module is designed to:
- read and write time values.
- measure the passage of time by doing basic arithmetic operations
The time module only holds the time in minutes but will display and read the time in the following format:
HH:MM
For example, when the Time object holds the value 125, it will display 02:05. Likewise if the time 13:55 is read by the Time object from a stream, 835 is stored in the object (i.e. 13x60+55). Note that since the Time object is also used for the passage of time, there is no limit to the number of hours and minutes and they may pass 24 and 60 if needed.
Note: 125:15 is a valid time that means 125 hours and 15 minutes also 0:96 is a valid entry and it is displayed as: 01:36 that is 1 hour and 36 minutes
Complete the implementation of the Time class with the following mandatory specs:
namespace sdds { class Time { unsigned int m_minutes; public: Time& setToNow(); Time(unsigned int minutes = 0); std::ostream& write(std::ostream& ostr) const; std::istream& read(std::istream& istr); Time& operator-=(const Time& D); Time operator-(const Time& D)const; Time& operator+=(const Time& D); Time operator+(const Time& D)const; Time& operator=(unsigned int val); Time& operator *= (unsigned int val); Time& operator /= (unsigned int val); Time operator *(unsigned int val)const; Time operator /(unsigned int val)const; operator unsigned int()const; operator int()const; };
Time& setToNow();
setToNow, sets the Time to the current time using sdds::getTime() (available in utils module) and then returns the reference of the current object.
Note that if the sdds::debug is set to true, the getTime() function will receive the time from the user instead. This will be used for debugging purposes and when submitting your work through the submitter program.
Time::Time
Time(unsigned int min = 0);
Constructs the Time by setting the number of minutes held in the object or set the time to zero by default.
Time::write
std::ostream& write(std::ostream& ostr) const;
Writes the time into a stream in HH:MM format padding the spaces with zero if the numbers are single digit (examples 03:02, 16:55 234:06 )
Time::read
std::istream& read(std::istream& istr);
Reads the time from a stream in H:M format. It makes sure that the two integers (hours and minute) are greater than zero and separated by ":", otherwise it will set the istream object to a failure state.
This function does not react to any invalid data, instead, it will work exactly how istream works; It will put the istream in a failure state if anything goes wrong by following these steps:
- reads the integer for the hours using istr and if the value is negative, it sets the istream object to a failure state.
- reads one character and makes sure it is ':'. If it is not ':', it will set the istream object to a failure state.
- reads the integer for the minutes using istr and if the value is negative, it sets the istream object to a failure state.
setting istream to a fail state
To set the istream to a fail state manually call the following method of istream:
setstate(ios::failbit);
Note: Do not clear or flush the istream object since this method complies with the istream standards. The caller of this function may check the state of the istream object to make sure that the read was successful if needed.
Time basic arithmetic operations
All the implemented basic arithmetic operations on Time are done exactly as it is defined in math except for the subtraction:
Time::operator-=
Design the subtraction in the Time as if you are turning a 24-hour clock backwards:
Time& operator-= (const Time& D);
Calculates the time difference between the current time and the incoming argument Time D and the returns the reference of the left operand object. Note that the difference can never be a negative value: 23:00 -= 9:00 will be 14:00. 18:00 -= 16:00 will be 2:00. 1:00 -= 22:00 will be 3:00. ((1:00 + 24:00) - 22:00) Also: 1:00 -= 46:00 will be 3:00. ((1:00 + 24:00 + 24:00) - 46:00) See the illustration below:
Time::operator-
Time operator-(const Time& D)const;
Works exactly like the operator-= but without side-effect. This operator will not modify the left operand and returns a Time object that is the result of the calculation.
Time::operator+=
Time& operator+=(const Time& D);
Add the minute value of the right operand to the value of the left operand and then returns the reference of the left operand.
Time::operator+
Time operator+(const Time& D)const;
Creates a Time object with the minute value that is the sum of the minute values of the left and right operands and then returns it.
Time::operator=
Time& operator=(unsigned int val);
Sets the minute value of the left operand to the value of the right operand and then returns the reference of the left operand.
Time::operator*=
Time& operator *= (unsigned int val);
Multiplies the minutes' value of the left operand by the value of the right operand and then returns the reference of the left operand.
Time::operator*
Time operator *(unsigned int val)const;
Creates a Time object with the minutes value being the product of the minutes' value of the left operand by the value of the right operand and returns the object.
Time::operator/=
Time& operator /= (unsigned int val);
Divides the minutes' value of the left operand by the value of the right operand and then returns the reference of the left operand.
Time::operator/
Time operator /(unsigned int val)const;
Creates a Time object with the minutes value being the division of the minutes' value of the left operand by the value of the right operand and returns the object.
Time::operator int
operator int()const;
When the time is cast to an integer, it will return the number of minutes as an integer.
Time::operator unsigned int
operator unsigned int()const;
When the time is cast to an unsigned integer, it will return the number of minutes.
operator<<
Overload the insertion operator to be able to insert a Time object into an ostream object
operator>>
Overload the extraction operator to be able to extract data from an istream object into the Time object
Time unit test
void timeTester() { Time D(1385u), C(65u), E; cout << "E: " << E << endl; cout << "D: " << D << endl; cout << "C: " << C << endl; cout << " D C D-=C" << endl; cout << D << " -= " << C << " = "; cout << (D -= C) << endl << endl; cout << " C D C-=D" << endl; cout << C << " -= " << D << " = "; cout << (C -= D) << endl << endl; cout << " C D C+=D" << endl; cout << C << " += " << D << " = "; cout << (C += D) << endl << endl; cout << " C = 245u" << endl; cout << "C: " << (C = 245u) << endl << endl; cout << " D = 2760u" << endl; cout << "D: " << (D = 2760u) << endl << endl; cout << " E = C + D" << endl; E = C + D; cout << E << " = " << C << " + " << D << endl << endl; cout << " E = C - D" << endl; E = C - D; cout << E << " = " << C << " - " << D << endl << endl; cout << "C: " << C << endl; cout << " C *= 2u;" << endl << "C: "; cout << (C *= 2u) << endl; cout << " C *= 12u" << endl << "C: "; cout << (C *= 12u) << endl << endl; cout << " C = 245u" << endl; cout << "C: " << (C = 245u) << endl << endl; cout << " E = C * 2u" << endl; E = C * 2u; cout << E << " = " << C << " * 2u" << endl << endl; cout << " E = C * 12u" << endl; E = C * 12u; cout << E << " = " << C << " * 12u" << endl << endl; cout << "C: " << C << endl; cout << " C /= 2u;" << endl << "C: "; cout << (C /= 2u) << endl; cout << " C = 245u" << endl; cout << "C: " << (C = 245u) << endl << endl; cout << " E = C / 2u" << endl; E = C / 2u; cout << E << " = " << C << " / 2u" << endl << endl; cout << "E: " << E << endl; cout << "D: " << D << endl; cout << "C: " << C << endl << endl; cout << "Enter the following values at the prompt:" << endl; cout << "aa:bb" << endl; cout << "12,12 " << endl; cout << "-12:12 " << endl; cout << "12:-12" << endl; cout << "12:12" << endl; cout << "Please enter the time (HH:MM): "; bool done; do { done = true; cin >> E; if (cin.fail()) { cin.clear(); cin.ignore(1000, ' '); done = false; cout << "Bad time entry, retry (HH:MM): "; } } while (!done); cout << "you entered: " << E << endl << endl; cout << "Enter 100:100 at the prompt: " << endl; sdds::debug = true; E.setToNow(); cout << E << endl; sdds::debug = false; cout << "The actual system time is: " << Time().setToNow() << endl; }
Time unit test output
E: 00:00 D: 23:05 C: 01:05 D C D-=C 23:05 -= 01:05 = 22:00 C D C-=D 01:05 -= 22:00 = 03:05 C D C+=D 03:05 += 22:00 = 25:05 C = 245u C: 04:05 D = 2760u D: 46:00 E = C + D 50:05 = 04:05 + 46:00 E = C - D 06:05 = 04:05 - 46:00 C: 04:05 C *= 2u; C: 08:10 C *= 12u C: 98:00 C = 245u C: 04:05 E = C * 2u 08:10 = 04:05 * 2u E = C * 12u 49:00 = 04:05 * 12u C: 04:05 C /= 2u; C: 02:02 C = 245u C: 04:05 E = C / 2u 02:02 = 04:05 / 2u E: 02:02 D: 46:00 C: 04:05 Enter the following values at the prompt: aa:bb12,12 -12:12 12:-12 12:12 Please enter the time (HH:MM): aa:bb Bad time entry, retry (HH:MM): 12,12 Bad time entry, retry (HH:MM): -12:12 Bad time entry, retry (HH:MM): 12:-12 Bad time entry, retry (HH:MM): 12:12 you entered: 12:12 Enter 100:100 at the prompt: Enter current time: 100:100 101:40 The actual system time is: 11:46
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started