Results 1 to 4 of 4
  1. #1
    alexd is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default calculator using stacks

    hello everyone.I got to make a java code for my class.I must make a calculator using stacks.i have a code but i got some mistakes and i don t know hot to fix them.here is the code
    Java Code:
    package stackscalc;
    
    import java.util.Scanner;
    import java.util.Stack;
    import java.util.EmptyStackException;
    
    
    class Arithmetic
    {
    int length;
    Stack stk;
    String exp,
    postfix;
    
    Arithmetic(String s)
    {
        stk = new Stack();
        exp = s;
        postfix = "";
        length = exp.length();
    
    }
    boolean isBalance()
    {
        boolean fail = false;
        int index = 0;
    
        try
        {
            while (index < length)
            {
                char ch = exp.charAt(index);
    
                switch(ch)
                {
                    case ')':
                        stk.pop();
                    break;
    
                    case '(':
                        stk.push(new Character(ch));
                    break;
    
                    default:
                    break;
                }
                    index++;
            }
        }
        catch(EmptyStackException e)
        {
            fail = true;
        }
            return stk.empty() && !fail;
    }
    void postfixExpression()
    {
        String token = "";
        Scanner scan = new Scanner(exp);
        stk.clear();
    
        while(scan.hasNext())
        {
            token = scan.next();
            char current = token.charAt(0);
    
            if (isNumber(token))
            {
                postfix = postfix + token + " ";
            }
            else if(isParentheses(current))
            {
                if(current == '(')
                {
                    stk.push(current);
                }
                else
                {
                    Character ch = (Character)stk.peek();
                    char nextToken = ch.charValue();
    
                    while(nextToken != '(')
                    {
                        postfix = postfix + stk.pop() + " ";
    
                        ch = (Character)stk.peek();
    
                        nextToken = ch.charValue();
                    }
                        stk.pop();
                }
            }
            else
            {
                if(stk.empty())
                {
                    stk.push(current);
                }
                else
                {
                    Character ch = (Character)stk.peek();
                    char top = ch.charValue();
    
                    if(hasHigherPrecedence(top, current))
                    {
                        stk.push(current);
                    }
                    else
                    {
                        ch = (Character)stk.pop();
    
                        top = ch.charValue();
    
                        stk.push(current);
    
                        stk.push(top);
                    }
                }
            }
        }
        try
        {
            Character ch = (Character)stk.peek();
            char nextToken = ch.charValue();
    
            while(isOperator(nextToken))
            {
                postfix = postfix + stk.pop() + " ";
                ch = (Character)stk.peek();
                nextToken = ch.charValue();
            }
        }catch(EmptyStackException e){}
    }
    boolean isNumber(String s)
    {
        try
        {
            int Num = Integer.parseInt(s);
        }
        catch(NumberFormatException e)
        {
            return false;
        }
            return true;
    }
    void evaluateRPN()
    {
        Scanner scan = new Scanner(postfix);
        String token = "";
        stk.clear();
    
        while(scan.hasNext())
        {
            try
            {
            token = scan.next();
            if (isNumber(token))
            {
                stk.push(token);
            }
            else
            {
                char current = token.charAt(0);
                double t1 = Double.parseDouble(stk.pop().toString());
                double t2 = Double.parseDouble(stk.pop().toString());
                double t3 = 0;
    
                switch(current)
                {
                    case'+':
                    {
                        t3 = t2 + t1;
                        stk.push(t3);
                        break;
                    }
                    case'-':
                    {
                        t3 = t2 - t1;
                        stk.push(t3);
                        break;
                    }
                    case'*':
                    {
                        t3 = t2 * t1;
                        stk.push(t3);
                        break;
                    }
                    case'/':
                    {
                        t3 = t2 / t1;
                        stk.push(t3);
                        break;
                    }
                    default:
                    {
                        System.out.println("Reverse Polish Notation was unable to be preformed.");
                    }
                }
          }
    
            }
    
            catch(EmptyStackException e){}
        }
    }
    String getResult()
    {
        return stk.toString();
    }
    
    int stackSize()
    {
        return stk.size();
    }
    
    boolean isParentheses(char current)
    {
        if((current == '(') || (current == ')'))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    boolean isOperator(char ch)
    {
        if((ch == '-'))
        {
            return true;
        }
        else if((ch == '+'))
        {
            return true;
        }
        else if((ch == '*'))
        {
            return true;
        }
        else if((ch == '/'))
        {
            return true;
        }
        else
        {
    
        }
            return false;
    }
    
    boolean hasHigherPrecedence(char top, char current)
    {
        boolean HigherPre = false;
    
        switch(current)
        {
        case '*':
            HigherPre = true;
        break;
    
        case '/':
            HigherPre = true;
        break;
    
        case '+':
    
            if((top == '*') || (top == '/') || (top == '-'))
            {
                 HigherPre = false;
            }
            else
            {
                 HigherPre = true;
            }
    
        break;
    
        case'-':
            if((top == '*') || (top == '/') || (top == '-'))
            {
                HigherPre = false;
            }
            else
            {
                HigherPre = true;
            }
        break;
    
       default:
                System.out.println("Higher Precedence Unsuccessful was unable to be preformed.");
       break;
        }
    
    return HigherPre;
    }
    
    String getPostfix()
    {
        return postfix;
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default Re: calculator using stacks

    i got some mistakes
    Can you explain what mistakes there are? Post some output from the program that shows what you are talking about and add some comments saying what the output should be.


    How is the code executed for testing? I don't see a main() method.
    Last edited by Norm; 02-05-2014 at 08:45 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: calculator using stacks

    When posting code, please use the code tags and post your code in the form of an SSCCE, not your whole program.

    What errors does your code generate?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,446
    Rep Power
    25

    Default Re: calculator using stacks

    Also posted at: problem with calculator
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Can somebody help me with stacks?
    By sim18 in forum New To Java
    Replies: 3
    Last Post: 10-08-2012, 07:06 PM
  2. stacks
    By nevets93 in forum New To Java
    Replies: 6
    Last Post: 02-25-2011, 03:32 AM
  3. Help with stacks
    By kMel90 in forum New To Java
    Replies: 3
    Last Post: 12-05-2010, 01:02 AM
  4. Help with stacks
    By Srcee in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 11:23 AM
  5. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 09:53 AM

Tags for this Thread

Posting Permissions

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