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, 22.06.2019 15:00
The three logical operators used to write compound conditions are "and," "or," and "not." a: true b: false
Answers: 2
question
Computers and Technology, 22.06.2019 16:10
Drag each label to the correct location on the imagelist the do’s and don’ts of safeguarding your password.keep yourself loggedin when you leave your computer.don’t write your password down and leave it whereothers can find it.share your password with your friends.each time you visit a website,retain the cookies on your computer.use a long password with mixed characters.
Answers: 1
question
Computers and Technology, 22.06.2019 18:30
Word vocabulary words: print, proofread, status line, graphics, font effects, left margin, justification, line spacing, copy/paste, data. review words: font point, bold, save, center, error. fill in the correct word for the definition and then transfer the letters to the appropriate spot by number. some numbers will be found multiple times. you will end up with a quotation about…… what else? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 k 16 17 18 19 20 21 22 23 24 25 8 27 28 29 w 31 32 k 34 35 36 w h 39 40 41 42 8 44 45 46 47 48 49 50 51 52 53 54 55 .1. a software function that records keystrokes on a disk or drive so information can be 5 4 52 9 later retrieved. p n 2. to produce a paper copy of information. 10 7 12u n 3. a display that shows the location of the cursor, pages, etc. 45 46 18 27 36 20 42p4. pictures or images, located in clip art or other files. 6 24 44 28 34 49 555. any mis-stroke of a key. 47 41 48 2 10 n6. allows major changes to the font such as shadow, emboss, etc. 21 25 46 35 23 21 29 14 22 17 n7. a feature that centers lines of text horizontally. 49 53 46 9 51 p8. size of the font 31 16 22 b l 9. a feature that prints designated text darker than the rest to add emphasis. 32 3 . p10. to compare copy on a display screen or printout to the original 24 39 25 23 54 9 50 3 and correct errors. j un 11. a feature that allows text to be aligned at the left 11 12 7 21 16 49 40 46 34 2 and right margins. leftn 12. amount of blank space on the left side of the paper. 8 18 41 6 34 linen 13. number of blank lines between lines of text. 17 4 49 13 1914. any information inputted into the computer. 3 4 46 44 p /p15. feature that duplicates text from one location and places it in another.
Answers: 2
question
Computers and Technology, 22.06.2019 22:30
Alex’s family members live in different parts of the world. they would like to discuss the wedding plans of one of their distant relatives. however, alex wants all the family members to talk to each other simultaneously so that they can make decisions quickly. which mode of internet communication should they use? a. blog b. email c. wiki d. message board e. instant messaging
Answers: 2
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, 11.01.2020 00:31
Questions on the website: 13722362