Results 1 to 4 of 4
  1. #1
    Jeremy5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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.

    Java 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;
    	}
    	
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    9

    Default 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.

  3. #3
    Jeremy5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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
    Java 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:
    Exception in thread "main" java.util.EmptyStackException
    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.

    Java 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;
    	}
    
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default 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
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. java stack RPN calculator
    By ali1 in forum New To Java
    Replies: 0
    Last Post: 10-30-2011, 10:04 PM
  2. Stack Calculator
    By Bgreen7887 in forum New To Java
    Replies: 3
    Last Post: 01-29-2011, 12:01 AM
  3. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 11:07 AM
  4. stack based program
    By hafizul in forum New To Java
    Replies: 5
    Last Post: 03-28-2009, 04:49 PM
  5. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-20-2008, 12:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •