Advertisement
CODEMALAYALAM

Untitled

Aug 3rd, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <assert.h>
  2. #include <limits.h>
  3. #include <math.h>
  4. #include <stdbool.h>
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. char* readline();
  12. char** split_string(char*);
  13.  
  14. // Complete the catAndMouse function below.
  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. char* catAndMouse(int x, int y, int z) {
  26.  
  27.  char* catA = "Cat A", *catB = "Cat B", *mouse = "Mouse C";
  28.  
  29.  if(abs(z-x)==abs(z-y)) return mouse;
  30.  
  31.  return abs(z-x)>abs(z-y)?catB:catA;
  32.  
  33. }
  34.  
  35. int main()
  36. {
  37.     FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
  38.  
  39.     char* q_endptr;
  40.     char* q_str = readline();
  41.     int q = strtol(q_str, &q_endptr, 10);
  42.  
  43.     if (q_endptr == q_str || *q_endptr != '\0') { exit(EXIT_FAILURE); }
  44.  
  45.     for (int q_itr = 0; q_itr < q; q_itr++) {
  46.         char** xyz = split_string(readline());
  47.  
  48.         char* x_endptr;
  49.         char* x_str = xyz[0];
  50.         int x = strtol(x_str, &x_endptr, 10);
  51.  
  52.         if (x_endptr == x_str || *x_endptr != '\0') { exit(EXIT_FAILURE); }
  53.  
  54.         char* y_endptr;
  55.         char* y_str = xyz[1];
  56.         int y = strtol(y_str, &y_endptr, 10);
  57.  
  58.         if (y_endptr == y_str || *y_endptr != '\0') { exit(EXIT_FAILURE); }
  59.  
  60.         char* z_endptr;
  61.         char* z_str = xyz[2];
  62.         int z = strtol(z_str, &z_endptr, 10);
  63.  
  64.         if (z_endptr == z_str || *z_endptr != '\0') { exit(EXIT_FAILURE); }
  65.  
  66.         char* result = catAndMouse(x, y, z);
  67.  
  68.         fprintf(fptr, "%s\n", result);
  69.     }
  70.  
  71.     fclose(fptr);
  72.  
  73.     return 0;
  74. }
  75.  
  76. char* readline() {
  77.     size_t alloc_length = 1024;
  78.     size_t data_length = 0;
  79.     char* data = malloc(alloc_length);
  80.  
  81.     while (true) {
  82.         char* cursor = data + data_length;
  83.         char* line = fgets(cursor, alloc_length - data_length, stdin);
  84.  
  85.         if (!line) { break; }
  86.  
  87.         data_length += strlen(cursor);
  88.  
  89.         if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
  90.  
  91.         size_t new_length = alloc_length << 1;
  92.         data = realloc(data, new_length);
  93.  
  94.         if (!data) { break; }
  95.  
  96.         alloc_length = new_length;
  97.     }
  98.  
  99.     if (data[data_length - 1] == '\n') {
  100.         data[data_length - 1] = '\0';
  101.     }
  102.  
  103.     data = realloc(data, data_length);
  104.  
  105.     return data;
  106. }
  107.  
  108. char** split_string(char* str) {
  109.     char** splits = NULL;
  110.     char* token = strtok(str, " ");
  111.  
  112.     int spaces = 0;
  113.  
  114.     while (token) {
  115.         splits = realloc(splits, sizeof(char*) * ++spaces);
  116.         if (!splits) {
  117.             return splits;
  118.         }
  119.  
  120.         splits[spaces - 1] = token;
  121.  
  122.         token = strtok(NULL, " ");
  123.     }
  124.  
  125.     return splits;
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement