Write a class to hold time. Time is the hour, minute and seconds. Write a constructor that will allow the user of your class to initialize a time or set the time to all zeros if not initialized. Include all the operators listed above.
Your program will need to read from a file of times and commands. There will be a number at the top of the file that represents the number of time values in the file. Although this is the actual number of times in the file, you must read the data as if it is the maximum number of times in the file. Use a dynamic array of time objects, the size of the array will be the number listed first in the file. Your program must work for any file, we will test it with a different file (different values but the same format). Once you have read all the times from the file, continue to read the commands from the file. They are in the format of index1 operator index2. The operator will be either + or -. So if the line reads 3+2, then write to the screen the following:
time[3] + time[2] = newTime.
where time[3] is the time in position 3, and time 2 is the time in position 2, and newTime is the result of the addition. If the resultant time is > 24, then print a message first that the time is > 24 hours. Times must always be output in the correct format (hh:mm:ss) and the minutes and seconds must have valid values (<60). If the resultant time is negative, then output that message first.
When you have finished the additions and subtractions, print the times (sorted in ascending order and in the correct format) to the output file specified by the user.
Name your files time.h, time.cpp and program3.cpp.
and here the timeclass
Expand|Select|Wrap|Line Numbers
- #ifndef time_H
- #define time_H
- #include <string>
- #include <iostream>
- #include <iomanip>
- using namespace std;
- class timeClass
- {
- public:
- //timeClass(int hours, int minuts, int seconds, float time)
- // timeClass(int h=0, int m=0, int s=0) { set(h, m, s); }
- void getTime(int&, int&, int&);
- void setTime(int , int , int );
- void printTime() const;
- //void incrementHours();
- //void incrementMinuts();
- //void incrementSeconds();
- //bool equalTime(const timeClass&) const;
- timeClass(int, int, int);// constructor with parameters
- timeClass();//defult constructor
- friend ostream& operator<<(ostream &out, const timeClass &Clock);
- friend istream& operator>>(istream &in, timeClass &Clock);
- //friend timeClass operator+(const timeClass &left, const timeClass &right);
- //friend timeClass operator-(const timeClass &left, const timeClass &right);
- void operator+=(timeClass &t);
- void operator-=(timeClass &t);
- private:
- int hrs;
- int min;
- int sec;
- };
- #endif
- #include "time.h"
- timeClass::timeClass(int hours, int minuts, int seconds)
- {
- if(0 <= hours && hours < 24)
- hrs= hours;
- else
- hrs = 0;
- if(0 <= minuts && minuts <60)
- min = minuts;
- else
- min = 0;
- if(0 <= seconds && seconds <60)
- sec=seconds;
- else
- sec=0;
- }
- void timeClass::setTime(int , int , int )
- {
- hrs=0;
- min=0;
- sec=0;
- }
- void timeClass::printTime() const
- {
- if(hrs < 10)
- cout<<"0";
- cout << hrs << ":";
- if(min < 10)
- cout << "0";
- cout<< min <<":";
- if(sec < 10)
- cout<<"0";
- cout<< sec;
- }
- void timeClass::getTime(int& hours, int& minutes,
- int& seconds)
- {
- hours = hrs;
- minutes = min;
- seconds = sec;
- }
- ostream& operator<<(ostream &out, const timeClass &Clock)
- {
- out << Clock.hrs << " " << ":" << Clock.min << ":" << Clock.sec << endl;
- return out;
- }
- istream& operator>>(istream &in, timeClass &Clock)
- {
- in >> Clock.hrs >> Clock.min >> Clock.sec;
- return in;
- }
- void timeClass::operator +=(timeClass &t)
- {
- // Use the set function to take care of the addition
- setTime(hrs + t.hrs, min + t.min, sec + t.sec);
- }
- void timeClass::operator -=(timeClass &t)
- {
- setTime(hrs - t.hrs, min - t.min, sec - t.sec);
- }
- #include "time.h"
- #include <fstream>
- using namespace std;
- int main()
- {
- string inputFile, oFileName;
- ifstream fin;
- ofstream fout;
- cout<< "which file to use for input? ";
- cin>> inputFile;
- fin.open(inputFile.c_str ());
- if (!fin)
- {
- cerr<< "Unable to open input file \n";
- exit(2);
- }
- cout << "which file to use for output? ";
- cin >> oFileName;
- fout.open(oFileName.c_str ());
- if (!fout)
- {
- cerr<< "Unable to open output file \n";
- exit(3);
- }
- int t1, t2, result;
- result=t1+t2;
- cout << "result >24"<<endl;
- //close all files
- fin.close();
- fout.close();
- return 0;
- }