Top Banner
Chapter 9 Calculator
16

Ch03

Jul 04, 2015

Download

Technology

leminhvuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ch03

Chapter 9

Calculator

Page 2: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-2

Chapter Objectives

• Provide a case study example from problem statement through implementation

• Demonstrate how a stack and a list can be used to solve a problem

Page 3: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-3

A Calculator

• A calculator is a simple utility that is provided with most operating systems

• A calculator provides – a keypad of integer digits,

– the associated operations,

– other useful characters such as parentheses and brackets

Page 4: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-4

A Calculator

• For this case study, we will create a graphical calculator that: – accepts an infix expression,

– converts that expression to postfix,

– evaluates the postfix expression,

– returns the result

Page 5: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-5

A Calculator

• Our infix expressions may include– Integers

– Basic arithmetic operators • addition, subtraction, multiplication, division

– Parentheses

• All of the tokens in our expression must be separated by a space

Page 6: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-6

A Calculator

• Our graphical user interface will allow the user to click on the numeric digits or simply press the numbers (and other characters) on the keyboard

• The user will click the equals button to start the evaluation of the given expression

Page 7: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-7

A Calculator - Initial Design

• A calculator consists of four high-level components:– The driver

– The graphical user interface

– The infix to postfix converter

– The postfix evaluator

Page 8: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-8

A Calculator - Initial Design

• Unlike the BlackJack case study, these high-level components are virtually unrelated, and we are not yet aware of any low-level components that will be needed

• Thus this problem lends itself to a top-down approach to design

Page 9: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-9

A Calculator - the CalculatorDemo Class

• The CalculatorDemo class will serve as the driver for our system

• This class will simply instantiate the GUI and call its display method

Page 10: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-10

A Calculator - the CalculatorGUI Class

• Other than the driver, the GUI is the highest-level component in this system

• The GUI must provide buttons to represent the digits, the operations, parentheses, and an equals button

Page 11: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-11

FIGURE 9.1 The calculator user interface

Page 12: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-12

A Calculator - the infixToPostfix Class

• The purpose of the infixToPostfix class to to provide a conversion method that accepts an infix expression as a string and returns the equivalent postfix expression

• Since we know that the postfix expression will then be evaluated,we will return the postfix expression as an unordered list rather than a string

Page 13: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-13

Page 14: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-14

A Calculator - the listPostfixEvaluator Class

• Like our example from Chapter 6, the listPostfixEvaluator class will take the postfix expression and use a stack to evaluate it

• The difference from our earlier example is that the expression has already been broken into tokens by the infixToPostfix class

Page 15: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-15

A Calculator - the listPostfixEvaluator Class

Page 16: Ch03

Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 9-16

FIGURE 9.2 UML description of the initial design of a calculator