Results 1 to 4 of 4
Thread: Stack Based Calculator
- 11-05-2011, 01:26 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
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; } }
- 11-05-2011, 01:47 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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, 04:15 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
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
I seem to be getting some errors. Thanks for all your help so far and thanks in advance for further explanation.Java Code:if stack.peek is "+"
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; } }
- 11-05-2011, 07:57 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
java stack RPN calculator
By ali1 in forum New To JavaReplies: 0Last Post: 10-30-2011, 09:04 PM -
Stack Calculator
By Bgreen7887 in forum New To JavaReplies: 3Last Post: 01-28-2011, 11:01 PM -
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 10:07 AM -
stack based program
By hafizul in forum New To JavaReplies: 5Last Post: 03-28-2009, 03:49 PM -
Problem in Calculator implementation using Stack
By realahmed8 in forum New To JavaReplies: 1Last Post: 12-19-2008, 11:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks