Loading in 5 sec....

Computer Science 2 Data Structures V22.0102 section 2 Recitation PowerPoint Presentation

Computer Science 2 Data Structures V22.0102 section 2 Recitation

Download Presentation

Computer Science 2 Data Structures V22.0102 section 2 Recitation

Loading in 2 Seconds...

- 84 Views
- Uploaded on
- Presentation posted in: General

Computer Science 2 Data Structures V22.0102 section 2 Recitation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

- 1

- General Questions/Confusions
- Assignment #2 – Overview & Questions
- The Calculator Class
- The Converter Class
- Sample Input/Output

- Assignment #2 – The Command-line & Keyboard Input

- 2

- IntroductionYour project is to design a program to implement a calculator. The calculator
will take an infix expression convert it

to a postfix expression and then evaluate it.

- Review: infix and postfix notation

You should implement the calculator in two parts:

- A Converter class that will convert the input string to postfix.
- A Calculator class that will evaluate the postfix expression.

The text contains explanations of the algorithms you will need.

- See pages 81 - 84 for the algorithm you should use for the Calculator class
- See pages 84 - 86 for the Converter algorithm
- (Another explanation of the two algorithms can be found here).
- You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment.

- You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment.
- Both the Converter and Calculator classes, should use the StackLI class for the Stack.
- Are the methods in StackLI & PostCalc.java clear to everyone?

The Calculator class

- Basically this class will be the same as the PostCalc class shown in lecture. The main differences will be:
- The input will be an infix expression instead of a postfix expression.
- It will be entered by the user instead of hard-coded by you.
- The input may have operands which consist of multiple digits -- not just single digits like PostCalc

- Parentheses are legal in the infix expression
(Note: The parentheses are needed in the infix expression. After your Converter class' algorithm converts the expression to postfix, it will no longer have parentheses because they are not necessary in postfix expressions.)

- The Calculator class will instantiate an object of the Converter class in order to have the infix expression converted to a postfix expression.
- You may have to modify the other methods in the PostCalc class (and/or add additional ones).

The Converter class

- The Converter class will use a Stack in order to implement the algorithm in the book.
- When the Converter class is instantiated, a String is passed representing the infix expression entered. This should be saved as an instance variable.
- The method that does all the work in the Converter class should be called toPostFix(). It will convert the infix expression to a postfix expression. (It should return a Queue (use the queue from the book) of Strings representing the postfix expression to the Calculator class.
- The details and helper methods used in this class are left up to you.

The Converter class: Hint

- You will probably want to implement a method that compares two operators for precedence:
/**

* @return 1 if opA has higher precedence

* -1 if opB has higher precedence

* 0 if they have equal precedence

*/

int comparePrecedence(char opA, char opB);

- Sample inputs
- 3+4*5/6
- (300+23)*(43-21)/(84+7)
- (4+8)*(6-5)/((3-2)*(2+2))

- Your output should show the converted postfix string and the result of the calculation.

- For instance, here is a sample run of the program (user input in italic, program output in courier):
- The input infix expression:
$(4+8)*(6-5)/((3-2)*(2+2)) <CR>

INFIX: (4+8)*(6-5)/((3-2)*(2+2))

- Converted to postfix:
POSTFIX: 4 8 + 6 5 - * 3 2 - 2 2 + * /

- And the answer:
RESULT: 3.0

- For a given input at prompt $
$ (4+8)*(6-5)/((3-2)*(2+2)) <CR>

INFIX: (4+8)*(6-5)/((3-2)*(2+2))

POSTFIX: 4 8 + 6 5 - * 3 2 - 2 2 + * /

RESULT: 3.0

- Questions?