In: Computer Science
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class timeStamp // uses 24-hour times (0..24)
{
public:
timeStamp(void);
timeStamp(int, int, int);
void setTime(int, int, int);
timeStamp *addTimes(timeStamp *);
timeStamp *subTimes(timeStamp *);
string toString(void);
int hour;
int minute;
int second;
};
int main()
{
timeStamp noon(12, 0, 0);
timeStamp *teaTime = new timeStamp(51, 35, 9);
cout << "noon = " << noon.toString() << endl;
cout << "teaTime = " << teaTime->toString() << endl;
cout << "Time sum = "
<< (noon.addTimes(teaTime))->toString() << endl;
cout << "Another sum = "
<< (teaTime->subTimes(&noon)).toString() << endl;
return 0;
}
timeStamp::timeStamp(void)
{
hour = 0;
minute = 0;
second = 0;
}
timeStamp::timeStamp(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
void timeStamp::setTime(int h, int m, int s)
{
hour = s;
minute = m;
second = s;
}
timeStamp *timeStamp::addTimes(timeStamp *ts)
{
int ourTime = (hour * 3600) + (minute * 60) + second;
int theirTime = (ts->hour * 3600) + (ts->minute * 60) + ts->second;
int timeSum = (ourTime + theirTime);
timeStamp *newTime = new timeStamp();
newTime->hour = timeSum / 3600;
newTime->minute = (timeSum % 3600) / 60;
newTime->second = ((timeSum % 3600) % 60);
return newTime;
}
timeStamp *timeStamp::subTimes(timeStamp *ts)
{
int ourTime = (hour * 3600) + (minute * 60) + second;
int theirTime = (ts->hour * 3600) + (ts->minute * 60) + ts->second;
int timeSum = (ourTime - theirTime);
timeStamp *newTime = new timeStamp();
newTime->hour = timeSum / 3600;
newTime->minute = (timeSum % 3600) / 60;
newTime->second = ((timeSum % 3600) % 60);
return newTime;
}
string timeStamp::toString(void)
{
ostringstream convert;
convert << "hour = " << hour << ", minute = " << minute
<< ", second = " << second;
return convert.str();
}
There are two changes needed to this program to make it compile & build correctly. Please be careful and don’t go changing things all over the place.
Please show the exact output from the 1stcout statement in main
noon = hour = 12, minute = 0, second = 0 |
Explanation:Since we created the noon object " timeStamp noon(12, 0, 0);" the above output will be printed
Please show the exact output from the 2ndcout statement in main
teaTime = hour = 3, minute = 35, second = 9 |
Explanation:Since 0..24 is allowed for Hours we divide it by 24
Please show the exact output from the 3rdcout statement in main
Time sum = hour = 15, minute = 35, second = 9 |
Explanation:We now added noon and teaTime 15 hours(12+3) ,35 minute(0+35), 9 second(0+9)
Please show the exact output from the 4thcout statement in maiN
AAnother sum = hour = 8, minute = 24, second = 51 |
2.OUTPUT :
3.CODE :
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class timeStamp // uses 24-hour times (0..24)
{
public:
timeStamp(void);
timeStamp(int, int, int);
void setTime(int, int, int);
timeStamp *addTimes(timeStamp *);
timeStamp *subTimes(timeStamp *);
string toString(void);
int hour;
int minute;
int second;
};
int main()
{
timeStamp noon(12, 0, 0);
timeStamp *teaTime = new timeStamp(51, 35, 9);
cout << "noon = " << noon.toString() << endl;
cout << "teaTime = " << teaTime->toString() << endl;
cout << "Time sum = "
<< (noon.addTimes(teaTime))->toString() << endl;
cout << "Another sum = "
<< (teaTime->subTimes(&noon))->toString() << endl;
return 0;
}
timeStamp::timeStamp(void)
{
hour = 0;
minute = 0;
second = 0;
}
timeStamp::timeStamp(int h, int m, int s)
{
hour = h%24;
minute = m;
second = s;
}
void timeStamp::setTime(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
timeStamp *timeStamp::addTimes(timeStamp *ts)
{
int ourTime = (hour * 3600) + (minute * 60) + second;
int theirTime = (ts->hour * 3600) + (ts->minute * 60) + ts->second;
int timeSum = (ourTime + theirTime);
timeStamp *newTime = new timeStamp();
newTime->hour = (timeSum / 3600)%24;
newTime->minute = (timeSum % 3600) / 60;
newTime->second = ((timeSum % 3600) % 60);
return newTime;
}
timeStamp *timeStamp::subTimes(timeStamp *ts)
{
int ourTime = (hour * 3600) + (minute * 60) + second;
int theirTime = (ts->hour * 3600) + (ts->minute * 60) + ts->second;
int timeSum = (ourTime - theirTime);
if(timeSum<0)
timeSum=-(timeSum);
timeStamp *newTime = new timeStamp();
newTime->hour = (timeSum / 3600)%24;
newTime->minute = (timeSum % 3600) / 60;
newTime->second = ((timeSum % 3600) % 60);
return newTime;
}
string timeStamp::toString(void)
{
ostringstream convert;
convert << "hour = " << hour << ", minute = " << minute
<< ", second = " << second;
return convert.str();
}