Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- using namespace std;
- int time12to24(char* str) {
- int type = -2;;
- char* format1[2];
- format1[0] = "a.m.";
- format1[1] = "p.m.";
- for (int i = 0; i < 4; i++) {
- if (type != -2 && str[i] != format1[type][i] || str[i] != format1[0][i] && str[i] != format1[1][i]) {
- return -1;
- }
- if (type == -2 && str[i] == format1[0][i]) {
- type = 0;
- }
- else if (type == -2 && str[i] == format1[1][i]) {
- type = 1;
- }
- }
- return type;
- }
- int main() {
- int hour, min;
- char format[4];
- scanf("%d:%d %s", &hour, &min, format);
- int timer = time12to24(format);
- if (hour > 12 || hour <= 0 || min <= 0 || min >= 60 || timer == -1) {
- printf("Incorrect time format\n");
- return 0;
- }
- if (hour + 12*timer == 24) {
- hour = -12;
- }
- printf("%02d:%02d\n", hour + 12*timer, min);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement