subject

In the language lisp1 , 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

this expression is evaluated successively as follows:

(+ (- 6) (* 2 3 4) (/ 3 1 -2))
(+ -6 24 -1.5)
16.5

write a program that reads such expressions and demonstrates your algorithm. we have provided you some starter code, and you are asked to complete the rest.

lisptoken

package hw5;
public class lisptoken
{

private character operator;
private double operand;
private boolean isoperator;
/** constructors for objects of class lisptoken. */
public lisptoken(character anoperator)

{

operator = anoperator;
isoperator = true;
operand = 0.0;

}

public lisptoken(double value)

{

operand = value;
isoperator = false;
operator = ' ';

}

/** todo: applies this operator to two given operand values.
@param value1 the value of the first operand.
@param value2 the value of the second operand.
@return the real result of the operation.
todo: you need to complete this method.

*/

public double applyoperator(double value1, double value2)

{

}

/** gets the identity value of this operator.
for example, x + 0 = x, so 0 is the identity for +
and will be the value associated with the expression (+).

@return the identity value of the operator. */

public double getidentity()

{

double result = 0.0;

switch (operator)

{
case '+':
result = 0.0;
break;
case '-':
result = 0.0;
break;
case '*':
result = 1.0;
break;
case '/':
result = 1.0;
break;
}

return result;
}

/** detects whether this operator returns a value when it has no operands.
@return true if the operator returns a value when it has no operands,
or false if not. */
public boolean takeszerooperands()
{

boolean result = false;

switch (operator)

{

case '+':
result = true;
break;
case '-':
result = false;
break;
case '*':
result = true;
break;
case '/':
result = false;
break;

}

return result;

}

/** gets the value of this operand.
@return the real value of the operand. */
public double getvalue()

{

return operand;

}

/** returns true if the object is an operator.
@return true is this object is an operator. */
public boolean isoperator()

{

return isoperator;

}

public string tostring()

{

string result = null;
if (isoperator)
result = operator. tostring();
else
result = operand. tostring();
return result;

}

}

lispquestion

package hw5;
import java. util. scanner;
import java. util. stack;
public class lispquestion

{

/** evaluates a lisp expression.

the algorithm:

scan the tokens in the string.
if you see "(", push the next operator onto the stack.
if you see an operand, push it onto the stack.
if you see ")",
pop operands and push them onto a second stack
until you find an operator.
apply the operator to the operands on the second stack.
push the result on the stack.
if you run out of tokens, the value on the top of the stack is
the value of the expression.
@param lispexp a string that is a valid lisp expression.
@return a double that is the value of the expression.
@todo: you need to complete this method. this method must call the applyoperator from lisptoken class.

*/

public static double evaluate(string lispexp)

{

}

public static void main (string args[])

{

double result;
string test1 = "(+ (- 6) (* 2 3 4) (/ (+ 3) (*) (- 2 3 ";
result = evaluate(test1);
system. out. println("expression " + test1 + " evaluates to " + result);
string test2 = "(+ (- 632) (* 21 3 4) (/ (+ 32) (*) (- 21 3 ";
result = evaluate(test2);
system. out. println("expression " + test2 + " evaluates to " + result);
string test3 = "(+ (/ 2) (* 2) (/ (+ 1) (+) (- 2 1 ";
result = evaluate(test3);
system. out. println("expression " + test3 + " evaluates to " + result);

}

}

/*

expression (+ (- 6) (* 2 3 4) (/ (+ 3) (*) (- 2 3 evaluates to 16.5
expression (+ (- 632) (* 21 3 4) (/ (+ 32) (*) (- 21 3 evaluates to -378.11764705882354
expression (+ (/ 2) (* 2) (/ (+ 1) (+) (- 2 1 evaluates to infinity

*/

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 11:00
Sports and entertainment class, your goal is to increase attendance and make a profit for a game by getting your team on a winning track with total salaries less than $3,000,000
Answers: 3
question
Computers and Technology, 24.06.2019 12:00
An npn transistor is correctly biased and turned on if the a. base is negative. b. collector is negative. c. collector is positive with respect to the emitter and negative with respect to the base. d. collector is the most positive lead followed by the base.
Answers: 1
question
Computers and Technology, 25.06.2019 03:00
What judgment would you make about open systems interconnect? not useful that it is a technology that it is a model that it does not pertain to technology
Answers: 1
question
Computers and Technology, 25.06.2019 04:40
Ineed ! i need a computer whiz to me complete my study guide on computers, it's 50 questions. if anyone can me i'll be greatly appreciated.
Answers: 1
You know the right answer?
In the language lisp1 , each of the four basic arithmetic operators appears before an arbitrary numb...
Questions
question
Computers and Technology, 28.09.2019 01:00
Questions on the website: 13722363