Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the language Lisp, each of the four basic arithmetic operators appears before an arbitrary number of operands, which are separated by spaces. The resulting

In the language Lisp, each of the four basic arithmetic operators appears before an arbitrary number of operands, which are separated by spaces. The resulting expression is enclosed in parentheses.

The operators behave as follows:

(+ a b c ...) returns the sum of all the operands, and (+) returns 0.

(- a b c ...) returns a - b - c - ..., and (- a) returns -a. The minus operator must have at least one operand.

(* a b c ...) returns the product of all the operands, and (*) returns 1.

(/ a b c ...) returns a / b / c / ..., and (/ a) returns 1 / a. The divide operator must have at least one operand.

You can form larger arithmetic expressions by combining these basic expressions using a fully parenthesized prefix notation.

For example, the following is a valid Lisp expression: (+ (- 6) (* 2 3 4) (/ (+ 3) (*) (- 2 3 1)))

This expression is evaluated successively as follows: (+ (- 6) (* 2 3 4) (/ 3 1 -2)) (+ -6 24 -1.5) 16.5

Design and implement an algorithm that uses any stack implementation (e.g. java.util.Stack ) to evaluate a legal Lisp expression composed of the four basic operators and integer values. Write a program that reads such expressions and demonstrates your algorithm.

Precondition :You can assume opening and closing parenthesis are all balanced in any given expression. Also you can assume all numbers are integer.

Demonstrate your implementation with the following expressions:

Expression (+ (- 6) (* 2 3 4) (/ (+ 3) (*) (- 2 3 1))) evaluates to 16.5

Expression (+ (- 632) (* 21 3 4) (/ (+ 32) (*) (- 21 3 1))) evaluates to -378.11764705882354

Expression (+ (/ 2) (* 2) (/ (+ 1) (+) (- 2 1 ))) evaluates to Infinity

Hint: You may use more than one stack if needed.

Make it simple as possible.

Codes I have so far:

import java.util.*; public class LispExpression { private static double evaluateLisp(String lisp) { StackInterface exprStack = new ArrayStack<>(); StackInterface currentOpStack = new ArrayStack<>(); int characterCount = lisp.length(); double result = 0; for (int index = 0; index < characterCount; index++) { switch () { case "*": result = currentOpStack.pop(); while (!currentOpStack.isEmpty()) { result *= currentOpStack.pop(); } break; case "/": result = currentOpStack.pop(); while (!currentOpStack.isEmpty()) { result /= currentOpStack.pop(); } break; case "+": result = currentOpStack.pop(); while (!currentOpStack.isEmpty()) { result += currentOpStack.pop(); } break; case "-": result = currentOpStack.pop(); while (!currentOpStack.isEmpty()) { result -= currentOpStack.pop(); } break; default: result = currentOpStack.pop(); break; } exprStack.push(result); } return result; } public static void main(String args[]) { System.out.println(evaluateLisp("(+(- 6)(* 2 3 4)(/(+ 3)(*)(- 2 3 1)))") + " "); System.out.println(evaluateLisp("(+(- 632)(* 21 3 4)(/ (+ 32)(*)(- 21 3 1)))") + " "); System.out.println(evaluateLisp("(+(/ 2)(* 2)(/(+ 1)(+)(- 2 1 )))") + " "); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

Draw the following signal. S(t) = 4 sin (27t+3/2)

Answered: 1 week ago

Question

=+C&B (especially taxation) laws, regulations, and practices?

Answered: 1 week ago

Question

=+designing international assignment C&B packages.

Answered: 1 week ago