sultan

Probnik_C

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