I made a postfix calculator in Java.
The code performs the functions I planned without problems, However, I'm not satisfied with it, because it's too verbose and I couldn't split it up into methods properly, especially PostfixCalculator.changeNotation().
Any help would be greatly appreciated.
Main.java
public class Main
{
public static void main(String[] args)
{
PostfixCalculator postCalc = new PostfixCalculator();
}
}
Stack.java (I didn't use the library because I'm studying data structures.)
import java.util.ArrayList;
import java.util.Scanner;
public class Stack
{
Scanner scan = null;
public ArrayList<String> stack;
public Stack()
{
scan = new Scanner(System.in);
stack = new ArrayList<String>();
}
public boolean isEmpty()
{
if (stack.size() == 0)
{
return true;
}
else
{
return false;
}
}
public void push(String element)
{
stack.add(element);
}
public void pop()
{
if(stack.isEmpty())
{
return;
}
else
{
stack.remove(stack.size()-1);
}
}
public int size()
{
return stack.size();
}
public String peek()
{
if (stack.isEmpty())
{
return null;
}
else
{
return stack.get(stack.size() - 1);
}
}
}
PostfixCalculator.java
import java.util.ArrayList;
import java.util.Scanner;
public class PostfixCalculator
{
String[] infixNotation = null;
String postfixNotation = "";
ArrayList<String> aListPostfixNotation = null;
Scanner scan = null;
Stack stk = null;
public PostfixCalculator()
{
aListPostfixNotation = new ArrayList<String>();
stk = new Stack();
scan = new Scanner(System.in);
String myInput = scan.nextLine();
infixNotation = myInput.split("",0);
postfixCalculate(changeNotation());
}
public String changeNotation() // Change the entered code from infixnotation to postfixnotation
{
for (int i = 0; i < infixNotation.length; i++)
{
if(infixNotation[i].equals(" "))
{
continue;
}
else if( !infixNotation[i].equals("+") && // when infixNotation[i] is operand
!infixNotation[i].equals("-") &&
!infixNotation[i].equals("*") &&
!infixNotation[i].equals("/") &&
!infixNotation[i].equals("(") &&
!infixNotation[i].equals(")") )
{
postfixNotation = postfixNotation + infixNotation[i];
}
else // when infixNotation[i] is operator or bracket
{
if(stk.isEmpty()) // It can push()ed, when stack is empty // Don't care ")~~~", because the ")" can't be first char
{
stk.push(infixNotation[i]);
}
else
{
if (infixNotation[i].equals("(")) // Just push() when it's '('
{
stk.push(infixNotation[i]);
}
else if (infixNotation[i].equals(")")) // When bracket is balanced (left and right), pop() a pair of bracket
{
stk.push(infixNotation[i]);
whenRightPush();
}
else // Have to the priority, when infixNotation[i] is operator
{
if (stk.peek().equals("("))
{
stk.push(infixNotation[i]);
}
else
{
if ( infixNotation[i].equals("*") || infixNotation[i].equals("/") )
{
if ( stk.peek().equals("+") || stk.peek().equals("-") )
{
stk.push(infixNotation[i]);
}
else if ( stk.peek().equals("*") || stk.peek().equals("/") )
{
postfixNotation = postfixNotation + stk.peek();
stk.pop();
stk.push(infixNotation[i]);
}
}
else if ( infixNotation[i].equals("+") || infixNotation[i].equals("-") )
{
if ( stk.peek().equals("+") || stk.peek().equals("-"))
// Equal level's operators can't enter the stack twice sequentially,
// so they need to be considered only once.
{
postfixNotation = postfixNotation + stk.peek();
stk.pop();
stk.push(infixNotation[i]);
}
else if ( stk.peek().equals("*") || stk.peek().equals("/") )
{
postfixNotation = postfixNotation + stk.peek();
stk.pop();
if ( stk.peek().equals("+") || stk.peek().equals("-") ) // ex + * -
{
postfixNotation = postfixNotation + stk.peek();
stk.pop();
}
stk.push(infixNotation[i]);
}
}
}
}
}
}
if (i == infixNotation.length-1) // All elements is pop()ed, when 'i' have last value
{
while(!stk.isEmpty())
{
if(stk.peek().equals("("))
{
stk.pop();
}
else if (stk.peek().equals(")"))
{
stk.pop();
}
else
{
postfixNotation = postfixNotation + stk.peek();
stk.pop();
}
}
}
}
System.out.println(postfixNotation);
return postfixNotation;
}
public void whenRightPush()
// This method will work when ')' is push()ed // I can't find proper name for this method
{
stk.pop();
while(true)
{
if ( stk.peek().equals("(") )
{
stk.pop();
break;
}
else // 연산자일 경우 후위표기식 문자열에 붙인 후 pop()
{
postfixNotation = postfixNotation + stk.peek();
stk.pop();
}
}
}
public void postfixCalculate(String postNotation) // Calculate the postfixnotation
{
int operatorCount = 0;
int resultTemp = 0;
String[] arrayPostfixNotation = postNotation.split("", 0);
for (String str : arrayPostfixNotation)
{
aListPostfixNotation.add(str);
}
for (String str : aListPostfixNotation)
{
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/"))
{
operatorCount++;
}
}
while(operatorCount > 0) {
for(int i = 0; i < aListPostfixNotation.size(); i++)
{
if (aListPostfixNotation.get(i).equals("+") ||
aListPostfixNotation.get(i).equals("-") ||
aListPostfixNotation.get(i).equals("*") ||
aListPostfixNotation.get(i).equals("/"))
{
if(aListPostfixNotation.get(i).equals("+"))
{
resultTemp = Integer.parseInt(aListPostfixNotation.get(i-2))
+ Integer.parseInt(aListPostfixNotation.get(i-1));
}
else if(aListPostfixNotation.get(i).equals("-"))
{
resultTemp = Integer.parseInt(aListPostfixNotation.get(i-2))
- Integer.parseInt(aListPostfixNotation.get(i-1));
}
else if(aListPostfixNotation.get(i).equals("*"))
{
resultTemp = Integer.parseInt(aListPostfixNotation.get(i-2))
* Integer.parseInt(aListPostfixNotation.get(i-1));
}
else
{
resultTemp = Integer.parseInt(aListPostfixNotation.get(i-2))
/ Integer.parseInt(aListPostfixNotation.get(i-1));
}
aListPostfixNotation.remove(i-2); // Remove the used operator and operand
aListPostfixNotation.remove(i-2);
aListPostfixNotation.remove(i-2);
aListPostfixNotation.add(i-2, Integer.toString(resultTemp)); // Add the result of operation into the arraylist
operatorCount--;
break;
}
}
}
System.out.println(resultTemp);
}
}
```