VF-

Untitled

VF-
Nov 27th, 2022
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main()
  6. {
  7.     int matrix[20][20];
  8.     FILE *txt;
  9.     char sor[1024];
  10.     int x, y, n, m, sugar = 1, hatra, elso = 1;
  11.  
  12.     txt = fopen("be.txt", "r");
  13.     fscanf(txt, "%d,%d\n", &y, &x);
  14.     x--;
  15.     y--;
  16.  
  17.     m = 0;
  18.     while (fgets(sor, sizeof(sor), txt)) {
  19.         if (sor[0] == '-') {
  20.             break;
  21.         }
  22.  
  23.         n = 0;
  24.         char *pozicio = strtok(sor, ",");
  25.         while (pozicio != NULL) {
  26.             matrix[m][n++] = atoi(pozicio);
  27.             pozicio = strtok(NULL, ",");
  28.         }
  29.         m++;
  30.     }
  31.     hatra = n * m;
  32.  
  33.     fclose(txt);
  34.  
  35.     txt = fopen("ki.txt", "w");
  36.  
  37.     if (x >= 0 && x < n && y >= 0 && y < m) {
  38.         fprintf(txt, "%d%s", matrix[y][x], --hatra ? "," : "");
  39.     }
  40.  
  41.     while (hatra) {
  42.         for (int i = elso ? 0 : -sugar + 1; i < sugar; i++) {
  43.             if (x - i >= 0 && x - i < n && y - sugar >= 0 && y - sugar < m) {
  44.                 fprintf(txt, "%d%s", matrix[y - sugar][x - i], --hatra ? "," : "");
  45.             }
  46.         }
  47.  
  48.         for (int i = -sugar; i < sugar; i++) {
  49.             if (x - sugar >= 0 && x - sugar < n && y + i >= 0 && y + i < m) {
  50.                 fprintf(txt, "%d%s", matrix[y + i][x - sugar], --hatra ? "," : "");
  51.             }
  52.         }
  53.  
  54.         for (int i = -sugar; i < sugar; i++) {
  55.             if (x + i >= 0 && x + i < n && y + sugar >= 0 && y + sugar < m) {
  56.                 fprintf(txt, "%d%s", matrix[y + sugar][x + i], --hatra ? "," : "");
  57.             }
  58.         }
  59.            
  60.         for (int i = -sugar; i <= sugar; i++) {
  61.             if (x + sugar >= 0 && x + sugar < n && y - i >= 0 && y - i < m) {
  62.                 fprintf(txt, "%d%s", matrix[y - i][x + sugar], --hatra ? "," : "");
  63.             }
  64.         }
  65.            
  66.         elso = 0;
  67.         sugar++;
  68.     }
  69.  
  70.     fprintf(txt, "\n");
  71.  
  72.     fclose(txt);
  73.  
  74.     return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment