sta-s2z

Untitled

Jul 24th, 2019
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct RESULT_T {
  5.     long sqsize, sqx, sqy, sqline;
  6.     } result_t;
  7.  
  8. inline long pw(long n, int p) {
  9.   long nn = 1;
  10.   if (p == 0) return 1;
  11.   int c = 0;
  12.   while (c < p) {
  13.     nn *= n;
  14.     c++;
  15.   }
  16.   return nn;
  17. }
  18.  
  19. long filesizecr(char filename[], long *crpos)
  20. {
  21.   char buf[300000];
  22.   unsigned long fs = 0, size = 1;
  23.   *crpos = -1;
  24.   FILE *fp;
  25.   if ((fp = fopen(filename, "rb")) == NULL) return -1;
  26.  
  27.   while (size > 0) {
  28.     size = fread(&buf, 1, sizeof(buf), fp);
  29.     if (*crpos == -1) {
  30.       long c = 0;
  31.       while (c < size) {
  32.         if ((buf[c] == '\n') && (*crpos == -1)) *crpos = (long) fs + c;
  33.         c++;
  34.       }
  35.     }
  36.     fs += size;
  37.   }
  38.   fclose(fp);
  39.   return fs;
  40. }
  41.  
  42. typedef struct SQUARE_FINDER {
  43.   long pos;
  44.   long isSquare;
  45.   long size;
  46. } finder_t;  
  47.  
  48. inline void findLargestSquareHere(long * h, result_t *t, long len, long nlen) {
  49.   long i;
  50.   finder_t f;
  51.   f.pos = 0;
  52.   while (f.pos < len) {
  53.     f.size = h[f.pos];
  54.       while (f.size > t->sqy) {
  55.             f.isSquare = nlen - (f.pos + f.size);
  56.             i = f.pos;
  57.             while ((f.isSquare >= 0) && (i < f.pos + f.size)) {
  58.         f.isSquare = h[i] - f.size;
  59.         i++;
  60.         }
  61.             if ((f.isSquare >= 0) && (f.size > t->sqy)) {
  62.               t->sqx = f.pos;
  63.               t->sqy = f.size;
  64.             }
  65.           f.size--;
  66.       }
  67.     f.pos++;
  68.   }
  69.   t->sqsize = t->sqy * t->sqy;
  70. }
  71.  
  72. int main(int ac, char **av) {
  73.   long i;
  74.   char cobs, cfree, cfill;
  75.   long firstcrpos = 0;
  76.   if (ac <= 1) {
  77.     printf("Usage %s [filename]\n", av[0]);
  78.     exit(1);
  79.   }
  80.   char * fname = av[1];
  81.   long fsize = filesizecr(fname, &firstcrpos);
  82.  
  83.   result_t res;
  84.   res.sqx = 0;
  85.   res.sqy = 0;
  86.   res.sqsize = 0;
  87.   res.sqline = 0;
  88.  
  89.   if (fsize != -1) printf("File size = %lu\n", fsize);
  90.   else {
  91.     printf("Error while opening file!\n");
  92.     exit(0);
  93.   }
  94.   printf("First \\n in file is %ld\n", firstcrpos);
  95.  
  96.   char *fbuf;
  97.   FILE *fp;
  98.   if ((fp = fopen(fname, "rb")) == NULL) {
  99.     printf("Error while opening file!\n");
  100.     exit(0);
  101.   }
  102.  
  103.   printf("Mallocing %ld bytes\n", firstcrpos * sizeof(char) + 1);
  104.   fbuf = (char*) malloc(firstcrpos * sizeof(char) + 1);
  105.   long bytesread = fread(fbuf, 1, firstcrpos * sizeof(char), fp);
  106.   printf("Bytes read: %ld\n", bytesread);
  107.  
  108.   if ((bytesread != firstcrpos * sizeof(char)) || (bytesread < 4)) {
  109.     printf("Error while reading file!\n");
  110.     exit(0);
  111.   }
  112.  
  113.   fbuf[bytesread] = '\0';
  114.   printf("Header read: %s\n", fbuf);
  115.   cfill = fbuf[bytesread - 1];
  116.   cobs = fbuf[bytesread - 2];
  117.   cfree = fbuf[bytesread - 3];
  118.   fbuf[bytesread - 3] = '\0';
  119.   i = bytesread - 4;
  120.   long cpow = 0;
  121.   long clines = 0;
  122.   while (i >= 0) { // atoi
  123.     if (fbuf[i] > '9' || fbuf[i] < '0') {
  124.       printf("Wrong symbols in lines count");
  125.       exit(0);
  126.     }
  127.     clines +=  (fbuf[i] - '0') * pw(10, cpow);
  128.     cpow++;
  129.     i--;
  130.   }
  131.   printf("Fill [%c], free [%c], obstacle[%c], Lines: %ld\n", cfill, cfree, cobs, clines);
  132.  
  133.   long linelen = ((fsize - bytesread) / clines) - 1;
  134.   free(fbuf);
  135.  
  136.   char temp;
  137.   bytesread = fread(&temp, 1, sizeof(char), fp);
  138.   if (temp != '\n') {
  139.     printf("Error while reading file!\n");
  140.     exit(0);
  141.   }
  142.  
  143.   i = 0;
  144.   long * hysto;
  145.   long * lasthysto;
  146.   lasthysto = (long *) malloc(linelen * sizeof(long));
  147.   long j = 0;
  148.   while (j < linelen) {
  149.     lasthysto[j] = 0;
  150.     j++;
  151.   }
  152.   j = 0;
  153.   while (i < clines) {
  154.     fbuf = (char *) malloc(linelen + 1);
  155.     bytesread = fread(fbuf, 1, linelen + 1, fp);
  156.     if ((fbuf[linelen] != '\n') || (bytesread != linelen + 1)) {
  157.       printf("Error while reading file (%x, %x, %ld)!\n", fbuf[linelen], '\n', bytesread);
  158.       exit(0);
  159.     }
  160.     fbuf[linelen] = '\0';
  161.     hysto = (long *) malloc(linelen * sizeof(long));
  162.     j = 0;
  163.     while (j < linelen) {
  164.       if ((fbuf[j] != cobs) && (fbuf[j] != cfree)) {
  165.         printf("Error (%ld, %x)\n", j, fbuf[j]);
  166.         exit(0);
  167.       }
  168.       if (fbuf[j] == cobs) hysto[j] = 0;
  169.       if (fbuf[j] == cfree) hysto[j] = lasthysto[j] + 1;
  170.       lasthysto[j] = hysto[j];
  171.       j++;
  172.     }
  173.     free(fbuf);
  174.     result_t tres;
  175.     tres.sqsize = 0;
  176.     tres.sqx = 0;
  177.     tres.sqy = res.sqy;
  178.     findLargestSquareHere(hysto, &tres, linelen - tres.sqy, linelen);
  179.     free(hysto);
  180.  
  181.     if (res.sqsize < tres.sqsize) {
  182.       res.sqsize = tres.sqsize;
  183.       res.sqx = tres.sqx;
  184.       res.sqy = tres.sqy;
  185.       res.sqline = i;
  186.     }
  187.     i++;
  188.   }
  189.   free(lasthysto);
  190.   fclose(fp);
  191.  
  192.   if ((fp = fopen(fname, "rb")) == NULL) {
  193.     printf("Error while opening file!\n");
  194.     exit(0);
  195.   }
  196.  
  197.   fbuf = (char*) malloc(firstcrpos * sizeof(char) + 1);
  198.   bytesread = fread(fbuf, 1, firstcrpos * sizeof(char) + 1, fp);
  199.   i = 0;
  200.   while (i < clines) {
  201.     fbuf = (char *) malloc(linelen + 1);
  202.     bytesread = fread(fbuf, 1, linelen + 1, fp);
  203.     if ((fbuf[linelen] != '\n') || (bytesread != linelen + 1)) {
  204.       printf("Error while reading file (%x, %x, %ld)!\n", fbuf[linelen], '\n', bytesread);
  205.       exit(0);
  206.     }
  207.     fbuf[linelen] = '\0';
  208.     j = 0;
  209.     char * linebuf = (char *) malloc (linelen * sizeof(char));
  210.     while (j < linelen) {
  211.       if ((i > res.sqline - res.sqy) && (i <= res.sqline) && (j >= res.sqx) && (j < res.sqx + res.sqy))
  212.         printf("%c", cfill);
  213.       else
  214.         printf("%c", fbuf[j]);
  215.       j++;
  216.     }
  217.     free(fbuf);
  218.     printf("\n");
  219.     i++;
  220.   }
  221.   fclose(fp);
  222. }
Advertisement
Add Comment
Please, Sign In to add comment