-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdmil.cpp
63 lines (48 loc) · 1.35 KB
/
stdmil.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
// Standard to military time converter
#include <iostream>
#include <string>
std::string change_format(const std::string& time);
int main(int argc, char *argv[])
{
if(argc != 2){
std::cout <<"Usage: stdmil TIME\nTIME: [hh:mm:ss]{AM|PM}\n"
<<"Convert standard time to military time.\n"
<<"Example: stdmil 07:54:30AM\n";
return -1;
}
std::cout << change_format(argv[1]) << std::endl;
}
std::string change_format(const std::string& time)
{
std::string result = time;
if(result[8] == 'A') {
result.pop_back();
result.pop_back();
if (result[0] == '1') {
std::string temp;
temp.append(1,result[0]);
temp.append(1,result[1]);
result.erase(0,2);
if(temp == "12"){
result.insert(0,"00");
return result;
}
return result;
}
return result;
}
result.pop_back();
result.pop_back();
// std::string temp = "" + result[0] + result[1];
std::string temp;
temp.append(1,result[0]);
temp.append(1,result[1]);
if(temp == "00")
return result;
if(temp == "12")
return result;
int hours = std::stoi(temp) + 12 ;
result.erase(0,2);
result.insert(0,std::to_string(hours));
return result;
}