Advertisement
KgCro

Untitled

May 29th, 2020
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. a#include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5.  
  6. #ifndef DEBUG
  7. #define DEBUG(...)printf(__VA_ARGS__)
  8. #endif
  9.  
  10. int main() {
  11.     int n, max_red = 1, max_stup = 1;// max_red/_stup su trenutni maksimalan red i stupac
  12.     char **ans;
  13.     //malloc za redove
  14.     ans = malloc(max_red * sizeof(char *));
  15.     for (int i = 0; i < max_red; ++i)
  16.     {
  17.         ans[i] = malloc(max_stup * sizeof(char)); // malloc za stupce
  18.         memset(ans[i], ' ', max_stup * sizeof(char)); // punjenje tablice praznim pozicijama
  19.     }
  20.  
  21.     scanf("%d", &n);
  22.  
  23.     for (int i = 0; i < n; ++i)
  24.     {
  25.         int r, s;
  26.         char c;
  27.         scanf(" %d %d %c", &r, &s, &c);
  28.         r++;
  29.         s++;
  30.         //zasto inkrementacija ????
  31.  
  32.         if (r > max_red) // ako je skenirani red veci od trenutnog makismalnog
  33.             // prosiri polje ans za toliko redova
  34.         {
  35.             ans = realloc(ans, r * sizeof(char *));
  36.             for (int j = max_red; j < r; ++j)
  37.             {
  38.                 ans[j] = malloc(max_stup * sizeof(char));//jer pri povecanju polja , nove ćelije u vec
  39.                 //postojecim stupcima nemaju alocirano za sebe nista, zato moramo opet alocirat stupce
  40.                 //ovaj put samo za te "nove" ćelije
  41.                 memset(ans[j], ' ', max_stup * sizeof(char)); // i naravno setat ih da sadrze razmak
  42.             }
  43.             max_red = r;
  44.         }
  45.  
  46.         if (s > max_stup)// ako je skenirani stupac veci od trenutnog maximalnog
  47.             //prosiri polje ans za toliko stupaca
  48.         {
  49.             for (int j = 0; j < max_red; ++j)
  50.             {
  51.                 ans[j] = realloc(ans[j], s * sizeof(char));
  52.                 memset(ans[j] + max_stup, ' ', (s - max_stup) * sizeof(char));
  53.                 // s - max_stup = onoliko koliko je prosireno, tj. setaj samo one koji su dodani ekstra
  54.                 //jer su ovi na pocetku vec setani
  55.                 //sto znaci ans[j] + max_stup ??????
  56.             }
  57.             max_stup = s;
  58.         }
  59.         ans[--r][--s] = c; // postavi znak na zadanu poziciju u polju
  60.         //zasto dekrementirati ??
  61.     }
  62.  
  63.     //ispis
  64.     for (int i = 0; i < max_red; ++i)
  65.     {
  66.         for (int j = 0; j < max_stup; ++j)
  67.         {
  68.             printf("%c", ans[i][j]);
  69.         }
  70.         printf("\n");
  71.     }
  72.  
  73.   return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement