chap2 - Chapter 2 Overloaded Operators, Class Templates,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 2 Overloaded Operators, Class Templates, and Abstraction 1 Binary Operators A binary operator acts upon two operands binary operator x+a operands 2 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 3 Example 1 struct CarType { 2 3 4 string maker; int year; float price; 5 bool operator >( int num ) 6 { if ( price > num ) return true; return false; } 6 7 }; 8 9 int main( ) { 10 CarType myCar; . . . 16 if ( myCar > 2000 ) 17 cout << “My car is more than 2000!” << endl; 18 19 return 0; } 4 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 5 Right Operand 1 struct CarType { 2 3 4 string maker; int year; float price; 5 bool operator >( int num ) 6 { if ( price > num ) return true; return false; } 6 7 }; 8 9 int main( ) { 10 CarType myCar; . . . 16 if ( myCar > 2000 ) 17 cout << “My car is more than 2000!” << endl; 18 19 return 0; } 6 Adding Another Overloaded Operator > for “myCar > yourCar” 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 }; 7 Adding an Overloaded Operator + for “myCar + yourCar” 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; } 8 }; 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 9 Adding an Overloaded Operator for “2000 < myCar” 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; } 10 A Check Struct for the Checkbook Class (to Replace float) struct Check { float checkAmount; int checkNum; string date; string receiver; }; 11 The First Class 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; }; 12 The Second Class Check struct placed here class Checkbook Necessary, so the compiler { knows what public: void setBalance( float amount ); “Check” is bool writeCheck( Check amount ); void deposit( float amount ); float getBalance( ); Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; 13 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. 14 Making a Class Template Check struct placed here class Checkbook First, we don’t need the Check struct here – the bool writeCheck( Check amount ); client can make void deposit( float amount ); it the way they float getBalance( ); want in the main Check getLastCheck( ); program (the float getLastDeposit( ); private: compiler will see float balance; it before it Check lastCheck; makes the class) float lastDeposit; { public: void setBalance( float amount ); }; 15 Making a Class Template (cont.) 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; }; 16 Making a Class Template (cont.) class Checkbook { public: void setBalance( float amount ); Now, we need a special line of bool writeCheck( Check amount ); tell the code to void deposit( float amount ); compiler this is float getBalance( ); a class template Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; 17 Making a Class Template (cont.) template <class DataType> class Checkbook { public: void setBalance( float amount ); Now, we need a special line of bool writeCheck( Check amount ); code to tell the void deposit( float amount ); compiler this is float getBalance( ); a class template Check getLastCheck( ); float getLastDeposit( ); private: float balance; Check lastCheck; float lastDeposit; }; 18 Making a Class Template (cont.) template <class DataType> class Checkbook you can think of DataType as a “variable name” bool writeCheck( Check amount ); for a data type – void deposit( float amount ); its value can be float getBalance( ); “float” or Check getLastCheck( ); “Check” or float getLastDeposit( ); private: anything else float balance; the client would Check lastCheck; like it to be float lastDeposit; { public: void setBalance( float amount ); }; 19 Making a Class Template (cont.) template <class DataType> class Checkbook Now, since we are using a more general bool writeCheck( Check amount ); DataType, we void deposit( float amount ); use it for the float getBalance( ); type wherever Check getLastCheck( ); appropriate float getLastDeposit( ); { public: void setBalance( float amount ); private: float balance; Check lastCheck; float lastDeposit; }; 20 Making a Class Template (cont.) 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; }; 21 Making a Class Template (cont.) 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; }; 22 Making a Class Template (cont.) 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; }; 23 Making a Class Template (cont.) 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; }; 24 Making a Class Template (cont.) 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; }; 25 Making a Class Template (cont.) 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; }; 26 Making a Class Template (cont.) template <class DataType> class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); Finally, we add a float getLastDeposit( ); line to include the private: implementation file float balance; (not done in an DataType lastCheck; ordinary class) float lastDeposit; }; 27 Making a Class Template (cont.) template <class DataType> class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); DataType getLastCheck( ); Finally, we add a float getLastDeposit( ); line to include the private: implementation file float balance; (not done in an DataType lastCheck; float lastDeposit; ordinary class) }; #include “checkbook.cpp” 28 Making a Class Template (cont.) template <class DataType> class Checkbook { public: void setBalance( float amount ); bool writeCheck( DataType amount ); void deposit( float amount ); float getBalance( ); The client can also DataType getLastCheck( have the compiler ); float getLastDeposit( ); make more than private: one class from the float balance; class template, DataType lastCheck; each having a float lastDeposit; }; different type for DataType #include “checkbook.cpp” 29 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. 18 template <class DataType> 19 void Checkbook<DataType>::setBalance( float amount ) 20 { 21 balance = amount; 22 } 30 The Implementation File for a Class Template (cont.) 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 } 31 The Implementation File for a Class Template (cont.) 23 template <class DataType> 24 bool Checkbook<DataType>::writeCheck( DataType amount ) If the client wants 25 { to 26 if ( amount > balance ) use a Check struct for 27 return false; DataType, we 28 balance -= amount; 29 lastCheck = amount; have some overloaded 30 return true; operators here… 31 } 32 The Implementation File for a Class Template (cont.) 23 template <class DataType> 24 bool Checkbook<DataType>::writeCheck( DataType amount ) 25 { 26 if ( amount > balance ) 27 return false; 28 balance -= amount; struct assignment 29 lastCheck = amount; is allowed 30 return true; 31 } 33 The Implementation File for a Class Template (cont.) 32 template <class DataType> 33 void Checkbook<DataType>::deposit( float amount ) 34 { 35 balance += amount; 36 lastDeposit = amount; 37 } 38 39 template <class DataType> 40 float Checkbook<DataType>::getBalance( ) 41 { 42 return balance; 43 } 34 The Implementation File for a Class Template (cont.) 61 template <class DataType> 62 DataType Checkbook<DataType>::getLastCheck( ) 63 { 64 return lastCheck; 65 } 66 67 template <class DataType> 68 float Checkbook<DataType>::getLastDeposit( ) 69 { 70 return lastDeposit; 71 } 35 Example of Comments for a Class Template Place these comments above the class template // 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 36 // -= left operand: float right Program Using the Class Template 1 2 3 4 5 6 #include #include #include #include <iostream> <iomanip> Needed in the main <string> "checkbook.h" program for both classes and class templates using namespace std; 37 Program Using the Class Template (cont.) 7 struct MyCheck { from writeCheck: 8 float amt; 26 if ( amount > balance ) 9 int checkNum; 10 string checkComment; 11 bool operator >( float bal ) 12 { if ( amt > bal ) return true; return false; } 13 }; 14 15 void operator -=( float & bal, MyCheck ch ) from writeCheck: 16 { bal -= ch.amt; } 28 balance -= amount; 38 Program Using the Class Template (cont.) 17 int main( ) 18 { 19 Checkbook<float> johnsCheckbook; 20 Checkbook<MyCheck> susansCheckbook; 21 22 MyCheck susansCheck; 23 float amount; 24 bool johnsCheckAccepted = false, 25 susansCheckAccepted = false; 39 Program Using the Class Template (cont.) 26 johnsCheckbook.setBalance( 1000 ); 27 susansCheckbook.setBalance( 2000 ); 28 29 cout << "John, your balance is $1000.00." << endl; 30 cout << "Susan, your balance is $2000.00." << endl; 40 Program Using the Class Template (cont.) 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 else 38 cout << 39 “Check amount is higher than balance” << endl; 41 Program Using the Class Template (cont.) 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; 42 Program Using the Class Template (cont.) 48 if ( susansCheckbook.writeCheck( susansCheck ) ) { 49 cout << "Your check was accepted." << endl; 50 susansCheckAccepted = true; 51 } 52 else 53 cout << 54 “Check amount is higher than balance” << endl; 43 Program Using the Class Template (cont.) 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; 44 Program Using the Class Template (cont.) 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; 45 72 } 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 46 Push Push means place a new data element at the top of the stack 11 5 3 17 47 Push (cont.) Push means place a new data element at the top of the stack 11 3 5 17 48 Push (cont.) Push means place a new data element at the top of the stack 3 11 5 17 49 Push (cont.) Push means place a new data element at the top of the stack 3 11 5 17 50 Pop Pop means take a data element off the top of the stack 3 11 5 17 51 Pop (cont.) Pop means take a data element off the top of the stack 3 11 5 17 52 Pop (cont.) Pop means take a data element off the top of the stack 11 3 5 17 53 Pop (cont.) Pop means take a data element off the top of the stack 11 5 3 17 54 Peek Peek means retrieve the top of the stack without removing it 3 11 5 17 55 Peek (cont.) Peek means retrieve the top of the stack without removing it 3 11 5 3 17 56 Peek (cont.) Peek means retrieve the top of the stack without removing it 3 11 5 3 17 57 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 58 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 59 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… 60 Infix to Postfix Conversion Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: Stack 61 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: First, we push an ‘(‘ onto an empty stack. Stack 62 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: First, we push an ‘(‘ onto an empty stack. ( Stack 63 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 ) Postfix Expression: Then, we append a ‘)’ to the infix expression ( Stack 64 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: Then, we append a ‘)’ to the infix expression ( Stack 65 Infix to Postfix Conversion (cont.) 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. ( Stack 66 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 67 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: ( Stack 68 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: ( ( Stack 69 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: ( ( Stack 70 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 5 ( ( Stack 71 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 5 ( ( Stack 72 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Not even an operator here, so we don’t pop Postfix anything. Expression: 5 ( ( Stack 73 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 5 ( ( Stack 74 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 5 + ( ( Stack 75 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 5 + ( ( Stack 76 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 53 + ( ( Stack 77 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 53 + ( ( Stack 78 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 This is an operator, but + 7 )) its precedence is less than *, so we don’t pop. Postfix Expression: 53 + ( ( Stack 79 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 53 + ( ( Stack 80 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 53 * + ( ( Stack 81 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 53 * + ( ( Stack 82 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532 * + ( ( Stack 83 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532 * + ( ( Stack 84 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532 + ( * ( Stack 85 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532* + ( ( Stack 86 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532* + ( ( Stack 87 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( ( Stack 88 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( ( Stack 89 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( Stack 90 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( Stack 91 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( Stack 92 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( Stack 93 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( ( Stack 94 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+ ( ( Stack 95 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+6 ( ( Stack 96 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+6 ( ( Stack 97 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+6 / ( ( Stack 98 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+6 / ( ( Stack 99 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62 / ( ( Stack 100 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62 / ( ( Stack 101 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62 ( / ( Stack 102 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/ ( ( Stack 103 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/ + ( ( Stack 104 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/ + ( ( Stack 105 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7 + ( ( Stack 106 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7 + ( ( Stack 107 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7 ( + ( Stack 108 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+ ( ( Stack 109 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+ ( ( Stack 110 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+ ( Stack 111 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+ ( Stack 112 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+ - ( Stack 113 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+( Stack 114 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+( Stack 115 Infix to Postfix Conversion (cont.) Infix Expression: ( 5 + 3 * 2 ) – ( 6 / 2 + 7 )) Postfix Expression: 532*+62/7+Stack 116 Postfix Expression Evaluation Postfix Expression: 5 3 2 * + 6 2 / 7 + - Stack 117 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + When evaluating a postfix expression, a number encountered is simply pushed onto the stack. Stack 118 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 5 Stack 119 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 5 Stack 120 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 5 Stack 121 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 5 Stack 122 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 2 3 5 Stack 123 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 2 3 5 Stack 124 Postfix Expression Evaluation (cont.) 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… 2 3 5 Stack 125 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 2 3 5 Stack 126 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 2 5 Stack 127 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 * 2 5 Stack 128 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 Was the topmost element – order can be important here! * 2 5 Stack 129 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 5 Stack 130 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 5 Stack 131 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 5 Stack 132 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 5 Stack 133 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 5 6 Stack 134 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 5 + 6 Stack 135 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 11 Stack 136 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 11 Stack 137 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 11 Stack 138 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 11 Stack 139 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 11 Stack 140 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 2 6 11 Stack 141 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 2 6 11 Stack 142 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 2 6 11 Stack 143 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 2 11 Stack 144 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 6 / 2 11 Stack 145 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 11 Stack 146 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 11 Stack 147 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 11 Stack 148 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 7 3 11 Stack 149 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 7 3 11 Stack 150 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 7 3 11 Stack 151 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 7 11 Stack 152 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 3 + 7 11 Stack 153 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 10 11 Stack 154 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 10 11 Stack 155 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 10 11 Stack 156 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 10 11 Stack 157 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 11 10 Stack 158 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 11 - 10 Stack 159 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 1 Stack 160 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 1 Stack 161 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - 1 Stack 162 Postfix Expression Evaluation (cont.) Postfix Expression: 5 3 2 * + 6 2 / 7 + - Answer: 1 Stack 163 ...
View Full Document

This note was uploaded on 04/29/2008 for the course CS 310 taught by Professor Dr.panja during the Spring '08 term at Morehead State.

Ask a homework question - tutors are online