Issue with array based implementation of a stack (java) - java

I'm having trouble implementing a stack as an array in java. When I try to utilize the array, I get nullPointerExceptions. I know that the rest of my code works because I'm able to run the program using the linked list implementation of the stack I have created. The code is below:
Edit: The stack trace is
Exception in thread "main" java.lang.NullPointerException
at LispEvaluator.evaluate(LispEvaluator.java:238)
at LispEvaluator.fileLoader(LispEvaluator.java:67)
at Driver.main(Driver.java:16)
Here is the code for the lispEvaluator class
//code starts here
import java.util.*;
import java.io.*;
public class LispEvaluator {
// Current input Lisp expression
private String inputExpr;
private Scanner sc;
private StackList exprStack;
private StackList tempStack;
// default constructor
// set inputExpr to ""
// create stack objects
public LispEvaluator()
{
inputExpr = "";
}
// default constructor
// set inputExpr to inputExpression
// create stack objects
public LispEvaluator(String inputExpression)
{
inputExpr = inputExpression;
}
// set inputExpr to inputExpression
// clear stack objects
public void reset(String inputExpression)
{
inputExpr = inputExpression;
StackList exprStack=new StackList(50);
StackList tempStack=new StackList(50);
}
private boolean checkifNumber() {
return false;
}
public void fileLoader(String mode,String file){
//Validate the user entered mode
if(mode.equals("0")){
exprStack=new StackList(50);
tempStack=new StackList(50);}
else{
ArrayStack exprStack=new ArrayStack(50);
ArrayStack tempStack=new ArrayStack(50);
}
try{
//Try to read the array from file
sc = new Scanner(new File(file));
while (sc.hasNextLine()) {
double result=0;
inputExpr=sc.nextLine();
reset(inputExpr);
result=evaluate();
System.out.println(result);
}
}
catch (FileNotFoundException e){
System.out.println("Cannot find requested input file");
}
catch(IOException io){
System.out.println("General file io error occured. Try again");
}
}
// This function evaluate current operator with its operands
// See complete algorithm in evaluate()
//
// Main Steps:
// Pop operands from expressionStack and push them onto
// tempStack until you find an operator
// Apply the operator to the operands on tempStack
// Push the result into expressionStack
//
private double add() {
double op1 = (Double) tempStack.pop();
double temp=op1;
while (tempStack.size()>0){
double op2 = (Double) tempStack.pop();
temp = temp + op2;}
return temp;
}
private double multiply() {
double op1 = (Double) tempStack.pop();
double temp=op1;
while (tempStack.size()>0){
double op2 = (Double) tempStack.pop();
temp = temp * op2;}
return temp;
}
private double subtract() {
if (tempStack.size() == 1) {
double temp = -1*(Double) tempStack.pop();
return temp;
} else {
double op1 = (Double) tempStack.pop();
double temp=op1;
while (tempStack.size()>0){
double op2 = (Double) tempStack.pop();
temp = temp - op2;}
return temp;
}
}
private double divide() {
double temp;
if (tempStack.size() == 1) {
temp = 1 / (Double) tempStack.pop();
return temp;
}
else if (tempStack.size() >=1) {
double op1 = (Double) tempStack.pop();
temp=op1;
while (tempStack.size()>0){
double op2 = (Double) tempStack.pop();
temp = temp / op2;}
return temp;
}
return 0;
}
private void evaluateCurrentOperation()
{
while( exprStack.top().getClass().getName().equals("java.lang.Double") ) {
tempStack.push( (Double)exprStack.pop() );
}
Character operator = (Character)exprStack.pop();
Double result = null;
switch( operator ) {
case '+':
result = add();
break;
case '*':
result = multiply();
break;
case '-':
result = subtract();
break;
case '/':
result = divide();
break;
}
exprStack.push( result );
}
/**
* This function evaluates Lisp expression in inputExpr
* It return result of the expression
*
* The algorithm:
*
* Step 1 Scan the tokens in the expression string.
* Step 2 If you see an operand, push operand object onto the expressionStack
* Step 3 If you see "(", next token should be an operator
* Step 4 If you see an operator, push operator object onto the expressionStack
* Step 5 If you see ")" // steps in evaluateCurrentOperation() :
* Step 6 Pop operands and push them onto tempStack
* until you find an operator
* Step 7 Apply the operator to the operands on tempStack
* Step 8 Push the result into expressionStack
* Step 9 If you run out of tokens, the value on the top of expressionStack is
* is the result of the expression.
*/
public double evaluate()
{
// only outline is given...
// you need to add statements
// you may delete or modify any statements in this method
// use scanner to tokenize inputExpr
Scanner inputExprScanner = new Scanner(inputExpr);
// Use zero or more white space as delimiter,
// which breaks the string into single character tokens
inputExprScanner = inputExprScanner.useDelimiter("\\s*");
// Step 1: Scan the tokens in the string.
while (inputExprScanner.hasNext())
{
// Step 2: If you see an operand, push operand object onto the expressionStack
if (inputExprScanner.hasNextInt())
{
// This force scanner to grab all of the digits
// Otherwise, it will just get one char
String dataString = inputExprScanner.findInLine("\\d+");
exprStack.push(new Double(dataString));
// more ...
}
else
{
// Get next token, only one char in string token
String aToken = inputExprScanner.next();
char item = aToken.charAt(0);
String nextToken;
char nextItem;
switch (item)
{
// Step 3: If you see "(", next token should be an operator
case '(':
nextToken = inputExprScanner.next();
nextItem = nextToken.charAt(0);
// Step 4: If you see an operator, push operator object onto the expressionStack
if (nextItem == '+') {
exprStack.push(nextItem);
} else if (nextItem == '-') {
exprStack.push(nextItem);
} else if (nextItem == '*') {
exprStack.push(nextItem);
}else if (nextItem == '/'){
exprStack.push(nextItem);
}
else {
exprStack.push(nextItem);
}
break;
// Step 5: If you see ")" // steps 6,7,8 in evaluateCurrentOperation()
case ')':
try {
evaluateCurrentOperation();
} catch (EmptyStackException e) {
break;
}
break;
default: // error
throw new RuntimeException(item + " is not a legal expression operator");
} // end switch
} // end else
} // end while
// Step 9: If you run out of tokens, the value on the top of expressionStack is
// is the result of the expression.
//
// return result
double result = (Double) exprStack.pop();
return result;
}
}
/*
// This static method is used by main() only
private static void evaluateExprt(String s, SimpleLispExpressionEvaluator expr)
{
Double result;
System.out.println("Expression " + s);
expr.reset(s);
result = expr.evaluate();
System.out.printf("Result %.2f\n", result);
System.out.println("-----------------------------");
}
// simple tests
public static void main (String args[])
{
SimpleLispExpressionEvaluator expr= new SimpleLispExpressionEvaluator();
String test1 = "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))";
String test2 = "(+ (- 632) (* 21 3 4) (/ (+ 32) (* 1) (- 21 3 1)))";
String test3 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 1) (- 2 1 )))";
String test4 = "(+ (/2))";
String test5 = "(+ (/2 3 0))";
String test6 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 3) (- 2 1 ))))";
evaluateExprt(test1, expr);
evaluateExprt(test2, expr);
evaluateExprt(test3, expr);
evaluateExprt(test4, expr);
evaluateExprt(test5, expr);
evaluateExprt(test6, expr);
} }
*/
And heres the Array Stack:
public class ArrayStack implements BoundedStack
{
private int top;
private Object stk[];
private int size;
private int capacity;
public ArrayStack(){
top=-1;
capacity=50;
stk=new Object[capacity];
}
public ArrayStack(int cap)
{
top=-1;
size=0;
capacity=cap;
stk=new Object[capacity];
}
public void push(Object item) throws FullStackException
{
if(isFull()){
throw new FullStackException("The stack is full. Cannot push more elements onto the stack");}
else
{
stk[++top]=item;
}
}
public Object pop() throws EmptyStackException
{
if(isEmpty()==true)
{
throw new EmptyStackException("The stack is empty. Cannot pop another element from the stack");
}
else{
Object e=stk[top];
stk [top]=null;
--top;
return e;
}
}
public Object top() throws EmptyStackException{
if(isEmpty()==true){
throw new EmptyStackException("The stack is empty. Thus, it has no top");
}
return stk[top];
}
public int size(){
return (top+1);
}
public boolean isFull(){
if (size()==capacity){
return true;}
return false;
}
public boolean isEmpty(){
if(size()==0){
return true;}
return false;
}
public int capacity(){
return capacity;
}
}

The most obvious reason for this is you have not initialized your array elements before using it.
If you are creating an array like: -
SomeClass[] arr = new SomeClass[5];
Then before using your array, you need to initialize each array elements like this: -
for (int i = 0; i < arr.length; i++) {
arr[i] = new SomeClass();
}

If the line causing the NullPointerException is the line
exprStack.push(nextItem);
then the problem is not in the stack class. The problem is that exprStack is null (probably because it has never been assigned anything before this line).
You're making the same error as the following one several times in your code:
public void reset(String inputExpression) {
inputExpr = inputExpression;
StackList exprStack=new StackList(50);
StackList tempStack=new StackList(50);
}
The two last lines are completely useless: they assign a new StackList to a local variable. So the stack is created, and is immediately eligible to garbage collection, since nothing references it except a local variable that goes out of scope.
Here's another place where it happens:
if (mode.equals("0")){
exprStack=new StackList(50);
tempStack=new StackList(50);
}
else {
ArrayStack exprStack=new ArrayStack(50);
ArrayStack tempStack=new ArrayStack(50);
}

Related

infixToPostfix algorithm but without operation priority

If i have this string infix expression 2*4+3-15/2 and i want as output the postfix expression without considering the priority of the operations like so
2 4 * 3 + 15 - 2 /
What modifications do i need to in this code sample to "remove" that priority. I took this code from geeksforgeeks here https://www.geeksforgeeks.org/stack-set-2-infix-to-postfix/. I find it a little difficult to change to meet what i want. Where should i start? thanks.
the current code gives me this output : 24*3+152/-
private int Prec(String ch)
{
switch (ch)
{
case "+":
case "-":
return 1;
case "*":
case "/":
return 2;
case "^":
return 3;
}
return -1;
}
private boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
private String infixToPostfix(String infixExpression){
// initializing empty String for result
StringBuilder postfixExpression = new StringBuilder(new String(""));
String[] infixExp = infixExpression.split(" ");
// initializing empty stack
Stack<String> stack = new Stack<>();
for (String token : infixExp) {
System.out.println(token+" ");
// If the scanned character is an operand, add it to output.
if (isNumeric(token))
postfixExpression.append(token);
// If the scanned character is an '(', push it to the stack.
else if (token.equals("("))
stack.push(token);
// If the scanned character is an ')', pop and output from the stack
// until an '(' is encountered.
else if (token.equals(")")) {
while (!stack.isEmpty() && !stack.peek().equals("("))
postfixExpression.append(stack.pop());
if (!stack.isEmpty() && !stack.peek().equals("("))
return "Invalid Expression"; // invalid expression
else
stack.pop();
} else // an operator is encountered
{
while (!stack.isEmpty() && Prec(token) <= Prec(stack.peek())) {
if (stack.peek().equals("("))
return "Invalid Expression";
postfixExpression.append(stack.pop());
}
stack.push(token);
}
}
// pop all the operators from the stack
while (!stack.isEmpty()){
if(stack.peek().equals("("))
return "Invalid Expression";
postfixExpression.append(stack.pop());
}
System.out.println(postfixExpression);
return postfixExpression.toString();
}
It seems you just need to reverse the order of each pair formed by an operator and a number. You could do this using regular expressions and replaceAll:
String infix = "2*4+3-15/2";
String postfix = infix.replaceAll("([*+-/])([0-9]+)", " $2 $1");
System.out.println(postfix);
Output:
2 4 * 3 + 15 - 2 /

recursive parsing calculator java

Im trying to create a recursive parsing calculator in java for addition, multiplication and factorial, but I'm struggling on the very first part of just reading through the user input to split the input into numbers and operators. While debugging I tried to see where it was going wrong, and I found that when the "+" was going through the if else statements it just skipped over it. I'm really unsure on what the issue is, I originally tried using tokens, and splitting up into substring, but it wasn't going well then either. Any help would be appreciated. Thank you
package com.company;
import java.util.Scanner;
class Main {
public static void main(String[] param) {
String input = input("Please enter an expression");
int n = input.length()-1;
String[] splitter = input.split("(?<=\\G.)");
split(input, n);
//int result = calculate(input);
//String[] splitter = input.split("(?<=\\G.)");
}
public static String split(String input, int n) {
String[] splitter = input.split("(?<=\\G.)");
System.out.println(splitter[n]);
String symbol = splitter[n];
if (symbol.equals("+")) {
evalADD(n, splitter);
}
if (symbol.equals("*")) {
evalMULT(n, splitter);
}
if (symbol.equals("!")) {
evalFACT(n, splitter);
}
else if (Integer.parseInt(splitter[n]) >= 0 && Integer.parseInt(splitter[n]) <=9)
{
if (n != 0) {
n = n - 1;
split(input, n);
}
}
if (n != 0)
n = n - 1;
split(input, n);
return input;
}
public static int evalADD(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 + arg2;
return result;
}
public static int evalMULT(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 * arg2;
return result;
}
public static int evalFACT(int n, String [] splitter){
int arg1;
int arg2;
int result;
arg1 = Integer.parseInt(splitter[n+1]);
arg2 = Integer.parseInt(splitter[n+2]);
result = arg1 - arg2;
return result;
}
public static String input(String message) {
Scanner scanner = new Scanner(System.in);
System.out.println(message);
return (scanner.nextLine());
}
}
I have noticed that you are using the java.util.Scanner. I wrote a script that should do the task for you by following all of your criteria:
import java.util.Scanner;
class recursiveParsingCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask user to input the expression
System.out.println("Please input the expression");
String userInput = scanner.nextLine();
System.out.println(
"And the final result is: " + recursiveCalculation(userInput, userInput.length() - 1, 0, 0, 0));
scanner.close();
System.exit(0);
}
// Identify the type of character at a specific position
public static char charOfString(String userInput, int i) {
return userInput.charAt(i);
}
/*
* Position must be userInput.length() - 1 initially. currentResults, operand1
* and operand2 are also meant to be initilized with 0.
*/
public static int recursiveCalculation(String userInput, int position, int operand1, int operand2,
int currentResults) {
// If position is zero, just output the operand.
if (position == 0) {
if (Character.isDigit(charOfString(userInput, position))) {
return charOfString(userInput, position) - '0';
} else {
System.out.println("Invalid input.");
}
}
if (position > -1) {
// Check if it is a number or an operator
if (Character.isDigit(charOfString(userInput, position))) {
operand1 = charOfString(userInput, position) - '0'; // First operand
// Check if 2nd char is a number or an operator.
if (Character.isDigit(charOfString(userInput, position - 1))) {
operand2 = charOfString(userInput, position - 1) - '0';
position = position - 1;
}
} else {
// If it is an operator, then proceed to compute the results so far
char operator = charOfString(userInput, position);
// If it is a binary situation
if (operator == '+' || operator == '*') {
currentResults = binaryOperator(operator, operand1, operand2);
operand2 = currentResults;
}
// If it is an unary situation
else if (operator == '!') {
if (currentResults == 0) {
currentResults = operand1;
}
currentResults = unaryOperator(currentResults);
operand2 = currentResults;
} else {
System.out.println("Invalid operator");
return 0; // Return zero by default
}
}
position = position - 1;
}
if (position > -1) {
return recursiveCalculation(userInput, position, operand1, operand2, currentResults);
} else {
return currentResults;
}
}
public static int binaryOperator(char operator, int operand1, int operand2) {
switch (operator) {
case '+':
return operand1 + operand2;
case '*':
return operand1 * operand2;
default:
System.out.println("Invalid binary Operator");
return 0; // Return zero by default
}
}
// Calculate the factorial
public static int unaryOperator(int operand) {
if (operand <= 1)
return 1;
else
return operand * unaryOperator(operand - 1);
}
}
Examples of usage: For a binary operator, input +21, and it will add them for you. With unary, input !3, and it will yield the factorial. Now, you can try any chain of combinations and permutations of numbers with unary and binary operators, and it will calculate the values for you recursively.
For example, consider the input !*3+12: It will add 1 and 2, then multiply it by 3 and finally, it calculates the factorial out of the whole expression, thereby resulting in 362880 as expected.
Why don't you assign the input calculation string to a character array and iterate through the array and match the characters '+', '-','*'?

Constructing a Tree from an arithmetic expression

I'm pretty lost at the moment on how I would go about implementing this Tree, I'm trying to construct a Tree from a string representation of input "(4 + 6) + (2 + 3)". How would I go about making a Tree from two Stacks?
public class Tree {
private Stack opStk = new Stack();
private Stack valStk = new Stack();
private Tree parent = null;
public Tree(String str){
System.out.println((EvaluateExpression(str)));
}
public void doOperation() {
Object x = valStk.pop();
Object y = valStk.pop();
Object op = opStk.pop();
if ((Integer) x <= 0 || (Integer) y <= 0){
throw new NumberFormatException();
}
if (op.equals("+")) {
int sum = (Integer) x + (Integer) y;
valStk.push(sum);
}
}
public void repeatOps(char refOp) {
while (valStk.count() > 1 &&
prec(refOp) <= prec((char)opStk.pop())) {
doOperation();
}
}
int prec(char op) {
switch (op) {
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
case '^':
return 2;
default:
throw new IllegalArgumentException("Operator unknown: " + op);
}
}
public Object EvaluateExpression(String str) {
System.out.println("Evaluating " + str);
Scanner s = null;
try {
s = new Scanner(str);
//while there is tokens to be read
while (s.hasNext()) {
//if token is an int
if (s.hasNextInt()) {
//read it
int val = s.nextInt();
if(val <= 0) {
throw new NumberFormatException("Non-positive");
}
System.out.println("Val " + val);
//push it to the stack
valStk.push(val);
} else {
//push operator
String next = s.next();
char chr = next.charAt(0);
System.out.println("Repeat Ops " + chr);
repeatOps(chr);
System.out.println("Op " + next);
opStk.push(chr);
}
repeatOps('+');
}
} finally {
if (s != null) {
s.close();
}
}
System.out.println("Should be the result: " + valStk.pop());
return valStk.pop();
}
I have a few suggestions to make that might set you on the right path (hopefully).
Firstly I suggest your expression tree follow the Composite Design Pattern. It works very well for these types of hierarchies. For your purpose it would look something like:
interface Expression {
int getValue();
}
class Constant implements Expression {
private int value;
public int getValue() {
return value;
}
}
class Operation implements Expression {
private Expression operand1;
private Operator operator;
private Expression operand2;
public int getValue() {
return operator.apply(operand1, operand2);
}
}
Note that you don't need any concept of operator precedence or parentheses: it's entirely implicit in how the tree is constructed. For example "3 + 4 * 2" should result in a tree "(+ 3 (* 4 2))" while "(3 + 4) * 2" should result in a tree "(* (+ 3 4) 2)".
Secondly I suggest you make your operators into an enum rather than relying on the string values:
enum Operator {
TIMES((n1, n2) -> n1 * n2),
DIVIDE((n1, n2) -> n1 / n2),
PLUS((n1, n2) -> n1 + n2),
MINUS((n1, n2) -> n1 - n2);
private final BinaryOperator<Integer> operation;
Operator(BinaryOperator<Integer> operation) {
this.operation = operation;
}
public int apply(int operand1, int operand2) {
return operation.apply(operand1, operand2);
}
}
The advantage of this approach is that it's trivial to add new operators without changing the structure of the tree at all.
Thirdly I suggest you split your conversion from string to expression tree into two steps. The first is to convert from string to tokens and the second from token to trees. These are call lexical and semantic analysis in the jargon.
If you are using the shunting yard algorithm for the semantic analysis then keep in mind that the output stack will hold Expression instances ready to become operands. I can give you more detail on how to shunt operators but it's probably worth you giving the suggestions above a try first.

Parenthesis Check within a Linked stack for infix to postfix

I have four classes.
One contains my linkedstack setup
One is infixtopostfix for prioritization and conversion
Parenthesis for matching
Postfix for evaluation
I have setup almost everything here but it is still returning false anyway I put it.
On another note my equals on !stackMatch.pop().equals(c) is not working due to it being a object type with '!' being a problem.
My programs are simple and straight forward:
LinkedStack.java
public class LinkedStack implements StackInterface {
private Node top;
public LinkedStack() {
top = null;
} // end default constructor
public boolean isEmpty() {
return top == null;
} // end isEmpty
public void push(Object newItem) {
Node n = new Node();
n.setData(newItem);
n.setNext(top);
top = n;
} // end push
public Object pop() throws Exception {
if (!isEmpty()) {
Node temp = top;
top = top.getNext();
return temp.getData();
} else {
throw new Exception("StackException on pop: stack empty");
} // end if
} // end pop
public Object peek() throws Exception {
if (!isEmpty()) {
return top.getData();
} else {
throw new Exception("StackException on peek: stack empty");
} // end if
} // end peek
} // end LinkedStack
InfixToPostfix.java
import java.util.*;
public class InfixToPostfix {
Parenthesis p = new Parenthesis();
LinkedStack stack = new LinkedStack();
String token = ""; // each token of the string
String output = ""; // the string holding the postfix expression
Character topOfStackObject = null; // the top object of the stack, converted to a Character Object
char charValueOfTopOfStack = ' '; // the primitive value of the Character object
/**
* Convert an infix expression to postfix. If the expression is invalid, throws an exception.
* #param s the infix expression
* #return the postfix expression as a string
* hint: StringTokenizer is very useful to this iteratively
*/
//public String convertToPostfix(String s) throws Exception {
//}
private boolean isOperand (char c){
return ((c>= '0' && c <= '9') || (c >= 'a' && c<= 'z'));
}
public void precedence(char curOp, int val) throws Exception {
while (!stack.isEmpty()) {
char topOp = (Character) stack.pop();
// charValueOfTopOfStack = topOfStackObject.charValue();
if (topOp == '(') {
stack.push(topOp);
break;
}// it's an operator
else {// precedence of new op
int prec2;
if (topOp == '+' || topOp == '-') {
prec2 = 1;
} else {
prec2 = 2;
}
if (prec2 < val) // if prec of new op less
{ // than prec of old
stack.push(topOp); // save newly-popped op
break;
} else // prec of new not less
{
output = output + topOp; // than prec of old
}
}
}
}
Parenthesis.java
import java.util.*;
public class Parenthesis{
private LinkedStack stack = new LinkedStack();
private Object openBrace;
private String outputString;
/**
* Determine if the expression has matching parenthesis using a stack
*
* #param expr the expression to be evaluated
* #return returns true if the expression has matching parenthesis
*/
public boolean match(String expr) {
LinkedStack stackMatch = new LinkedStack();
for(int i=0; i < expr.length(); i++) {
char c = expr.charAt(i);
if(c == '(')
stackMatch.push(c);
else if(c == ')'){
if (stackMatch.isEmpty() || !stackMatch.pop().equals(c))
return false;
}
}
return stackMatch.isEmpty();
}
}
Just wanted to give you all of it so you could help me. I have tests written already just struggling with the parenthesis problem of pushing it on the stack but unable to compare it to the closing parenthesis so it can check if there is enough while checking to be sure it is not empty.
The problem probably is, that you are trying to test if matching ( is currently on top of the stack when ) comes, but in c is acctual character, ), so you test if ) is on top of stack, not ( as you should.

Trouble with my prefix expression evaluator

I'm running into an error with my prefix expression evaluator.
The error that I get when I try to run it is
Expression (+ (- 6) (* 2 3 4) (/ (+ 3) (- 2 3 1)))
Expression in thread "main" java.lang.NumberFormatException: For input string: "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))"
at sun.misc.FloatingDecimal.readJavaFormatException: For input string "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
//code starts here
import java.util.*;
public class SimpleLispExpressionEvaluator {
// Current input Lisp expression
private String inputExpr;
// Main stack & temp stack, see algorithm in evaluate()
private Stack<Object> expressionStack;
private Stack<Double> tempStack;
// default constructor
// set inputExpr to ""
// create stack objects
public SimpleLispExpressionEvaluator()
{
inputExpr = "";
expressionStack = new Stack<Object>();
tempStack = new Stack<Double>();
}
// default constructor
// set inputExpr to inputExpression
// create stack objects
public SimpleLispExpressionEvaluator(String inputExpression)
{
inputExpr = inputExpression;
expressionStack = new Stack<Object>();
tempStack = new Stack<Double>();
}
// set inputExpr to inputExpression
// clear stack objects
public void reset(String inputExpression)
{
inputExpr = inputExpression;
Stack<Object> expressionStack = new Stack<Object>();
Stack<Double> tempstack = new Stack<Double>();
}
private boolean checkifNumber() {
return false;
}
// This function evaluate current operator with its operands
// See complete algorithm in evaluate()
//
// Main Steps:
// Pop operands from expressionStack and push them onto
// tempStack until you find an operator
// Apply the operator to the operands on tempStack
// Push the result into expressionStack
//
`private double add() {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 + op2;
return temp;
}`
private double multiply() {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 * op2;
return temp;
}
private double subtract() {
if (tempStack.size() == 1) {
double temp = -tempStack.pop();
return temp;
} else {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 - op2;
return temp;
}
}
private double divide() {
if (tempStack.size() == 1) {
double temp = 1 / tempStack.pop();
return temp;
} else if (tempStack.pop() == 0 || tempStack.pop() == null) {
throw new IndexOutOfBoundsException(); } else {
double op1 = tempStack.pop();
double op2 = tempStack.pop();
double temp = op1 - op2;
return temp;
}
}
private void evaluateCurrentOperation()
{
while( expressionStack.peek().getClass().getName().equals("java.lang.Double") ) {
tempStack.push( (Double)expressionStack.pop() );
}
Character operator = (Character)expressionStack.pop();
Double result = null;
switch( operator ) {
case '+':
result = add();
break;
case '*':
result = multiply();
break;
case '-':
result = subtract();
break;
case '/':
result = divide();
break;
}
expressionStack.push( result );
}
/**
* This function evaluates Lisp expression in inputExpr
* It return result of the expression
*
* The algorithm:
*
* Step 1 Scan the tokens in the expression string.
* Step 2 If you see an operand, push operand object onto the expressionStack
* Step 3 If you see "(", next token should be an operator
* Step 4 If you see an operator, push operator object onto the expressionStack
* Step 5 If you see ")" // steps in evaluateCurrentOperation() :
* Step 6 Pop operands and push them onto tempStack
* until you find an operator
* Step 7 Apply the operator to the operands on tempStack
* Step 8 Push the result into expressionStack
* Step 9 If you run out of tokens, the value on the top of expressionStack is
* is the result of the expression.
*/
public double evaluate()
{
// only outline is given...
// you need to add statements
// you may delete or modify any statements in this method
// use scanner to tokenize inputExpr
Scanner inputExprScanner = new Scanner(inputExpr);
// Use zero or more white space as delimiter,
// which breaks the string into single character tokens
inputExprScanner = inputExprScanner.useDelimiter("\\s*");
// Step 1: Scan the tokens in the string.
while (inputExprScanner.hasNext())
{
// Step 2: If you see an operand, push operand object onto the expressionStack
if (inputExprScanner.hasNextInt())
{
// This force scanner to grab all of the digits
// Otherwise, it will just get one char
String dataString = inputExprScanner.findInLine("\\d+");
expressionStack.push(new Double(dataString));
// more ...
}
else
{
// Get next token, only one char in string token
String aToken = inputExprScanner.next();
char item = aToken.charAt(0);
String nextToken;
char nextItem;
switch (item)
{
// Step 3: If you see "(", next token should be an operator
case '(':
nextToken = inputExprScanner.next();
nextItem = nextToken.charAt(0);
// Step 4: If you see an operator, push operator object onto the expressionStack
if (nextItem == '+') {
expressionStack.push(nextItem);
} else if (nextItem == '-') {
expressionStack.push(nextItem);
} else if (nextItem == '*') {
expressionStack.push(nextItem);
} else {
expressionStack.push(nextItem);
}
break;
// Step 5: If you see ")" // steps 6,7,8 in evaluateCurrentOperation()
case ')':
try {
evaluateCurrentOperation();
} catch (EmptyStackException e) {
break;
}
break;
default: // error
throw new RuntimeException(item + " is not a legal expression operator");
} // end switch
} // end else
} // end while
// Step 9: If you run out of tokens, the value on the top of expressionStack is
// is the result of the expression.
//
// return result
double result = new Double(inputExpr);
return result;
}
// This static method is used by main() only
private static void evaluateExprt(String s, SimpleLispExpressionEvaluator expr)
{
Double result;
System.out.println("Expression " + s);
expr.reset(s);
result = expr.evaluate();
System.out.printf("Result %.2f\n", result);
System.out.println("-----------------------------");
}
// simple tests
public static void main (String args[])
{
SimpleLispExpressionEvaluator expr= new SimpleLispExpressionEvaluator();
String test1 = "(+ (- 6) (* 2 3 4) (/ (+ 3) (* 1) (- 2 3 1)))";
String test2 = "(+ (- 632) (* 21 3 4) (/ (+ 32) (* 1) (- 21 3 1)))";
String test3 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 1) (- 2 1 )))";
String test4 = "(+ (/2))";
String test5 = "(+ (/2 3 0))";
String test6 = "(+ (/ 2) (* 2) (/ (+ 1) (+ 3) (- 2 1 ))))";
evaluateExprt(test1, expr);
evaluateExprt(test2, expr);
evaluateExprt(test3, expr);
evaluateExprt(test4, expr);
evaluateExprt(test5, expr);
evaluateExprt(test6, expr);
} }
Step 9 of evaluate is not doing what is commented:
// Step 9: If you run out of tokens, the value on the top of expressionStack is
// is the result of the expression.
//
// return result
double result = new Double(inputExpr);
it is trying to convert the whole input string into a double, not retrieving the top of the expression stack. It should be something like
double result = (Double)expressionStack.pop();
also beware that the operators, as implemented, do not accept 3 or more arguments - (* 2 3 4) should not work.

Categories

Resources