Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default [SOLVED] Evaluating an Airthmetic Expression

    Hi All,
    I need to write a Java program, which reads and Airthmetic Expression and evaluates it.
    I have done some part of the program, using classes, but got stuck in coding the Evaluation method. But let me admit that I do have some idea of evaluating the inputted expression.
    NOTE: Its assumed that user always enters valid expression and the expression has only *,/,+ and - operators only.
    Evaluation method:

    I am reading a valid arithmetic expression and looking for the first occurrence of * or / sign and then from that index I am taking the string on the left side of the sign and string to right side of the sign.

    Then in Left String I am looking for the lastindexOf( ) function which will give me the last occurrence of the + or - sign(since from the precedence rule Multiplication/Division is first and then + -)

    And on the Right side I am looking for indexOf( ) string function which will give me the index of the first occurrence of the operator + , - , / or * . Based on these two left and right indices I am extracting two operands and one operator(either * or / whichever comes first) and performing the operation between them and will update the string with this operation result.

    I will be doing this till my expression has only + and - operators and then at the end I can evaluate the string.

    But now what I did, is very long and its not even giving the required result.
    I will really appreciate if any one can help me with this.

    Thanks,
    learning_javaNow

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    why not make yourself an example and walk through your algorithm step by step to see where you went wrong? or some pseudocode.

  3. #3
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    why not make yourself an example and walk through your algorithm step by step to see where you went wrong? or some pseudocode.
    Is there any way we can do this, with out using stacks?
    or should we HAVE to use stacks to evaluate the expression?

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you don't need to implement your own stack, but it's one way to solve the problem. there's probably a few good ways to do this. personally, i find recursion to be the simplest.

    btw, a google search would get you this link, which is probably similar (but possibly more advanced) than what you're trying to do: Sample Code to Evaluate an Infix Expression

  5. #5
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Thank you that was helpful.

  6. #6
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    But the thing I am asked to do it without recursions, since its more like How to use different methods in String class.

  7. #7
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Question Help with Evaluatig an Airthmetic expression code

    Can any body help me with this execution error???

    I have written this program, but it shows me an error saying that "} is expected", though I have put all brackets properly.
    this is my code:
    import java.util.Scanner;

    class AirthExp
    {
    private String e;

    AirthExp()
    {
    e = "";
    }

    AirthExp(String s)
    {
    e = s;
    }

    /*public void set(String s)
    {
    e = s;
    }*/

    public void print()
    {
    System.out.println(e.substring(0, e.length()-1));
    }

    public int evaluate()
    {
    int n1=0, n2=0;

    int op1 = -1, op2 = -1;

    for(int i=0; i<e.length(); i++)
    {
    if(e.charAt(i)=='*' ||e.charAt(i) =='/' ||e.charAt(i)=='#')

    {
    String eleft = e.substring(0,i);

    String eright = e.substring(i+1, e.length());

    op1 = eleft.lastIndexOf('+');

    op2 = eright.lastIndexOf('-');

    if(op1 > op2)

    n1 = Integer.parseInt(eleft.substring(op1+1, i));

    else

    n2 = Integer.parseInt(eleft.substring(op2+1, i));
    for(int k = 0; k < eright.length()-1; k++)
    {
    if(eright.charAt(k)== '+' || eright.charAt(k)== '-' ||eright.charAt(k)== '*' || eright.charAt(k)== '/' ||eright.charAt(k)== '#')
    {
    String erightNum = eright.substring(0, k);

    n2 = Integer.parseInt(erightNum);
    }
    }

    if(e.charAt(i)=='*')

    n1 = n1 * n2;

    else if(e.charAt(i) =='/')

    n1 = n1 / n2;
    }

    return n1;
    }
    }


    class AirthExpDemo
    {
    public static void main(String [] args)
    {
    Scanner keyin = new Scanner(System.in);

    String str;

    System.out.println("Please Enter a valid Airthmetic Expression followed by #:");

    str = keyin.next();

    AirthExp expStr = new AirthExp(str);

    expStr.print();

    System.out.println("= ");

    }
    }

Similar Threads

  1. Quick reg. expression help
    By RR_QQ in forum New To Java
    Replies: 4
    Last Post: 02-10-2009, 07:51 PM
  2. illegal start of expression
    By razmyasdfg in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-27-2008, 10:44 PM
  3. Complex Regular Expression HELP
    By hiklior in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 01:52 PM
  4. Airthmetic statement shortcuts
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 10:24 AM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 04:11 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
  •