# Stack Based Calculator

• 11-05-2011, 02:26 AM
Jeremy5
Stack Based Calculator
I'm attempting to create a program that reads a string full of operations and numbers and prints the result.
So far I only have compatibility with only addition, however that does not seem to hard to add. I'm actually having problems rewriting the string... For example, when I do 5+5+3, I first do 5+5 then 10+3. In order to do this I'm going to have to re-write my string in this way:
5+5+3
10+3
13

So write now, my program does 5+5 then 5+3
returning 10 and 8

So I figure I need to re-write my string somehow... I need to re-write my string to 10+3 then just repeat the process? How would I do this?
All help is appreciated! Thanks.

Code:

```public class Stack_Based_Calculator {         public static String input;         public static void main (String args[]) {                 input = "5+5+3";                 String operation = null;                 //getCharacter(String, letternumber)                 for(int i = 1; i <= input.length(); i++) {                 //System.out.println(getCharacter(input,i));                                         if(getCharacter(i).equalsIgnoreCase("+")) {                         System.out.println(doAddition(i));                         operation = "+";                                 }                 }                         }         private static int doAddition(int i) {                 int result = 0;                 int firstnumber = Integer.parseInt(getCharacter(i-1));                 int secondnumber = Integer.parseInt(getCharacter(i+1));                 result = firstnumber + secondnumber;                 return result;         }         private static String getCharacter(int i) {                 String letter = input.substring(i-1,i);                 return letter;         }         }```
• 11-05-2011, 02:47 AM
sunde887
Re: Stack Based Calculator
Consider for a moment why this is called a stack based calculator. Perhaps a stack type data structure will be helpful. You can pop data off until you get a valid string (two operands and an operator), then push the result back onto the stack.
• 11-05-2011, 05:15 AM
Jeremy5
Re: Stack Based Calculator
Thank you for the response, I found it very useful. I decided to take a step back so I don't get to ahead of myself. I'm going to start with 2 additives only. Then attempt to solve the problem using stacks. I'm having trouble checking to see if the current item we're on is not a number (i.e: "+") but i'm having some trouble with the
Code:

`if stack.peek is "+"`
I seem to be getting some errors. Thanks for all your help so far and thanks in advance for further explanation.

Errors:
at java.util.Stack.peek(Unknown Source)
at Stack_Based_Calculator.main(Stack_Based_Calculator .java:19) <--- The if statement I was previously talking about.

Code:

```import java.util.Stack; public class Stack_Based_Calculator {         public static String input;         public static void main (String args[]) {                 Stack stack = new Stack();                 input = "5+3";                 int total = 0;                 for(int i = 1; i <= input.length(); i++) {                         stack.push (new String(getCharacter(i)));                 }                 if(stack.contains("+") == true) {                         for(int i = 1; i <= stack.capacity(); i++) {                                 if (stack.peek().toString().equalsIgnoreCase("+")) {                                         stack.pop();                                 }                                 else                                 {                                         total = total + Integer.parseInt((String) stack.pop());                                 }                         }                 }                 System.out.println(total);         }         private static String getCharacter(int i) {                 String letter = input.substring(i-1,i);                 return letter;         } }```
• 11-05-2011, 08:57 AM
JosAH
Re: Stack Based Calculator
One of my blog entries talks about compilation and interpretation of expressions; I think it's usable here.

kind regards,

Jos