Time Library and Application: Write an application to produce the following outp
ID: 3734421 • Letter: T
Question
Time Library and Application:
Write an application to produce the following output in C++
Note: User is presented with a menu of three items: change time A, change time B, or exit. Once the user inputs time A (or B) the program shows the time A plus duration B, and the time difference between A and B.
Note: Time arithmetics are done by converting each time pair (hr, min) to minutes since midnight, arithmetics are done on the minutes units and then converted back to time.
It will be better if you follow the functions below.
Explanation / Answer
ANSWER:
/*bool time_is_valid(int hr, int min); //true if hr:min is valid
bool time_is_valid(int hr, int min, char ap); //true if hr:min [ap]m is valid
void time_input(int& hr, int& min, char &ap,
string prompt = ":"); //input hr:min ap
void time_input(int& hr, int& min,
string prompt=":"); //input hr:min
void time_output(int hr, int min); //output hr:min
void time_output(int hr, int min, char ap); //output hr:min ap
void time_convert(int hr_24,
int& hr_12, char &ap); //hr:min -> hr:min ap
void time_convert(int hr_12, char ap,
int& hr_24); //hr:min ap -> hr:min
int time_minutes_to_time(int hr, int min); //mins since midnight
void time_add(int& hr_start, int& min_start,
int hr_duration, int min_duration); //hr:min += hr:min
void time_add(int hr_start, int min_start,
int hr_duration, int min_duration
int& hr_end, int& min_end); //hr:min += hr:min
void time_subtract(int &hr_end, int &min_end,
int hr_start, int min_start); //hr:min -= hr:min
void time_subtract(int hr_end, int min_end,
int hr_start, int min_start,
int& hr_duration, int& min_duration); //hr:min -= hr:min*/
CODE:
int main()
{
int hrA, minA, hrB, minB;
string prompt;
char ap;
cout << "Time A: ";
time_input(&hrA, &minA, prompt); //Reads time A.
cout << "Time B: ";
time_input(&hrB, &minB, prompt); //Reads time B.
/*if(!time_is_valid(hrA, minA) || time_is_valid(hrB, minB))
{
}*/
int totalHr, totalMin;
time_add(hrA, minA, hrB, minB, &totalHr, &totalMin); //Adds timeA, and timeB.
cout << "added: ";
time_output(totalHr, totalMin); //Prints the time added.
time_subtract(hrA, minA, hrB, minB, &totalHr, &totalMin); //Subtracts timeB from timeA.
cout << " duration: ";
time_output(totalHr, totalMin); //Prints the duration.
while(true) //Now this repeats.
{
cout << "[A] [B] [X] >>"; //Prints the prompt.
cin >> choice; //Reads the choice.
switch(choice)
{
case 'a':
case 'A': cout << "A: ";
time_input(&hrA, &minA, prompt); //Reads time A.
cout << "Time A: ";
time_output(hrA, minA); //Prints the timeA.
cout << endl << "Time B: ";
time_output(hrB, minB); //Prints the timeB.
cout << endl << "added: ";
time_add(hrA, minA, hrB, minB, &totalHr, &totalMin); //Adds timeA, and timeB.
time_output(totalHr, totalMin); //Prints the time added.
time_subtract(hrA, minA, hrB, minB, &totalHr, &totalMin); //Subtracts timeB from timeA.
cout << " duration: ";
time_output(totalHr, totalMin); //Prints the duration.
break;
case 'b':
case 'B': cout << "B: ";
time_input(&hrB, &minB, prompt); //Reads time B.
cout << "Time A: ";
time_output(hrA, minA); //Prints the timeA.
cout << endl << "Time B: ";
time_output(hrB, minB); //Prints the timeB.
cout << endl << "added: ";
time_add(hrA, minA, hrB, minB, &totalHr, &totalMin); //Adds timeA, and timeB.
time_output(totalHr, totalMin); //Prints the time added.
time_subtract(hrA, minA, hrB, minB, &totalHr, &totalMin); //Subtracts timeB from timeA.
cout << " duration: ";
time_output(totalHr, totalMin); //Prints the duration.
break;
case 'x':
case 'X': break;
default : cout << "Invalid selection." << endl;
}
if(choice == 'x' || choice == 'X')
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.