Results 1 to 2 of 2
- 01-30-2012, 01:39 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
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.
- 01-30-2012, 01:57 AM #2
Similar Threads
-
Newbie here! Can i have a java program GUI or Console in Order System
By utakberto in forum AWT / SwingReplies: 5Last Post: 02-05-2011, 02:52 PM -
Connecting to linux web server and performing command line operations with Java
By Jojomofoman in forum New To JavaReplies: 3Last Post: 01-11-2011, 11:45 PM -
Java Run Order
By collin389 in forum New To JavaReplies: 15Last Post: 11-24-2009, 07:22 AM -
HashMapTest.java uses unchecked or unsafe operations.
By jeer in forum New To JavaReplies: 22Last Post: 05-08-2008, 12:00 PM -
adding a variable in order to a list
By Jrr in forum New To JavaReplies: 2Last Post: 11-19-2007, 01:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks