Question

C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...

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.

What to Hand In

  • linkedlist.hpp - the header file containing the entire class declaration and external definitions of the linkedlist class.
  • stack.hpp - the header file containing the entire class declaration and external definitions of the stack class.
  • infixToPrefix.hpp - the header file containing the entire class declaration and external definitions of the infixToPrefix class.
  • Your driver with main.cpp
0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2)   One alphabetic character - "x" (representing a value to be supplied later). (3)   Binary operators (+, -, *, / and % (modulo)). (4)   Parentheses          You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks....

    I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming:...

  • EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation;...

    EVALUATING GENERAL INFIX EXPRESSIONS INTRODUCTION The notation in which we usually write arithmetic expressions is called infix notation; in it, operators are written between their operands: X + Y. Such expressions can be ambiguous; do we add or multiply first in the expression 5 + 3 * 2? Parentheses and rules of precedence and association clarify such ambiguities: multiplication and division take precedence over addition and subtraction, and operators associate from left to right. This project implements and exercises a stack-based algorithm that evaluates...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

  • implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of...

    implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of single digit operands; the operators +, -, *, and / ; and parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. Use the algorithms given in the chapter 6 to evaluate infix expressions as entered into the calculator. You must first convert the infix expression to postfix form and then...

  • Using Java- Write a program that takes an arithmetic expression in an infix form, converts it...

    Using Java- Write a program that takes an arithmetic expression in an infix form, converts it to a postfix form and then evaluates it. Use linked lists for the infix and postfix queues and the operator and value stacks. You must use sperate Stack and Queue classes with a driver class to run the program. example Input : 111++ Output : (1+ (1+ 1)) Answer: 3

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT