Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im working on a calculator that converts an infix expression to postfix then eva

ID: 659388 • Letter: I

Question

Im working on a calculator that converts an infix expression to postfix then evaluates the expression. So far my program is able to evaluate most expressions aside from those that contain variables. I need the program to be able to recognize variable declaration and then use the associated value in an expression. For example if variable = 200, then variable/2 should result in 100. Im thinking i should use a map to associate the name with its respective value, not sure how to implement it.

evaluator: http://pastebin.com/L3FYSBk6

infix to postfix class: http://pastebin.com/ZsbFpZSP

Explanation / Answer

As you given links where entire code was given by you. I splitted those codes so that they are working fine.

You given links where entire code given.

InfixToPrefix.java

import java.util.*;

public class InfixToPostfix {

private Deque<String> postfix; // Used as a queue of String
private boolean isOperator(char op)
{
if(op=='+'||op=='-'||op=='*'||op=='/'||op=='^'
||op=='('||op==')')
{
return true;
}
return false;
}
private boolean lowerEqualPrec(char op1, char op2)
{
boolean flag = false;
if(op1=='+'|| op1=='-')
{
if(op2=='+'||op2=='-'||op2=='*'||op2=='/'||op2=='^')
{
flag= true;
}
}else if(op1=='*' || op1=='/')
{
if(op2=='*'||op2=='/'||op2=='^')
{
flag= true;
}
}else if(op1=='^')
{
flag= false;
}else if(op1=='(')
{
flag= false;
}
return flag;
}

public void InfixToPostfix(String infix)
{
for(int i=0; i<infix.length(); i++)
{
if(infix.length() ==0 || infix.charAt(0)==')' ||
infix.charAt(i)=='&' || infix.charAt(infix.length()-1)=='(')
{
throw new IllegalArgumentException();
}
}
// Build postfix as a queue representing a postfix expression
postfix = new LinkedList<String>();
Stack<Character> stack = new Stack<Character>();
Character ch;
String digits="";
String letters = "";
  
for(int i=0; i<infix.length(); i++)
{
ch=infix.charAt(i);
if(ch == ' ')
{
//do nothing   
}
//If character is a digit, convert to string, add to queue
if(Character.isDigit(ch))
{
digits = ch+"";
while(i<infix.length()-1 && Character.isDigit(infix.charAt(i+1)))
{

digits=digits+infix.charAt(i+1);
i++;
}
  
postfix.add(digits);
}

//If chracter is a letter, convert to string, add to queue
if(Character.isLetter(infix.charAt(i)))
{
letters=ch+"";
while(i<infix.length()-1 && Character.isLetter(infix.charAt(i+1)))
{
letters = letters + infix.charAt(i+1);
i++;
}
postfix.add(letters);
}

//if Character is an operator, verify precedence, push to stack
if(isOperator(infix.charAt(i)))
{
if(ch == ')')
{
  
if(!stack.isEmpty() && stack.peek() != '(')
{
postfix.add(""+stack.pop());
if(!stack.isEmpty())
{
stack.pop();
}
}
}
  
else
{
//if stack is empty and current operator does not have lower
//or equal precedence than operator on top of the stack
//puch operator onto stack
if(!stack.isEmpty() && !lowerEqualPrec(ch, stack.peek()))
{
stack.push(ch);
}

else
{
while(!stack.isEmpty() && lowerEqualPrec(ch, stack.peek()))
{
char pop = stack.pop();
if(ch!='(')
{
postfix.add(pop+"");
}
}
  
stack.push(ch);   
}
}
}

}
while(!stack.isEmpty()&&stack.peek()!='(')
{
postfix.add(stack.pop()+"");
}
//System.out.println(postfix);
}

/**
*Method Iterator returns an iterator of newly created
*postfix notation
*Method contains no parameters
*@return Iterator of type string
*/
public Iterator<String> iterator()
{
return new PostfixIterator(postfix) ;
}

public static void main(String[] args)
{
// Simple embedded unit-test for InfixToPostfix
InfixToPostfix test = new InfixToPostfix("12+3*4");
}

}

Evaluator.java

import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.io.IOException;

public class Evaluator
{
private HashMap<String, String> vars;
//private Deque<String> deque;



public BigInteger evaluate(String infix)
{
try
{
Stack<String> evalStack = new Stack<String>();
InfixToPostfix eval = new InfixToPostfix(infix);
Iterator<String> itr = eval.iterator();
BigInteger answer = BigInteger.ZERO;
while(itr.hasNext())
{
String res = itr.next();
if(isInteger(res))
{
evalStack.push(res);
}else if(isOp(res) && evalStack.size()>1)
{
String pop1 = evalStack.pop();
String pop2 = evalStack.pop();
BigInteger num1 = new BigInteger(pop1);
BigInteger num2 = new BigInteger(pop2);
answer = evalOp(res, num1, num2);
evalStack.push(answer.toString());
}

}
return answer;
}
catch (IllegalArgumentException iae)
{
System.out.println("You have enetered an illegal argument");
BigInteger wrong = new BigInteger("null");
return wrong;
}
}

public static boolean isInteger(String num)
{
try
{
Integer.parseInt(num);
return true;
} catch (NumberFormatException nfe)
{
return false;
}
}
public static boolean isOp(String op)
{
boolean flag = false;
if(op == "+" || op =="-"|| op=="*"
|| op == "/" || op=="^" || op == "%");
{
flag = true;;
}
return flag;
}
public static BigInteger evalOp(String op, BigInteger a, BigInteger b)
{
BigInteger operation = BigInteger.valueOf(0);
switch(op)
{
case "+": operation = a.add(b);
break;
case "-": operation = b.subtract(a);
break;
case "*": operation = a.multiply(b);
break;
case "/": operation = b.divide(a);
break;
case "%": operation = b.mod(a);
break;
case "^": operation = b.pow(a.intValue());
break;
}
return operation;
}

}

PostfixIterator.java

class PostfixIterator implements Iterator<String> {

private Deque<String> postfix;

public PostfixIterator(Deque<String> postfix)
{
this.postfix = postfix;
}
public boolean hasNext()
{
if (postfix.size() ==0)
{
return false;
}
return true;
}
  
// Method returns next value in postfix while postfix is not empty
public String next()
{
  
while(postfix.size()!=0)
{
return postfix.remove();
}
return null;
}
  

}