Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Implementing order of operations for a Java calculator.

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Implementing order of operations for a Java calculator.

    How would I go about implementing order of operations for a calculator program that takes input from its GUI and stores it for further processing in the form of Strings?

    ETA: I accidentally hit the "Submit thread" button; I will be posting code shortly.


  2. #2
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Implementing order of operations for a Java calculator.

    I already have some code that implements order of operations without algebraic grouping (i.e., parentheses/bracket):

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Stack;
    import javax.swing.JTextField;
    import java.lang.*;
     
    public class ArithmeticListener implements ActionListener
    {
        boolean optPsh = false;
        boolean pntPsh = false;
        double fOpd;
        double sOpd;
        double res;
        String ariStr = "";
        String digStr = "";
        String pntStr = "";
        String opdStr = "";
        String curOpdStr = "";
        String prvOpdStr = "";
        String curOptStr = "";
        String prvOptStr = "";
        Stack<String> expStck = new Stack<String> ();
        Stack<String> opdStck = new Stack<String> ();
        Stack<String> optStck = new Stack<String> ();
        JTextField scrn;
     
        ArithmeticListener(CalculatorGUI calGUI)
        {
            scrn = calGUI.cs.screen;
        }
     
        public void actionPerformed(ActionEvent ae)
        {
            ariStr = ae.getActionCommand();
            opdStr = scrn.getText();
     
            /*Operand entry omitted for brevity*/
     
            if (ariStr.equals("+") || ariStr.equals("-"))
            {
                curOptStr = ariStr;
                curOpdStr = scrn.getText();
     
                optPsh = true;
                pntPsh = false;
     
                if (opdStck.empty() && optStck.empty())
                {
                    opdStck.push(curOpdStr);
                    optStck.push(curOptStr);
                }
                else
                {
                    if (opdStck.size() == 1 && optStck.size() == 1)
                    {
                        prvOptStr = optStck.pop();
                        prvOpdStr = opdStck.pop();
     
                        sOpd = Double.parseDouble(curOpdStr);
                        fOpd = Double.parseDouble(prvOpdStr);
     
                        if (prvOptStr.equals("+"))
                        {
                            res = fOpd + sOpd;
                        }
                        else if (prvOptStr.equals("-"))
                        {
                            res = fOpd - sOpd;
                        }
                        else if (prvOptStr.equals("*"))
                        {
                            res = fOpd * sOpd;
                        }
                        else if (prvOptStr.equals("/"))
                        {
                            res = fOpd / sOpd;
                        }                                    
                    opdStck.push(res + "");
                    optStck.push(curOptStr);
                    scrn.setText(res + "");
                    }
                    else
                    {
                        while (opdStck.size() > 0 && optStck.size() > 0)
                        {
                            prvOptStr = optStck.pop();
                            prvOpdStr = opdStck.pop();
     
                            sOpd = Double.parseDouble(curOpdStr);
                            fOpd = Double.parseDouble(prvOpdStr);
     
                            if (prvOptStr.equals("+"))
                            {
                                res = fOpd + sOpd;
                            }
                            else if (prvOptStr.equals("-"))
                            {
                                res = fOpd - sOpd;
                            }
                            else if (prvOptStr.equals("*"))
                            {
                                res = fOpd * sOpd;
                            }
                            else if (prvOptStr.equals("/"))
                            {
                                res = fOpd / sOpd;
                            }
                            curOpdStr = (res + "");
     
                            for (String s : opdStck)
                            {
                                System.out.print(s);
                            }
                            System.out.println();
                            for (String s : optStck)
                            {
                                System.out.print(s);
                            }
                            System.out.println();
                            System.out.println();
     
                        }
                    opdStck.push(res + "");
                    optStck.push(curOptStr);
                    scrn.setText(res + "");
                    }
                }
            }
            else if (ariStr.equals("*")|| ariStr.equals("/"))
            {
                curOptStr = ariStr;
                curOpdStr = scrn.getText();
     
                optPsh = true;
                pntPsh = false;
     
                if (opdStck.empty() && optStck.empty())
                {
                    opdStck.push(curOpdStr);
                    optStck.push(curOptStr);
                }
                else
                {
                    if (opdStck.size() == 1 && optStck.size() == 1)
                    {
                        prvOptStr = optStck.pop();
                        prvOpdStr = opdStck.pop();
     
                        if (prvOptStr.equals("+") || prvOptStr.equals("-"))
                        {
                            opdStck.push(prvOpdStr);
                            optStck.push(prvOptStr);
                            opdStck.push(curOpdStr);
                            optStck.push(curOptStr);
                        }
                        else if (prvOptStr.equals("*") || prvOptStr.equals("/"))
                        {
                            sOpd = Double.parseDouble(curOpdStr);
                            fOpd = Double.parseDouble(prvOpdStr);
     
                            if (prvOptStr.equals("*"))
                            {
                                res = fOpd * sOpd;
                            }
                            else if (prvOptStr.equals("/"))
                            {
                                res = fOpd / sOpd;
                            }
                            opdStck.push(res + "");
                        }                    
                    }
                    else
                    {
                        opdStck.push(curOpdStr);
                        optStck.push(curOptStr);
                    }
                }
            }
            else if (ariStr.equals("="))
            {
                        while (opdStck.size() > 0 && optStck.size() > 0)
                        {
                            prvOptStr = optStck.pop();
                            prvOpdStr = opdStck.pop();
     
                            sOpd = Double.parseDouble(curOpdStr);
                            fOpd = Double.parseDouble(prvOpdStr);
     
                            if (prvOptStr.equals("+"))
                            {
                                res = fOpd + sOpd;
                            }
                            else if (prvOptStr.equals("-"))
                            {
                                res = fOpd - sOpd;
                            }
                            else if (prvOptStr.equals("*"))
                            {
                                res = fOpd * sOpd;
                            }
                            else if (prvOptStr.equals("/"))
                            {
                                res = fOpd / sOpd;
                            }
                            curOpdStr = (res + "");
                        }
                    opdStck.push(res + "");
                    optStck.push(curOptStr);
                    scrn.setText(res + "");
     
            }
            else if (ariStr.equals("C"))
            {
                while (!opdStck.empty() && !optStck.empty())
                {
                    optStck.pop();
                    opdStck.pop();
                }
     
                optPsh = false;
                pntPsh = false;
                fOpd = 0;
                sOpd = 0;
                res = 0;
                ariStr = "";
                digStr = "";
                opdStr = "";
                curOpdStr = "";
                prvOpdStr = "";
                curOptStr = "";
                prvOptStr = "";
     
                scrn.setText("0");
            }
        }
    }

    Now, the way I see things, I have two options:

    1. Add a parenthesis Stack to the current method
    2. Get rid of the operator Stack and the operand Stack and just have an expression Stack

    The latter option seems to be more straightforward because I don't have to come up with a way to "sync" up to the Stacks in order to keep track of where the parentheses are entered.

    The problem that I am currently having, though, is that, when I enter an open parenthesis, the previous operand is pushed onto the Stack as well. I think that this is happening because I have misplaced a call to the screen's getText() method and the operand text on the screen is getting pushed onto the the Stack
    with the open parenthesis. However, I cannot justify this thought when I look at the code:

    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
     
    public class Dummy implements ActionListener
    {
        boolean optPsh = false;
        boolean pntPsh = false;
        int numOpnPar;
        double fOpd;
        double sOpd;
        double res;
        String ariStr = "";
        String digStr = "";
        String pntStr = "";
        String opdStr = "";
        String curOpdStr = "";
        String prvOpdStr = "";
        String curOptStr = "";
        String prvOptStr = "";
        Stack<String> expStck = new Stack<String> ();
        Stack<String> opdStck = new Stack<String> ();
        Stack<String> optStck = new Stack<String> ();
        JTextField scrn;
     
        Dummy(CalculatorGUI cg)
        {
            scrn = cg.cs.screen;
        }
     
        public void actionPerformed(ActionEvent ae)
        {
            ariStr = ae.getActionCommand();
            char [] ariChrArr = ariStr.toCharArray();
            opdStr = scrn.getText();
            Character ariChr = ariChrArr[0];
     
            System.out.println("ariChr: " + ariChr);
     
            if (!ariStr.equals("C") && !ariStr.equals("CE"))
            {
                if (isOpd(ariChr))
                {
                	System.out.println("Operand Character");
                    if(!optPsh)
                    {
                    	if (!pntPsh)
                    	{
                    		if (opdStr.equals("0"))
                    		{
                    	        if (Character.isDigit(ariChr))
                    	        {
                    	        	opdStr = ariStr;
                    	        }
                    	        else if (ariStr.equals("."))
                    	        {
                    	        	opdStr = "0" + ariStr;
                    	        	pntPsh = true;
                    	        }
                    		}
                	        else
                    		{
                    	        if (Character.isDigit(ariChr))
                    	        {
                    	        	opdStr += ariStr;
                    	        }
                    	        else if (ariStr.equals("."))
                    	        {
                    	        	opdStr += ariStr;
                    	        	pntPsh = true;
                    	        }
                    		}
                    	}
                    	else
                    	{
                	        if (Character.isDigit(ariChr))
                	        {
                	        	opdStr += ariStr;
                	        }
                	        else if (ariStr.equals("."))
                	        {
                	        	opdStr += ariStr;
                	        	pntPsh = true;
                	        }
                    	}
                    }
                    else
                    {
                    	if (!pntPsh)
                    	{
                    		if (opdStr.equals("0"))
                    		{
                    	        if (Character.isDigit(ariChr))
                    	        {
                    	        	opdStr = ariStr;
                    	        }
                    	        else if (ariStr.equals("."))
                    	        {
                    	        	opdStr = "0" + ariStr;
                    	        	pntPsh = true;
                    	        }
                    		}
                	        else
                    		{
                    	        if (Character.isDigit(ariChr))
                    	        {
                    	        	opdStr = ariStr;
                    	        }
                    	        else if (ariStr.equals("."))
                    	        {
                    	        	opdStr = "0" + ariStr;
                    	        	pntPsh = true;
                    	        }
                    		}
                    	}
                    	optPsh = false;
                    }
                    scrn.setText(opdStr);
                    ariStr = "";
                }
                else
                {
                	curOptStr = ariChr.toString();
     
                	optPsh = true;
     
                	if (curOptStr.equals("("))
                	{
                		expStck.push(curOptStr);
                	}
                	if (curOptStr.equals(")"))
                	{
                		expStck.push(curOptStr);
                	}
                	else
                	{
                    	curOpdStr = scrn.getText();
                		expStck.push(curOpdStr);
                		expStck.push(curOptStr);
                	}
                	System.out.print("expStck: ");
                	prntExprStck(expStck);
     
                	curOpdStr = "";
                	curOptStr = "";
                }
            }
            else if (ariStr.equals("C"))
            {
                while (!expStck.empty())
                {
                	expStck.pop();
                }
                scrn.setText("0");
                optPsh = false;
                pntPsh = false;
                fOpd = 0;
                sOpd = 0;
                res = 0;
                ariStr = "";
                digStr = "";
                pntStr = "";
                opdStr = "";
                curOpdStr = "";
                prvOpdStr = "";
                curOptStr = "";
                prvOptStr = "";
            }
            else if (ariStr.equals("CE"))
            {
                scrn.setText("0");
            }
        }
     
        boolean isOpd(Character chr)
        {
        	if (Character.isDigit(chr) || chr == '.')
        	{
        		return true;
        	}
        	else
        	{
        		return false;
        	}
        }
    }

    Am I completely missing something else I have not mentioned?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Implementing order of operations for a Java calculator.

    Since a lot of your code is missing, the only thing I can suggest is to add some print statements to your code to display variables as they change and to trace execution flow.

Similar Threads

  1. Help With Java Homework: Set Operations
    By kilroyjr in forum Java Theory & Questions
    Replies: 10
    Last Post: April 1st, 2011, 09:41 AM
  2. Java Calculator Square Root Function
    By laser1992 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 3rd, 2011, 09:34 AM
  3. Java Calculator
    By helloworld922 in forum Algorithms & Recursion
    Replies: 7
    Last Post: January 10th, 2011, 06:01 AM
  4. Replies: 4
    Last Post: November 14th, 2010, 11:44 AM
  5. Implementing HTML tags in Java Source Code
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2010, 09:29 PM