daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2.  
  3. using namespace std;
  4.  
  5. int time12to24(char* str) {
  6.     int type = -2;;
  7.     char* format1[2];
  8.     format1[0] = "a.m.";
  9.     format1[1] = "p.m.";
  10.     for (int i = 0; i < 4; i++) {
  11.         if (type != -2 && str[i] != format1[type][i] || str[i] != format1[0][i] && str[i] != format1[1][i]) {
  12.             return -1;
  13.         }
  14.         if (type == -2 && str[i] == format1[0][i]) {
  15.             type = 0;
  16.         }
  17.         else if (type == -2 && str[i] == format1[1][i]) {
  18.             type = 1;
  19.         }
  20.     }
  21.     return type;
  22. }
  23.  
  24. int main() {
  25.     int hour, min;
  26.     char format[4];
  27.     scanf("%d:%d %s", &hour, &min, format);
  28.     int timer = time12to24(format);
  29.     if (hour > 12 || hour <= 0 || min <= 0 || min >= 60 || timer == -1) {
  30.         printf("Incorrect time format\n");
  31.         return 0;
  32.     }
  33.     if (hour + 12*timer == 24) {
  34.         hour = -12;
  35.     }
  36.     printf("%02d:%02d\n", hour + 12*timer, min);
  37.     return 0;
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top