1 / 13

Computer Science 2 Data Structures V22.0102 section 2 Recitation

Design and implement a calculator program that converts infix expressions to postfix and evaluates them. This program consists of a Converter class for the conversion process and a Calculator class for evaluation.

kotter
Download Presentation

Computer Science 2 Data Structures V22.0102 section 2 Recitation

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 2Data Structures V22.0102 section 2Recitation • 1

  2. Plan for Today • General Questions/Confusions • Assignment #2 – Overview & Questions • The Calculator Class • The Converter Class • Sample Input/Output • Assignment #2 – The Command-line & Keyboard Input • 2

  3. Assignment #2: Calculator • 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

  4. Assignment #2: Calculator 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.

  5. Assignment #2: Calculator 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.

  6. Assignment #2: Calculator • 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?

  7. Assignment #2: Calculator 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

  8. Assignment #2: Calculator • 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).

  9. Assignment #2: Calculator 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.

  10. Assignment #2: Calculator 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);

  11. Assignment #2: Calculator • 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.

  12. Assignment #2: Calculator • 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

  13. Assignment #2: Calculator • 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?

More Related