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…
3 What you need to do
For the first part of this assignment, think about what classes you need. Java has a Stack class for you, but write your own ( do nt use the Java Stack class). Use encapsulation, think about what methods it should have, and call it something like CalculatorStack. Add an option to ”roll” the stack; shift it left or right by one (and the end number rollls) to the other end). Other classes may include Controller, Handler, or SpecialOperationsHandler.
4 Why?
• It’s one of the better assignments I can think of as a teaching tool
• RPN calculators are awesome - they’re far more logical than infix calculators once you get used to them
• You should learn RPN calculators; besides the historical importance, it forces you to think differently. Several example done during class lectures.
The following code below is a sample on how to evaluate the RPN expression …
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class InfixToPostfix {
Stack<Character> stack;
String infix;
private String postfix = "";
public InfixToPostfix() {
stack = new Stack();
}
public String Convert() {
for (int j = 0; j <
infix.length(); j++) {
char ch =
infix.charAt(j);
switch (ch)
{
case '+':
case '-':
Precdence(ch, 1);
break;
case '*':
case '/':
Precdence(ch, 2);
break;
case '(':
stack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
postfix = postfix + ch;
break;
}
}
while (!stack.isEmpty()) {
postfix =
postfix + stack.pop();
}
return postfix;
}
public void Precdence(char opThis, int prec1)
{
while (!stack.isEmpty()) {
char opTop =
stack.pop();
if (opTop ==
'(') {
stack.push(opTop);
break;
} else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
stack.push(opTop);
break;
} else
postfix = postfix +
opTop;
}
}
stack.push(opThis);
}
public void gotParen(char ch) {
while (!stack.isEmpty()) {
char chx =
stack.pop();
if (chx ==
'(')
break;
else
postfix = postfix + chx;
}
}
static String checkInvalid(String str)
{
char ch[] =
str.toCharArray();
Stack<Character> st = new
Stack<Character>();
for(int i=0;i<ch.length;i++)
{
if ( ch[i] == '(')
st.push(ch[i]);
if ( ch[i] == ')')
{
if (st.isEmpty())
{
return "There is no matching open parenthesis.";
}
st.pop();
}
}
if (!st.isEmpty())
return "There is no matching close parenthesis.";
return null;
}
public static int calculation(String s) {
//Stack<Integer> postfix =
new Stack<Integer>();
Stack<Integer> postfix = new
Stack<Integer>();
for (int i = 0; i < s.length();
i++) {
char c =
s.charAt(i);
if
(Character.isDigit(c))
postfix.push((c - '0'));
else {
int val1 = postfix.peek();
postfix.pop();
int val2 = postfix.peek();
postfix.pop();
switch (c) {
case '+':
postfix.push(val2 +
val1);
break;
case '-':
postfix.push(val2 -
val1);
break;
case '/':
postfix.push(val2 /
val1);
break;
case '*':
postfix.push(val2 *
val1);
break;
}
}
}
return postfix.peek();
}
public static void main(String[] args) throws
IOException {
Scanner sc = new
Scanner(System.in);
System.out.println("Enter Infix
Expression:");
String input = sc.nextLine();
String ret =
checkInvalid(input);
if(ret!=null){
System.out.println("Invalid Expression: ");
System.exit(0);
}
String output;
InfixToPostfix infixToPostfix = new
InfixToPostfix();
infixToPostfix.infix = input;
output =
infixToPostfix.Convert();
System.out.println("Postfix
expression is: " + output + '\n');
System.out.println("Evaluate: " +
calculation(output));
}
}
==============================================
See OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
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...
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...
(In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I have already created the postfix class that converts the expression from infix to postfix. Here's the postFix class and its stack class (https://paste.ofcode.org/bkwPyCMEBASXQL4pR2ym43) ---> PostFix class (https://paste.ofcode.org/WsEHHugXB38aziWRrp829n)--------> Stack class Your calculator should have: Text field for the postfix expression Text field for the result Two buttons: Evaluate and Clear You can start with the code written below and improvise to work with this...
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....
Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...
For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...
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...
This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...
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...
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...
Description: \\ implement in Java Based on Chapter 6, programming project 1 Hewlett-Packard has a tradition of creating stack-based calculators. Rather than using standard algebraic notation ( 1 + 1 = ), the user would enter the values, then the operator. The calculator had an “enter” key to push each value onto a stack, then pop the stack when an operation key (e.g. “+” or “-”) was pressed. Assignment: Create a Calculator class that implements the provided StackCalculator interface Requirement Constructor: + Calculator() ...