Tuesday, July 28, 2009

Need help for writing a function in C++?

I need help in writing 1 of the functions for a program. The function is suppose to determine the minute difference between two clock times. I need to actually write out the function and not just use the one from the std. library. The two clock times are suppose to be strings not just integers. The string time format should be: hh:mm (AM/PM)





Some parameters are you have to use a 24-hour clock not a 12-hour clock and if the user enters in bad input, the function will return "error" and if the value of the first time input is later than the second time input, the function will return the negative value difference.





Here are some examples;


Function Time ("2:04AM", "4:21AM") returns 137


Function TIme ("10:37PM", "9:36PM") returns -61


Function Time ("07:58AM", "8:48AM") returns 50


Function Time ("22:14AM", "-7:43MM") returns error because invalid input





Heres my function heading:


int TimeDist (string time1, string time2)


{





}





Please help me out with this function


Thanks

Need help for writing a function in C++?
Not that it is your fault but this is a rather silly assignment. For instance, if you must use a 24 hour clock what is the point of the AM/PM in the string? You already know that!





Anyway, this should be close to what you want. You can thank my boredom and insomnia.








#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


#include %26lt;cstdlib%26gt;





using namespace std;





bool parseAMPM(string s)


{


if ((s[s.length() - 2] == 'A')


|| (s[s.length() - 2] == 'P'))


{


return(true);


}





return(false);


}








bool isNumeric(string s)


{


for (int i = 0; i %26lt; s.length(); ++i)


{


if (! isdigit(s[i]))


{


return(false);


}


}





return(true);


}








bool parseHour(string s, string%26amp; hourStr)


{


size_t x = s.find(':');





if (x == string::npos)


{


return(false);


}





hourStr = s.substr(0, x);





return(isNumeric(hourStr));


}








bool parseMinute(string s, string%26amp; minStr)


{


size_t x = s.find(':');





if (x == string::npos)


{


return(false);


}





x++;


minStr = s.substr(x, s.length() - 2 - x);





return(isNumeric(minStr));


}











bool TimeDist(string time1, string time2, int%26amp; result)


{


if ((! parseAMPM(time1))


|| (! parseAMPM(time2)))


{


return(false);


}





string hourStr1, hourStr2;





if ((! parseHour(time1, hourStr1))


|| (! parseHour(time2, hourStr2)))


{


return(false);


}





string minStr1, minStr2;





if ((! parseMinute(time1, minStr1))


|| (! parseMinute(time2, minStr2)))


{


return(false);


}





int t1 = (atoi(hourStr1.c_str()) * 60) + atoi(minStr1.c_str());


int t2 = (atoi(hourStr2.c_str()) * 60) + atoi(minStr2.c_str());





result = t2 - t1;





return(true);


}











int main (void)


{


int result = 0;





TimeDist("2:04AM", "4:21AM", result) ? cout %26lt;%26lt; result %26lt;%26lt; endl : cout %26lt;%26lt; "ERROR" %26lt;%26lt; endl;


TimeDist("10:37PM", "9:36PM", result) ? cout %26lt;%26lt; result %26lt;%26lt; endl : cout %26lt;%26lt; "ERROR" %26lt;%26lt; endl;


TimeDist("07:58AM", "8:48AM", result) ? cout %26lt;%26lt; result %26lt;%26lt; endl : cout %26lt;%26lt; "ERROR" %26lt;%26lt; endl;


TimeDist("22:14AM", "-7:43MM", result) ? cout %26lt;%26lt; result %26lt;%26lt; endl : cout %26lt;%26lt; "ERROR" %26lt;%26lt; endl;





}
Reply:May be you can search project assignment website like http://askexpert.info/


No comments:

Post a Comment