SHARE
TWEET

Untitled

a guest Nov 19th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void printmat(char **matrice, int n) {
  6.   int **a = (int **) matrice;
  7.   for (int i = 0; i < n; i++) {
  8.     printf("\n");
  9.     for (int j = 1; j < (matrice[i][0] + 1); j++){
  10.       printf("%08X  ", a[i][j]);
  11.     }
  12.   }
  13.   printf("\n");
  14. }
  15.  
  16. void modify (char *linie, int dimensiune, int indexBytes, int valNoua, int numarBytes) {
  17.   int lungimeL = (linie[0] + 1), i = 0, j = 0;
  18.   char val[4];
  19.   val[0] = valNoua & 0xFF;
  20.   val[1] = (valNoua >> 8) & 0xFF;
  21.   val[2] = (valNoua >> 16) & 0xFF;
  22.   val[3] = (valNoua >> 24) & 0xFF;
  23.  
  24.   if((indexBytes) > lungimeL * 4) {
  25.     linie = realloc (linie, (numarBytes) * sizeof(int));
  26.     for (i = lungimeL * 4; i < numarBytes; i++) {
  27.       linie[i] = 0;
  28.     }
  29.   }
  30.  
  31.  
  32.   for (int i = indexBytes - dimensiune; i < indexBytes; i++) {
  33.     linie[i]=val[j];
  34.     j++;
  35.     if (j == 4) {
  36.       break;
  37.     }
  38.   }
  39. }
  40.  
  41. void delete (char *linie, int indexDate, int dimensiune) {
  42.   int j = 0;
  43.   while(j < dimensiune) {
  44.     linie[(indexDate-1)*dimensiune+4+j] = 0x00;
  45.     j++;
  46.   }
  47. }
  48.  
  49. void swap (char *linie, int indexBytes, int dimensiune) {
  50.   int i = 0, j = dimensiune - 1, v[5] = {0};
  51.   int start = indexBytes;
  52.  
  53.   if (dimensiune == 1) {
  54.     /* v[0] = linie[start];
  55.     linie[start] = linie[start + 1];
  56.     linie[start + 1] = v[0]; */
  57.     return;
  58.   } else {
  59.     while (i < 4) {
  60.       v[i] = linie[start + i];
  61.       i++;
  62.     }
  63.   }
  64.   i = 0;
  65.   while(i < dimensiune) {
  66.     linie[start+i] = v[j];
  67.     i++;
  68.     j--;
  69.   }
  70. }
  71.  
  72. int** genMat(int n) {
  73.   FILE *f;
  74.   f = fopen("input_task1.txt", "r");
  75.  
  76.   int **matrice, i, j, col;
  77.  
  78.   matrice = malloc(n * sizeof(int));
  79.   for (i = 0; i < n; i++) {
  80.     fscanf(f, "%d", &col);
  81.     matrice[i] = calloc(col + 1, sizeof(int));
  82.     matrice[i][0] = col;
  83.     for (j = 1; j < col + 1; j++) {
  84.       fscanf(f, "%x", &matrice[i][j]);
  85.     }
  86.   }
  87.  
  88.   return matrice;
  89. }
  90.  
  91. char** genviz(char **matrice, int n) {
  92.   int i, j;
  93.   char **viz;
  94.   viz = malloc(n * sizeof(int));
  95.   for (i = 0; i < n; i++) {
  96.     viz[i] = calloc((matrice[i][0] + 1), sizeof(int));
  97.   }
  98.   return viz;
  99. }
  100.  
  101. void fill(char **matrice, int n, char **viz, int i, int j, int *count) {
  102.   if (i < 0 || i >= n || j < 4 || j >= (matrice[i][0] + 1) * 4) {
  103.     return;
  104.   }
  105.   if (matrice[i][j] != viz[i][j]) {
  106.     return;
  107.   }
  108.  
  109.   viz[i][j] += 1;
  110.   ++*count;
  111.  
  112.   fill(matrice, n, viz, i+1, j, count);
  113.   fill(matrice, n, viz, i-1, j, count);
  114.   fill(matrice, n, viz, i, j+1, count);
  115.   fill(matrice, n, viz, i, j-1, count);
  116. }
  117.  
  118. void task_one(char **matrice, int n) {
  119.   int i, j, s = 0, cnt = 0;
  120.  
  121.   for (i = 0; i < n; i++) {
  122.     for (j = 4; j < (matrice[i][0] + 1) * 4; j++) {
  123.       if (i == 0 || j == 4 || i == n - 1 || j == (matrice[i][0] + 1) * 4 - 1) {
  124.         s += matrice[i][j];
  125.         cnt++;
  126.       }
  127.     }
  128.   }
  129.  
  130.   printf("%.7f\n", (double)s / cnt);
  131. }
  132.  
  133. void task_two(char **matrice, int n) {
  134.   FILE *o;
  135.   o = fopen("input_task2.txt", "r");
  136.  
  137.   int i, k, linie, indexDate, valNoua, numarBytes, indexBytes, dimensiune;
  138.   //Fol vector pentru a evita "stack smashing", nu am gasit alta cale
  139.   char actiuni[4];
  140.   fscanf(o, "%d", &k);
  141.  
  142.   while (k) {
  143.     for (i = 0; i < 2; i++) {
  144.       fscanf(o, "%s", &actiuni[i]);
  145.     }
  146.     fscanf(o, "%d%d", &linie, &indexDate);
  147.  
  148.     if (actiuni[1] == 'C') {
  149.       dimensiune = sizeof(char);
  150.     }
  151.     if (actiuni[1] == 'S') {
  152.       dimensiune = sizeof(short);
  153.     }
  154.     if (actiuni[1] == 'I') {
  155.       dimensiune = sizeof(int);
  156.     }
  157.  
  158.     indexBytes = indexDate * dimensiune + 4;
  159.     if (indexBytes % 4 == 0) {
  160.       numarBytes = indexBytes;
  161.     } else {
  162.       numarBytes = (indexBytes) + 4 - ((indexBytes) % 4);
  163.     }
  164.    
  165.     if(actiuni[0] == 'M') {
  166.       fscanf(o, "%x", &valNoua);
  167.       //modify(*(matrice + linie), dimensiune, indexBytes, valNoua, numarBytes);
  168.       int lungimeL = (matrice[linie][0] + 1), i = 0, j = 0;
  169.       char val[4];
  170.       val[0] = valNoua & 0xFF;
  171.       val[1] = (valNoua >> 8) & 0xFF;
  172.       val[2] = (valNoua >> 16) & 0xFF;
  173.       val[3] = (valNoua >> 24) & 0xFF;
  174.       if((indexBytes) > lungimeL * 4) {
  175.         matrice[linie] = realloc (matrice[linie], (numarBytes) * sizeof(int));
  176.         for (i = lungimeL * 4; i < numarBytes; i++) {
  177.           matrice[linie][i] = 0;
  178.         }
  179.       }
  180.  
  181.       for (int i = indexBytes - dimensiune; i < indexBytes; i++) {
  182.         matrice[linie][i]=val[j];
  183.         j++;
  184.         if (j == 4) {
  185.           break;
  186.         }
  187.       }
  188.       if(matrice[linie][0] < numarBytes / 4 - 1) {
  189.         matrice[linie][0] = numarBytes / 4 - 1;  
  190.       }
  191.     }
  192.  
  193.     if(actiuni[0] == 'D') {
  194.       delete(*(matrice + linie), indexDate, dimensiune);
  195.     }
  196.  
  197.     if(actiuni[0] == 'S') {
  198.       swap(*(matrice + linie), indexBytes, dimensiune);
  199.     }
  200.     k--;
  201.   }
  202. }
  203.  
  204. void task_three(char **matrice, int n) {
  205.   int i, j, *count;
  206.   int max = 0, x, y, reset = 0;
  207.   count = &reset;
  208.   char **viz = genviz(matrice, n);
  209.  
  210.   for (i = 0; i < n; i++) {
  211.     for (j = 4; j < (matrice[i][0]+1)*4; j++) {
  212.       if (matrice[i][j] == 0 && viz[i][j] == 0) {
  213.         fill(matrice, n, viz, i, j, count);
  214.         if (*count > max) {
  215.           x = i;
  216.           y = j;
  217.           max = *count;
  218.         }
  219.         reset = 0;
  220.         count = &reset;
  221.       }
  222.     }
  223.   }
  224.   printf("%d %d %d", x, y-4, max);
  225. }
  226.  
  227. int main(void){
  228.   int i, j, n;
  229.  
  230.   scanf("%d", &n);
  231.  
  232.   int **a = genMat(n);
  233.   char **matrice = (char **) a;
  234.  
  235.   printf("task 1\n");
  236.   task_one(matrice, n);
  237.  
  238.   printf("task 2");
  239.   task_two(matrice, n);
  240.   printmat(matrice, n);
  241.  
  242.   printf("task 3\n");
  243.   task_three(matrice, n);
  244.  
  245. /*   for (i = 0; i < n; i++) {
  246.     free(a[i]);
  247.   }
  248.   free(a); */
  249.  
  250.   return 0;
  251. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top