C++ Question converting seconds into HH:MM:SS using functions?

2016-04-26 2:25 pm
Write a program in C++ that create a function named time. The time function receives number of
seconds as parameter and returns time as string in the following format: H:M:S.
For example, if seconds are 25412 then function should display
Time = 7:3:32

my attempt i am stuck and couldnt solve this return time as string has confused me

#include <iostream>
#include<string>
using namespace std;

string CLOCK(int seconds, int minutes,int hours);
int main()
{
int hours;
int minutes;
int seconds;

int time = 0;
cout << "Enter time elapsed, in seconds: ";
cin >> time;
cout << endl;

cout<<"Time is : "<<CLOCK(time,seconds,minutes,hours);


return 0;
}
string CLOCK(int seconds, int minutes,int hours)
{
int time=0
int hours;
int minutes;
int seconds;

string str1=":0"
string str2=":"

hours = static_cast<int>(time / 3600);

time = time - hours * 3600;

minutes = static_cast<int>(time / 60);

time = time - minutes * 60;

seconds = time;


return (hours)
if (minutes < 10)
return(":0" minutes)
else
return( ":" minutes)

if (seconds < 10)
return(":0" seconds)
else
return(":"seconds)

}

Runnable link:http://code.runnable.com/me/Vx9p-GbTq1UJNvbQ

回答 (2)

2016-04-26 3:37 pm
✔ 最佳答案
There are two different things being done here. One is converting total elapsed time into hours, minutes and seconds. The other is formatting those three results into a string.

Both need to get done in order to get a string version of the elapsed time, but which operation does the main() program (or other program) require? If it only needs the string, then there's no need to have hours and seconds in main(). Just pass the total elapsed seconds and let CLOCK() define and compute the hh, mm and ss values as local variables.

If main() will eventually use the int versions of hours, minutes and seconds, then you'll need reference arguments, pointers or an array to return multiple results; and I suggest making two functions in that case: One to split seconds into hh, mm, ss int values (with a new variable for the reduced seconds value, not overwriting the original total elapsed seconds), and a second function that takes hh, mm, ss and returns a string.

void calc_hhmmss(int seconds, int &hh, int &mm, int &ss)
{
ss = seconds % 60;
mm = seconds / 60 % 60;
hh = seconds / 3600; // hint: there's no need for static_cast<> when dividing ints
}

string format_hhmmss(int hh, int mm, int ss)
{
std::ostringstream sstr; // you need to #include <sstream> for this
sstr << hh << ":" << mm << ":" << ss;
return sstr.str();
}

The string streams let you use stream I/O operations like << and >> using a string object instead of the console or a file. See docs at:
http://www.cplusplus.com/reference/sstream/

If you have C++11 support in your compiler, you can simplify that to a one-liner:

string format_hhmmss(int hh, int mm, int ss)
{
return to_string(hh) + ":" + to_string(mm) + ":" + to_string(ss);
}

The stringstream version will let you do fancier output formatting later, like filling in leading zeroes on times like "03:04:56" using setfill() and setw() manipulators (defined in <iomanip>).

Either way, if main() really only wants to pass total elapsed seconds and get a string back, you can use those two functions to make a higher-level function that does that:

string CLOCK(int seconds)
{
int hh, mm, ss;
calc_hhmmss(seconds, hh, mm, ss);
return format_hhmmss(hh, mm, ss);
}

Three itty-bitty functions that you can use in a variety of ways, instead of one more complicated one that only does one more complex thing.
2016-04-26 3:22 pm
here is one way
#include <iostream>
string toHMS(int t){
stringstream ss;
string res;
int H,M,S;
H=t/3600;
t%=3600;
M=t/60;
S=t%60;
ss<< H << ":" << M << ":" << S;
ss >> res;
return res;
}
int main(void){
int s;
s=25412;
cout << toHMS(s);
return 0;
}
==========================
here is another....
string CLOCK(int t ) {
int H,M,S;
H=t/3600;
t%=3600;
M=t/60;
S=t%60;
string res;
res=to_string(H)+":"+to_string(M) +":"+to_string(S);
return res
}


收錄日期: 2021-04-21 18:17:45
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20160426062528AAIWcBK

檢視 Wayback Machine 備份