Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. struct Word {
  6.     char tab[30];
  7.     int count;
  8. };
  9.  
  10. struct Node {
  11.     struct Word * data;
  12.     struct Node * next;
  13. };
  14.  
  15. struct List {
  16.     struct Node * head;
  17. };
  18.  
  19. void addToList(struct List * list, char* p) {
  20.     struct Node * newNode = malloc(sizeof(struct Node));
  21.     newNode->next = list->head;
  22.     list->head = newNode;
  23.     newNode->data = malloc(sizeof(struct Word));
  24.     strcpy(newNode->data->tab, p);
  25.     newNode->data->count = 1;
  26. }
  27.  
  28. void printList(struct List *list) {
  29.     struct Node * i = list->head;  
  30.  
  31.     while(i != NULL) {
  32.         printf("%s, %d\n", i->data->tab, i->data->count);
  33.         i = i->next;  
  34.     }
  35. }
  36.  
  37. int countUniqueWordOccurences(struct List * list) {
  38.     struct Node * n;
  39.     int counter = 0;
  40.     n = list->head;
  41.     while(n != NULL) {
  42.         n = n->next;
  43.         counter += 1;
  44.     }
  45.     return counter;
  46. }
  47.  
  48. int countAllWordOccurences(struct List * list) {
  49.     struct Node * n;
  50.     int counter = 0;
  51.     n = list->head;
  52.     while(n != NULL) {
  53.         counter += n->data->count;
  54.         n = n->next;
  55.     }
  56.     return counter;
  57. }
  58.  
  59. char* getLongestWord(struct List* list) {
  60.     struct Node *current = list->head;
  61.     struct Node *longest = current;
  62.     size_t maxLen = strlen(longest->data->tab);
  63.  
  64.     while (current != NULL) {
  65.         size_t currentLen = strlen(current->data->tab);
  66.         if(currentLen > maxLen) {
  67.             longest = current;
  68.             maxLen = currentLen;
  69.         }
  70.         current = current->next;
  71.     }
  72.     return longest->data->tab;
  73. }
  74.  
  75. char* readNextWordFromFile(FILE * f, struct List * list) {
  76.     char * p = malloc(sizeof(char)*30);
  77.     fscanf(f, "%s", p);
  78.     return p;
  79. }
  80.  
  81. void buildListFromFile(FILE* file, struct List *list) {
  82.     while(feof(file) == 0) {
  83.         struct Node * currentNode;
  84.         char * currentWord;
  85.         int listContainsCurrentWord = 0;
  86.  
  87.         listContainsCurrentWord = 0;
  88.         currentWord = readNextWordFromFile(file, list);
  89.         currentNode = list->head;
  90.        
  91.         while (currentNode != NULL) {
  92.             if (strcmp(currentWord, currentNode->data->tab) == 0) {
  93.                 currentNode->data->count += 1;
  94.                 listContainsCurrentWord = 1;
  95.                 free(currentWord);          
  96.                 break;
  97.             }
  98.             currentNode = currentNode->next;
  99.         }
  100.         if (!listContainsCurrentWord) {
  101.             addToList(list, currentWord);
  102.         }
  103.     }
  104. }
  105.  
  106. int main() {
  107.     struct List list = { NULL };
  108.     char * longestWord;
  109.     int uniqueWordsCount, allWordsCount;
  110.  
  111.     FILE * f = fopen("tekst.txt", "r");
  112.     if(f == NULL) {
  113.         printf("Blad przy otwieraniu pliku\n");
  114.         return 1;
  115.     }  
  116.     buildListFromFile(f, &list);
  117.     uniqueWordsCount = countUniqueWordOccurences(&list);
  118.     allWordsCount = countAllWordOccurences(&list);
  119.     printf("Liczba niepowtarzajacych sie slow: %d\nLiczba wszystkich slow: %d\n", uniqueWordsCount, allWordsCount);
  120.    
  121.     printList(&list);
  122.     longestWord = getLongestWord(&list);
  123.     printf("Najdluzsze slowo: %s\n", longestWord);
  124.     fclose(f);
  125.    
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement