Advertisement
Guest User

Blyat

a guest
Jan 27th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6.  
  7.  
  8.  
  9. int main (int argc, char **argv) {
  10.  
  11. FILE * stream;
  12.  int opt=0;
  13. opt = getopt(argc, argv, "i");
  14.  
  15.  
  16. if (opt == 'i')  {
  17.  
  18. if (argc > 4) {printf ("slishkom mnogo argumentov"); return EXIT_SUCCESS;}
  19.  
  20. if (argc > 2) stream = fopen(argv[2],"r");
  21.     else stream = stdin;
  22.  
  23. } else {
  24.  
  25. if (argc > 3) {printf ("slishkom mnogo argumentov"); return EXIT_SUCCESS;}
  26. if (argc > 1) stream = fopen(argv[1],"r");
  27.     else stream = stdin;
  28.  
  29.  
  30. }
  31.  
  32.  
  33. if (stream==NULL) {perror("fopen() "); return EXIT_SUCCESS;}
  34.  
  35.  
  36. char *ptr = 0;
  37. char *PTR = 0;
  38. int  i = 0, m = 0, nl = 0, nc = 0, g = 0, k;
  39. int *a, *u=0;
  40. char **A, **B = 0;
  41.    
  42. while ((k = getc(stream)) != EOF)
  43.  
  44. {
  45.  
  46.  
  47.  
  48. if (k == '\n') {
  49.  
  50. nl++;
  51.  
  52. }
  53.    
  54.    ptr=(char*)realloc(ptr,sizeof(char)*(i+1));
  55.    ptr[i]=k;
  56.    
  57.  
  58. PTR=(char*)realloc(PTR,sizeof(char)*(i+1));
  59.  
  60. if ('a' <= k && k <= 'z')
  61. k += 'A' - 'a';
  62.    PTR[i]=k;
  63.  
  64. i++;
  65.  
  66.  
  67. }
  68.  
  69. if (i == 0) {printf ("pozhaluista vvedite text"); return EXIT_SUCCESS;}
  70.  
  71.     a = (int*)malloc(nl * sizeof(int));
  72.     u = (int*)malloc(nl * sizeof(int));
  73.  
  74.  
  75. m=i;
  76.  
  77.  
  78.  
  79.  
  80. if (ptr[m-1] != '\n') {
  81.  
  82. nl++;
  83.  
  84. a = (int*)realloc(a, sizeof(int)*(nl));
  85. u = (int*)realloc(u, sizeof(int)*(nl));
  86.  
  87. }
  88.  
  89. A = (char **)malloc(nl*sizeof(char *));
  90. B = (char **)malloc(nl*sizeof(char *));
  91. for (i=0; i<nl; i++) {A[i] = (char*)malloc(1 * sizeof(char)); B[i] = (char*)malloc(1 * sizeof(char));}
  92.  
  93.  
  94.  
  95.  int f = 0;
  96.  int l=0;
  97.     int chetchik = 0;
  98.     for (i=0; i<m;i++) {
  99.         ++nc;
  100. f = nc;
  101.         if (ptr[i] == '\n') {
  102.             printf ("%d, ", nc);
  103.          
  104.             A[l] = (char *)realloc(A[l], sizeof(char)*(nc));
  105.            
  106.             B[l] = (char *)realloc(B[l], sizeof(char)*(nc));
  107.  
  108.             a[l]= nc -1;
  109.            
  110.             l++;
  111.             if (nc > chetchik ) chetchik = nc-2;
  112.             nc = 0;
  113.  
  114.         }
  115.     }
  116.  
  117.  printf ("nl = %d, l = %d\n", nl, l);
  118.    
  119.    if (nl != l) {A[l] = (char *)malloc((f+1)*sizeof(char));
  120.             B[l] = (char *)malloc((f+1)*sizeof(char));
  121.             a[l]= f; }
  122.  
  123.  
  124.     int j=0;
  125.     int b=0;
  126.  
  127.    for (i=0; i<m;i++) {
  128.  
  129.         B[b][j]= PTR[i];
  130.  
  131.         j++;
  132.  
  133.         if (PTR[i] == '\n') {
  134.          
  135.             j = 0;
  136.             b++;
  137.         }
  138.  
  139.     }
  140.  
  141.  
  142.  j=0;
  143.  b=0;
  144.  
  145.  
  146.  
  147.  
  148. for (i=0; i<m;i++) {
  149.  
  150.         A[b][j] = ptr[i];
  151.  
  152.         printf ("%c", ptr[i]);
  153.  
  154.         j++;
  155.         printf ("{%s}", A[b]);
  156.  
  157.         if (ptr[i] == '\n') {
  158.          printf ("[%s]", A[b]);
  159.          
  160.             j = 0;
  161.             b++;
  162.         }
  163.  
  164.     }
  165.  
  166.  
  167.  
  168.  
  169. if (nl != l) { strcat(A[b], "\n"); strcpy(B[b], "\n"); printf ("nl != l");}
  170.  
  171.  
  172.  
  173.  
  174.  
  175. for (i=0; i<nl; i++) printf ("%d\n", a[i]);
  176.    
  177.  
  178.     for (i=0; i<nl; i++)
  179.         u[i]=nl+1;
  180.  
  181.  
  182. if (opt == 'i') {
  183.  
  184.  
  185.     for (i=0; i<nl-1; i++) {
  186.     for (g = i + 1; g < nl; g++) {
  187.         if (a[i] == a[g]) {
  188.             if (strcmp(B[i], B[g]) == 0) {
  189.                 if (((u[i] & u[g]) == nl + 1) || ((u[i] == i + 1) & (u[g] == nl + 1))) {
  190.                     u[i] = i + 1;
  191.                     u[g] = i + 1;
  192.                 }
  193.             }
  194.         }
  195.     }
  196.  
  197.  
  198.  
  199.  
  200. }
  201. } else {
  202.  
  203.  
  204.   for (i=0; i<nl-1; i++) {
  205.     for (g = i + 1; g < nl; g++) {
  206.         if (a[i] == a[g]) {
  207.             if (strcmp(A[i], A[g]) == 0) {
  208.                 if (((u[i] & u[g]) == nl + 1) || ((u[i] == i + 1) & (u[g] == nl + 1))) {
  209.                     u[i] = i + 1;
  210.                     u[g] = i + 1;
  211.                 }
  212.             }
  213.         }
  214.     }
  215. }
  216. printf("%s", A[0]);
  217.  
  218. }
  219.  
  220.  fclose(stream);
  221.  FILE *mf;
  222. if (opt == 'i'){
  223. if (argc > 3) mf=fopen (argv[3],"w+");
  224.     else mf=stdout;
  225.     if (argc > 4) {
  226. puts("Ошибка (many arguments)");
  227. return 1;
  228. }
  229. } else {
  230.  
  231. if (argc > 2) mf=fopen (argv[2],"w+");
  232.     else mf=stdout;
  233.     if (argc > 3) {
  234. puts("Ошибка (many arguments)");
  235. return 1;
  236.  
  237.  
  238.  
  239. }
  240.  
  241.  
  242. }
  243.  
  244. g=0;
  245. int pm = 0;
  246.     fprintf (mf,"\nПовторяющиеся строки: ");
  247.     fprintf (mf,"\n");
  248.     int q=1;
  249.     for (i=0; i<nl; i++) {
  250.         if ((u[i]<nl) & (u[i]>pm)) {
  251.             for (j=i+1; j<nl; j++) {
  252.                 if (u[j]==u[i]) q=q+1;
  253.             }
  254.             fprintf(mf,"%d раз(а): ",q);
  255.             if (strcmp(A[i], "\n") == 0) fprintf(mf, "pustaya stroka"); else
  256.             fprintf(mf,"%s", A[i]);
  257.             g++;
  258.             q=1;
  259.         }
  260.         pm = pm+1;
  261.  
  262.     }
  263.  
  264. if (g == 0) fprintf(mf, "povtoryaushihsya strok net ");
  265.  
  266.  
  267.     // Закрытие файла
  268.  
  269.    
  270.  
  271.  
  272.  
  273.  
  274. fclose (mf);
  275.  
  276. for (i = 0; i < nl; i++) free(A[i]);
  277.  
  278. for (i = 0; i < nl; i++) free(B[i]);
  279.  
  280. free(B);
  281. free(ptr);
  282. free(PTR);
  283. free(A);
  284. free(a);
  285. free(u);
  286. fclose (mf);
  287.  
  288. return 0;
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement