forked from knakul853/spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeconversion.cpp
130 lines (104 loc) · 2.01 KB
/
timeconversion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//am-pm to 24 hr format
#include<bits/stdc++.h>
using namespace std;
int twenty4(){
string i,r,o,t;
int h,m,s;
cout<<"Enter time in 24hr format :: \n HH:MM:SS";
cin>>i;
t=i;
stringstream j(t);
j>>h;
if(h>24){
cout<<"wrong input";
return 0;
}
t="ab";
t[0]=i[3];
t[1]=i[4];
stringstream j1(t);
j1>>m;
if(m>60){
cout<<"wrong input";
return 0;
}
t[0]=i[6];
t[1]=i[7];
stringstream j2(t);
j2>>s;
if(s>60){
cout<<"wrong input";
return 0;
}
t[0]=i[8];
t[1]=i[9];
if(h<12)
h=h-12;
cout<<h<<":";
if(m<10)
cout<<"0";
cout<<m<<":";
if(s<10)
cout<<"0";
cout<<s<<"\n";
return 0;
}
int AMPM(){
string i,r,o,t;
int h,m,s;
cout<<"Enter time in AM PM format :: \n hh:mm:ssAM \n";
cin>>i;
t=i;
stringstream j(t);
j>>h;
if(h>12){
cout<<"wrong input";
return 0;
}
t="ab";
t[0]=i[3];
t[1]=i[4];
stringstream j1(t);
j1>>m;
if(m>60){
cout<<"wrong input";
return 0;
}
t[0]=i[6];
t[1]=i[7];
stringstream j2(t);
j2>>s;
if(s>60){
cout<<"wrong input";
return 0;
}
t[0]=i[8];
t[1]=i[9];
if(h==12&&(t=="AM"||t=="am"))
h=00;
if(h<12&&(t=="PM"||t=="pm"))
h=h+12;
if(h<10)
cout<<"0";
cout<<h<<":";
if(m<10)
cout<<"0";
cout<<m<<":";
if(s<10)
cout<<"0";
cout<<s<<"\n";
}
int main(){
int x;
do{cout<<"\n";
cout<<" Enter 1 for AM PM to 24 \n Enter 2 for 24 to AM PM \n Enter 0 to exit\n";
cin>>x;
if(x==1)
AMPM();
else if(x==2)
twenty4();
else if(x!=0)
cout<<"enter correct choice";
}while(x);
return 0;
}