In: Computer Science
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:
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.
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)
After all characters have been evaluated, perform one last check:
The InfixToPrefix Class
Create a new class called InfixToPrefix.hpp containing the following fields.
It will also have the following constructors and methods.
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.
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!...