Advertisement
KrimsN

Untitled

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