Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define BUFFER_SIZE 1024 * 16
  6.  
  7. int getRotatedX(int x, int rotate, int width) {
  8.     if(x >= rotate) {
  9.         return x - rotate;
  10.     } else {
  11.         return width - (rotate - x);
  12.     }
  13. }
  14.  
  15. int main(int argc, char *argv[]) {
  16.     if(argc == 1) {
  17.         fprintf(stderr, "Chybi cesta ke vstupnimu a vystupnimu souboru.");
  18.         exit(1);
  19.     } else if(argc == 2) {
  20.         fprintf(stderr, "Chybi cesta k vystupnimu souboru.");
  21.         exit(1);
  22.     }
  23.  
  24.     FILE* input = fopen(argv[1], "r");
  25.     FILE *output = fopen(argv[2], "w");
  26.     char line[BUFFER_SIZE];
  27.     int **arr;
  28.     int width;
  29.     int height;
  30.     int y = -1;
  31.     int rotate;
  32.  
  33.     if (input == NULL)
  34.     {
  35.         fprintf(stderr, "Vstupni soubor \"%s\" neexistuje", argv[1]);
  36.         exit(1);
  37.     }
  38.  
  39.     while (fgets(line, BUFFER_SIZE, input))
  40.     {
  41.         char* tmp = strdup(line);
  42.         int x = 0;
  43.         const char* tok;
  44.  
  45.         for (tok = strtok(line, " ");
  46.              tok && *tok;
  47.              tok = strtok(NULL, " \n"))
  48.         {
  49.             if(y == -1) {
  50.                 if(x == 0) {
  51.                     height = strtol(tok, NULL, 10);
  52.                 } else if(x == 1) {
  53.                     width = strtol(tok, NULL, 10);
  54.  
  55.                     arr = (int **)malloc(height * sizeof(int *));
  56.                     for (int i=0; i<height; i++)
  57.                         arr[i] = (int *)malloc(width * sizeof(int));
  58.                 } else if(x = 2) {
  59.                     rotate = strtol(tok, NULL, 10);
  60.                 }
  61.             }
  62.             if(y != -1) {
  63.                 arr[y][getRotatedX(x, rotate, width)] = strtol(tok, NULL, 10);
  64.             }
  65.             x++;
  66.         }
  67.  
  68.         free(tmp);
  69.         y++;
  70.     }
  71.  
  72.     for (int i = 0; i <  height; i++) {
  73.         for (int j = 0; j < width; j++) {
  74.             if(j == 0) {
  75.                 fprintf(output, "%d", arr[i][j]);
  76.             } else if(j == width - 1) {
  77.                 fprintf(output, " %d\n", arr[i][j]);
  78.             } else {
  79.                 fprintf(output, " %d", arr[i][j]);
  80.             }
  81.         }
  82.     }
  83.  
  84.     fclose(input);
  85.     fclose(output);
  86.  
  87.     for (int i = 0; i < height; i++)
  88.     {
  89.         int* currentIntPtr = arr[i];
  90.         free(currentIntPtr);
  91.     }
  92.     free(arr);
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement