3axap_010

file_&_tree

Jun 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. // file_and_tree.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. // Из текстового файла добавлять и упорядочивать слова в дерево
  3.  
  4. #include "pch.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <string.h>
  9.  
  10. struct tree
  11. {
  12.     char* str;
  13.     struct tree* parent;
  14.     struct tree* right;
  15.     struct tree* left;
  16.     int repeats;
  17. };
  18.  
  19. FILE* file_create(const char* filename, const char* mode)
  20. {
  21.     FILE* file = fopen(filename, mode);
  22.     if (!file)
  23.     {
  24.         fprintf(stderr, "Can't open file\n");
  25.         return NULL;
  26.     }
  27.  
  28.     return file;
  29. }
  30.  
  31. void file_fill(FILE* file)
  32. {
  33.     char c;
  34.  
  35.     for (;;)
  36.     {
  37.         c = _getch();
  38.         fputc(c, stdout);
  39.  
  40.         if (c == 'x')
  41.         {
  42.             break;
  43.         }
  44.  
  45.         fprintf(file, "%c", c);
  46.     }
  47. }
  48.  
  49. void file_output(FILE* file)
  50. {
  51.     rewind(file);
  52.  
  53.     printf("\n");
  54.  
  55.     char c;
  56.  
  57.     while (!feof(file))
  58.     {
  59.         c = fgetc(file);
  60.  
  61.         if (feof(file)) break;
  62.  
  63.         fputc(c, stdout);
  64.     }
  65.  
  66.     printf("\n");
  67. }
  68.  
  69. struct tree* add_element_recursive(struct tree* node, char* str)
  70. {
  71.     if (!node)
  72.     {
  73.         node = (struct tree*)malloc(sizeof(struct tree));
  74.         node->left = node->right = NULL;
  75.         node->repeats = 1;
  76.         node->str = (char*)malloc(sizeof(char) * (strlen(str) + 1));
  77.         strcpy(node->str, str);
  78.     }
  79.     else
  80.     {
  81.         if (strcmp(str, node->str) == 0)
  82.         {
  83.             node->repeats++;
  84.         }
  85.         else
  86.         {
  87.             if (strcmp(str, node->str) < 0)
  88.             {
  89.                 node->left = add_element_recursive(node->left, str);
  90.             }
  91.             else
  92.             {
  93.                 node->right = add_element_recursive(node->right, str);
  94.             }
  95.         }
  96.     }
  97.  
  98.     return node;
  99. }
  100.  
  101. void find_words(FILE* file, struct tree** node)
  102. {
  103.     char* str = NULL;
  104.     char c;
  105.     int lenght = 0;
  106.  
  107.     rewind(file);
  108.  
  109.     while (!feof(file))
  110.     {
  111.         c = fgetc(file);
  112.  
  113.         if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || feof(file))
  114.         {
  115.             if (lenght > 0)
  116.             {
  117.                 str = (char*)realloc(str, sizeof(char) * (++lenght));
  118.                 str[lenght - 1] = '\0';
  119.                 *node = add_element_recursive(*node, str);
  120.             }
  121.  
  122.             free(str);
  123.  
  124.             if (feof(file))
  125.             {
  126.                 return;
  127.             }
  128.             else
  129.             {
  130.                 lenght = 0;
  131.                 str = NULL;
  132.             }
  133.         }
  134.         else
  135.         {
  136.             str = (char*)realloc(str, sizeof(char) * (++lenght));
  137.             str[lenght - 1] = c;
  138.         }
  139.     }
  140. }
  141.  
  142. void tree_output(struct tree* node)
  143. {
  144.     if (node)
  145.     {
  146.         if (node->left)
  147.         {
  148.             tree_output(node->left);
  149.         }
  150.  
  151.         puts(node->str);
  152.  
  153.         if (node->right)
  154.         {
  155.             tree_output(node->right);
  156.         }
  157.     }
  158. }
  159.  
  160.  
  161. int main()
  162. {
  163.     FILE* file = file_create("text.txt", "w+");
  164.  
  165.     file_fill(file);
  166.  
  167.     file_output(file);
  168.  
  169.     struct tree* node = NULL;
  170.  
  171.     find_words(file, &node);
  172.  
  173.     tree_output(node);
  174.  
  175.     fclose(file);
  176.  
  177.     return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment