Now, solve the postfix expression and return a result. You must now use an opera
ID: 3856334 • Letter: N
Question
Now, solve the postfix expression and return a result. You must now use an operand stack to process the postfix expression. Display this result on the screen as well as the postfix expression as shown above. If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate. The following examples illustrate various types of errors: Enter infix expression: 1 2+ Error in expression!! No operator between operands. Also last token must be an operand. Enter infix expression: 10.4 Error in expression!! Cannot accept floating point numbers. Enter infix expression: 12) Error in expression!! No operator between operand and left parentheses Enter infix expression: 5- (x -2)) Error in expression!! No matching left parentheses for a right parentheses. Enter infix expression: 1** 2 Error in expression!! The * operator cannot be preceded by a *operator.Explanation / Answer
Java Code-
1.Infix to postfix and validation of postfix-
package com.vinja;
import java.util.*;
import java.lang.*;
import java.util.Stack;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
public class main {
// Method for checking parenthesis
public static boolean Parenthesis(String s){
int stack = 0;
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if(c == '(')
++stack;
else if(c == ')'){
--stack;
if(stack < 0)
return false;
}
}
return stack == 0;
}
//Method for checking operand
public static boolean checkOperand(char c) {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
//Method for define precedence
public static int precedence(char c){
if(c == '|')
return 1;
if(c == '.')
return 2;
if(c == '>' || c == '<' || c == '=' || c == '#')
return 3;
if(c == '-' || c == '+')
return 4;
if(c == '*' || c == '/')
return 5;
if(c == '^')
return 6; //maximum
return 0;
}
//method for change Infix to postfix
public static String infixToPost(String s) {
StringBuilder output = new StringBuilder("");
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (checkOperand(c)) {
output.append(c);
continue;
}
if (c == '(') {
stack.add(c);
continue;
}
if (c == ')') {
char ch = stack.pop();
//check if the expression has an empty parenthesis ()
StringBuilder temp = new StringBuilder("");
while (ch != '(') {
temp.append(ch);
ch = stack.pop();
}
if(temp.length() == 0) //empty parenthesis
return ""; //will be invalidated by the postfix checker method
output.append(temp.toString());
continue;
}
//here are only operators
if(stack.empty()){
stack.push(c);
}
else{
while(!stack.empty() && (precedence(stack.peek()) >= precedence(c) ))
output.append(stack.pop());
stack.push(c);
}
}
while(!stack.empty())
output.append(stack.pop());
return output.toString();
}
//method for validation of postfix
public static boolean validatePostfix(String s){
if(s.equals(""))
return false;
int stack = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == '(' || c == ')')
continue;
if(checkOperand(c)){
++stack;
}
else {
stack -= 2;
if(stack < 0)
return false;
++stack;
}
}
return stack == 1;
}
// Method for analysis lexically
public static boolean lexiAnalysis(String s){
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(checkOperand(c) == false && precedence(c) == 0 && c != '(' && c != ')')
return false;
}
return true;
}
//main method to get input from system.in and check
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintStream out = new PrintStream(System.out);
while (true) {
String s = in.readLine();
//check: 1.Lexically 2.parenthesis 3.Infix to postfix 4.validity of postfix
if (lexiAnalysis(s)) {
if(Parenthesis(s)){
String postfix = infixToPost(s);
if(validatePostfix(postfix)){
System.out.println(postfix);
}
else{
out.println("Cannot evaluate postfix or postfix is not validate!");
}
}
else{
out.println("Parenthesis mismatch in expression!");
}
} else {
out.println("Lexical Error means operand and precedence mismatch or not properly used!");
}
}
}
}
2.Evaluation of postfix-
//Method for evaluate postfix expression
public int postfixEval(String e){
int number1;
int number2;
int result=0;
Stack<Integer> s = new Stack<>();
String[] ts = e.split(" ");
for(int j = 0; j < ts.length; j++){
String t = ts[j];
if (!"+".equals(t) && !"*".equals(t) && !"-".equals(t) && !"/".equals(t)) {
s.push(Integer.parseInt(t));
} else {
String Operator = ts[j];
number1 = s.pop();
number2 = s.pop();
if (Operator.equals("/")){
result = number1 / number2;}
else if(Operator.equals("*")){
result = number1 * number2;}
else if(Operator.equals("+")){
result = number1 + number2;}
else if(Operator.equals("-")){
result = number1 - number2;}
else System.out.println("Illeagal symbol");
}
s.push(result);
s.pop();
}
//s.pop();
System.out.println("Postfix Evauation = " + result);
return result;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.