I'm working on a calculator and am wondering about parsing functions, such as trigonometric functions, into postfix notation. I currently have order of operations working for multiplication, division, addition, subtraction, and exponentiation. I also parse equations with parentheses correctly. Right now I am only taking integer input. So far this is what I have for my conversion:
I was thinking that I could read functions like I am multi-digit numbers, having a String hold the current operator/function. If a number comes up, see if there is anything in that variable. If there is, push it onto the stack. If a letter comes up add it to the current operator/function variable. This way if I was parsing something like sin(45) it should read in sin correctly and push it onto the stack. Right?
Or I suppose I could store functions as single letters and read those in, such as a=sin b=cos c=tan d=ln and so on.
What do you think?
/** * All this is in a conversion method. * getPrecedence() returns the precedence of an operator * isInteger() returns whether the given token is a number */ ArrayList<String> postfixEquation = new ArrayList<String>(); Stack<String> stack = new Stack<String>(); String curNum = ""; String curToken = ""; int curTokenPrecedence = 0; for(int i = 0; i < input.length(); i++) // The input variable is a String containing the input equation { curToken = input.substring(i, i+1); if(isInteger(curToken)) // If the current token is a number curNum += curToken; // Add it to the curNumber string else // Not a number { if(!curNum.isEmpty()) // If there is a number in curNum { postfixEquation.add(curNum); // Add the current number to the postfix string curNum = ""; // Reset curNum } curTokenPrecedence = getPrecedence(curToken); // Get the current token's precedence // If there is no number on the stack or the token's operator is greater than the first operator on the stack if(stack.empty() || curTokenPrecedence > getPrecedence(stack.peek()) || curToken.equals("(")) stack.push(curToken); else // Current token's precedence less than token on the stack { // Until there is no operator on the stack or an operator with lower precedence while(!stack.empty() && curTokenPrecedence <= getPrecedence(stack.peek())) postfixEquation.add(stack.pop()); // Pop operators off the stack stack.push(curToken); } } if(curToken.equals(")")) { stack.pop(); // Discard the right parenthesis while(!stack.empty()) { if(stack.peek().equals("(")) // Opening parenthesis encountered { stack.pop(); // Discard the opening parenthesis break; } postfixEquation.add(stack.pop()); // Pop operator and put it into the postfix equation } } } if(!curNum.isEmpty()) postfixEquation.add(curNum); // Pop remaining operators off the stack while(!stack.empty()) postfixEquation.add(stack.pop()); for(int i = 0; i < postfixEquation.size(); i++) System.out.println(postfixEquation.get(i));
I was thinking that I could read functions like I am multi-digit numbers, having a String hold the current operator/function. If a number comes up, see if there is anything in that variable. If there is, push it onto the stack. If a letter comes up add it to the current operator/function variable. This way if I was parsing something like sin(45) it should read in sin correctly and push it onto the stack. Right?
Or I suppose I could store functions as single letters and read those in, such as a=sin b=cos c=tan d=ln and so on.
What do you think?