Advertisement
Guest User

вывод из дерева по 20 слов

a guest
Jun 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <string.h>
  4. #include <windows.h>
  5.  
  6. struct Tree {
  7.     char name[30];
  8.     int count = 0;
  9.     Tree *left, *right;
  10. }*root;
  11.  
  12. int kol = 0, kolout = 0;
  13. int k20 = 0;
  14.  
  15. FILE *f;
  16.  
  17. void create_Tree(Tree**root, char a[]) {
  18.     if (!(*root)) {
  19.         (*root) = new Tree;
  20.         (*root)->left = NULL;
  21.         (*root)->right = NULL;
  22.         strcpy((*root)->name, a);
  23.         (*root)->count++;
  24.         if ((*root)->count == 1)
  25.             kol++;
  26.     }
  27.     else if (strcmp(a, (*root)->name) < 0)
  28.         create_Tree(&((*root)->left), a);
  29.     else if (strcmp(a, (*root)->name) > 0)
  30.         create_Tree(&((*root)->right), a);
  31.     else {
  32.         (*root)->count++;
  33.         if ((*root)->count == 1)
  34.             kol++;
  35.  
  36.     }
  37. }
  38.  
  39. void print_Tree(Tree *root) {
  40.     if (root&&k20 < 20) {
  41.         print_Tree(root->left);
  42.         printf("%d ", root->count);
  43.         printf("%s", root->name);
  44.         k20++;
  45.         kolout++;
  46.         print_Tree(root->right);
  47.     }
  48.     if (k20 == 20 || kolout == kol)
  49.     {
  50.         getchar();
  51.         k20 = 0;
  52.     }
  53.     /*if (root)
  54.         goto here;*/
  55. }
  56.  
  57. int main()
  58. {
  59.     SetConsoleCP(1251);
  60.     SetConsoleOutputCP(1251);
  61.     f = fopen("f.txt", "r");
  62.     char a[30];
  63.     int i = 0;
  64.     i = 0;
  65.     while (!feof(f)) {
  66.         fscanf(f, "%c", &a[i]);
  67.         if (!((a[i] >= 'A'&&a[i] <= 'Z') || (a[i] >= 'a'&&a[i] <= 'z') || (a[i] >= 'А'&&a[i] <= 'Я') || (a[i] >= 'а' && a[i] <= 'п') || (a[i] >= 'р' && a[i] <= 'я'))) {
  68.             a[i] = '\n';
  69.             a[i + 1] = '\0';
  70.             if (i != 0)
  71.                 create_Tree(&root, a);
  72.             for (int i = 0; i < 30; i++)
  73.                 a[i] = '\0';
  74.             i = 0;
  75.         }
  76.         if (a[0] != '\0')
  77.             i++;
  78.     }
  79.     /*int k;
  80.     scanf("%d", &k);*/
  81.  
  82.     print_Tree(root);
  83.     fclose(f);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement