Could somebody give an example of how to optimally code a time conversion calculator for seconds to x Years, x Days, x Hours, x Minutes, x Seconds format? For example it should give output like:
1 2 3
int Userinput = 988977;
[couts the following once calculations are done]
988977 seconds = 0 Years, 11 Days, 10 Hours, 42 Minutes, 57 Seconds
This will be using long long variables instead of int because int can not store a large enough number in seconds to show up as years when converted. Pseudocode for this would be greatly appreciated!
That is a pretty vague concept you are saying. Please provide detailed pseudocode that gives a template for how to code this program. This program is not as simple as dividing the total number of seconds down for each unit of time, as I have tried that.
FINALLY! I figured it out! Now I need help optimizing this code, as I feel there is a simpler way to code this program with less lines than what I have. This is the source:
longlong totalSeconds;
//constants
constint DAYS = 365; //does not account for leap years
constint HOURS = 24;
constint MINUTES = 60;
constint SECONDS = 60;
//class declaration
class UserSeconds
{
private:
longlong TotalSeconds;
longlong Years;
longlong Days;
longlong Hours;
longlong Minutes;
longlong Seconds;
public:
UserSeconds();
//Notes for how I did the conversions are written down on paper.
//It was quite long. ph stands for place holder basically.
void Convert(longlong totalSeconds)
{
TotalSeconds = totalSeconds;
longlong phDays, phHours, phHours2, phMinutes, phMinutes2, phMinutes3, phSeconds, phSeconds2, phSeconds3, phSeconds4;
//QUICK NOTES ON CALCULATIONS
//Each ph without numbers (ph1) converts Years to the unit of time being calculated
//Each ph2 finds the userInput converted to that unit of time, then subtracts ph1 from it
//Each ph3 subtracts Days converted to that unit of time from ph2
//Each ph4 subtracts Hours converted to that unit of time from ph3
//This finds the number of years
Years = TotalSeconds / (DAYS*HOURS*MINUTES*SECONDS);
//This finds the number of days
phDays = Years*DAYS;
Days = (TotalSeconds / (HOURS*MINUTES*SECONDS)) - phDays;
//this finds the number of hours
phHours = Years*DAYS*HOURS;
phHours2 = (TotalSeconds / (MINUTES*SECONDS)) - phHours;
Hours = phHours2 - Days*HOURS;
//this finds the number of minutes
phMinutes = Years*DAYS*HOURS*MINUTES;
phMinutes2 = (TotalSeconds / SECONDS) - phMinutes;
phMinutes3 = phMinutes2 - Days*HOURS*MINUTES;
Minutes = phMinutes3 - Hours*MINUTES;
//this finds the number of seconds
phSeconds = Years*DAYS*HOURS*MINUTES*SECONDS;
phSeconds2 = TotalSeconds - phSeconds;
phSeconds3 = phSeconds2 - Days*HOURS*MINUTES*SECONDS;
phSeconds4 = phSeconds3 - Hours*MINUTES*SECONDS;
Seconds = phSeconds4 - Minutes*SECONDS;
};
};