Пример #1
0
int main()
{
    DisplayOpeningScreen("Lastname:Firstname:A00123456:CSC34201",
        "Submission ??: "
        "Implementing an ADT Binary Search Tree of Characters, Part ?");

    BSTChar bst;

    Menu menu;
    BuildMenu(menu);
    int menuChoice;
    do
    {
        menu.display();
        menuChoice = menu.getChoice();
        switch(menuChoice)
        {
        case -1:
        case 1:  cout << "Program now terminating.\n"; break;
        case 2:  DescribeProgram();    break;
        case 3:  BuildTree(bst);       break;
        case 4:  DisplayTree(bst);     break;
        case 5:  GetTreeSize(bst);     break;
        case 6:  GetTreeHeight(bst);   break;
        case 7:  DeleteAllValues(bst); break;
        case 8:  FindAValue(bst);      break;
        case 9:  InsertAValue(bst);    break;
        case 10: DeleteAValue(bst);    break;
        case 11: ModifyAllValues(bst); break;
        case 12: BalanceTree(bst);     break;
        }
        Pause();
    }
    while (menuChoice != -1  &&  menuChoice != 1);
}
Пример #2
0
int main()
{
    DisplayOpeningScreen("Firstname:Lastname:A00123456:csc34201",
        "Submission 05: Building and Displaying Power Sets");
    cout << "\nThis program prompts for an integer value n and builds "
        "the set\n{1, 2, 3, ..., n}. It then calls a (recursive) function "
        "powerSet()\nthat returns the power set of the set. The power set "
        "is the set of\nall subsets of the set and has 2^n elements. "
        "After displaying the\nsize of the power set, the program calls "
        "the function WriteAllSubsets()\nthat outputs each subset using "
        "the notation {e_1, e_2, ..., e_m},\nfour sets per line.\n\n";
    Pause();

    int n;
    ReadInt("Enter a non-negative integer "
    	"(<= 6 if you don't want a lot of output): ", n);

    set<int> sInt; //Set of integers whose power set we construct
    for (int i=1; i<=n; i++) sInt.insert(i); //Build set {1,2,3, ..., n}

    set<set<int>> pSet = powerSet(sInt); //Build the power set

    //Output number of subsets, followed by all subsets, four per line
    cout << "\nNumber of subsets = " << pSet.size()
        << ", and here they are:\n";
    WritePowerSet(pSet);
    Pause();
}
Пример #3
0
int main(int argc, char* argv[])
{
    if (argc == 1)
    {
        DisplayOpeningScreen("Lastname:Firstname:A00123456:CSC34201",
                             "Submission 07: Implementing a Simple Four-Function Calculator");
        DisplayUsage();
        return 1;
    }

    //Pseudocode for your main driver ...

    //Put all command-line parameters into a string
    //Create an input string stream object from that string
    //Try to read a valid expression from the input string stream and
    //    put the expression into an expression tree
    //If reading an expression succeeds, check for garbage characters
    //    following the valid expression in the input stream
    //If reading an expression succeeds and no garbage is found
    //    output the expression and its value
    //else
    //    output the bad input message
}
int main()
{
    DisplayOpeningScreen(MY_ID_INFO,
        "Submission 05: Computing with Fractions, Part 2");
    Menu menu;
    BuildMenu(menu);
    menu.display();
    int choice;
    choice = menu.getChoice();
    while(choice > 0)
    {
        //if user chooses 1, terminate program
        if(choice == 1)
        {
            cout << "Program is now terminating.\n";
            Pause();
            return 0;
        }

        //if user chooses 2, display program info
        if(choice == 2)
        {
            TEXT.displayItem("Program Description");
        }

        //if user chooses 3 , evaluate the fractions
        if(choice == 3)
        {
            string answer;
            do
            {
                cout << "\nEnter fractional expression to be evaluated: ";
                //declare all variables in this scope
                Fraction fraction1, fraction2, finalFraction;
                char operators;
                cin >> fraction1;
                cin >> operators;
                cin >> fraction2;
                
                //if operator is + , do addition
                if(operators == '+')
                    finalFraction = fraction1+fraction2;
                
                //if operator is - , do subtraction
                else if(operators == '-')
                    finalFraction = fraction1-fraction2;
                
                //if operator is * , do multiplication
                else if(operators == '*')
                    finalFraction = fraction1*fraction2;
                
                //if operator is / , do division
                else if(operators == '/')
                    finalFraction = fraction1/fraction2;

                cout << "Answer: " << fraction1 << " " << operators << " "
                    << fraction2 << " = " << finalFraction;
                cout << "\nDo another calculation?";
                cin.ignore();
            }
            while(userSaysYes(answer));
        }

        //if user chooses 4, compare the fractions
        if(choice == 4)
        {
            string answer;
            do
            {
                cout << "\nEnter two fractions to be compared: ";
                //declare all variables in this scope
                Fraction fraction1, fraction2, differenceFraction1,
                    differenceFraction2; 
                cin >> fraction1;                
                cin >> fraction2;
                //if the 2nd one is bigger than the first , calculate the
                //difference between the two
                differenceFraction1 = fraction2 - fraction1;
                //if the 1st one is bigger than the second , calculate the
                //difference between the two
                differenceFraction2 = fraction1 - fraction2;

                //if the 1st fraction is less than the second, print it as well
                //as the difference between the two
                if(fraction1 < fraction2)
                {
                    cout << fraction2 << " is greater than " << fraction1
                        << " and the difference is " << differenceFraction1
                        << ".";
                }

                //if the 2nd fraction is less than the first, print it as well
                //as the difference between the two
                if(fraction2 < fraction1)
                {
                    cout << fraction1 << " is greater than " << fraction2
                        << " and the difference is " << differenceFraction2
                        << ".";
                }

                //if the fractions are equal print it
                else if(fraction1==fraction2)
                {
                    cout << "The two fractions are equal.";
                }
                cout << "\nDo another comparison?";
                cin.ignore();
            }
            while(userSaysYes(answer));
            
        }
Пример #5
0
int main()
{
    DisplayOpeningScreen("Iqbal:Arsum:A00351433:CSC34218",
        "Creating and Displaying Balanced Binary Search Trees");
    vector<char> treeValues;
    RandomGenerator randomValues;
    TreeNode node;
    int count = 0;
    int height = 0;
    bool success = false;
    Menu m("Menu for Balanced Character BST Creation and Display");
    AddMenuOptions(m);
    int menuChoice;
    do
    {
        m.display();
        menuChoice = m.getChoice();
        switch (menuChoice)
        {
        case 1: case -1:
            break;
        case 2:
            cout << "Enter characters into the vector (terminate with an end"
                "-of-file on a new line): ";
            //clears previous values in the vector
            treeValues.clear();
            char values;
            while(cin >> values)
                //store characters into vector
                treeValues.push_back(values);
            //clear input stream
            cin.clear();
            Pause(0, "All characters have been entered into the vector.");
            //set success to false because bst hasnt been created yet
            success = false;
            break;
        case 3:
            int requiredVal;
            cout << "Enter required number of values: ";
            cin >> requiredVal;
            char firstVal;
            cout << "Enter first value of character range: ";
            cin >> firstVal;
            char secondVal;
            cout << "Enter last value of character range: ";
            cin >> secondVal;
            //clear previous value of vector
            treeValues.clear();
            while(count != requiredVal)
            {
                //get a random string from given character range
                string temp = randomValues.getNextStringFromCharRange
                    (1,firstVal,secondVal);
                //convert to a character
                const char* generatedVal = temp.c_str();
                vector<char>::iterator p1 = treeValues.begin();
                //find the character in the vector
                p1 = find(treeValues.begin(),treeValues.end(),*generatedVal);
                //if it couldnt find the value
                if(p1 == treeValues.end())
                {
                    //insert the value into the vector
                    treeValues.push_back(*generatedVal);
                    count++;
                }
            }
            //reset counter
            count = 0;
            //clear input stream
            cin.ignore();
            Pause(0, "All values have been generated.");
            //set success to false because bst hasnt been created yet
            success = false;
            break;
        case 4:
            cout << "\nHere are the current contents of the vector:\n" << endl;
            //Display values from the vector
            copy(treeValues.begin(),treeValues.end(),
                ostream_iterator<char>(cout, " "));
            Pause(0, "\n");
            break;
        case 5:
            //create the binary search tree
            createBst(treeValues,node);
            Pause(0, "The balanced binary search tree has been created.");
            //set success to true because bst has been created
            success = true;
            break;
        case 6:
            //if the bst wasnt created
            if(!success)
            {
                cout << "Tree is empty. Nothing to display.\n";
                Pause();
                break;
            }
            //find the height of the bst
            height = findHeight(&node);
            if (height <= 3)
                displayTree(&node);
            else
                cout << "\nHeight of tree is too great for display (>=4).\n";
            Pause();
            break;
        }
    }
    while (menuChoice != 1 && menuChoice != -1);
    Pause(0, "Program now terminating.");
}