|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
calculating user time and elapsed time
Hi im a little bit stuck on this program im working on.
im creating a program which uses the data type Mytime the output should look something like Enter start hh mm : 7 30 Enter finish hh mm : 7 30 Time elapsed from 07:30 until 07:30 is 00:00 2 functions have not yet been defined display1Time() & elapsed(). The program currently compiles in unix g++ i have filled the un defined function to get the program running. Basically i cant work out how to get the time displayed and the total time elapsed working. I think the time the user enters has to be converted to minutes. Can anyone help please ? Code:
// Incomplete version
#include <iostream>
using namespace std;
struct Mytime
{
int hours;
int mins;
};
int timecmp(Mytime,Mytime);
// returns 0 if times are same
// returns >0 if first is later than second
// returns <0 if first is before second
int setTime(Mytime& t, int h, int m );
// if valid time sets hh and mm to h and m
// and returns 0
// if invalid returns integer > 0 as error code
// error code +1 = underflow hours
// error code +2 = overflow hours
// error code +4 = underflow mins
// error code +8 = overflow mins
void display1Time(Mytime t);
// displays in form hh:mm
void elapsed(Mytime t1, Mytime t2, Mytime& duration);
// determines the time duration between t1 and t2
// t1 assumed to be earlier than t2
void main()
{
Mytime now;
Mytime then;
Mytime howlong;
int h,m;
do // validate input
{
cout << "Enter start hh mm : ";
cin >> h >> m ;
} while (setTime(now,h,m));
do
{
cout << "Enter finish hh mm : ";
cin >> h >> m ;
} while (setTime(then,h,m));
elapsed(now,then,howlong);
cout << "Time elapsed from ";
display1Time(now);
cout << " until ";
display1Time(then);
cout << " is ";
display1Time(howlong);
cout << endl;
}
int timecmp(Mytime t1,Mytime t2)
{
if (t1.hours == t2.hours)
{
if (t1.mins == t2.mins)
{
return 0;
}
else // mins not same
{
if (t1.mins > t2.mins)
{
return 1; // greater than zero
}
else
{
return -1;
}
}
}
else // hours not same
{
if (t1.mins > t2.mins)
{
return 1;
}
else
{
return -1;
}
}
}
int setTime(Mytime& t, int h, int m )
{
int errorCode = 0;
if (h < 0) errorCode += 1;
if (h > 23) errorCode += 2;
if (m < 0) errorCode += 4;
if (m > 59) errorCode += 8;
if (errorCode==0){t.hours = h; t.mins = m;}
return errorCode;
}
//////////// THE FOLLOWING FUNCTIONS ARE INCOMPLETE
void display1Time(Mytime t)
{
cout << "00:00";
}
void elapsed(Mytime t1, Mytime t2, Mytime& duration)
{
setTime(duration,0,0);
}
|
|
#2
|
||||
|
||||
|
For displaying the time, you can use setw to set the width of the displayed variables and setfill to leftpad the numbers with 0. Don't forget to #include iomanip on top, as these functions are declared there.
As for computing the time difference, the thing to do is convert the times from hours and minutes into minutes. Then convert the resulting difference back into hours and minutes. Please note that the following code is off the top of my head and is untested, but it should go something like this: Code:
#include <iomanip>
...
...
void display1Time(Mytime t)
{
cout << setw(2) << setfill('0') << x << ":" << setw(2) << setfill('0') << y;
}
void elapsed(Mytime t1, Mytime t2, Mytime& duration) {
int mins1, mins2, difference;
// Convert the times into minutes
mins1 = t1.hours *60 + t1.mins;
mins2 = t2.hours *60 + t2.mins;
// Compute the difference
difference = mins2 - mins1;
// Convert the minutes back into hours and minutes
duration.hours = difference / 60;
duration.hours = difference % 60;
}
Hope this helps! |
|
#3
|
||||
|
||||
|
If you are trying to compute the length of some operation, the code below might work for you...
Code:
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include <iostream>
const int MINUTE = 60;
const int HOUR = MINUTE * 60;
const int DAY = HOUR * 24;
string get_elapsed(int sec)
{
int days =0;
int hours =0;
int minutes =0;
int seconds =0;
char* dstr = new char;
char* et = new char;
if (days = int(sec / DAY)) sec -= DAY * days;
if (hours = int(sec / HOUR)) sec -= HOUR * hours;
if (minutes = int(sec / MINUTE)) sec -= MINUTE * minutes;
seconds = sec;
sprintf(et, "%02d:%02d:%02d", hours, minutes, seconds);
string res(et);
if (days) {
sprintf(dstr, "%s Day%s, ", days, &"s"[days == 1]);
strncat(dstr, et, strlen(et));
res = string(dstr);
}
delete dstr;
delete et;
return res;
}
int main(void) {
// cache start time
time_t start, end;
(void)time(&start);
// do something...
sleep(5);
// get end time
(void)time(&end);
cout << "elapsed: " << get_elapsed(end - start) << endl;
return 0;
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > calculating user time and elapsed time |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|