Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- int main (int argc, char **argv) {
- FILE * stream;
- int opt=0;
- opt = getopt(argc, argv, "i");
- if (opt == 'i') {
- if (argc > 4) {printf ("slishkom mnogo argumentov"); return EXIT_SUCCESS;}
- if (argc > 2) stream = fopen(argv[2],"r");
- else stream = stdin;
- } else {
- if (argc > 3) {printf ("slishkom mnogo argumentov"); return EXIT_SUCCESS;}
- if (argc > 1) stream = fopen(argv[1],"r");
- else stream = stdin;
- }
- if (stream==NULL) {perror("fopen() "); return EXIT_SUCCESS;}
- char *ptr = 0;
- char *PTR = 0;
- int i = 0, m = 0, nl = 0, nc = 0, g = 0, k;
- int *a, *u=0;
- char **A, **B = 0;
- while ((k = getc(stream)) != EOF)
- {
- if (k == '\n') {
- nl++;
- }
- ptr=(char*)realloc(ptr,sizeof(char)*(i+1));
- ptr[i]=k;
- PTR=(char*)realloc(PTR,sizeof(char)*(i+1));
- if ('a' <= k && k <= 'z')
- k += 'A' - 'a';
- PTR[i]=k;
- i++;
- }
- if (i == 0) {printf ("pozhaluista vvedite text"); return EXIT_SUCCESS;}
- a = (int*)malloc(nl * sizeof(int));
- u = (int*)malloc(nl * sizeof(int));
- m=i;
- if (ptr[m-1] != '\n') {
- nl++;
- a = (int*)realloc(a, sizeof(int)*(nl));
- u = (int*)realloc(u, sizeof(int)*(nl));
- }
- A = (char **)malloc(nl*sizeof(char *));
- B = (char **)malloc(nl*sizeof(char *));
- for (i=0; i<nl; i++) {A[i] = (char*)malloc(1 * sizeof(char)); B[i] = (char*)malloc(1 * sizeof(char));}
- int f = 0;
- int l=0;
- int chetchik = 0;
- for (i=0; i<m;i++) {
- ++nc;
- f = nc;
- if (ptr[i] == '\n') {
- printf ("%d, ", nc);
- A[l] = (char *)realloc(A[l], sizeof(char)*(nc));
- B[l] = (char *)realloc(B[l], sizeof(char)*(nc));
- a[l]= nc -1;
- l++;
- if (nc > chetchik ) chetchik = nc-2;
- nc = 0;
- }
- }
- printf ("nl = %d, l = %d\n", nl, l);
- if (nl != l) {A[l] = (char *)malloc((f+1)*sizeof(char));
- B[l] = (char *)malloc((f+1)*sizeof(char));
- a[l]= f; }
- int j=0;
- int b=0;
- for (i=0; i<m;i++) {
- B[b][j]= PTR[i];
- j++;
- if (PTR[i] == '\n') {
- j = 0;
- b++;
- }
- }
- j=0;
- b=0;
- for (i=0; i<m;i++) {
- A[b][j] = ptr[i];
- printf ("%c", ptr[i]);
- j++;
- printf ("{%s}", A[b]);
- if (ptr[i] == '\n') {
- printf ("[%s]", A[b]);
- j = 0;
- b++;
- }
- }
- if (nl != l) { strcat(A[b], "\n"); strcpy(B[b], "\n"); printf ("nl != l");}
- for (i=0; i<nl; i++) printf ("%d\n", a[i]);
- for (i=0; i<nl; i++)
- u[i]=nl+1;
- if (opt == 'i') {
- for (i=0; i<nl-1; i++) {
- for (g = i + 1; g < nl; g++) {
- if (a[i] == a[g]) {
- if (strcmp(B[i], B[g]) == 0) {
- if (((u[i] & u[g]) == nl + 1) || ((u[i] == i + 1) & (u[g] == nl + 1))) {
- u[i] = i + 1;
- u[g] = i + 1;
- }
- }
- }
- }
- }
- } else {
- for (i=0; i<nl-1; i++) {
- for (g = i + 1; g < nl; g++) {
- if (a[i] == a[g]) {
- if (strcmp(A[i], A[g]) == 0) {
- if (((u[i] & u[g]) == nl + 1) || ((u[i] == i + 1) & (u[g] == nl + 1))) {
- u[i] = i + 1;
- u[g] = i + 1;
- }
- }
- }
- }
- }
- printf("%s", A[0]);
- }
- fclose(stream);
- FILE *mf;
- if (opt == 'i'){
- if (argc > 3) mf=fopen (argv[3],"w+");
- else mf=stdout;
- if (argc > 4) {
- puts("Ошибка (many arguments)");
- return 1;
- }
- } else {
- if (argc > 2) mf=fopen (argv[2],"w+");
- else mf=stdout;
- if (argc > 3) {
- puts("Ошибка (many arguments)");
- return 1;
- }
- }
- g=0;
- int pm = 0;
- fprintf (mf,"\nПовторяющиеся строки: ");
- fprintf (mf,"\n");
- int q=1;
- for (i=0; i<nl; i++) {
- if ((u[i]<nl) & (u[i]>pm)) {
- for (j=i+1; j<nl; j++) {
- if (u[j]==u[i]) q=q+1;
- }
- fprintf(mf,"%d раз(а): ",q);
- if (strcmp(A[i], "\n") == 0) fprintf(mf, "pustaya stroka"); else
- fprintf(mf,"%s", A[i]);
- g++;
- q=1;
- }
- pm = pm+1;
- }
- if (g == 0) fprintf(mf, "povtoryaushihsya strok net ");
- // Закрытие файла
- fclose (mf);
- for (i = 0; i < nl; i++) free(A[i]);
- for (i = 0; i < nl; i++) free(B[i]);
- free(B);
- free(ptr);
- free(PTR);
- free(A);
- free(a);
- free(u);
- fclose (mf);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement