-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-stream.cpp
46 lines (39 loc) · 936 Bytes
/
string-stream.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
// string str = "17,5,87,23,24";
string str = "12:05:45AM";
stringstream s(str);
// int word;
// char ch;
// int i = 0;
// while (s >> word >> ch)
// {
// cout << word << endl;
// }
// s >> word;
// cout << word;
int hour, min, sec;
char delimeter;
string zone;
s >> hour >> delimeter >> min >> delimeter >> sec >> zone;
if (zone == "PM" && hour < 12)
{
hour += 12;
}
else if (zone == "AM" && hour == 12)
{
hour = 0;
}
cout << hour << " " << min << " " << sec << " " << zone << endl;
stringstream new_time;
hour < 10 ? new_time << "0" << hour : new_time << hour;
new_time << ":";
min < 10 ? new_time << "0" << min : new_time << min;
new_time << ":";
sec < 10 ? new_time << "0" << sec : new_time << sec;
// new_time << hour << ":" << min << ":" << sec;
str = new_time.str();
cout << str << endl;
}