1 c++ classes and data structures jeffrey s. childs chapter 2 overloaded operators, class templates,...

163
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

Upload: primrose-cross

Post on 04-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

1

C++ Classes and Data StructuresJeffrey S. Childs

Chapter 2Overloaded Operators, Class

Templates, and Abstraction

Jeffrey S. Childs

Clarion University of PA

© 2008, Prentice Hall

2

Binary Operators

• A binary operator acts upon two operands

x + a

binary operator

operands

3

Overloaded Operators

• The operands can be constants, variables, or objects

• If one or more of the operands are objects, the operator must be an overloaded operator

• An overloaded operator calls a function, which carries out the operation

4

1 struct CarType {2 string maker;3 int year;4 float price;

5 bool operator >( int num ) 6 { if ( price > num ) return true; return false; }6 };7

8 int main( )9 {

10 CarType myCar;...

16 if ( myCar > 2000 )17 cout << “My car is more than 2000!” << endl;18 return 0;19 }

Example

5

The Function Call

• “myCar > 2000” is the function call

• The operator > function is called for the myCar object

• The right operand is always passed as a parameter into the function

6

1 struct CarType {2 string maker;3 int year;4 float price;

5 bool operator >( int num ) 6 { if ( price > num ) return true; return false; }6 };7

8 int main( )9 {

10 CarType myCar;...

16 if ( myCar > 2000 )17 cout << “My car is more than 2000!” << endl;18 return 0;19 }

Right Operand

7

1 struct CarType {2 string maker;3 int year;4 float price;5 bool operator >( int num ) 6 { if ( price > num ) return true; else return false; }7 bool operator >( CarType car ) 8 { if ( price > car.price ) return true; else return false; }9 };

Adding Another Overloaded Operator > for

“myCar > yourCar”

8

struct CarType {string maker;int year;float price;bool operator >( int num ) { if ( price > num ) return true; else

return false; }bool operator >( CarType car ) { if ( price > car.price ) return true; else

return false; }float operator +( CarType car ) { return price + car.price; }

};

Adding an Overloaded Operator + for “myCar + yourCar”

9

Where to Place Overloaded Operator Functions

• If the left operand is an object of a struct, place the overloaded operator within the struct definition (as shown previously)

• If the left operand is not an object, place the overloaded operator directly underneath the struct definition

10

struct CarType {string maker;int year;float price;...float operator +( CarType car ) { return price + car.price; }

};

bool operator <( int num, CarType car ) { if ( num < car.price ) return true; else

return false; }

Adding an Overloaded Operator for “2000 < myCar”

11

struct Check {float checkAmount;int checkNum;string date;string receiver;

};

A Check Struct for the Checkbook Class

(to Replace float)

12

class Checkbook{public:

void setBalance( float amount );bool writeCheck( float amount ); void deposit( float amount );float getBalance( );float getLastCheck( );float getLastDeposit( );

private:float balance;float lastCheck;float lastDeposit;

};

The First Class

13

class Checkbook {public:

void setBalance( float amount );bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );Check getLastCheck( );float getLastDeposit( );

private:float balance;Check lastCheck;float lastDeposit;

};

The Second Class

Check struct placed hereNecessary, so the compiler knows what “Check” is

14

Class Templates

• Instead of making different classes for different data types, we can make one class template.

• A class template is a blueprint for making a class.

• The client uses one line of code to have the compiler make a class from the class template.

• The client can make his/her own Check struct for the Checkbook, and place it in the main program.

15

class Checkbook {public:

void setBalance( float amount );

bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class TemplateCheck struct placed here

First, we don’t need the Check struct here – the client can make it the way they want in the main program (the compiler will see it before it makes the class)

16

class Checkbook {public:

void setBalance( float amount );

bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

17

class Checkbook {public:

void setBalance( float amount );

bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

Now, we need a special line of code to tell the compiler this is a class template

18

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

Now, we need a special line of code to tell the compiler this is a class template

19

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

you can think of DataType as a “variable name” for a data type – its value can be “float” or “Check” or anything else the client would like it to be

20

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

Now, since we are using a more general DataType, we use it for the type wherever appropriate

21

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( Check amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

22

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

23

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

Check getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

24

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

DataType getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

25

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

DataType getLastCheck( );float getLastDeposit( );

private:float balance;

Check lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

26

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

DataType getLastCheck( );float getLastDeposit( );

private:float balance;

DataType lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

27

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

DataType getLastCheck( );float getLastDeposit( );

private:float balance;

DataType lastCheck;float lastDeposit;

};

Making a Class Template (cont.)

Finally, we add a line to include the implementation file (not done in an ordinary class)

28

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

DataType getLastCheck( );float getLastDeposit( );

private:float balance;

DataType lastCheck;float lastDeposit;

};

#include “checkbook.cpp”

Making a Class Template (cont.)

Finally, we add a line to include the implementation file (not done in an ordinary class)

29

template <class DataType>class Checkbook {public:

void setBalance( float amount );

bool writeCheck( DataType amount ); void deposit( float amount );float getBalance( );

DataType getLastCheck( );float getLastDeposit( );

private:float balance;

DataType lastCheck;float lastDeposit;

};

#include “checkbook.cpp”

Making a Class Template (cont.)

The client can also have the compiler make more than one class from the class template, each having a different type for DataType

30

18 template <class DataType>19 void Checkbook<DataType>::setBalance( float amount )20 {21 balance = amount;22 }

The Implementation File for a Class Template

In a class template, we don’t include the class specification file at the top of the implementation file.

31

23 template <class DataType>24 bool Checkbook<DataType>::writeCheck( DataType amount )25 {26 if ( amount > balance )27 return false;28 balance -= amount;29 lastCheck = amount;30 return true;31 }

The Implementation File for a Class Template (cont.)

32

23 template <class DataType>24 bool Checkbook<DataType>::writeCheck( DataType amount )25 {26 if ( amount > balance )27 return false;28 balance -= amount;29 lastCheck = amount;30 return true;31 }

The Implementation File for a Class Template (cont.)

If the client wants to use a Check struct for DataType, we have some overloaded operators here…

33

23 template <class DataType>24 bool Checkbook<DataType>::writeCheck( DataType amount )25 {26 if ( amount > balance )27 return false;28 balance -= amount;29 lastCheck = amount;30 return true;31 }

The Implementation File for a Class Template (cont.)

struct assignment is allowed

34

32 template <class DataType>33 void Checkbook<DataType>::deposit( float amount )34 {35 balance += amount;36 lastDeposit = amount;37 }3839 template <class DataType>40 float Checkbook<DataType>::getBalance( ) 41 {42 return balance;43 }

The Implementation File for a Class Template (cont.)

35

61 template <class DataType>62 DataType Checkbook<DataType>::getLastCheck( )63 {64 return lastCheck;65 }6667 template <class DataType>68 float Checkbook<DataType>::getLastDeposit( )69 {70 return lastDeposit;71 }

The Implementation File for a Class Template (cont.)

36

// checkbook.h – a class template for a Checkbook, where the// check is any data type

// to use an object for the DataType, overload the following // operators:// > left operand: object right operand: float// used to compare the amount of the check in the// struct object with the balance// -= left operand: float right operand: object// used to subtract the amount of the check in the// struct object from the balance

Example of Comments for a Class Template

Place these comments above the class template

37

1 #include <iostream>2 #include <iomanip>3 #include <string>4 #include "checkbook.h"56 using namespace std;

Program Using the Class Template

Needed in the main program for both classes and class templates

38

7 struct MyCheck {8 float amt;9 int checkNum;10 string checkComment;11 bool operator >( float bal ) 12 { if ( amt > bal ) return true; return false; }13 };1415 void operator -=( float & bal, MyCheck ch ) 16 { bal -= ch.amt; }

Program Using the Class Template (cont.)

from writeCheck:

26 if ( amount > balance )

from writeCheck:

28 balance -= amount;

39

17 int main( )18 {19 Checkbook<float> johnsCheckbook;20 Checkbook<MyCheck> susansCheckbook;2122 MyCheck susansCheck;23 float amount;24 bool johnsCheckAccepted = false, 25 susansCheckAccepted = false;

Program Using the Class Template (cont.)

40

26 johnsCheckbook.setBalance( 1000 );27 susansCheckbook.setBalance( 2000 );2829 cout << "John, your balance is $1000.00." << endl;30 cout << "Susan, your balance is $2000.00." << endl;

Program Using the Class Template (cont.)

41

31 cout << "John, enter your check amount: $";32 cin >> amount;33 if ( johnsCheckbook.writeCheck( amount ) ) {34 cout << "Your check was accepted." << endl;35 johnsCheckAccepted = true;36 }37 else38 cout << 39 “Check amount is higher than balance” << endl;

Program Using the Class Template (cont.)

42

40 cout << "Susan, enter the check number for your check: ";41 cin >> susansCheck.checkNum;42 cin.ignore( );43 cout << "Please also enter any comment you wish” << 44 “ to make about the check: “ << endl;45 getline( cin, susansCheck.checkComment );46 cout << "Susan, enter your check amount: $";47 cin >> susansCheck.amt;

Program Using the Class Template (cont.)

43

48 if ( susansCheckbook.writeCheck( susansCheck ) ) {49 cout << "Your check was accepted." << endl;50 susansCheckAccepted = true;51 }52 else53 cout << 54 “Check amount is higher than balance” << endl;

Program Using the Class Template (cont.)

44

55 cout << fixed << showpoint << setprecision( 2 );56 cout << "John, your balance is: $" <<

57 johnsCheckbook.getBalance( ) << endl;58 if ( johnsCheckAccepted )59 cout << "Your last check amount is: $" << 60 johnsCheckbook.getLastCheck( ) << endl;61 cout << "Susan, your balance is: $" << 62 susansCheckbook.getBalance( ) << endl;

Program Using the Class Template (cont.)

45

63 if ( susansCheckAccepted ) {64 MyCheck testSusansCheck;65 testSusansCheck = susansCheckbook.getLastCheck( );66 cout << "Your last check amount was: $" <<67 testSusansCheck.amt << endl;68 cout << "for check number: " << 69 testSusansCheck.checkNum << endl;70 cout << "with check comment: " << 71 testSusansCheck.checkComment << endl;72 }7374 return 0;75 }

Program Using the Class Template (cont.)

46

Stack

• A stack is a data structure

• It is like a stack of plates, except it is a stack of data

• It can be a stack of int’s, a stack of float’s, a stack of strings, a stack of struct objects, etc.– why class templates are used for data

structures

47

Push

• Push means place a new data element at the top of the stack

17

5

11

3

48

Push (cont.)

17

5

11

3

• Push means place a new data element at the top of the stack

49

Push (cont.)

17

5

11

3

• Push means place a new data element at the top of the stack

50

Push (cont.)

17

5

11

3

• Push means place a new data element at the top of the stack

51

Pop

• Pop means take a data element off the top of the stack

17

5

11

3

52

Pop (cont.)

• Pop means take a data element off the top of the stack

17

5

11

3

53

Pop (cont.)

• Pop means take a data element off the top of the stack

17

5

11

3

54

Pop (cont.)

• Pop means take a data element off the top of the stack

17

5

11

3

55

Peek

• Peek means retrieve the top of the stack without removing it

17

5

11

3

56

Peek (cont.)

• Peek means retrieve the top of the stack without removing it

17

5

11

3

3

57

Peek (cont.)

• Peek means retrieve the top of the stack without removing it

17

5

11

3

3

58

Abstraction• Abstraction occurs when we use something

without thinking about the details of how it works– Driving a car– The string class

• An abstract data type (ADT) is a data type that contains both data and functions (operations on data); we use the functions without thinking about how they work

• The stack is an abstract data type• Abstraction makes large program development

much easier

59

Chapter 2 Exercises

• A stack is often used as an aid in more complex algorithms

• For example, stacks can be used in expression evaluation

• Consider the problem of having the user enter an expression to be evaluated– must account for operator precedence: 5 + 2 * 3– may be an indefinite number of nested parentheses:

( ( 5 + 2 ) * 3 + 4 ) / 5• Ordinarily, this would be a very difficult problem,

but an algorithm using stacks makes it easy

60

Chapter 2 Exercises (cont.)

• First, a stack is used to convert an infix expression to a postfix expression

• Then, a stack is used to evaluate the postfix expression

• The following slides show how…

61

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 )

Postfix Expression:

Stack

Infix to Postfix Conversion

62

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 )

Postfix Expression:

First, we push an ‘(‘ onto an empty stack.

Infix to Postfix Conversion (cont.)

63

Stack(

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 )

Postfix Expression:

First, we push an ‘(‘ onto an empty stack.

Infix to Postfix Conversion (cont.)

64

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 )

Postfix Expression:

Then, we append a ‘)’ to the infix expression

Infix to Postfix Conversion (cont.)

65

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:

Then, we append a ‘)’ to the infix expression

Infix to Postfix Conversion (cont.)

66

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:

Then, we read “tokens” from the infix expression, performing actions based on what we read.

Infix to Postfix Conversion (cont.)

67

Infix to PostfixConversion (cont.)

• If the token is a ‘(‘, we push it on the stack• If the token is a number, we append it to the

postfix expression• If the token is a ‘)’, we pop the stack and append

each popped token to the postfix expression, until we pop a ‘(‘

• If the token is an operator, then while the top of the stack is an operator with precedence greater than or equal to the token, we pop the stack and append the popped operator to the postfix expression; when we are done popping, we push the token onto the stack

68

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:

Infix to Postfix Conversion (cont.)

69

(

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:

Infix to PostfixConversion (cont.)

70

(

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:

Infix to PostfixConversion (cont.)

71

(

(5

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:

Infix to PostfixConversion (cont.)

72

(

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5

Infix to PostfixConversion (cont.)

73

(

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5

Not even an operator here, so we don’t pop anything.

Infix to PostfixConversion (cont.)

74

(

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5

Infix to PostfixConversion (cont.)

75

(

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5

Infix to PostfixConversion (cont.)

76

(

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5

Infix to PostfixConversion (cont.)

77

(

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3

Infix to PostfixConversion (cont.)

78

(

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3

Infix to PostfixConversion (cont.)

79

(

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3

This is an operator, but its precedence is less than *, so we don’t pop.

Infix to PostfixConversion (cont.)

80

(

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3

Infix to PostfixConversion (cont.)

81

(

(

+

*

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3

Infix to PostfixConversion (cont.)

82

(

(

+

*

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3

Infix to PostfixConversion (cont.)

83

(

(

+

*

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2

Infix to PostfixConversion (cont.)

84

(

(

+

*

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2

Infix to PostfixConversion (cont.)

85

(

(

+

*Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2

Infix to PostfixConversion (cont.)

86

(

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 *

Infix to PostfixConversion (cont.)

87

(

(+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 *

Infix to PostfixConversion (cont.)

88

(

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

89

((

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

90

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

91

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

92

(

-

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

93

(

-

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

94

(

-

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

95

(

-

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * +

Infix to PostfixConversion (cont.)

96

(

-

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6

Infix to PostfixConversion (cont.)

97

(

-

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6

Infix to PostfixConversion (cont.)

98

(

-

(

/

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6

Infix to PostfixConversion (cont.)

99

(

-

(

/

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6

Infix to PostfixConversion (cont.)

100

(

-

(

/

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2

Infix to PostfixConversion (cont.)

101

(

-

(

/

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2

Infix to PostfixConversion (cont.)

102

(

-

(

/

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2

Infix to PostfixConversion (cont.)

103

(

-

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 /

Infix to PostfixConversion (cont.)

104

(

-

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 /

Infix to PostfixConversion (cont.)

105

(

-

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 /

Infix to PostfixConversion (cont.)

106

(

-

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7

Infix to PostfixConversion (cont.)

107

(

-

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7

Infix to PostfixConversion (cont.)

108

(

-

(

+

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7

Infix to PostfixConversion (cont.)

109

(

-

(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 +

Infix to PostfixConversion (cont.)

110

(

-(

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 +

Infix to PostfixConversion (cont.)

111

(

-

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 +

Infix to PostfixConversion (cont.)

112

(

-

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 +

Infix to PostfixConversion (cont.)

113

(-

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 +

Infix to PostfixConversion (cont.)

114

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Infix to PostfixConversion (cont.)

115

( Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Infix to PostfixConversion (cont.)

116

Stack

Infix Expression:( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) )

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Infix to PostfixConversion (cont.)

117

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Stack

Postfix ExpressionEvaluation

118

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Stack

When evaluating a postfix expression, a number encountered is simply pushed onto the stack.

Postfix ExpressionEvaluation (cont.)

119

5 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

120

5 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

121

5

3

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

122

5

3

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

123

5

3

2

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

124

5

3

2

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

125

5

3

2

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

When an operator is encountered, it is used to perform an operation on the top 2 elements of the stack – in the following slides, take careful note of how it is done…

Postfix ExpressionEvaluation (cont.)

126

5

32

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

127

53 2

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

128

53 2*

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

129

53 2*

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Was the topmost element – order can be important here!

Postfix ExpressionEvaluation (cont.)

130

56

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

131

5

6

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

132

5

6

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

133

56 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

134

5 6 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

135

5 6+ Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

136

11 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

137

11 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

138

11 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

139

11

6

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

140

11

6

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

141

11

6

2

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

142

11

6

2

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

143

11

62

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

144

11

6 2

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

145

11

6 2/

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

146

11

3

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

147

11

3

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

148

11

3

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

149

11

3

7

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

150

11

3

7

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

151

11

37

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

152

11

3 7

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

153

11

3 7+

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

154

11

10

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

155

11

10

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

156

11

10

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

157

11

10

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

158

11 10

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

159

11 10-

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

160

1

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

161

1 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

162

1 Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)

163

1

Answer:

Stack

Postfix Expression:5 3 2 * + 6 2 / 7 + -

Postfix ExpressionEvaluation (cont.)