Advertisement
Guest User

Untitled

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