raxbg

main.cpp

May 9th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1. #include <iostream>
  2. #include "BinaryTree.h"
  3.  
  4. using namespace std;
  5.  
  6. int ShowMenu();
  7.  
  8. int main()
  9. {
  10.     BinaryTree *tree = 0;
  11.     int *treeElements;
  12.     int choice = 0;
  13.     for (;;)
  14.     {
  15.         choice = ShowMenu();
  16.         if (choice != 0)
  17.         {
  18.             switch (choice)
  19.             {
  20.                 case 1:
  21.                 {
  22.                     int elementsCount;
  23.                     if (tree != 0)
  24.                     {
  25.                         BinaryTree *delTree;
  26.                         delTree = tree;
  27.                         tree = 0;
  28.                         delete delTree;
  29.                     }
  30.                     cout << "How many elements?: ";
  31.                     cin >> elementsCount;
  32.                     treeElements = new int [elementsCount];
  33.                     for (int i=0; i<elementsCount;i++)
  34.                         {
  35.                             cout << "Enter element " << i+1 << ": ";
  36.                             cin >> treeElements[i];
  37.                         }
  38.                     tree = new BinaryTree();
  39.                     tree->Build(treeElements,elementsCount);
  40.                     break;
  41.                 }
  42.                 case 2:
  43.                 {
  44.                     if (tree != 0)
  45.                     {
  46.                         int startRow;
  47.                         cout << "Enter row number to print from (number between 1 -" << tree->GetRowsCount() << "): ";
  48.                         cin >> startRow;
  49.                         tree->Walk(startRow);
  50.                     }
  51.                     else cout << "There is no tree to walk" << endl;
  52.                     break;
  53.                 }
  54.                 default:
  55.                     cout << "Wrong command number" << endl;
  56.                     break;
  57.             }
  58.         }
  59.         else
  60.         {
  61.             delete treeElements;
  62.             delete tree;
  63.             break;
  64.         }
  65.     }
  66.     return 0;
  67. }
  68.  
  69. int ShowMenu()
  70. {
  71.     int choice = 0;
  72.     cout << "1 - Create tree" << endl;
  73.     cout << "2 - Print tree" << endl;
  74.     cout << "0 - exit" << endl;
  75.     cout << "Enter your choice: ";
  76.     cin >> choice;
  77.     if (choice == 0)
  78.     {
  79.         char confirm;
  80.         cout << "Do you really want to exit the program? (y/n) ";
  81.         cin >> confirm;
  82.         return (confirm == 'n' || confirm == 'N') ? -1 : choice;
  83.     }
  84.     return choice;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment