Getting wrong outputs in infix to postfix application with java - java

i recently wrote a java program that takes an infix expression and converts it into a postfix expression. It works for the most part but i am getting wrong outputs for some expressions. For example the expression a+b+c+d+e will output abcde+++++ when it should output
a b + c + d + e +.
import java.util.Stack;
public class ITP {
public static Stack<Character> stack;
public static String inFixExp;
public static String postFixExp = "";
public static String infixToPostfix(String exp){
ITP o = new ITP();
stack = new Stack<Character>();
inFixExp = exp;
for (int i = 0; i < inFixExp.length(); i++) {
if (inFixExp.charAt(i) == '(')
stack.push(inFixExp.charAt(i));
else if (inFixExp.charAt(i)==')'){
while (stack.peek()!='('){
postFixExp += stack.pop();
}
stack.pop();
}else if ((inFixExp.charAt(i)=='*')||(inFixExp.charAt(i)=='/')||(inFixExp.charAt(i)=='+')||(inFixExp.charAt(i)=='-')){
while(!stack.isEmpty() && o.getPredence(inFixExp.charAt(i)) < o.getPredence(stack.peek()))
postFixExp += stack.pop();
stack.push(inFixExp.charAt(i));
}else
postFixExp += inFixExp.charAt(i);
}
while(!stack.isEmpty())
postFixExp += stack.pop();
return postFixExp;
}
public int getPredence(Object op) {
if((op.equals("*")) || (op.equals("/")))
return 3;
else if((op.equals("+"))||(op.equals("-")))
return 1;
else
return 0;
}
}
I found out that if i change the < with <= in line 24 it will fix this problem but then i will get an empty stack error and some other expressions will output incorrectly, such as a+b*c which will output ab+c*, when it is supposed to be abc*+.

Your
if ((inFixExp.charAt(i) == '*') || ...
checks charAt() but your getPredence(precedence?) checks for a String, try comparing against a char instead.

Related

Decode String in Java

I am trying to convert this Python Solution in Java. For some reason, my Java Solution is not working. How can this be done correctly?
https://leetcode.com/problems/decode-string/description/
Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4].
The test cases are generated so that the length of the output will never exceed 105.
Example 1:
Input: s = "3[a]2[bc]"
Output: "aaabcbc"
Example 2:
Input: s = "3[a2[c]]"
Output: "accaccacc"
Python Solution:
class Solution:
def decodeString(self, s: str) -> str:
stack = []
for char in s:
if char is not "]":
stack.append(char)
else:
sub_str = ""
while stack[-1] is not "[":
sub_str = stack.pop() + sub_str
stack.pop()
multiplier = ""
while stack and stack[-1].isdigit():
multiplier = stack.pop() + multiplier
stack.append(int(multiplier) * sub_str)
return "".join(stack)
Java Attempt:
class Solution {
public String decodeString(String s) {
Deque<String> list = new ArrayDeque<String>();
String subword = "";
String number = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ']' ) {
list.add(String.valueOf(s.charAt(i)));
}
else {
subword = "";
while (list.size() > 0 && !list.getLast().equals("[") ) {
subword = list.pop() + subword;
}
if (list.size() > 0) list.pop();
number = "";
while (list.size() > 0 && isNumeric(list.getLast())){
number = list.pop() + number;
}
for (int j = 1; (isNumeric(number) && j <= Integer.parseInt(number)); j++) list.add(subword);
}
}
return String.join("", list);
}
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch(NumberFormatException e){
return false;
}
}
}
The reason why your posted code is not working is because the pop() method in python removes the last element by default.
But in Java, the ArrayDeque class's pop() method removes the first element.
In order to emulate the python code with the ArrayDeque, you'll need to use the removeLast() method of the ArrayDeque instance instead.
public class Solution{
public static String decodeString(String s) {
StringBuilder stack = new StringBuilder();
for(char c : s.toCharArray()) {
if(c != ']') {
stack.append(c);
} else {
StringBuilder sub_str = new StringBuilder();
while(stack.charAt(stack.length() - 1) != '[') {
sub_str.insert(0, stack.charAt(stack.length() - 1));
stack.deleteCharAt(stack.length() - 1);
}
stack.deleteCharAt(stack.length() - 1);
StringBuilder multiplier = new StringBuilder();
while(stack.length() > 0 && Character.isDigit(stack.charAt(stack.length() - 1))) {
multiplier.insert(0, stack.charAt(stack.length() - 1));
stack.deleteCharAt(stack.length() - 1);
}
for(int i = 0; i < Integer.parseInt(multiplier.toString()); i++) {
stack.append(sub_str);
}
}
}
return stack.toString();
}
public static void main(String[] args) {
System.out.println( decodeString("3[a2[c]]"));
//Output: "accaccacc"
System.out.println( decodeString("3[a]2[bc]"));
//Output: "aaabcbc"
}
}

Converting infix to postfix and getting an EmptyStackException

I am working on a project to convert infix notation to postfix notation and then evaluate the equation. I established the precedence for each operator. When I use the ConvertToPostfix method I get the Exception. I understand the concept of the reverse polish notation calculator and I am just struggling with doing it with my code. I am new to stack overflow so if there is something that may seem a little confusing just let me know and ill try to edit it.
import java.util.Stack;
public class RPNCalctest {
public static void main( String[] args) throws InvalidInfixEquationException {
String example= "3+4/3*2"; //postfix notation would be 3 4 3 / 2 * +
System.out.println(ConvertToPostfix(example));
// TODO
}
//establish precedence
static int precedence(String c){
switch(c){
case"+":
case"-":
return 1;
case"*":
case"/":
return 2;
case")":
return 3;
case"(":
return 4;
default:
return -1;
}
}
// Precondition: every operator/operand will be separated by at least one space
public static String ConvertToPostfix(String infix) throws InvalidInfixEquationException {
String[] tokens = infix.split(" ");
String result = "";
Stack<String> stack = new Stack<>();
for (int i = 0; i < tokens.length; i++) {
String current = tokens[i];
if (precedence(current) > 0) {
while (!stack.isEmpty() && precedence(stack.peek()) >= precedence(current)) {
result += stack.pop() + " ";
}
stack.push(current);
} else {
result += current + " ";
}
}
for (int i = 0; i <= stack.size(); i++) {
result += stack.pop();
}
return result;
}
}
Exception in thread "main" java.util.EmptyStackException
at java.base/java.util.Stack.peek(Stack.java:101)
at java.base/java.util.Stack.pop(Stack.java:83)
at RPNCalctest.ConvertToPostfix(RPNCalctest.java:50)
at RPNCalctest.main(RPNCalctest.java:7)
Your problem is here. You pop off one more entry than there is.
for (int i = 0; i <= stack.size(); i++) {
result += stack.pop();
}
Consider size=2. You execute the loop for i=0, 1, 2.
I assume that the 'pop' line is line 53 as indicated in the stack trace, so for future reference, that's useful debugging info and you should use it.
It might be clearer if that loop were coded:
while (!stack.isEmpty()) {
result += stack.pop();
}
No need for the extraneous variable 'i'.

Infix to Postfix Notation Parentheses issue

So I am currently working on a project to convert infix to postfix notation and I am almost finished with the project. It correctly converts infix to postfix until I try adding parentheses it ends up incorrectly formatted.
static int precedence(String c){
switch(c){
case"+":
case"-":
return 1;
case"*":
case"/":
return 2;
default:
return -1;
}
}
I believe that this next part the code is good because everything works correctly without parentheses.
// Precondition: every operator/operand will be separated by at least one space
public static String ConvertToPostfix(String infix) throws InvalidInfixEquationException
{
String[] tokens = infix.split("");
String result= " ";
Stack<String> stack= new Stack<>();
for(int i = 0; i<tokens.length; i++){
String current = tokens[i];
if(precedence(current)>0){
while(!stack.isEmpty() && precedence(stack.peek())>= precedence(current)){
result += stack.pop() + " ";
}
stack.push(current);
} else if(!stack.isEmpty()&& precedence(stack.peek())<= precedence(current)){
result += stack.pop() + " ";
}
I believe that the issue I'm having is with the next part of my code involving manipulating the parentheses.
else if(current== ")"){
String s = stack.pop();
while(s !="("){
result += s;
s= stack.pop();
}
} else if(current == "("){
stack.push(current);
}
else {
result += current + " ";
}
}
for(int i=0;i <= stack.size(); i++){
result+= stack.pop() + " ";
}
return result;
}
This is my main method and I have included an example output.
import java.util.Stack;
public class RPNcalc {
public static void main( String[] args) throws InvalidInfixEquationException, InvalidPostfixEquationException {
String result= "(3+4)/3*2"; // output 3 4 + 3 / 2 *
System.out.println(ConvertToPostfix(result));
This is the output that I am getting
( 3 4 ) 3 / 2 * +

Reverse Polish Notation Code Review

I've been trying to solve this question on SPOJ: http://www.spoj.com/problems/ONP/.
I've tried to implement a two Stack solution for the problem stated above. It works fine on my system, but I get a 'Wrong Answer' every time I try to submit the following code to the SPOJ Engine.
import java.io.*;
import java.util.Stack;
import java.util.Scanner;
public class InfixToPostfix {
public static String postfixString(String expression) {
Stack <Character> valueStack = new Stack <Character>();
Stack <Character> operatorStack = new Stack <Character>();
char[] tokens = expression.toCharArray();
for(char c : tokens) {
if(c == '('){
continue;
}
else if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
operatorStack.push(c);
continue;
}
else if(c == ')') {
valueStack.push(operatorStack.peek());
operatorStack.pop();
continue;
}
else {
valueStack.push(c);
continue;
}
}
return valueStack.toString();
}
public static void main (String [] args)throws java.lang.Exception {
String inputString = "";
int n1;
Scanner numberOfTestCases = new Scanner(System.in);
try
{
n1 = numberOfTestCases.nextInt();
StringBuilder[] sbuf = new StringBuilder[n1];
Scanner inputExpression = new Scanner(System.in);
for(int i = 0; i < n1; i++) {
sbuf[i] = new StringBuilder();
if(inputExpression.hasNextLine()) {
inputString = inputExpression.nextLine();
sbuf[i].append(postfixString(inputString).replaceAll("\\[", "").replaceAll("]", "").replaceAll(", ", ""));
}
else {
break;
}
}
for(int i = 0; i < n1; i++) {
System.out.println(sbuf[i].toString());
}
}
catch (Exception e) {
// System.out.println("");
System.out.println(e.getMessage());
// numberOfTestCases.next();
}
System.exit(0);
}
}
I can't figure out where I'm going wrong; I've tried all possible test cases.
P.S.: The problem assumes all inputs to be parenthesized; there's no need to include any code for resolving operator precedence.
return valueStack.toString();
This doesn't return the required format: it returns a comma-separated format bounded by [ and ], which isn't what is specified.
But I'm sure there are other problems.
I don't see anything in the problem statement that corresponds to your PS: on the contrary, it specifically mentions operator precedence.
Are you sure you've 'tried all possible test cases'? Try (1+2)/3 and 1+3/2.
You're peeking something you haven't pushed in the case of ')'.
I don't see why you need two stacks either.
Given the problem statement, what you need is either a recursive-descent expression parser or the Dijkstra Shunting-yard algorithm.

Infix to postfix not working as expected [duplicate]

This question already has answers here:
Handling parenthesis while converting infix expressions to postfix expressions
(2 answers)
Closed 5 years ago.
I have this homework in Java where I have to convert an infix string without parenthesis to a postfix string. I've been tinkering with the code from two days but I haven't been able to catch the bug. Here's 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;
}
}
}
Here, the variables are as follows:
st is a character stack
ch is the current character
pre is the topmost char on the stack
exp is the input infix expression
post is the output postfix expression
The pop() methods works as expected except when the stack is empty, where it will return $. The function val() takes a char input and returns 4 for /, 3 for *. 2 for -. 1 for +. The integer l holds the length of exp.
It works well in most cases except when I give it a string like a*b-b*c+c*d-d*e where it outputs ab*bc*-cd*de*- which is the expected output without a + in the end.
Any advice would be much appreciated. This bug is making me crazy!
Here's the entire 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's 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's 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());
}
}
First of all the post fix of a*b-b*c+c*d-d*e is not ab*bc*-cd*de*-+ but ab*bc*-cd*+de*-.
Secondly the mistake is in your val() function. It should instead be :
case '$' : return 0;
case '+' : return 1;
case '-' : return 1;
case '*' : return 2;
case '/' : return 2;
default : return -1;
Change it and check. It will certainly work.

Categories