Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <math.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. char* readline();
  10.  
  11. /*
  12.  * Complete the timeConversion function below.
  13.  */
  14.  
  15. /*
  16.  * Please either make the string static or allocate on the heap. For example,
  17.  * static char str[] = "hello world";
  18.  * return str;
  19.  *
  20.  * OR
  21.  *
  22.  * char* str = "hello world";
  23.  * return str;
  24.  *
  25.  */
  26. char* timeConversion(char* s) {
  27.     int lastchar=strlen(s)-1;//position of last char in the array
  28.     char* pplast=s+(lastchar-1);//pointer to previous of last char so can check (A||P)
  29.     char* fDigit=s;char* sDigit=fDigit+1;//first and second digit
  30.     int ifDigit=*fDigit-'0';
  31.     int isDigit=*sDigit-'0';
  32.     int concat=(ifDigit*10+isDigit);
  33.     if ((*pplast=='P'&&concat!=12)||(*pplast=='A'&&concat==12)){
  34.         int plus=(concat==12)?-12:12;
  35.         int sum;
  36.         sum=concat+plus;
  37.         int f,s;
  38.         f=(sum/10);
  39.         s=(sum%10);
  40.         *fDigit=f+'0';
  41.         *sDigit=s+'0';
  42.     }
  43.     *pplast='\0';
  44.     return s;
  45. }
  46. int main()
  47. {
  48.     FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
  49.  
  50.     char* s = readline();
  51.  
  52.     char* result = timeConversion(s);
  53.  
  54.     fprintf(fptr, "%s\n", result);
  55.  
  56.     fclose(fptr);
  57.  
  58.     return 0;
  59. }
  60.  
  61. char* readline() {
  62.     size_t alloc_length = 1024;
  63.     size_t data_length = 0;
  64.     char* data = malloc(alloc_length);
  65.  
  66.     while (true) {
  67.         char* cursor = data + data_length;
  68.         char* line = fgets(cursor, alloc_length - data_length, stdin);
  69.  
  70.         if (!line) { break; }
  71.  
  72.         data_length += strlen(cursor);
  73.  
  74.         if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
  75.  
  76.         size_t new_length = alloc_length << 1;
  77.         data = realloc(data, new_length);
  78.  
  79.         if (!data) { break; }
  80.  
  81.         alloc_length = new_length;
  82.     }
  83.  
  84.     if (data[data_length - 1] == '\n') {
  85.         data[data_length - 1] = '\0';
  86.     }
  87.  
  88.     data = realloc(data, data_length);
  89.  
  90.     return data;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement