Question
below is some of the work i began on LispEvaluate on java. but i'm having a hard time figuring out the rest of the work
below is some of the work i began on LispEvaluate on java. but i'm having a hard time figuring out the rest of the work i need to do. please help me
package lisp;
import java.util.Scanner;
import stack.*;
/**
* The lispCalculate method takes a valid arithmetic
* Lisp expression and evaluates it to calculate the numeric
* output. This code must be provided by you, and will
* make use of stacks.
*
*/
public class LispEvaluate {
/**
* Sets up a loop that treats entered user inputs from the console as Lisp
* expressions, then computes and prints their values. Behavior for badly
* formatted Lisp expressions is not defined, but will likely cause the code
* to crash or otherwise misbehave.
*
* @param args Ignored
*/
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String input = "";
while(true) {
System.out.print("Enter a Lisp expression or use \"q\" or \"quit\" to exit the program: ");
input = console.nextLine();
if(input.equals("q") || input.equals("quit")) break; // Exit the loop
System.out.println("Evaluates to: " + lispCalculate(input));
}
System.out.println("Goodbye");
console.close();
}
/**
* Given a String containing a valid Lisp expression using only
* the operators +, -, /, and *, evaluate the expression and
* return the result.
*
* NOTE: You are NOT allowed to use arrays or ArrayLists. Your algorithm(s)
* should be based on stacks.
*
* @param lispExpression Valid arithmetic Lisp expression.
* @return Numeric result of evaluating the Lisp expression.
*/
public static double lispCalculate(String lispExpression) {
String spacedOut = addSpaces(lispExpression); // Adds extra white space around parentheses
String postfixSpaced = prefixToPostfix(spacedOut); // Converts spaced prefix Lisp expression to spaced postfix Lisp expression
return calculateSpacedPostfix(postfixSpaced); // Calculates the numeric result from processing the spaced postfix Lisp expression
}
///// You MUST provide complete and correctly formatted Javadoc comments to all methods below this point /////
private static String addSpaces(String lispExpression) {
String result = "";
for(int i = 0; i < lispExpression.length(); i++) {
char c = lispExpression.charAt(i);
switch(c) {
case '(':
result += " ( "; // put spaces around (
break;
//more
default:
result += c;
}
}
return result;
}
private static String prefixToPostfix(String spacedOut) {
StackInterface
String result = "";
Scanner scan = new Scanner(spacedOut);
while(scan.hasNext()) {
String token = scan.next();
switch(token) {
case "+": case "-": case "/": case "*":
operators.push(token);
break;
case "(":
// MORE? WHAT TO DO HERE?
break;
case ")":
result += " " + operators.pop() + " ) ";
break;
default:
result += " " + token + " ";
}
}
scan.close();
return result;
}
private static double calculateSpacedPostfix(String postfixSpaced) {
Scanner scan = new Scanner(postfixSpaced);
StackInterface
while(scan.hasNext()) {
String token = scan.next();
switch(token) {
case "+":
//TODO
break;
case "(":
break;
case ")":
//ANYTHING HERE
break;
default:
double value = Double.parseDouble(token);
operands.push(value); // Put each operand on the stack
}
}
scan.close();
return 0;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started