Question
You will create a calculator in Java that parses an infix expression into postfix, and then evaluates it. You will use ant to build your
You will create a calculator in Java that parses an infix expression into postfix, and then evaluates it. You will use ant to build your program.
The Task:
You will write a program that parses infix expressions (described below) into appropriate Tokens (operator or operand), stored in some linear container (ArrayList ?), passes the infix expression to a function that returns the expression to postfix form, then passes it to a function which evaluates the postfix expression, returns an integer.
We'll be doing only integer arithmetic. No float types. Also, though the inputs are positive, the intermediate results might not be.
The operators you will encounter are +, -, *, /, % (modulus). You must also be prepared to handle parenthesis.
Your code will be documented properly.
All will be done through an ant file.
Your code:
I expect to see, at a minimum, 2 methods:
infix2postfix evalPostfix
I've written Token classes for you (the files are in this directory). You do need to fill in the Operator.getPrec() method, assign appropriate (relative) precedences to the operators.
The 2 types, Operand and Operator share a base class, Token, so that you can store an entire expression, in a generic ArrayList which holds Tokens. We need to get used to generics.
Yes, you need to use my Token classes.
Input:
Your program will read a file called input.infix that contains a number of expressions, one per line.
Each line has, at most, 80 characters. Tokens will be separated by white space. Operands will be strictly non-negative integers. Operators are: { + - * / % } for addition, subtraction, multiplication, division, and modulus, respectively.
Here is a sample input:
13 + 23 - 42 * 2 3 * ( 5 - 2 ) % 5
A sample input file can be found here.
For my part, I promise that all expressions are valid. Output
Your program will output, for each input expression, on one line:
postfix expression = result
, where result is the value of expression.
There will be one expression per line (same as the input). Single-space only, please.
So, given the input, above, I'd expect the output to be:
13 23 + 42 2 * - = -48 3 5 2 - * 5 % = 4
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started