Read Times in 12 or 24 Hour Format
Solution
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
/*
In this program we are given an input of a data which has
a start time and an end time in a 12 hr / 24 hr format.
we have to read all datasets from this data and display the
start and end times and the interval between them with proper formatting.
*/
// this function converts a string passed to it into an integer
inttoInt(string s){
//cout<<s<<endl;
int i=0;
for(int j=s.length()-1;j>=0;j–){
//cout<<j<<endl;
i+=pow(10,s.length()-1-j)*(s[j]-‘0′);
//cout<<i<<endl;
}
return i;
}
// this function removes spaces in the string
stringremoveSpaces(string s){
string r=””;
for(int i=0;i<s.length();i++){
if(s[i]!=’ ‘){
r+=s[i];
}
}
return r;
}
// this function converts integers to string
stringtoString(int i){
string c=”0123456789″;
intu,d;
u = i%10;
d = i/10;
string s=””;
s+=c[d];
s+=c[u];
if(s.length()<2){
s = “0”+s;
}
return s;
}
// this function processes the dataset and displays it with formatting
voidprocessLine(string line){
string p;
p = removeSpaces(line);
string c = p.substr(0,2);
intca = toInt(c);
p = p.substr(2);
//cout<<“here”<<endl;
//separately deal with the cases
if(ca==12){
//cout<<“here1″<<endl;
int i=0;
string h1=””;
while(p[i]!=’:’){
h1+=p[i];
i++;
}
if(h1.length()<2){
h1 = “0”+h1;
}
i++;
string m1=””;
while(isdigit(p[i])){
m1+=p[i];
i++;
}
if(m1.length()<2){
m1 = “0”+m1;
}
string tag1;
if(p[i]==’a’ || p[i]==’A’){
tag1=”AM”;
}
else{
tag1=”PM”;
}
i++;
//cout<<“here111″<<endl;
string h2=””;
while(p[i]!=’:’){
h2+=p[i];
i++;
}
if(h2.length()<2){
h2 = “0”+h2;
}
i++;
string m2=””;
while(isdigit(p[i])){
m2+=p[i];
i++;
}
if(m2.length()<2){
m2 = “0”+m2;
}
string tag2;
if(p[i]==’a’ || p[i]==’A’){
tag2=”AM”;
}
else{
tag2=”PM”;
}
//cout<<“here111111″<<endl;
int H1,H2,M1,M2;
H1 = toInt(h1);
H2 = toInt(h2);
M1 = toInt(m1);
M2 = toInt(m2);
//cout<<m1<<m2<<endl;
//cout<<“here”<<endl;
int d1=0;
int d2=0;
if(M2<M1){
M2 = M2+60;
H2–;
}
d2=M2-M1;
if(tag1!=tag2){
H2+=12;
d1 = H2-H1;
}
else{
if(H2<H1){
H2+=24;
}
d1 = H2-H1;
}
cout<<“Start Time: “<<h1<<“:”<<m1<<tag1<<” End Time : “<<h2<<“:”<<m2<<tag2<<” Time Interval : “<<toString(d1)<<“:”<<toString(d2)<<endl;
//cout<<“here1″<<endl;
}
else{
//cout<<“here2″<<endl;
int i=0;
string h1=””;
while(p[i]!=’:’){
h1+=p[i];
i++;
}
if(h1.length()<2){
h1 = “0”+h1;
}
i++;
//cout<<h1<<endl;
string m1=””;
for(int j=0;j<2;j++){
m1+=p[i];
i++;
}
if(m1.length()<2){
m1 = “0”+m1;
}
//cout<<m1<<endl;
//cout<<“here”<<endl;
string h2=””;
while(p[i]!=’:’){
h2+=p[i];
i++;
}
if(h2.length()<2){
h2 = “0”+h2;
}
i++;
string m2=””;
while(i<p.length() &&isdigit(p[i])){
m2+=p[i];
i++;
}
if(m2.length()<2){
m2 = “0”+m2;
}
//cout<<h1<<” “<<h2<<” “<<m1<<” “<<m2<<endl;
//cout<<“here123″<<endl;
int d1=0;
int d2=0;
int H1,H2,M1,M2;
H1 = toInt(h1);
H2 = toInt(h2);
M1 = toInt(m1);
M2 = toInt(m2);
//cout<<“here”<<endl;
if(M2<M1){
M2 = M2+60;
H2–;
}
d2=M2-M1;
//cout<<“here111″<<endl;
if(H2<H1){
H2 = H2+24;
}
d1 = H2-H1;
cout<<“Start Time: “<<h1<<“:”<<m1<<” End Time : “<<h2<<“:”<<m2<<” Time Interval : “<<toString(d1)<<“:”<<toString(d2)<<endl;
}
}
int main(){
string line;
// we are reading the dataset line by line and passing it to processLine function
while(getline(cin,line)){
//processLine(line);
processLine(line);
}
}