Advertisement
Noam_15

תוכנית שקולטת טקסט ומילים ובודקת כמה פעמים כל מילה מופיעה

Aug 24th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.30 KB | None | 0 0
  1. /*
  2. long_text א. התוכנית מקבלת מהמשתמש טקסט ארוך ושומרת אותו במערך
  3. [עם הזיכרון המינימלי שנחוץ לשם כך]
  4.  
  5. מהמשתמש n ב. התוכנית מקבלת מספר
  6.  
  7. מילים n ג. התוכנית קולטת מהמשתמש
  8. [עם הזיכרון המינימלי שנחוץ לשם כך]
  9. כל מילה חייבת להכיל לפחות 2 תווים
  10. כל מילה תהיה מילה אחת, לא יותר ובלי רווחים
  11. אסור לאותה מילה להופיע פעמיים
  12.  
  13.  
  14.  
  15. long_text ד. התוכנית מדפיסה כמה פעמים מופיעה כל מילה במערך
  16.  
  17.  
  18.  
  19. לשם כך עשה שימוש בהקצאת זיכרון הדינאמי                        
  20. */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. int len_of_long_text;
  27.  
  28. char* From_Big_String_To_dynamic_String(){
  29.     char get_a_big_string[2000], *Pstring;
  30.  
  31.     flushall();
  32.     do{
  33.         gets(get_a_big_string);
  34.         len_of_long_text = strlen(get_a_big_string);
  35.         Pstring = (char*) malloc ((len_of_long_text+1) * sizeof(char)); // המסיים NULL מוסיפים 1 עבור ה
  36.         if (Pstring == NULL) { printf("\nNo enough memory!.\nEnter again:\n"); continue; } // הבדיקה הזו והלולאה הם רק למקרי קיצון
  37.         if(len_of_long_text == 0) { printf("Please type at least one character.\nEnter again:\n"); continue;}
  38.         break;
  39.     }while(1);
  40.  
  41.     strcpy(Pstring, get_a_big_string);
  42.     return Pstring;
  43. }
  44.  
  45. int How_many_times_the_word_appear(const char *word, const char *long_tex){
  46.     int sum = 0, i, j, word_len;
  47.     word_len = strlen(word);
  48.     if(word_len>len_of_long_text) return 0;
  49.     for(i=0; i<len_of_long_text; i++){
  50.         for(j=0; j<word_len && j+i<len_of_long_text && word[j] == long_tex[i+j]; j++);
  51.         if(j==word_len) sum++;
  52.     }
  53.     return sum;
  54. }
  55.  
  56. int This_word_already_exists(char tmpStr[50], const char **Array_of_words){ // C זו אמורה להיות פונקציה בוליאנית, אבל אנחנו משתמשים בשפת
  57.     int i;
  58.     for(i=0; Array_of_words[i]; i++){
  59.         if(How_many_times_the_word_appear(tmpStr, Array_of_words[i])==1)
  60.             return 1;
  61.     }
  62.     return 0;
  63. }
  64.  
  65. void From_Big_word_To_dynamic_word(int num, const char **Array_of_words){
  66.     char tmpStr[50], *Pstring;
  67.     int len, i;
  68.  
  69.     flushall();
  70.     do{
  71.         printf("%d. Enter word: ", num+1);
  72.         gets(tmpStr);
  73.         len = strlen(tmpStr);
  74.         Pstring = (char*) malloc ((len+1) * sizeof(char));
  75.         if (Pstring == NULL) { printf("\nNo enough memory!.\nEnter again:\n"); continue; } // הבדיקה הזו והלולאה הם רק למקרי קיצון
  76.         if(len <= 1) { printf("Please type at least two character.\nEnter again:\n"); continue;}
  77.  
  78.         for(i=0; i<len; i++){
  79.             if(tmpStr[i] == ' ' || tmpStr[i] == '\t' || tmpStr[i] == '\n'){
  80.                 printf("Please type only one word.\nEnter again:\n");
  81.                 break; // for-זה מוציא אותנו רק מחוץ ללולאת ה
  82.             }
  83.         }
  84.         if(i!=len) continue; // כאן נגמרת בדיקת התווים
  85.  
  86.        
  87.         if(This_word_already_exists(tmpStr, Array_of_words)==1){ // בודק שמילה לא מופיעה כבר
  88.             printf("This word already exists.\nEnter again:\n");
  89.             continue;
  90.         }
  91.  
  92.  
  93.  
  94.         break;
  95.     }while(1);
  96.  
  97.     strcpy(Pstring, tmpStr);
  98.     Array_of_words[num] = Pstring;
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. void main(){
  108.     char *P_long_text, **Array_of_words;
  109.     int n, i,  *The_sum_of_occurrences_of_each_word;
  110.  
  111.     printf("Enter the long text:\n");
  112.     P_long_text = From_Big_String_To_dynamic_String();
  113.  
  114.     do{
  115.         printf("Enter the number of words you want to search:  ");
  116.         scanf("%d", &n);
  117.     }while(n<=0);
  118.     The_sum_of_occurrences_of_each_word = (int*) malloc (n * sizeof(int));
  119.     Array_of_words = (char**) calloc (n , sizeof(char*)); /* "This_word_already_exists()" בכוונה כאן יש קאלוק ולא מאלוק, עבור הפונקציה */
  120.     for(i=0; i<n; i++){
  121.         From_Big_word_To_dynamic_word(i, Array_of_words);
  122.         The_sum_of_occurrences_of_each_word[i] = How_many_times_the_word_appear(Array_of_words[i], P_long_text);
  123.     }
  124.  
  125.     printf("\n\n\n---Results:\n");
  126.     for(i=0; i<n; i++){
  127.         printf("Word number %d (\"%s\") appears in the text:\n%d times\n\n", i+1, Array_of_words[i], The_sum_of_occurrences_of_each_word[i]);
  128.     }
  129.  
  130.    
  131.  
  132.     {
  133.         free(P_long_text);
  134.         for(i=0; i<n; i++){
  135.             free(Array_of_words[i]);
  136.         }
  137.         free(Array_of_words);
  138.         free(The_sum_of_occurrences_of_each_word);
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement