C++ Classes and Data Structures

Download Report

Transcript C++ Classes and Data Structures

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
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
6
bool operator >( int num )
{ if ( price > num ) return true; return false; }
6 };
7
8 int main( )
9 {
10
CarType myCar;
.
.
.
16
17
if ( myCar > 2000 )
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
6
bool operator >( int num )
{ if ( price > num ) return true; return false; }
6 };
7
8 int main( )
9 {
10
CarType myCar;
.
.
.
16
17
if ( myCar > 2000 )
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
{
public:
void setBalance( float amount );
bool writeCheck( Check amount );
Necessary, so
the compiler
knows what
“Check” is
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
{
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;
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)
};
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 );
bool writeCheck( Check amount );
void deposit( float amount );
float getBalance( );
Check getLastCheck( );
Now, we need a
special line of
code to tell the
compiler this is
a class template
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 );
bool writeCheck( Check amount );
void deposit( float amount );
float getBalance( );
Check getLastCheck( );
Now, we need a
special line of
code to tell the
compiler this is
a class template
float getLastDeposit( );
private:
float balance;
Check lastCheck;
float lastDeposit;
};
18
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;
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
};
19
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;
Now, since we
are using a more
general
DataType, we
use it for the
type wherever
appropriate
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( );
float getLastDeposit( );
private:
float balance;
DataType lastCheck;
float lastDeposit;
Finally, we add a
line to include the
implementation file
(not done in an
ordinary class)
};
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( );
float getLastDeposit( );
private:
float balance;
DataType lastCheck;
float lastDeposit;
};
Finally, we add a
line to include the
implementation file
(not done in an
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( );
DataType getLastCheck( );
float getLastDeposit( );
private:
float balance;
DataType lastCheck;
float lastDeposit;
};
#include “checkbook.cpp”
The client can also
have the compiler
make more than
one class from the
class template,
each having a
different type for
DataType
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
19
20
21
22
template <class DataType>
void Checkbook<DataType>::setBalance( float amount )
{
balance = amount;
}
30
The Implementation File
for a Class Template (cont.)
23
24
25
26
27
28
29
30
31
template <class DataType>
bool Checkbook<DataType>::writeCheck( DataType amount )
{
if ( amount > balance )
return false;
balance -= amount;
lastCheck = amount;
return true;
}
31
The Implementation File
for a Class Template (cont.)
23
24
25
26
27
28
29
30
31
template <class DataType>
bool Checkbook<DataType>::writeCheck( DataType amount )
{
if ( amount > balance )
If the client wants
return false;
to use a Check
balance -= amount;
struct for
lastCheck = amount;
DataType, we have
return true;
some overloaded
}
operators here…
32
The Implementation File
for a Class Template (cont.)
23
24
25
26
27
28
29
30
31
template <class DataType>
bool Checkbook<DataType>::writeCheck( DataType amount )
{
if ( amount > balance )
return false;
balance -= amount;
struct assignment
lastCheck = amount;
is allowed
return true;
}
33
The Implementation File
for a Class Template (cont.)
32
33
34
35
36
37
38
39
40
41
42
43
template <class DataType>
void Checkbook<DataType>::deposit( float amount )
{
balance += amount;
lastDeposit = amount;
}
template <class DataType>
float Checkbook<DataType>::getBalance( )
{
return balance;
}
34
The Implementation File
for a Class Template (cont.)
61
62
63
64
65
66
67
68
69
70
71
template <class DataType>
DataType Checkbook<DataType>::getLastCheck( )
{
return lastCheck;
}
template <class DataType>
float Checkbook<DataType>::getLastDeposit( )
{
return lastDeposit;
}
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
//
-=
left operand: float
right operand: object
//
used to subtract the amount of the check in the
//
struct object from the balance
36
Program Using the
Class Template
1
2
3
4
5
6
#include <iostream>
#include <iomanip>
#include <string>
#include "checkbook.h"
using namespace std;
Needed in the main
program for both
classes and class
templates
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 )
16
{ bal -= ch.amt; }
from writeCheck:
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
27
28
29
30
johnsCheckbook.setBalance( 1000 );
susansCheckbook.setBalance( 2000 );
cout << "John, your balance is $1000.00." << endl;
cout << "Susan, your balance is $2000.00." << endl;
40
Program Using the
Class Template (cont.)
31
32
33
34
35
36
37
38
39
cout << "John, enter your check amount: $";
cin >> amount;
if ( johnsCheckbook.writeCheck( amount ) ) {
cout << "Your check was accepted." << endl;
johnsCheckAccepted = true;
}
else
cout <<
“Check amount is higher than balance” << endl;
41
Program Using the
Class Template (cont.)
40
41
42
43
44
45
46
47
cout << "Susan, enter the check number for your check: ";
cin >> susansCheck.checkNum;
cin.ignore( );
cout << "Please also enter any comment you wish” <<
“ to make about the check: “ << endl;
getline( cin, susansCheck.checkComment );
cout << "Susan, enter your check amount: $";
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
56
57
58
59
60
61
62
cout << fixed << showpoint << setprecision( 2 );
cout << "John, your balance is: $" <<
johnsCheckbook.getBalance( ) << endl;
if ( johnsCheckAccepted )
cout << "Your last check amount is: $" <<
johnsCheckbook.getLastCheck( ) << endl;
cout << "Susan, your balance is: $" <<
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;
72
}
73
74 return 0;
75 }
45
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 ))
Postfix Expression:
5
Not even an operator
here, so we don’t pop
anything.
(
(
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 + 7 ))
Postfix Expression:
53
This is an operator, but
its precedence is less
than *, so we don’t pop.
+
(
(
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 + Was the topmost
element – order can
be important here!
3
*
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