Untitled

 avatar
unknown
plain_text
3 years ago
12 kB
3
Indexable
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <sstream>
#include <exception>

#include "BinaryTree.h"

using namespace std;

int main(int argc, char **argv) {
    // Checking for command line args
    if (argc <= 1) {
        cout << "Too few command line arguments..." << endl;
        cout << "Format: ./Main file.txt" << endl;
        exit(1);
    } else if (argc > 2) {
        cout << "More than one command line argument..." << endl;
        cout << "Format: ./Main file.txt" << endl;
        exit(1);
    }
    BinaryTree<int> intTree;
    BinaryTree<float> floatTree;
    BinaryTree<std::string> stringTree;
    string file = argv[1];
    ifstream inputFile(file);

    string userInput;
    cout << "Enter tree type (i - int, f - float, s - std::string): ";
    cin >> userInput;

    bool input = true;
    while (input) {
        if (userInput.compare("i") == 0) {
            int item;
            while (inputFile >> item) {
                intTree.insert(item);
            }
            inputFile.close();
            input = false;
        } else if (userInput.compare("f") == 0) {
            float item;
            while (inputFile >> item) {
                floatTree.insert(item);
            }
            inputFile.close();
            input = false;
        } else if (userInput.compare("s") == 0) {
            string item;
            while (inputFile >> item) {
                stringTree.insert(item);
            }
            inputFile.close();
            input = false;
        } else {
            cout << "Invalid list type" << endl;
            cout << "Enter list type (i - int, f - float, s - std::string): ";
            cin >> userInput;
        }
    } // While

    // Main game loop
    bool status = true;
    string input2;
    cout << "insert (i), delete (d), retrieve (r), length (l), in-order (n), pre-order (p),"
         << endl
         << "post-order (o), getNumSingleParent (s), getNumLeafNodes (f),"
         << endl
         << "getSumOfSubtrees (t), quit (q)"
         << endl;

    while (status) {
        cout << "Enter a command: ";
        cin >> input2;

        if (input2.compare("i") == 0) {             // insert
            if (userInput.compare("i") == 0) {
                // int tree
                int userItem;
                cout << "Enter an item to insert: ";
                cin >> userItem;
                while (cin.fail()) {
                    cout << "Please enter an integer item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> userItem;
                }
                intTree.insert(userItem);
                intTree.inOrder();
                cout << endl;
            } else if (userInput.compare("f") == 0) {
                // float tree
                float userItem;
                cout << "Enter an item to insert: ";
                cin >> userItem;
                while (cin.fail()) {
                    cout << "Please enter an integer item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> userItem;
                }
                floatTree.insert(userItem);
                floatTree.inOrder();
                cout << endl;
            } else if (userInput.compare("s") == 0) {
                // string tree
                string userItem;
                cout << "Enter an item to insert: ";
                cin >> userItem;
                while (cin.fail()) {
                    cout << "Please enter an integer item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> userItem;
                }
                stringTree.insert(userItem);
                stringTree.inOrder();
                cout << endl;
            }
        } else if (input2.compare("d") == 0) {      // delete
            if (userInput.compare("i") == 0) {
                // int tree
                int userItem;
                cout << "Item to delete: ";
                cin >> userItem;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> userItem;
                }
                intTree.deleteItem(userItem);
                intTree.inOrder();
                cout << endl;
            } else if (userInput.compare("f") == 0) {
                // float tree
                float userItem;
                cout << "Item to delete: ";
                cin >> userItem;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> userItem;
                }
                floatTree.deleteItem(userItem);
                floatTree.inOrder();
                cout << endl;
            } else if (userInput.compare("s") == 0) {
                // string tree
                string userItem;
                cout << "Item to delete: ";
                cin >> userItem;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> userItem;
                }
                stringTree.deleteItem(userItem);
                stringTree.inOrder();
                cout << endl;
            }
        } else if (input2.compare("r") == 0) {      // retrieve
            if (userInput.compare("i") == 0) {
                // int tree
                int itemRetrieved;
                cout << "Item to be retrieved: ";
                cin >> itemRetrieved;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> itemRetrieved;
                }
                intTree.getRetrieve(itemRetrieved);
            } else if (userInput.compare("f") == 0) {
                // float tree
                float itemRetrieved;
                cout << "Item to be retrieved: ";
                cin >> itemRetrieved;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> itemRetrieved;
                }
                floatTree.getRetrieve(itemRetrieved);
            } else if (userInput.compare("s") == 0) {
                // string tree
                string itemRetrieved;
                cout << "Item to be retrieved: ";
                cin >> itemRetrieved;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> itemRetrieved;
                }
                stringTree.getRetrieve(itemRetrieved);
            }
        } else if (input2.compare("l") == 0) {      // length
            if (userInput.compare("i") == 0) {
                // int tree
                cout << "Length: " << intTree.getLength() << endl;
            } else if (userInput.compare("f") == 0) {
                // float tree
                cout << "Length: " << floatTree.getLength() << endl;
            } else if (userInput.compare("s") == 0) {
                // string tree
                cout << "Length: " << stringTree.getLength() << endl;
            }
        } else if (input2.compare("n") == 0) {      // in-order
            if (userInput.compare("i") == 0) {
                // int tree
                intTree.inOrder();
            } else if (userInput.compare("f") == 0) {
                // float tree
                floatTree.inOrder();
            } else if (userInput.compare("s") == 0) {
                // string tree
                stringTree.inOrder();
            }
        } else if (input2.compare("p") == 0) {      // pre-order
            if (userInput.compare("i") == 0) {
                // int tree
                intTree.preOrder();
            } else if (userInput.compare("f") == 0) {
                // float tree
                floatTree.preOrder();
            } else if (userInput.compare("s") == 0) {
                // string tree
                stringTree.preOrder();
            }
        } else if (input2.compare("o") == 0) {      // post-order
            if (userInput.compare("i") == 0) {
                // int tree
                intTree.postOrder();
            } else if (userInput.compare("f") == 0) {
                // float tree
                floatTree.postOrder();
            } else if (userInput.compare("s") == 0) {
                // string tree
                stringTree.postOrder();
            }
        } else if (input2.compare("s") == 0) {      // getNumSingleParent
            if (userInput.compare("i") == 0) {
                // int tree
                cout << "Number of Single Parents: " << intTree.getNumSingleParent() << endl;
            } else if (userInput.compare("f") == 0) {
                // float tree
                cout << "Number of Single Parents: " << floatTree.getNumSingleParent() << endl;
            } else if (userInput.compare("s") == 0) {
                // string tree
                cout << "Number of Single Parents: " << stringTree.getNumSingleParent() << endl;
            }
        } else if (input2.compare("f") == 0) {      // getNumLeafNodes
            if (userInput.compare("i") == 0) {
                // int tree
                cout << "Number of Leaf Nodes: " << intTree.getNumLeafNodes() << endl;
            } else if (userInput.compare("f") == 0) {
                // float tree
                cout << "Number of Leaf Nodes: " << floatTree.getNumLeafNodes() << endl;
            } else if (userInput.compare("s") == 0) {
                // string tree
                cout << "Number of Leaf Nodes: " << stringTree.getNumLeafNodes() << endl;
            }
        } else if (input2.compare("t") == 0) {      // getSumOfSubtrees
            if (userInput.compare("i") == 0) {


                // int tree
                int item;
                cout << "Item to be retrieved: ";
                cin >> item;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> item;
                }
                intTree.getSumOfSubTrees(item);
            } else if (userInput.compare("f") == 0) {
                // float tree
                float item;
                cout << "Item to be retrieved: ";
                cin >> item;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> item;
                }
                floatTree.getSumOfSubTrees(item);
            } else if (userInput.compare("s") == 0) {
                // string tree
                string item;
                cout << "Item to be retrieved: ";
                cin >> item;
                while (cin.fail()) {
                    cout << "Please enter an int item: ";
                    cin.clear();
                    cin.ignore();
                    cin >> item;
                }
                stringTree.getSumOfSubTrees(item);
            }
        } else if (input2.compare("q") == 0) {      // quit
            cout << "Exiting..." << endl;
            exit(1);
        } else {
            cout << "Invalid input. Please try again." << endl;
            status = true;
        }
    }


}
Editor is loading...