In C++, From time to time in programming, it comes in handy to be able to determ
ID: 3797526 • Letter: I
Question
In C++, From time to time in programming, it comes in handy to be able to determine how long it takes/took to perform a certain action(s). (See, for instance, the sort timing lab.) You are to write a class which will allow an easy interface to the computer's timing facilities. When you first create a Timerobject, it should be set to a 'stop' state. When the programmer wants to use it to time something, they can call the start() method before the 'something' executes and then call the stop() method afterward the 'something' finishes. To determine how long the 'something' took, they'll call the elapsed() method. This elapsed time will be reported in (whole) seconds. Unfortunately, this describes extremely simple operation and may not suit all programmers' needs. To allow more general usage of the class, we'll also allow the programmer to start the timer at the object's declaration. We'll also allow the programmer to get the elapsed time whether the timer is still running or not. Finally, we'll provide an output() method so the programmer can directly print the currently elapsed time without having to retrieve it separately. In addition, allow the programmer to subtract() and add() two Timerobjects to receive a Timerobject that is the difference/total of the original two Timers' currently elapsed times. (The resulting object should not be running.) Then we have to boobie-proof the class. Make sure that things work correctly if the programmer tries to get/print the elapsed time of a Timerobject which has never been started. Make sure nothing goes wrong if the programmer tries to stop a Timerobject that isn't currently running. We won't need a reset() method since the programmer can simply call start() again to start over...
Explanation / Answer
Timer.h
class Show {
public:
Show(int LT);
void inc();
bool setValue(int value);
int get() const;
int getValue() const;
void show() const;
private:
const int LT;
int value;
};
class timer {
public:
timer();
timer(int hour, int minute);
void increment()
bool set(int hour, int minute);
void show() const;
timer add(const timer &t) const;
private:
Show hours, minutes;
};
timer.cpp
#include <iostream>
#include “timer.h”
using namespace std;
Show::Show(int l) : LT(l) { value = 0; }
void Show::increment() {
value = (value + 1) % LT;
}
bool Show::setValue(int val) {
if (val < 0 || value >= LT) return false;
value = val; return true;
}
int Show::getValue() const { return value; }
int Show::get() const { return LT; }
void Show::show() const {
if (value < 10) cout << ‘0’; cout << value;
timer::timer() : hours(24), minutes(60) { }
timer::timer(int hour, int minute) : hours(24), minutes(60) {
if (set(hour, minute) == false) set (0, 0);
}
void timer::increment() {
minutes.increment();
if (minutes.getValue() == 0) hours.increment();
}
bool timer::set(int hour, int minute) {
if (hour < 0 || hour >= hours.get() ||
minute < 0 || minute >= minutes.get())
return false;
hours.setValue(h); minutes.setValue(m);
return true;
}
void timer::show() const {
hours.show(); cout << ‘:’ << minutes.show();
}
timer timer::add(const timer &t) const {
int hour = hours.getValue() + t.hours.getValue();
int minute = minutes.getValue() +
t.minutes.getValue();
if (minute >= minutes.get()) {
minute = minute – minutes.get();
hour = hour + 1;
}
if (hour >= hours.get())
hour = hour – hours.get();
return timer(hour, minute);
}
Main.cpp
#include <iostream>
#include “timer.h”
using namespace std;
void Input(timer &t, const char *count) {
int hour, minute;
bool S;
do {
cout << “Enter number of hours “ << count
<< “ timer: “; cin >> hour;
cout << “Enter number of minutes “ << count
<< “timer: “; cin >> minute;
S = t.set(hour, minute);
if (!S)
cout << “Invalid timer ”;
} while (!S)
cout << ‘ ’;
}