Question

In: Computer Science

Code in C++ Learning Outcomes Implement two stacks and use them to implement an infix to...

Code in C++

Learning Outcomes

Implement two stacks and use them to implement an infix to prefix expression convertor

Stacks

A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string.

The Stack should only have one data member: the Linked List. It should implement the following methods:

  • Stack(): Empty constructor.
  • void clear(): removes all of the elements from the stack
  • int size() const: returns the number of elements in the stack
  • bool isEmpty() const: returns true if the stack is empty, false otherwise
  • string top() const: returns the top of the stack. It must throw an error if the stack is empty.
  • string pop(): returns the top of the stack and removes it. It must throw an error if the stack is empty.
  • void push(string): adds a value of type string to the stack.

Your linked list should support adding to the end of the list.

Infix to Prefix Conversion

Suppose you have an infix expression a+c^d/g-h. Using the math operation priority, we learned this is equal to a+(((c^d)/g)-h).

The prefix expression of the above infix one is:   + a - / ^ c d g h

Your program must read a math expression in string and prints its prefix equivalent.

An example of program run:

Please enter the infix math Expression: a+c^d/g-h

The equivalent prefix math expression is : + a - / ^ c d g h

Do you want to add another expression [Y/N]: Y

Another example of the program in which program returns an error message.

Please enter the infix math Expression: a++c^d/g-h

Sorry! The expression is not valid.

Do you want to add another expression [Y/N]: Y

The user must use * to declare the multiplication, and ^ to declare power.

The only valid operators are: ^ * / + -

User may enter ( and ) as many as they want provided the expression is correct. For example, the following expression is valid:

(((((((a+b)))))))+((((c))))

And the output must be: + + a b c

Program must treat variables such as ab as one operand and not the multiplication of a and b. (ab!=a*b)

See following example:

Please enter the infix math Expression: aa+c^dsa/gl-h

The equivalent prefix math expression is : + aa - / ^ c dsa g h

The aa and dsa are two operands.

Rules for Implementation

Start at the beginning of the expression string and evaluate each character based on these rules.

  • You need two stacks. One is used to store the operators. The second one is used to store the operands. Be cautious that the operand stack not only stores the single operands such as a, b, cd but also combined operands. For example, in expression

a+b*c the b and c are the operands of the * operator. Then * b c all together is an operand to the + operator (the other operand of this operator is a)

  • Checking the priority is the key in this assignment. Whenever you want to insert a new operator you should check the top of the operator stack. If the top one has a higher or equal priority you need to calculate the prefix one and then insert the new operator (look at the example I wrote for you.)

After all characters have been evaluated, perform one last check:

  • If the operator stack is empty and operand stack has more than one item the expression is not valid.
  • The final expression in the operand stack is the answer.

The InfixToPrefix Class

Create a new class called InfixToPrefix.hpp containing the following fields.

  • std::string mExpression;
  • Stack operator;
  • Stack operand;

It will also have the following constructors and methods.

  • A constructor which passes the expression in one string
  • string toPrefix(): A method which evaluates the stored expression and returns the prefix expression.

Write the Driver.

The program will begin with a single prompt: " Please enter the infix math Expression: "

The user will supply an expression. Next, the program will prints the prefix expression of the given expression. If the expression is not valid, report print that the expression is not a valid infix expression. Finally, the program will wait for a final keystroke (i.e. pause). Make sure that your program works for all of the sample expressions.

Solutions

Expert Solution

now the main function for the program

Run this program in your pc once.

If you come across any problems feel free to send me the problems that you encounter.

Thank you!...


Related Solutions

We are trying to use two stacks to implement a queue. Name the two stacks as...
We are trying to use two stacks to implement a queue. Name the two stacks as E and D. We will enqueue into E and dequeue from D. To implement enqueue(e), simply call E.push(e). To implement dequeue(), simply call D.pop(), provided that D is not empty. If D is empty, iteratively pop every element from E and push it onto D, until E is empty, and then call D.pop(). Considering the worst case running time, what is the performance in...
Write a C++ class that implement two stacks using a single C++ array. That is, it...
Write a C++ class that implement two stacks using a single C++ array. That is, it should have functions pop_first(), pop_second(), push_first(…), push_second(…), size_first(), size_second(), …. When out of space, double the size of the array (similarly to what vector is doing). Notes: Complete all the functions in exercise_2.cpp, then submit this cpp file. pop_first() and pop_second() should throw std::out_of_range exception when stack is empty. CODE: #include <cstdio> #include <stdexcept> template <class T> class TwoStacks { public:   // Constructor, initialize...
write code in python and test Conversation with an AI ChatBot Learning Outcomes: Use the print()...
write code in python and test Conversation with an AI ChatBot Learning Outcomes: Use the print() function to output a variety of data Use the input() function to ask for multiple data types Use basic arithmetic operations in a program Use string methods and operations Use if/else if/else statements to determine the flow of the program Comment your code Debug your code Test your code Scenario A new marketing company is launching an Artificial Intelligence (AI) ChatBot for their website...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
C++, Take this 3 pseudocode and transform them into C++ then implement them making a quick...
C++, Take this 3 pseudocode and transform them into C++ then implement them making a quick sort program using the function of the pseudocode that you changed to C++, also in the main the user need to put the array that he wants to do quick sort with. plz comment everything you do in details this is for a presentation 1. A first draft of pseudocode for the quick sort algorithm follows: // Sorts theArray[first..last]. quickSort(theArray: ItemArray, first: integer, last:...
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes. Example Program Session (implement some linefeed ‘\n’ formatting): Enter the limit: 1000 Primes up to 1000    2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53 59   61   67   71   73   79   83   89   97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows:...
languague c++ Write a program to demonstrate the use of STACKS. The scenario is as follows: There is a MAZE. There is a mouse inside the maze. The mouse must find the exit from the maze. When the user clicks the letter C or c a CAT is added to the maze. The cat will run through the maze and if it finds the mouse it should eat it and the game is over! User can add as many cats...
Learning Outcomes Implement a dynamically resizable array and analyze its performance. Instructions Our last program was...
Learning Outcomes Implement a dynamically resizable array and analyze its performance. Instructions Our last program was to maintain a list of Planets in the STL’s vector class. In this assignment, you will be writing your own vector class. Implement class vector consisting of an array of a generic type. This class will require the following fields: mData: An array of items of a generic type. mCapacity: An integer representing the capacity of the vector, which is the number of items...
Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?
Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT