Results 1 to 2 of 2
  1. #1
    ratyo8 is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Adding order of operations in Java

    Hi everyone,

    I am trying to write a java program that can be used as a calculator. I want it to be able to evaluate an expression of any length with any number of operands or operators (as long as they're in one line). So far I have made a program that can evaluate expressions correctly, however it can only evaluate an expression with two operands and one operator. The other problem I have is that I want it to be able to use the order of operators correctly (so I need it to be able to evaluate negatives first, followed by * and / and then + and -). So far the program can evaluate expressions with negatives, but it will not evaluate the other operators in order and cannot evaluate more than one. Any help I can get would be appreciated! Here is the code I have so far (it is in two files entitled "LabCalculator.java" and "LabExpression.java"):


    Part one
    public class LabCalculator
    {
    String expstring;
    int cursor;

    public LabCalculator()
    {
    expstring = null;
    }

    public LabCalculator(String instring)
    {
    expstring = instring;
    cursor = 0;
    }

    public void setExpression(String estring)
    {
    expstring = estring;
    cursor = 0;
    }

    public int getResult()
    {
    if (expstring == null)
    {
    System.err.println("There is no expression to evaluate.");
    System.err.println("Call setExpression() before calling getResult().");
    return 0;
    }
    int result = 0;
    cursor = 0;
    int op1 = getOperand();
    char operator = getOperator();
    int op2 = getOperand();
    switch(operator)
    {
    case '+':
    result = op1 + op2;
    break;
    case '-':
    result = op1 - op2;
    break;
    case '*':
    result = op1 * op2;
    break;
    case '/':
    if (op2 == 0)
    {
    System.err.println("Can not divide by 0.");
    return 0;
    }
    else
    {
    result = op1 / op2;
    break;
    }
    }
    return result;
    }

    private int getOperand()
    {
    char nextchar = expstring.charAt(cursor);
    int value = 0;
    int neg=0;
    if (nextchar == '-')
    {
    neg=1;
    cursor++;
    nextchar = expstring.charAt(cursor);
    }

    while (cursor < expstring.length() && Character.isDigit(nextchar))
    {
    value = (value * 10 + Character.digit(nextchar, 10));
    cursor++;
    if (cursor < expstring.length())
    nextchar = expstring.charAt(cursor);

    }

    if (neg==1)
    return -value;
    else
    return value;
    }

    private char getOperator()
    {
    char operator = expstring.charAt(cursor);
    cursor++;
    return operator;
    }
    }

    Part Two
    import java.util.*;

    class LabExpression
    {
    Scanner keyreader;
    LabCalculator calc;

    public LabExpression()
    {
    keyreader = new Scanner(System.in);
    calc = new LabCalculator();
    }

    public void run()
    {
    String expression;
    showMenu();
    expression = getNextExpression();
    while(!expression.equals("quit"))
    {
    calc.setExpression(expression);
    System.out.print(expression + " = ");
    System.out.println(calc.getResult());
    expression = getNextExpression();
    }
    }

    private String getNextExpression()
    {
    System.out.print("? ");
    return keyreader.next();
    }

    private void showMenu()
    {
    System.out.println("Enter a simple arithmetic expression");
    System.out.println("Or enter \"quit\" to terminate the program");
    }

    public static void main(String[] args)
    {
    LabExpression dialog = new LabExpression();
    dialog.run();
    }
    }
    Last edited by ratyo8; 01-30-2012 at 01:45 AM.

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

    Default Re: Adding order of operations in Java

    There are well defined algorithms for parsing expressions. You need to find one and implement it in java.
    I wouldn't try to patch one together. The logic has been worked out.

Similar Threads

  1. Replies: 5
    Last Post: 02-05-2011, 02:52 PM
  2. Replies: 3
    Last Post: 01-11-2011, 11:45 PM
  3. Java Run Order
    By collin389 in forum New To Java
    Replies: 15
    Last Post: 11-24-2009, 07:22 AM
  4. Replies: 22
    Last Post: 05-08-2008, 12:00 PM
  5. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:10 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
  •