Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <locale.h>
  6.  
  7. char *readSentence();
  8. char **readText(char **Text, int *Count_sentence);
  9. char **DeleteNumbers(char **Text, int *Count_sentence);
  10. void OutputisPalindrome(char **Text, int *Count_sentence);
  11. char **DeleteSentence(char **Text, int *Count_sentence);
  12. char **SortbyLengthoftheThirdWord(char **Text, int *Count_sentence);
  13.  
  14. int main(){    
  15.     setlocale(LC_ALL,"");  
  16.     char** Text = (char**)malloc(sizeof(char*));
  17.     int Count_sentence = 0;
  18.     Text = readText(Text, &Count_sentence);
  19.     int choise;
  20.     do{
  21.         wprintf(L"Выбор функции:\nЧтобы удалить все встречающиеся в предложениях цифры, нажмите 1\nЧтобы найти предложения-палиндромы, нажмите 2\nЧтобы удалить все предложения, в которых совпадает первый и последний символ(без учета регистра), нажмите 3\nЧтобы отсортировать предложения по увеличению длины третьего слова, нажмите 4\nЧтобы выйти из программы, нажмите 0\n");
  22.         scanf("%d",&choise);
  23.         switch(choise){
  24.             case 1:
  25.                 Text = DeleteNumbers(Text, &Count_sentence);
  26.                 break;
  27.             case 2:
  28.                 OutputisPalindrome(Text, &Count_sentence);
  29.                 break;
  30.             case 3:
  31.                 Text = DeleteSentence(Text, &Count_sentence);
  32.                 break;
  33.             case 4:
  34.                 Text = SortbyLengthoftheThirdWord(Text, &Count_sentence);
  35.                 break;
  36.             case 0:
  37.                 printf("Goodbye");
  38.                 break;
  39.             default:
  40.                 wprintf(L"Данные некорректны");
  41.         }
  42.     }while(choise != 0);  
  43.     for (int i = 0; i < Count_sentence; i++){
  44.         //printf("%s\n",Text[i]);
  45.         free(Text[i]);
  46.     }
  47.     free(Text);
  48.     return 0;
  49. }
  50.  
  51. char **readText(char **Text,int *Count_sentence){
  52.     int size = 10; //память в text
  53.     char *sentence;
  54.     do{
  55.         sentence = readSentence();
  56.         if (*Count_sentence == size -1){
  57.             size += 10;
  58.             Text = realloc(Text, size * sizeof(char*));
  59.         }
  60.         Text[*Count_sentence] = sentence;
  61.         (*Count_sentence)++;
  62.     }while(strcmp(sentence,"End!"));
  63.     return Text;
  64. }
  65.  
  66. char *readSentence(){
  67.     char *sentence = malloc(10*sizeof(char));
  68.     char current_symbol;    
  69.     int Count_symbol = 0; //символ в предложении
  70.     int size_sentence = 10; //память в предложении        
  71.     do{
  72.         current_symbol = getchar();
  73.         if ((current_symbol == ' ' || current_symbol == '\t') && !Count_symbol){
  74.             current_symbol = getchar();
  75.         }
  76.         if (Count_symbol == size_sentence - 2 ){
  77.             size_sentence += 10;
  78.             sentence = realloc(sentence,size_sentence*sizeof(char));
  79.         }
  80.         sentence[Count_symbol] = current_symbol;
  81.         Count_symbol++;          
  82.     }while ((current_symbol != '\n')&&(current_symbol != '.')&&(current_symbol !='!'));
  83.     sentence[Count_symbol] = '\0';
  84.     return sentence;
  85. }
  86.  
  87. char **DeleteNumbers(char **Text, int *Count_sentence){
  88.     int i = 0;
  89.     while (i < *Count_sentence){
  90.         for (int j = 0; j < strlen(Text[i]); j++){
  91.             if (isdigit(Text[i][j])){
  92.                 if (Text[i][j+1] == ' ')
  93.                     for (int s = j; s < strlen(Text[i])-1; s++){
  94.                         //printf("%d: %c %c\n",s,Text[i][s],Text[i][s+2]);
  95.                         Text[i][s] = Text[i][s+2];                      
  96.                 }
  97.                 else
  98.                 {
  99.                     for (int s = j; s < strlen(Text[i]); s++){
  100.                         //printf("%d: %c %c\n",s,Text[i][s],Text[i][s+1]);
  101.                         Text[i][s] = Text[i][s+1];
  102.                     }
  103.                 }
  104.                 j--;
  105.             }
  106.         }
  107.         if (strlen(Text[i]) == 1){
  108.             int sen;
  109.             for (sen = i; sen < *Count_sentence - 1; sen++){
  110.                 Text[sen] = Text[sen+1];    
  111.             }
  112.             free(Text[sen+1]);
  113.             (*Count_sentence)--;
  114.         }
  115.         else{
  116.             i++;
  117.         }
  118.        
  119.     }
  120.     for (int i = 0; i < *Count_sentence; i++){
  121.         printf("%s\n",Text[i]);
  122.         //free(Text[i]);
  123.     }
  124.     //free(Text);
  125.     //return Text;
  126. }
  127.  
  128. void OutputisPalindrome(char **Text, int *Count_sentence){
  129.     int i = 0;
  130.     while (i < *Count_sentence){
  131.         int end = strlen(Text[i]) - 2;
  132.         int start = 0;
  133.         while (start != end){
  134.             if (Text[i][start] == ' ')
  135.                 start++;
  136.             if(Text[i][end] == ' '){
  137.                 end--;
  138.                 if (Text[i][end] == ',')
  139.                     end--;
  140.             }
  141.             if (Text[i][start] == ',')
  142.                 start += 2;
  143.             if (Text[i][start] == Text[i][end]){
  144.                 start++;
  145.                 end--;
  146.             }
  147.             else{
  148.                 printf("%s - Nothing interesting\n", Text[i]);
  149.                 break;
  150.             }
  151.             if (start >= end){
  152.                 printf("%s - Palindrome\n", Text[i]);
  153.                 break;
  154.             }
  155.         }
  156.         i++;
  157.     }
  158.     /*for (int i = 0; i < *Count_sentence; i++){
  159.         free(Text[i]);
  160.     }
  161.     free(Text);*/
  162.  
  163. }
  164.  
  165. char **DeleteSentence(char **Text, int *Count_sentence){
  166.     int i = 0;
  167.     int s;
  168.     while (i < *Count_sentence){
  169.         int j = strlen(Text[i]) - 2;
  170.         if (tolower(Text[i][0])==tolower(Text[i][j])){
  171.             for (s = i; s < *Count_sentence - 1; s++){
  172.                 Text[s] = Text[s+1];    
  173.             }
  174.             free(Text[s+1]);
  175.             (*Count_sentence)--;
  176.         }
  177.         else{
  178.             i++;
  179.         }
  180.     }
  181.     for (int i = 0; i < *Count_sentence; i++){
  182.         printf("%s\n",Text[i]);
  183.         //free(Text[i]);
  184.     }
  185.     //free(Text);
  186. }
  187.  
  188. int compare (const void *i, const void *j){
  189.     if(*(int*)i > *(int*)j)
  190.         return 1;
  191.     else if (*(int*)i == *(int*)j)
  192.         return 0;
  193.     else
  194.         return -1;
  195. }
  196.  
  197. char **SortbyLengthoftheThirdWord(char **Text, int *Count_sentence){//как-то работает
  198.     char **sortText = malloc((*Count_sentence)*sizeof(char));
  199.     int i = 0;
  200.     int lenThirdWorld[*Count_sentence];
  201.     int *sortlenThirdWorld = (int*)calloc((*Count_sentence),sizeof(int));
  202.     for (int i = 0; i < *Count_sentence; i++){
  203.         lenThirdWorld[i] = 3;
  204.         sortlenThirdWorld[i] = 3;
  205.     }
  206.     while (i < *Count_sentence){
  207.         int numSpace = 0;
  208.         for (int j = 0; j < strlen(Text[i]); j++){
  209.             if (Text[i][j] == ' '){
  210.                 numSpace += 1;
  211.             }
  212.             if (numSpace == 2){
  213.                 int s = j + 1;
  214.                 lenThirdWorld[i] = 0;
  215.                 sortlenThirdWorld[i] = 0;
  216.                 while (Text[i][s] != ' ' && Text[i][s] != ',' && Text[i][s] != '.'){
  217.                     lenThirdWorld[i] += 1;
  218.                     sortlenThirdWorld[i] += 1;
  219.                     s++;
  220.                 }
  221.                 break;
  222.             }
  223.         }
  224.         i++;
  225.     }
  226.     qsort(sortlenThirdWorld, *Count_sentence, sizeof (int), compare);
  227.     int count = 0;
  228.     for (int k = 0; k < *Count_sentence; k++){
  229.         for (int p = 0; p < *Count_sentence; p++){
  230.             if (Text[p] != NULL){
  231.                 if (lenThirdWorld[p] == sortlenThirdWorld[k]){
  232.                     sortText[count] = Text[p];
  233.                     Text[p] = NULL;
  234.                     count += 1;
  235.                     break;
  236.                 }
  237.             }
  238.         }
  239.     }
  240.     for (int i = 0; i < *Count_sentence; i++)
  241.         free(Text[i]);
  242.     /*free(Text);
  243.     for (int i = 0; i < *Count_sentence; i++){
  244.         printf("%s\n", sortText[i]);
  245.         free(sortText[i]);
  246.     }
  247.     free(sortText);*/
  248.     return sortText;    
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement