Advertisement
KrimsN

Untitled

Nov 7th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ctime>
  4. #include <windows.h>
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9.     int x;
  10.     Node *l,*r;
  11. };
  12.  
  13. void show_a_b(Node *&Tree, int a, int b, int *i)
  14. {
  15.  
  16.     if (Tree != NULL)
  17.     {
  18.         show_a_b(Tree->l, a, b, i);
  19.  
  20.         if (Tree->x > a && Tree->x < b)
  21.         {
  22.             (*i)++;
  23.             cout << "  " << Tree->x << "  ";
  24.         }
  25.        
  26.         show_a_b(Tree->r, a, b, i);
  27.     }
  28. }
  29. void show_inx(Node *&Tree)
  30. {
  31.     if (Tree != NULL)
  32.     {
  33.         show_inx(Tree->l);
  34.             cout << Tree->x << '\t';
  35.         show_inx(Tree->r);
  36.     }
  37. }
  38.  
  39. void show_psf(Node *&Tree)
  40. {
  41.     if (Tree != NULL)
  42.     {
  43.         cout << Tree->x <<'\t';
  44.         show_psf(Tree->l);
  45.         show_psf(Tree->r);
  46.     }
  47. }
  48. void del(Node *&Tree){
  49.     if (Tree != NULL)
  50.     {
  51.         del(Tree->l);
  52.         del(Tree->r);
  53.         delete Tree;
  54.         Tree = NULL;
  55.     }
  56.  
  57. }
  58. void show_Tree(Node * Tree,int level = 0)
  59. {
  60.     if(Tree)
  61.     {
  62.         show_Tree(Tree->r,level + 1);
  63.         for(int i = 0;i< level;i++)
  64.             cout <<"   ";
  65.         cout << Tree->x << endl;
  66.         show_Tree(Tree->l,level + 1);
  67.     }
  68. }
  69.  
  70. void add_node(int x,Node *&MyTree)
  71. {
  72.     if (NULL == MyTree)
  73.     {
  74.         MyTree = new Node;
  75.         MyTree->x = x;
  76.         MyTree->l = MyTree->r = NULL;
  77.         return;
  78.     }
  79.     if (x <= MyTree->x)
  80.     {
  81.         if (MyTree->l != NULL)
  82.             add_node(x, MyTree->l);
  83.         else
  84.         {
  85.             MyTree->l = new Node;
  86.             MyTree->l->l = MyTree->l->r = NULL;
  87.             MyTree->l->x = x;
  88.         }
  89.         return;
  90.     }
  91.     if (MyTree->r != NULL)
  92.         add_node(x, MyTree->r);
  93.     else
  94.     {
  95.         MyTree->r = new Node;
  96.         MyTree->r->l = MyTree->r->r = NULL;
  97.         MyTree->r->x = x;
  98.     }
  99. }
  100. void add_cin(Node *&MyTree, int N)
  101. {
  102.     int wh_0;
  103.         for(int i = 1; i <= N; i++)
  104.         {
  105.             cout << "|  [" << i << "] : ";
  106.             cin >> wh_0;
  107.             add_node(wh_0, MyTree);
  108.         }
  109. }
  110. void add_rand(Node *&MyTree, int count)
  111. {
  112.    
  113.     if(count <= 0)
  114.         return;
  115.     srand(time(NULL));
  116.     int a;
  117.     for (int i = 0; i < count; ++i)
  118.     {
  119.         a = -50 + rand() % 100;
  120.         add_node(a, MyTree);
  121.     }
  122. }
  123. bool add_f(Node *&MyTree, const char * const f_name)
  124. {
  125.     int a;
  126.     ifstream myfile (f_name);
  127.     if (!myfile)
  128.     {
  129.         cout << "|  Ошибка при открытии файла или такого файла нет" << endl;
  130.         return false;
  131.     }
  132.     while (myfile)
  133.     {
  134.         myfile >> a;
  135.         add_node(a, MyTree);
  136.     }
  137.     return true;
  138. }
  139. int UI_s()
  140. {
  141.     int tmp;
  142.     setlocale(LC_ALL, "Russian");
  143.     system("cls");
  144.     cout << endl;
  145.     cout << "\t+--------------------------------------------------------------+" << endl;
  146.     cout << "\t|       Выберете, каким оброзом нужно заполнять дерево?        |" << endl;
  147.     cout << "\t+--------------------+--------------------+--------------------+" << endl;
  148.     cout << "\t|         1.         |          2.        |         3.         |" << endl;
  149.     cout << "\t|                    |                    |                    |" << endl;
  150.     cout << "\t|       Ввести       |      Используя     |         Из         |" << endl;
  151.     cout << "\t|         в          |     псевдорандом   |    *.txt фойла     |" << endl;
  152.     cout << "\t|       ручную       |                    |                    |" << endl;
  153.     cout << "\t|                    |                    |                    |" << endl;
  154.     cout << "\t+--------------------+--------------------+--------------------+" << endl << endl;
  155.     cout << "\t                                          +--------------------+" << endl;
  156.     cout << "\t                                          |   4.    Выход      |" << endl;
  157.     cout << "\t                                          +--------------------+" << endl << endl << endl << endl;
  158.     cout << "|  Ввод >> ";
  159.     cin >> tmp;
  160.     return tmp;
  161.  
  162. }
  163. int UI_e()
  164. {
  165.     int tmp;
  166.     setlocale(LC_ALL, "Russian");
  167.     system("cls");
  168.     cout << endl;
  169.     cout << "\t+--------------------------------------------------------------+" << endl;
  170.     cout << "\t|        Выберете, каким оброзом нужно вывести дерево?         |" << endl;
  171.     cout << "\t+--------------------+--------------------+--------------------+" << endl;
  172.     cout << "\t|         1.         |          2.        |         3.         |" << endl;
  173.     cout << "\t|                    |                    |                    |" << endl;
  174.     cout << "\t|         В          |          В         |         В          |" << endl;
  175.     cout << "\t|     инфиксной      |     префиксной     |        виде        |" << endl;
  176.     cout << "\t|       форме        |        форме       |       дерева       |" << endl;
  177.     cout << "\t|                    |                    |                    |" << endl;
  178.     cout << "\t+--------------------+--------------------+--------------------+" << endl << endl;
  179.     cout << "\t                                          +--------------------+" << endl;
  180.     cout << "\t                                          |   4.    Выход      |" << endl;
  181.     cout << "\t                                          +--------------------+" << endl << endl << endl << endl;
  182.     cout << "|  Ввод >> ";
  183.     cin >> tmp;
  184.     return tmp;
  185.  
  186. }
  187. int main(int argc, char const *argv[])
  188. {
  189.     Node* Tree = NULL;
  190.     int con = 0, how_i = 0;
  191.     bool how_b = false;
  192.     char st;
  193. start:
  194.     do{
  195.         how_i = UI_s();
  196.         switch (how_i) {
  197.             case 1: {
  198.                 cout << "|  Введите сколько цифр вы хотите" << endl << "|  Ввод >> ";
  199.                 int a;
  200.                 cin >> a;
  201.                 if (a < 0) a = -a;
  202.                 add_cin(Tree, a);
  203.                 how_b = true;  
  204.                 break;
  205.             }
  206.             case 2: {
  207.                 cout << "|  Введите сколько цифр вы хотите" << endl << "|  Ввод >> ";
  208.                 int a;
  209.                 cin >> a;
  210.                 if (a < 0) a = -a;
  211.                 add_rand(Tree, a);
  212.                 how_b = true;  
  213.                 break;
  214.             }
  215.             case 3: {
  216.                 char text[20];
  217.                     add_f_m:
  218.  
  219.                 cout << endl << "|  Введите относительный путь к файлу" << endl << "|  Ввод >> ";
  220.                 scanf("%s", &text);
  221.                 if(!add_f(Tree, text)) goto add_f_m;
  222.                 how_b = true;
  223.                 break;
  224.             }
  225.             case 4: {
  226.                 cout << "|  Вы уверены? " << endl << "|  [y/n] >> ";
  227.                 cin >> st;
  228.                 if (st == 'n') goto start;
  229.                 del(Tree);
  230.                 system("cls");
  231.                 return 1;
  232.             }
  233.             default : break;
  234.  
  235.         }
  236.     }while(!how_b);
  237.    
  238.     cout << "|  [";
  239.     for (int i = 0; i < 50; ++i)
  240.     {
  241.         cout << "=";
  242.         Sleep(15);
  243.     }
  244.     cout << "] Done";
  245.     Sleep(400);
  246.  
  247. s_ui:
  248.     how_b = false;
  249.     do{
  250.         how_i = UI_e();
  251.         switch (how_i) {
  252.             case 1: {
  253.                 cout << "| << ";
  254.                 show_inx(Tree);
  255.                 cout << endl;
  256.                 how_b = true;  
  257.                 break;
  258.             }
  259.             case 2: {
  260.                 cout << "| << ";
  261.                 show_psf(Tree);
  262.                 cout << endl;
  263.                 how_b = true;
  264.                 break;
  265.             }
  266.             case 3: {
  267.                 show_Tree(Tree);
  268.                 //cout << endl;
  269.                 how_b = true;
  270.                 break;
  271.             }
  272.             case 4: {
  273.                 cout << "Вы уверены? " << endl << "|   [y/n] >> ";
  274.                 cin >> st;
  275.                 if (st == 'n') goto s_ui;
  276.                 del(Tree);
  277.                 system("cls");
  278.                 return 1;
  279.             }
  280.             default : break;
  281.  
  282.         }
  283.     }while(!how_b);
  284.     int a, b, col = 0;
  285.     cout << "|  Введите интервал (a, b)" << endl << "| Ввод >> ";
  286.     cin >> a >> b;
  287.     cout << "|  Числа в вашем интервале : ";
  288.     if (a <= b)
  289.         show_a_b(Tree, a, b, &col);
  290.     else
  291.         show_a_b(Tree, b, a, &col);
  292.     cout << endl << "|  Чисел в вашем интервале : " << col << endl << endl;
  293.  
  294.     cout << "Запустить заново? [y/n] ";
  295.     cin >> st;
  296.     if (st == 'y') goto start;
  297.     system("pause && cls");
  298.     return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement