sultan

Work

Jan 23rd, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5.  
  6. const int MAX_SIZE = 256;
  7.  
  8. char* separ;
  9. char** word;
  10. int count_str, count_separator, count_same_words;
  11.  
  12. char** GetText()
  13. {
  14.     char tmp[MAX_SIZE];
  15.     char** temp;
  16.     char** buf;
  17.     int i;
  18.     int end_input;
  19.  
  20.     end_input=0;
  21.     count_str = 0;
  22.     temp=NULL;buf=NULL;
  23.     printf("Text:\n");
  24.     fflush(stdin);
  25.  
  26.     for (i=0; !end_input; i++)
  27.     {
  28.                 gets(tmp);
  29.                 free(temp);
  30.                 temp=(char**)malloc((count_str+1)*sizeof(char*));
  31.                 int j;
  32.                 for(j=0;j<count_str;j++)
  33.                 {
  34.                         temp[j]=(char*)malloc((strlen(buf[j])+1)*sizeof(char));
  35.                         strcpy(temp[j],buf[j]);
  36.                         free(buf[j]);
  37.                 }
  38.                 temp[i]=(char*)malloc((strlen(tmp)+1)*sizeof(char));
  39.                 strcpy(temp[i],tmp);
  40.                 count_str++;
  41.                 free(buf);
  42.                 buf=(char**)malloc(count_str*sizeof(char*));
  43.                 for(j=0;j<count_str;j++)
  44.                 {
  45.                         buf[j]=(char*)malloc((strlen(temp[j])+1)*sizeof(char));
  46.                         strcpy(buf[j],temp[j]);
  47.                 }
  48.                 for(j=0;j<count_str-1;j++)
  49.                 {
  50.                         if(strcmp(buf[j],buf[i])==0)
  51.                         {
  52.                                 int k;
  53.                                 for(k=0;k<count_str;k++) free(temp[k]); free(temp);
  54.                                 return buf;
  55.                         }
  56.                 }
  57.  
  58.     }
  59. }
  60.  
  61. void GetSeparator()
  62. {
  63.         puts("Size of separators:");
  64.         scanf("%d%*c",&count_separator);
  65.         fflush(stdin);
  66.         separ = (char*)malloc((count_separator+2)*sizeof(char));
  67.         gets(separ);
  68.         separ[count_separator]='\n';separ[count_separator+1]='\0';
  69. }
  70.  
  71. void OutSep()
  72. {
  73.         printf("Separators:\n");
  74.         printf("%s", separ);
  75.         fflush(stdin);
  76. }
  77.  
  78. void OutText(char** _in)
  79. {
  80.         printf("Text:\n");
  81.         int i;
  82.         for (i = 0; i < count_str; i++)
  83.                 printf("%s\n", _in[i]);
  84.         fflush(stdin);
  85.         getchar();
  86. }
  87.  
  88. int CountSeparators(char* str)
  89. {
  90.     int result,i;
  91.     result=1;
  92.  
  93.     for (i=0; i<strlen(str); i++)
  94.      if ( strchr(separ, str[i]) != NULL)
  95.       result++;
  96.  
  97.     return result;
  98. }
  99.  
  100. char* CheckStr(char* stroka, int *how)
  101. {
  102.     int count_word;
  103.     int result;
  104.     result = 0;
  105.     count_word=0;
  106.     count_word = CountSeparators(stroka);
  107.  
  108.     word = (char**)malloc( (count_word+1) * sizeof(char*));
  109.     char *tmp;
  110.     tmp = strtok(stroka, separ);
  111.     int i,j;
  112.     i=0;
  113.  
  114.     while ( tmp != NULL)
  115.     {
  116.         word[i] = (char*)malloc( (strlen(&tmp[0])+1) * sizeof(char));
  117.         word[i][0]='\0';
  118.         strcat(word[i], tmp);
  119.         word[i][strlen(tmp)+1] = '\0';
  120.         word[i][strlen(tmp)]=' ';
  121.         //printf("%s\n", word[i]);
  122.         i++;
  123.         tmp = strtok(NULL, separ);
  124.     }
  125.  
  126.     int count_same;
  127.     count_same=0;
  128.  
  129.     for (i=0;i<count_word; i++)
  130.     {
  131.         int t;
  132.         t=1;
  133.         for (j=i+1; j<count_word; j++)
  134.         {
  135.             if ( strcmp(word[i], word[j]) == 0)
  136.              t++;
  137.         }
  138.         if ( t> count_same)
  139.          count_same=t;
  140.     }
  141.  
  142.     if ( count_same > count_same_words )
  143.     {
  144.         int max_len;
  145.         max_len=0;
  146.  
  147.         for (i=0; i<count_word; i++)
  148.          if ( strlen(&word[i][0]) > max_len)
  149.           max_len= strlen(&word[i][0]);
  150.  
  151.         for (i=0; i<count_word; i++)
  152.          if ( strlen(&word[i][0]) == max_len)
  153.          {
  154.              word[i][0]='\0';
  155.          }
  156.     }
  157.  
  158.     free(stroka);
  159.     stroka = (char*)malloc( sizeof(word));
  160.     stroka[0]='\0';
  161.  
  162.     for (i=0; i<count_word; i++)
  163.     {
  164.      if ( strlen(word[i]) != 0)
  165.      {
  166.          strcat(stroka, word[i]);
  167.          result=1;
  168.      }
  169.      free(word[i]);
  170.     }
  171.     free(word);
  172.  
  173.     *how=result;
  174.     return stroka;
  175. }
  176.  
  177. int main()
  178. {
  179.         char** text;
  180.         setlocale(LC_ALL, "rus");
  181.         char inpChar='1';
  182.         text=NULL;
  183.         while(inpChar!='0')
  184.         {
  185.                 printf("1) Ввод текста\n");
  186.                 printf("2) Ввод разделителей\n");
  187.                 printf("3) Ввод количества повторяющихся слов\n");
  188.                 printf("4) Обработка текста\n");
  189.                 printf("5) Вывести текст\n");
  190.                 printf("6) Вывод разделителей\n");
  191.                 printf("0) Выход\n\n");
  192.                 printf("Выбор:\t");
  193.                 inpChar = getchar();
  194.                 while ( inpChar == '\n') inpChar = getchar();
  195.                 fflush(stdin);
  196.                 switch(inpChar)
  197.                 {
  198.                         case '1':
  199.                                 {
  200.                                         text=GetText();
  201.                                         break;
  202.                                 }
  203.                         case '2':
  204.                                 {
  205.                                         GetSeparator();
  206.                                         break;
  207.                                 }
  208.                         case '3':
  209.                                 {
  210.                                         fflush(stdin);
  211.                                         puts("Количество повторяющихся слов:");
  212.                                         scanf("%d%*c",&count_same_words);
  213.                                         fflush(stdin);
  214.                                         break;
  215.                                 }
  216.                         case '4':
  217.                                 {
  218.                                         int i;
  219.                                         for (i=0; i<count_str; i++)
  220.                                         {
  221.                                             int flag;
  222.                                             flag=0;
  223.                                             text[i] = CheckStr(text[i], &flag);
  224.                                             if ( !flag )
  225.                                             {
  226.                                                 int j;
  227.                                                 for (j=i+1; j<count_str; j++)
  228.                                                 {
  229.                                                     free(text[j]);
  230.                                                     text[j-1] = (char*)malloc( (strlen(text[j])+1) * sizeof(char) );
  231.                                                     text[j-1] = text[j];
  232.                                                 }
  233.  
  234.                                                 count_str--;
  235.                                                 i--;
  236.                                             }
  237.                                         }
  238.                                         break;
  239.                                 }
  240.                         case '5': OutText(text); break;
  241.                         case '6': OutSep(); break;
  242.                         case '0': break;
  243.                         default:
  244.                                 {
  245.                                         printf("Неверно выбранный пункт! Повторите снова\n");
  246.                                         break;
  247.                                 }
  248.                 }
  249.         }
  250.         if(text!=NULL)
  251.         {
  252.                 int i;
  253.                 for (i = 0; i < count_str; i++)
  254.                         free(text[i]);
  255.                 free(text);
  256.         }
  257.         return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment