Advertisement
KrimsN

Untitled

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