1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks Chapter 11.
Prefix, Postfix, Infix Notation
COSC 2006 Chapter 7 Stacks III
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Stacks  Introduction  Applications  Implementations  Complex Applications.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Classes and Data Structures Jeffrey S. Childs
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures & Algorithms
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
1 C++ Classes and Data Structures Course link…..
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Revised based on textbook author’s notes.
Infix to postfix conversion
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
PART II STACK APPLICATIONS
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stack and Queues Stack implementation using Array
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

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

2 Binary Operators A binary operator acts upon two operands x + ax + 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 { 2string maker; 3int year; 4float price; 5bool operator >( int num ) 6{ if ( price > num ) return true; return false; } 6 }; 7 8 int main( ) 9 { 10CarType myCar;. 16if ( myCar > 2000 ) 17cout << “My car is more than 2000!” << endl; 18return 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 { 2string maker; 3int year; 4float price; 5bool operator >( int num ) 6{ if ( price > num ) return true; return false; } 6 }; 7 8 int main( ) 9 { 10CarType myCar;. 16if ( myCar > 2000 ) 17cout << “My car is more than 2000!” << endl; 18return 0; 19 } Right Operand

7 1 struct CarType { 2string maker; 3int year; 4float price; 5bool operator >( int num ) 6 { if ( price > num ) return true; else return false; } 7bool 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 here Necessary, 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 Template Check 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 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 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 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 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 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 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 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 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 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 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 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 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 19 void Checkbook ::setBalance( float amount ) 20 { 21balance = 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 24 bool Checkbook ::writeCheck( DataType amount ) 25 { 26if ( amount > balance ) 27return false; 28balance -= amount; 29lastCheck = amount; 30return true; 31 } The Implementation File for a Class Template (cont.)

32 23 template 24 bool Checkbook ::writeCheck( DataType amount ) 25 { 26if ( amount > balance ) 27return false; 28balance -= amount; 29lastCheck = amount; 30return 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 24 bool Checkbook ::writeCheck( DataType amount ) 25 { 26if ( amount > balance ) 27return false; 28balance -= amount; 29lastCheck = amount; 30return true; 31 } The Implementation File for a Class Template (cont.) struct assignment is allowed

34 32 template 33 void Checkbook ::deposit( float amount ) 34 { 35balance += amount; 36lastDeposit = amount; 37 } template 40 float Checkbook ::getBalance( ) 41 { 42return balance; 43 } The Implementation File for a Class Template (cont.)

35 61 template 62 DataType Checkbook ::getLastCheck( ) 63 { 64return lastCheck; 65 } template 68 float Checkbook ::getLastDeposit( ) 69 { 70return 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: objectright operand: float //used to compare the amount of the check in the // struct object with the balance //-=left operand: floatright 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 2 #include 3 #include 4 #include "checkbook.h" 5 6 using namespace std; Program Using the Class Template Needed in the main program for both classes and class templates

38 7 struct MyCheck { 8float amt; 9int checkNum; 10string checkComment; 11bool operator >( float bal ) 12{ if ( amt > bal ) return true; return false; } 13 }; 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{ 19Checkbook johnsCheckbook; 20Checkbook susansCheckbook; 21 22MyCheck susansCheck; 23float amount; 24bool johnsCheckAccepted = false, 25susansCheckAccepted = false; Program Using the Class Template (cont.)

40 26johnsCheckbook.setBalance( 1000 ); 27susansCheckbook.setBalance( 2000 ); 28 29cout << "John, your balance is $ " << endl; 30cout << "Susan, your balance is $ " << endl; Program Using the Class Template (cont.)

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

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

43 48if ( susansCheckbook.writeCheck( susansCheck ) ) { 49cout << "Your check was accepted." << endl; 50susansCheckAccepted = true; 51} 52else 53 cout << 54 “Check amount is higher than balance” << endl; Program Using the Class Template (cont.)

44 55cout << fixed << showpoint << setprecision( 2 ); 56cout << "John, your balance is: $" << 57johnsCheckbook.getBalance( ) << endl; 58if ( johnsCheckAccepted ) 59cout << "Your last check amount is: $" << 60johnsCheckbook.getLastCheck( ) << endl; 61cout << "Susan, your balance is: $" << 62susansCheckbook.getBalance( ) << endl; Program Using the Class Template (cont.)

45 63if ( susansCheckAccepted ) { 64MyCheck testSusansCheck; 65testSusansCheck = susansCheckbook.getLastCheck( ); 66cout << "Your last check amount was: $" << 67 testSusansCheck.amt << endl; 68cout << "for check number: " << 69testSusansCheck.checkNum << endl; 70cout << "with check comment: " << 71testSusansCheck.checkComment << endl; 72} 73 74return 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

48 Push (cont.) Push means place a new data element at the top of the stack

49 Push (cont.) Push means place a new data element at the top of the stack

50 Push (cont.) 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

52 Pop (cont.) Pop means take a data element off the top of the stack

53 Pop (cont.) Pop means take a data element off the top of the stack

54 Pop (cont.) Pop means take a data element off the top of the stack

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

56 Peek (cont.) Peek means retrieve the top of the stack without removing it

57 Peek (cont.) Peek means retrieve the top of the stack without removing it

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: * 3 –may be an indefinite number of nested parentheses: ( ( ) * ) / 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: ( * 2 ) – ( 6 / ) Postfix Expression: Stack Infix to Postfix Conversion

62 Stack Infix Expression: ( * 2 ) – ( 6 / ) Postfix Expression: First, we push an ‘(‘ onto an empty stack. Infix to Postfix Conversion (cont.)

63 Stack ( Infix Expression: ( * 2 ) – ( 6 / ) Postfix Expression: First, we push an ‘(‘ onto an empty stack. Infix to Postfix Conversion (cont.)

64 ( Stack Infix Expression: ( * 2 ) – ( 6 / ) Postfix Expression: Then, we append a ‘)’ to the infix expression Infix to Postfix Conversion (cont.)

65 ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: Then, we append a ‘)’ to the infix expression Infix to Postfix Conversion (cont.)

66 ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) 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 Postfix Conversion (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: ( * 2 ) – ( 6 / ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

69 ( ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

70 ( ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

71 ( ( 5 Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

72 ( ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

73 ( ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 Not even an operator here, so we don’t pop anything. Infix to Postfix Conversion (cont.)

74 ( ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

75 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

76 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 Infix to Postfix Conversion (cont.)

77 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

78 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

79 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 3 This is an operator, but its precedence is less than *, so we don’t pop. Infix to Postfix Conversion (cont.)

80 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

81 ( ( + * Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

82 ( ( + * Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: 5 3 Infix to Postfix Conversion (cont.)

83 ( ( + * Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

84 ( ( + * Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

85 ( ( + * Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: Infix to Postfix Conversion (cont.)

86 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * Infix to Postfix Conversion (cont.)

87 ( ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * Infix to Postfix Conversion (cont.)

88 ( ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

89 ( ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

90 ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

91 ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

92 ( - Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

93 ( - Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

94 ( - ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

95 ( - ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + Infix to Postfix Conversion (cont.)

96 ( - ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + 6 Infix to Postfix Conversion (cont.)

97 ( - ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + 6 Infix to Postfix Conversion (cont.)

98 ( - ( / Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + 6 Infix to Postfix Conversion (cont.)

99 ( - ( / Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * + 6 Infix to Postfix Conversion (cont.)

100 ( - ( / Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * Infix to Postfix Conversion (cont.)

101 ( - ( / Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * Infix to Postfix Conversion (cont.)

102 ( - ( / Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * Infix to Postfix Conversion (cont.)

103 ( - ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / Infix to Postfix Conversion (cont.)

104 ( - ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / Infix to Postfix Conversion (cont.)

105 ( - ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / Infix to Postfix Conversion (cont.)

106 ( - ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 Infix to Postfix Conversion (cont.)

107 ( - ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 Infix to Postfix Conversion (cont.)

108 ( - ( + Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 Infix to Postfix Conversion (cont.)

109 ( - ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 + Infix to Postfix Conversion (cont.)

110 ( - ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 + Infix to Postfix Conversion (cont.)

111 ( - Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 + Infix to Postfix Conversion (cont.)

112 ( - Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 + Infix to Postfix Conversion (cont.)

113 ( - Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / 7 + Infix to Postfix Conversion (cont.)

114 ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / Infix to Postfix Conversion (cont.)

115 ( Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / Infix to Postfix Conversion (cont.)

116 Stack Infix Expression: ( * 2 ) – ( 6 / ) ) Postfix Expression: * / Infix to Postfix Conversion (cont.)

117 Postfix Expression: * / Stack Postfix Expression Evaluation

118 Postfix Expression: * / Stack When evaluating a postfix expression, a number encountered is simply pushed onto the stack. Postfix Expression Evaluation (cont.)

119 5Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

120 5Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / 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 Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

* Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

* Stack Postfix Expression: * / Was the topmost element – order can be important here! Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

/ Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

160 1 Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

161 1Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

162 1Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)

163 1 Answer: Stack Postfix Expression: * / Postfix Expression Evaluation (cont.)