Docsity

Stacks, Queues and Deques - Data Structures - Homework #5 | CS 261, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
CS 261 — Data Structures
Homework #5: Stacks, Queues, & Deques (50 pts)
Due date: Friday, May 2, 2003 (5:00 PM)
Treat this paper as a specification for your assignment. Your code will be graded on correctness, readability,
and style. Study problems and exercises must be legible and will be graded on correctness.
What to do:
1. (25 pts) Write a class called RPNCalc that uses one stack (either using Vector or LinkedList) to
implement a command line RPN (Reverse Polish Notation) calculator. The “main” method in this class
reads in a sequence of numbers and operators separated by a white space (such as “5 3.12 * 4 + 78
29.35 6 - / *”) and computes and displays the resulting value. The sequence of numbers (including
negatives and the constants “pi” and “e”) and operators are passed to main via the “String [] args”
parameter. Your program should change the strings “pi” and “e” into the values 3.14159265... and
2.7182818... respectively. Your RPNCalc class should treat all numbers as doubles and should implement
the following operators:
Operator Description Example Output
+addition 4 5 + 9
-subtraction 4 5 - -1
/division 4 5 / 0.8
xmultiplication (* is treated as wildcard) 4 5 x 20
^power 4 5 ^ 1024
^2 squared 4 ^2 16
^3 cubed 4 ^3 64
abs absolute value -4 abs 4
sqrt square root 16 sqrt 4
exp exponential (same as “e x ^” = ex)2 exp 7.389...
ln natural logarithm 7.389... ln 2
log base 10 logarithm 100 log 2
log2 base 2 logarithm 128 log2 7
sin sine of an angle (in radians)pi 2 / sin 1
cos cosine of an angle (in radians)-pi cos -1
tan tangent of an angle (in radians)pi 4 / tan 1
Of course, you are free to add more operators if you would like (such as asin, acos, atan, ceil, floor,
rand, etc.). Your RPNCalc should test for illegal input—incorrect count of numbers (too many, “4 5 3
+”, or too few, “4 +”), unknown operators (which should be treated as a incorrectly formatted number), and/
or numbers that do not have a valid format (e.g., “45+”, “5.32.1”, etc.)—and report an error along with the
offending argument string when illegal input is detected.
Hint: one way to implement this is to read in each string and compare it with the list of accepted operators, if
it is not a valid operator then assume that it is a number, convert it to a Double (the wrapper class for dou-
ble) using one of he following methods in the Double class:
public static double parseDouble(String s)
public static Double valueOf(String s)
and push it onto the stack. (Note that both conversion methods will throw a NumberFormatException if
the string is not in the correct format.) If the argument string is a valid operator, pop the appropriate number
of Doubles off the stack (depending on the given operator), convert them to double (or an int for the
value of n used in the last three operators), and then perform the appropriate computation and push the result
back onto the stack. When all of the command line arguments are processed, the result is output by popping
the stack (which should result in an empty stack) and printing the value.
pf3

Partial preview of the text

Download Stacks, Queues and Deques - Data Structures - Homework #5 | CS 261 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CS 261 — Data Structures

Homework #5: Stacks, Queues, & Deques (50 pts) Due date: Friday, May 2, 2003 (5:00 PM) Treat this paper as a specification for your assignment. Your code will be graded on correctness, readability, and style. Study problems and exercises must be legible and will be graded on correctness.

What to do:

  1. ( 25 pts ) Write a class called RPNCalc that uses one stack (either using Vector or LinkedList) to implement a command line RPN (Reverse Polish Notation) calculator. The “main” method in this class reads in a sequence of numbers and operators separated by a white space (such as “5 3.12 * 4 + 78 29.35 6 - / *”) and computes and displays the resulting value. The sequence of numbers (including negatives and the constants “pi” and “e”) and operators are passed to main via the “String [] args” parameter. Your program should change the strings “pi” and “e” into the values 3.14159265... and 2.7182818... respectively. Your RPNCalc class should treat all numbers as doubles and should implement the following operators:

Operator Description Example Output

  • addition 4 5 + 9
  • subtraction 4 5 - - / division 4 5 / 0. x multiplication (* is treated as wildcard) 4 5 x 20 ^ power 4 5 ^ 1024 ^2 squared 4 ^2 16 ^3 cubed 4 ^3 64 abs absolute value - 4 abs 4 sqrt square root 16 sqrt 4 exp exponential (same as “e x ^” = e x ) 2 exp 7.389... ln natural logarithm 7.389... ln 2 log base 10 logarithm 100 log 2 log2 base 2 logarithm 128 log2 7 sin sine of an angle ( in radians ) pi 2 / sin 1 cos cosine of an angle ( in radians ) -pi cos - tan tangent of an angle ( in radians ) pi 4 / tan 1

Of course, you are free to add more operators if you would like (such as asin, acos, atan, ceil, floor, rand, etc.). Your RPNCalc should test for illegal input—incorrect count of numbers (too many, “ 4 5 3 +”, or too few, “4 +”), unknown operators (which should be treated as a incorrectly formatted number), and/ or numbers that do not have a valid format (e.g., “45+”, “5.32.1”, etc.)—and report an error along with the offending argument string when illegal input is detected. Hint: one way to implement this is to read in each string and compare it with the list of accepted operators, if it is not a valid operator then assume that it is a number, convert it to a Double (the wrapper class for dou- ble) using one of he following methods in the Double class: public static double parseDouble(String s) public static Double valueOf(String s) and push it onto the stack. (Note that both conversion methods will throw a NumberFormatException if the string is not in the correct format.) If the argument string is a valid operator, pop the appropriate number of Doubles off the stack (depending on the given operator), convert them to double (or an int for the value of n used in the last three operators), and then perform the appropriate computation and push the result back onto the stack. When all of the command line arguments are processed, the result is output by popping the stack (which should result in an empty stack) and printing the value.

As a side note, you can make this command line RPN calculator easier to use if you add the following alias to your “.cshrc” file: alias calc java RPNCalc You can then simply type commands such as the following: calc pi 5 ^2 * (computes the area of a circle with a radius of 5)

  1. ( 10 pts ) Copy the Maze.java and mazedata files from the /usr/local/classes/cs/cs261/ assign6/ directory (or from the web page) into your assignment directory and modify Maze.java to read in the mazedata file and then solve the maze using a breadth first search. Also, modify the mazedata file to create a 10×10 maze that looks like the following:

Written homework:

  • Chapter 10 Excercise 5.

E 10.5) ( 2 pts ) What is the Polish notation representation of the following expression?

( a * ( b + c )) + ( b / d ) * a

  • Chapter 11 Excercise 1 and 5.

E 11.1) ( 5 pts ) How might you use a single deque to determine whether a string is a palindrome? (See pro- gramming project 2, Chapter 10).

E 11.5) ( 8 pts ) Consider the following diagram. Starting from node A, list the vertices as they might be visited in a breadth-first search and as they might be visited in a depth-first search. Note that there are many different sequences for both forms of search.

What to turn in:

In class: a printout of written homework in the file Answers.txt and a printout of your source files RPN- Calc.java and Maze.java.

Via the web: the files Answers.txt, RPNCalc.java, and Maze.java and a screen grab of the solu- tion to the maze problem called Maze.jpg (or Maze.tif).

E

B

B

A D

C

F

E J

G

H

I