Stack coding
/** * @author Jane Programmer * @cwid 123 45 678 * @class COSC 2336, Spring 2019 * @ide Visual Studio Community 2017 * @date March 8, 2019 * @assg Assignment 10 * * @description Assignment 10 Applications of stacks. Implement * the given functions and algorithms using a stack data type. */ #include <iostream> #include <cassert> #include "Stack.hpp" using namespace std; // you can put your function implementations here, or alternatively // create a header named StackApplications.hpp and implementation file // named StackApplicaitons.cpp and include the header file here // #include "StackApplications.hpp" /** main * The main entry point for this program. Execution of this program * will begin with this main function. * * @param argc The command line argument count which is the number of * command line arguments provided by user when they started * the program. * @param argv The command line arguments, an array of character * arrays. * * @returns An int value indicating program exit status. Usually 0 * is returned to indicate normal exit and a non-zero value * is returned to indicate an error condition. */ int main(int argc, char** argv) { // ----------------------------------------------------------------------- cout << "-------------- Test doParenthesisBalance() ----------------------" << endl << endl; string expression = "()"; cout << "<doParenthesisBalance()> testing balanced expression: '" << expression << "'" << endl; //bool balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(balanced); expression = "(()((())))"; cout << "<doParenthesisBalance()> testing balanced expression: '" << expression << "'" << endl; //balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(balanced); expression = "((((()))(()((()))))()(()))"; cout << "<doParenthesisBalance()> testing balanced expression: '" << expression << "'" << endl; //balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(balanced); expression = ""; cout << "<doParenthesisBalance()> testing empty expression, should evaluate as balanced: '" << expression << "'" << endl; //balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(balanced); expression = "("; cout << "<doParenthesisBalance()> simple unbalanced expression: '" << expression << "'" << endl; //balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(!balanced); expression = ")"; cout << "<doParenthesisBalance()> simple unbalanced expression: '" << expression << "'" << endl; //balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(!balanced); expression = "((()(())())"; cout << "<doParenthesisBalance()> complex unbalanced expression: '" << expression << "'" << endl; //balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(!balanced); expression = "((((()))(()((())))()(()))"; cout << "<doParenthesisBalance()> complex unbalanced expression: '" << expression << "'" << endl; //balanced = doParenthesisBalance(expression); //cout << " balanced: " << boolalpha << balanced << endl; //assert(!balanced); cout << endl << endl; // ----------------------------------------------------------------------- cout << "-------------- Test decodeIDSequence() --------------------------" << endl << endl; string result; string sequence = "IIII"; cout << "<decodeIDSequence()> testing simple increase sequence: '" << sequence << "'" << endl; //result = decodeIDSequence(sequence); //cout << " result: " << result << endl; //assert(result == "12345"); sequence = "DDDD"; cout << "<decodeIDSequence()> testing simple decrease sequence: '" << sequence << "'" << endl; //result = decodeIDSequence(sequence); //cout << " result: " << result << endl; //assert(result == "54321"); sequence = ""; cout << "<decodeIDSequence()> testing empty: '" << sequence << "'" << endl; //result = decodeIDSequence(sequence); //cout << " result: " << result << endl; //assert(result == "1"); sequence = "IDIDII"; cout << "<decodeIDSequence()> testing general sequence: '" << sequence << "'" << endl; //result = decodeIDSequence(sequence); //cout << " result: " << result << endl; //assert(result == "1325467"); sequence = "IIDDIDID"; cout << "<decodeIDSequence()> testing general sequence: '" << sequence << "'" << endl; //result = decodeIDSequence(sequence); //cout << " result: " << result << endl; //assert(result == "125437698"); cout << endl << endl; // ---------------------------------------------------------------------- cout << "-------------- Test insertItemOnSortedStack() ------------------" << endl << endl; LStack<int> sortedStack; sortedStack.push(1); sortedStack.push(3); sortedStack.push(5); sortedStack.push(7); sortedStack.push(8); // general test cout << "<insertItemOnSortedStack()> general test, insert in middle:" << endl << endl; cout << "before inserting: " << endl << sortedStack << endl; //insertItemOnSortedStack(4, sortedStack); cout << "after inserting: " << endl << sortedStack << endl; //int stackSize = 6; //int expectedItems1[] = {1, 3, 4, 5, 7, 8}; //AStack<int> expectedStack1(expectedItems1, stackSize); //assert(sortedStack == expectedStack1); // test insert on empty stack sortedStack.clear(); cout << "<insertItemOnSortedStack()> test inesrtion to empty stack:" << endl << endl; cout << "before inserting: " << endl << sortedStack << endl; //insertItemOnSortedStack(5, sortedStack); cout << "after inserting: " << endl << sortedStack << endl; //stackSize = 1; //int expectedItems2[] = {5}; //AStack<int> expectedStack2(expectedItems2, stackSize); //assert(sortedStack == expectedStack2); // test insert at top of stack cout << "<insertItemOnSortedStack()> test insertion to top of stack:" << endl << endl; cout << "before inserting: " << endl << sortedStack << endl; //insertItemOnSortedStack(9, sortedStack); cout << "after inserting: " << endl << sortedStack << endl; //stackSize = 2; //int expectedItems3[] = {5, 9}; //AStack<int> expectedStack3(expectedItems3, stackSize); //assert(sortedStack == expectedStack3); // test insert at bottom of stack cout << "<insertItemOnSortedStack()> test insertion to bottom of stack:" << endl << endl; cout << "before inserting: " << endl << sortedStack << endl; //insertItemOnSortedStack(1, sortedStack); cout << "after inserting: " << endl << sortedStack; //stackSize = 3; //int expectedItems4[] = {1, 5, 9}; //AStack<int> expectedStack4(expectedItems4, stackSize); //assert(sortedStack == expectedStack4); cout << endl << endl; cout << "-------------- Test sortStack() --------------------------------" << endl << endl; LStack<string> aStack; aStack.push("Susan"); aStack.push("Tom"); aStack.push("Allan"); aStack.push("Bobbie"); aStack.push("Chris"); // general test of stackSort() function cout << "<sortStack()> general test:" << endl << endl; cout << "before sorting:" << endl << aStack << endl; //sortStack(aStack); cout << "after sorting: " << endl << aStack << endl; //stackSize = 5; //string expectedItems5[] = {"Allan", "Bobbie", "Chris", "Susan", "Tom"}; //AStack<string> expectedStack5(expectedItems5, stackSize); //assert(aStack == expectedStack5); // sort an empty stack aStack.clear(); cout << "<sortStack()> sort an empty stack:" << endl << endl; cout << "before sorting:" << endl << aStack << endl; //sortStack(aStack); cout << "after sorting: " << endl << aStack << endl; //AStack<string> expectedStack6; // empty stack //assert(aStack == expectedStack6); // sort stack with single item aStack.push("Alice"); cout << "<sortStack()> sort single item sized stack:" << endl << endl; cout << "before sorting:" << endl << aStack << endl; //sortStack(aStack); cout << "after sorting: " << endl << aStack << endl; //stackSize = 1; //string expectedItems7[] = {"Alice"}; //AStack<string> expectedStack7(expectedItems7, stackSize); //assert(aStack == expectedStack7); // sort already sorted stack aStack.push("Bob"); aStack.push("Carol"); aStack.push("Dave"); cout << "<sortStack()> sort already sorted stack:" << endl << endl; cout << "before sorting:" << endl << aStack << endl; //sortStack(aStack); cout << "after sorting: " << endl << aStack << endl; //stackSize = 4; //string expectedItems8[] = {"Alice", "Bob", "Carol", "Dave"}; //AStack<string> expectedStack8(expectedItems8, stackSize); //assert(aStack == expectedStack8); // return 0 to indicate successful completion return 0; }