Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this program, you will write a command line calculator that can evaluate simple mathematical expressions on ints typed in postfix notation (also called reverse

image text in transcribed

image text in transcribed

image text in transcribed

In this program, you will write a command line calculator that can evaluate simple mathematical expressions on ints typed in postfix notation (also called reverse polish notation, or RPN), as well as store variables for later use in other expressions. Postfix notation: In a typical mathematical expression that you are probably used to, such as 5 2, the operator goes in-between the two operands (5 and 2). This is called "infix" notation. Postfix notation places the two operands first, and the sign last, giving 5 2 One of the main benefits of postfix notation is that it is easy to evaluate using a "stack". Parentheses are not needed, nor are precedence rules, since there is only one reasonable way to evaluate the expression. The algorithm to do this uses a stack ofnumbers, and can be stated in a few steps as follows: Given a Postfix expression such as "2 3 x break it into tokens, and initialize an empty stack of numbers. Then do the following steps: 1. Process the tokens from left to right. For each token If the token is an operand (a number or variable), push the value of the operand on the stack (of integers in our case) For an operator etc.), pop the needed number of operands off the stack, compute the result, and push it back on the stack. 2. The expression's value will be on the top of the stack at the end. Error checking is also easy. The following are error conditions: 1. If at any point there are not enough operands for an operation on the stack, the postfix expression is not valid 2. If more than one value is on the stack at the end, the expression is not valid Memory: Your calculator must have a memory that allows it to store or modify named variables. These can easily be stored in a HashMap, or parallel ArrayLists. In this program, you will write a command line calculator that can evaluate simple mathematical expressions on ints typed in postfix notation (also called reverse polish notation, or RPN), as well as store variables for later use in other expressions. Postfix notation: In a typical mathematical expression that you are probably used to, such as 5 2, the operator goes in-between the two operands (5 and 2). This is called "infix" notation. Postfix notation places the two operands first, and the sign last, giving 5 2 One of the main benefits of postfix notation is that it is easy to evaluate using a "stack". Parentheses are not needed, nor are precedence rules, since there is only one reasonable way to evaluate the expression. The algorithm to do this uses a stack ofnumbers, and can be stated in a few steps as follows: Given a Postfix expression such as "2 3 x break it into tokens, and initialize an empty stack of numbers. Then do the following steps: 1. Process the tokens from left to right. For each token If the token is an operand (a number or variable), push the value of the operand on the stack (of integers in our case) For an operator etc.), pop the needed number of operands off the stack, compute the result, and push it back on the stack. 2. The expression's value will be on the top of the stack at the end. Error checking is also easy. The following are error conditions: 1. If at any point there are not enough operands for an operation on the stack, the postfix expression is not valid 2. If more than one value is on the stack at the end, the expression is not valid Memory: Your calculator must have a memory that allows it to store or modify named variables. These can easily be stored in a HashMap, or parallel ArrayLists

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions

Question

1. Outline the listening process and styles of listening

Answered: 1 week ago