Results 1 to 3 of 3
  1. #1
    Belbis's Avatar
    Belbis is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default String to Expression? calculator app question

    I am trying to write a "simple" gui calculator program using BreezyGUI and I have run into some difficulty...I run into a problem when I want to evaluate the string expression that my switch statement creates..I've tried to find some sort of conversion method so far have been unsuccessful...So I guess my questions are:
    Do I need to write (or better is it possible to write) a method that converts a string expression such as "1+2/3" into something that can be evaluated?
    Or would it be better to scrap it and find a whole new algorithm?

    Here is my code that I have so far..

    Java Code:
    /*My first Calculator app with java*/
    import java.io.*;
    import java.awt.*;
    import BreezyGUI.*;
    
    public class SimpleCalculator extends GBFrame {
    
        DoubleField displayField = addDoubleField(0,2,3,2,1);
    
        Button cButton = addButton("C",2,1,1,1);
        Button dotButton = addButton(".",6,2,1,1);
        Button oneButton = addButton("1",5,1,1,1);
        Button twoButton = addButton("2",5,2,1,1);
        Button threeButton = addButton("3",5,3,1,1);
        Button fourButton = addButton("4",4,1,1,1);
        Button fiveButton = addButton("5",4,2,1,1);
        Button sixButton = addButton("6",4,3,1,1);
        Button sevenButton = addButton("7",3,1,1,1);
        Button eightButton = addButton("8",3,2,1,1);
        Button nineButton = addButton("9",3,3,1,1);
        Button zeroButton = addButton("0",6,1,1,1);
        Button plusButton = addButton("+",4,4,1,1);
        Button minusButton = addButton("-",5,4,1,1);
        Button timesButton = addButton("*",3,4,1,1);
        Button divideButton = addButton("/",6,4,1,1);
        Button modButton = addButton("%",2,2,1,1);
        Button equalsButton = addButton("=",6,3,1,1);
    
    
        public void buttonClicked(Button buttonObject) {
    	String evalString = "";
    	while (buttonObject != cButton)
    	switch (buttonObject) {
    	case oneButton: { 
    	    evalString = evalString + "1";
    	    displayField.setNumber(1);
    	}
    	case twoButton: { 
    	    evalString = evalString + "2";
    	     displayField.setNumber(2);
    	}
    	case threeButton: { 
    	    evalString = evalString + "3";
    	     displayField.setNumber(3);
    	}
    	case fourButton: { 
    	    evalString = evalString + "4";
    	    displayField.setNumber(4);
    	}
    	case fiveButton: { 
    	    evalString = evalString + "5";
    	     displayField.setNumber(5);
    	}
    	case sixButton: { 
    	    evalString = evalString + "6";
    	    displayField.setNumber(6);
    	}
    	case sevenButton: { 
    	    evalString = evalString + "7";
    	    displayField.setNumber(7);
    	}
    	case eightButton: { 
    	    evalString = evalString + "8";
    	    displayField.setNumber(8);
    	}
    	case nineButton: { 
    	    evalString = evalString + "9";
    	    displayField.setNumber(9);
    	}
    	case zeroButton: { 
    	    evalString = evalString + "0";
    	    displayField.setNumber(0);
    	}
    	case dotButton: { evalString = evalString + ".";
    	}
    	case plusButton: { evalString = evalString + "+";
    	}
    	case minusButton: { evalString = evalString + "-";
    	}
    	case timesButton: { evalString = evalString + "*";
    	}
    	case divideButton: { evalString = evalString + "/";
    	}
    	case equalsButton: { /*Evaluate string expression somehow;
    			       displayField.seeNumber(Evaluated expression);
    			       break;*/
    	}
    	}
    	if (buttonObject == cButton) {
    	    evalString = "";
    	    displayField.setNumber(0);
    	}
        }
    
        public static void main(String[] args){
        Frame frm = new SimpleCalculator();
        frm.setSize(200,200);
        frm.setVisible(true);
        }
    }

  2. #2
    PatoDB is offline Member
    Join Date
    Dec 2008
    Posts
    14
    Rep Power
    0

    Default

    If you want to make an algorithm that evaluated one String that contains one Mathematic Expression, its gonna be difficult.

    If for example, you want to get the result of:

    Java Code:
    String function = "2+(3*5)/3"
    You're going to make, in words of one of my teachers, a "Tunning Machine", because there are a lot of precedences, parenthesis, that makes this algorithm very difficult to create.

    Search in Google for "algorithms to evaluate expressions", there is a web of IBM with a great example of one of this.

    I hope you can Understand my English... :-)
    ..:: abretumundo.wordpress.com ::..

  3. #3
    Belbis's Avatar
    Belbis is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    yeah I figured it would be difficult once I tried it that way...thanks for responding. I think I'll try and approach it another way...
    -the google search is helpful though-

Similar Threads

  1. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 AM
  2. question on string tokenizer
    By munigantipraveen in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 05:00 AM
  3. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  4. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 03:27 PM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 11:07 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
  •