Infix for postfix not working as expected

I have this homework in Java where I need to convert an infix string without parentheses to a postfix string. I have been doing code since two days, but I could not catch the error. Here is my code.

public class itp { String exp, post; double res; int l; stack st; public itp(String s) { exp = s; post = ""; l = exp.length(); st = new stack(l); conv(); calc(); System.out.println("The postfix notation of "+exp+" is "+post); System.out.println("The result of "+exp+" is "+res); } public void conv() { char ch = ' '; char pre = ' '; for(int i =0;i<l;i++) { ch = exp.charAt(i); if("+-*/".indexOf(ch)==-1)post = post + ch; else { pre = st.pop(); if(val(ch)>=val(pre)) { st.push(pre); st.push(ch); } else { while((val(ch)<=val(pre))&&(pre!='$')) { post = post + pre; pre = st.pop(); } st.push(ch); } } } for(pre = st.pop();pre!='$';pre = st.pop()) { post = post + pre; } } public void calc() { res = 0.0; } public int val(char c) { switch(c) { case '$' : return 0; case '+' : return 1; case '-' : return 2; case '*' : return 3; case '/' : return 4; default : return -1; } } } 

The variables here are:

  • st is a character stack
  • ch - current character
  • pre is the topmost char on the stack
  • exp is the input expression of infix
  • post is an expression of postfix output

The pop () methods work as expected, except when the stack is empty, where it will return $ . The val () function takes a char input and returns 4 for / , 3 for * . 2 for - . 1 for + . The integer l contains the length exp .

It works well in most cases, except when I give it a string like a*bb*c+c*dd*e , where it outputs ab*bc*-cd*de*- , which is the expected output without end + in the end.

Any advice would be highly appreciated. This mistake makes me crazy!

Here is the whole code:

 public class itp { String exp, post; double res; int l; stack st; public itp(String s) { exp = s; post = ""; l = exp.length(); st = new stack(l); conv(); calc(); System.out.println("The postfix notation of "+exp+" is "+post); System.out.println("The result of "+exp+" is "+res); } public void conv() { char ch = ' '; char pre = ' '; for(int i =0;i<l;i++) { ch = exp.charAt(i); if("+-*/".indexOf(ch)==-1)post = post + ch; else { pre = st.pop(); if(val(ch)>=val(pre)) { st.push(pre); st.push(ch); } else { while((val(ch)<=val(pre))&&(pre!='$')) { post = post + pre; pre = st.pop(); } st.push(ch); } } } for(pre = st.pop();pre!='$';pre = st.pop()) { post = post + pre; } } public void calc() { res = 0.0; } public int val(char c) { switch(c) { case '$' : return 0; case '+' : return 1; case '-' : return 2; case '*' : return 3; case '/' : return 4; default : return -1; } } } 

here is the stack class:

 public class stack { char[] a; int top,size; public stack(int s) { size = s; a = new char[size]; top = -1; } public void push(char el) { a[++top] = el; } public char pop() { if(empty()) return '$'; else return a[top--]; } public boolean empty() { return (top == -1); } } 

Here is the main class

 import java.util.Scanner; class client { public static void main(String args[]) { System.out.println("Enter the expression"); Scanner in = new Scanner(System.in); itp i = new itp(in.next()); } } 
+6
source share
1 answer

First of all, the post-post a*bb*c+c*dd*e not ab*bc*-cd*de*-+ , but ab*bc*-cd*+de*- .

Secondly, there is an error in your val() function. Instead, it should be:

 case '$' : return 0; case '+' : return 1; case '-' : return 1; case '*' : return 2; case '/' : return 2; default : return -1; 

Change it and check. This will certainly work.

+7
source

Source: https://habr.com/ru/post/954691/


All Articles