Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. int main(void) {
  7.     FILE *strmIn = fopen("input.bin", "rb");
  8.     FILE *strmOut = fopen("output.bin", "wb");
  9.     enum {
  10.         lowPyr = -1,
  11.         highPyr = 1,
  12.         notPyr = 0,
  13.         unDef
  14.     } a = unDef;
  15.     int size = 1;
  16.     int *arr = (int *)calloc(size, sizeof(int));
  17.     int i = 0;
  18.     while(fread(&arr[i], sizeof(int), 1, strmIn)) {
  19.         i++;
  20.         if (i == size) {
  21.             size *= 2;
  22.             arr = (int *)realloc(arr, size * sizeof(int));
  23.         }
  24.     }
  25.     size = i;
  26.     for (int i = 0; 2 * i + 1 < size; i++) {
  27.         for (int ii = 1; ii <= 2; ii++) {
  28.             int newI = 2 * i + ii;
  29.             if (newI < size) {
  30.                 if (arr[i] > arr[newI]) {
  31.                     if (a == unDef) {
  32.                         a = lowPyr;
  33.                     } else if (a == highPyr) {
  34.                         a = notPyr;
  35.                         fwrite(&a, sizeof(int), 1, strmOut);
  36.                         free(arr);
  37.                         return 0;
  38.                     }
  39.                 } else if (arr[i] < arr[newI]) {
  40.                     if (a == unDef) {
  41.                         a = highPyr;
  42.                     } else if (a == lowPyr) {
  43.                         a = notPyr;
  44.                         fwrite(&a, sizeof(int), 1, strmOut);
  45.                         free(arr);
  46.                         return 0;
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     if (a == unDef) {
  53.         a = highPyr;
  54.     }
  55.     fwrite(&a, sizeof(int), 1, strmOut);
  56.     free(arr);
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement