In: Computer Science
Develop a set of test cases for the algorithm In a scheduling program, we want to check whether two appointments overlap. For simplicity, appointments start at a full hour, and we use military time (with hours 0–24). The following pseudocode describes an algorithm that determines whether the appointment with start time start1 and end time end1 overlaps with the appointment with start time start2 and end time end2.
If start1 > start2
s = start1
Else s = start2
If end1 < end2
e = endl
Else e = end2
If s < e
The appointments overlap.
Else The appointments don’t overlap.
Trace this algorithm with an appointment from 10–12 and one from 11–13, then with an appointment from 10–11 and one from 12–13.
Given a time in 12-hour AM/PM format, convert it to military
(24-hour) time.
Note: Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a
24-hour clock. Noon is 12:00:00 PM on 12-hour clock and 12:00:00 on
24-hour clock
Examples:
Input : A single string containing a time in 12-hour clock format(hh:mm:ss AM or hh:mm:ss PM where 01 <= hh <= 12 or 01 <= mm,ss <= 59 Output :Convert and print the given time in 24-hour format, where 00 <= hh <= 23 Input : 07:05:45PM Output : 19:05:45
// C++ program to convert 12 hour to 24 hour
// format
#include<iostream>
using
namespace
std;
void
print24(string str)
{
// Get
hours
int
h1 =
(
int
)str[1] -
'0'
;
int
h2 =
(
int
)str[0] -
'0'
;
int
hh =
(h2 * 10 + h1 % 10);
// If time is in
"AM"
if
(str[8] ==
'A'
)
{
if
(hh == 12)
{
cout
<<
"00"
;
for
(
int
i=2; i <= 7;
i++)
cout
<< str[i];
}
else
{
for
(
int
i=0; i <= 7;
i++)
cout
<< str[i];
}
}
// If time is in
"PM"
else
{
if
(hh == 12)
{
cout
<<
"12"
;
for
(
int
i=2; i <= 7;
i++)
cout
<< str[i];
}
else
{
hh
= hh + 12;
cout
<< hh;
for
(
int
i=2; i <= 7;
i++)
cout
<< str[i];
}
}
}
// Driver code
int
main()
{
string str =
"07:05:45PM"
;
print24(str);
return
0;
}
Output:- 19:05:45