18
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks

1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks

Embed Size (px)

Citation preview

1

Data Structures

CSCI 132, Spring 2014Lecture 6

Applications using Stacks

2

Recall the Stack classtypedef int Stack_entry;

const int maxstack =10 ; //small value for testing

class Stack {

public:

Stack();

bool empty()const;

Error_code pop();

Error_code top(Stack_entry &item)const;

Error_code push(const Stack_entry &item);

private:

int count ;

Stack_entry entry [maxstack ];

};

3

Implementing push( )

Error_code Stack ::push(const Stack_entry &item)

{

//Implement push() here

}

4

Implementing push( )

Error_code Stack ::push(const Stack_entry &item)

{

Error_code outcome =success ;

if (count >=maxstack) {

outcome =overflow ;

} else {

entry [count ]=item ;

count++;

}

return outcome ;

}

5

Implementing pop( )

Error_code Stack ::pop()

{

}

6

Implementing pop( )

Error_code Stack ::pop()

{

Error_code outcome =success ;

if (count ==0) {

outcome =underflow ;

} else {

count-- ;

}

return outcome ;

}

7

Implementing top( )

Error_code Stack ::top(Stack_entry &item) const

{

//implement top( )

}

8

Implementing top( )

Error_code Stack ::top(Stack_entry &item) const

{

Error_code outcome =success ;

if (count ==0){

outcome =underflow ;

} else {

item =entry [count - 1];

}

return outcome ;

}

9

Implementing empty( )

bool Stack ::empty( )const

{

//implement empty( )

}

10

Implementing empty( )

bool Stack ::empty( )const

{

bool outcome =true;

if (count >0) {

outcome =false;

}

return outcome ;

}

11

Implementing Stack( )

Stack ::Stack()

{ //implement Stack( )

}

12

Implementing Stack( )

Stack ::Stack()

{

count =0 ;

}

13

Reverse Polish Calculator:A stack application

• Numbers are pushed onto a stack as they are entered.

• An arithmetic operation is done on the top two items in the stack, which are popped off the stack. The result is pushed onto the stack.

• Reverse polish arithmetic allows you to do any arithmetic calculation without using parentheses.

• For example, the computation (6 + 2) is written as 6 2 + in reverse polish notation.

14

Example

Regular notation: (6 + 2) / (7 - 3)

Reverse Polish: 6 2 + 7 3 - /

6 66 2 2

287

888+ 7 7 43

3

- /

15

Rules for Calculator use

User enters a command:

?, =, +, -, *, or /

? means the next entry is a number.

= means print the result (top of stack).

All others mean perform the given arithmetic operation.

The main calculator program #include <stack> //stack class from the Standard Template Library

int main (void) {

char command ;

Stack stored_numbers;

cout <<"Enter a command: ";

cin >> command;

command = tolower(command);

while (command != 'q'){

if (command == '?'||command == '='||command == '+' ||

command == '-'||command == '*'||command == '/' ) {

do_command(command, stored_numbers);

} else {

cout <<"Please enter a valid command:"<<endl

<<"[?]push to stack [=]print top "<<endl

<<"[+][-][*][/]are arithmetic operations "<<endl

<<"[Q ]uit."<<endl;

}

cout << "Enter a command: ";

cin >> command;

command = tolower(command);

}

}

17

Processing commands

void do_command(char command ,Stack &numbers)

{

double p ,q ;

switch (command){

case '?':

break;

case '=':

break;

cout <<"Enter a real number:" ;

cin >>p ;

if (numbers.push(p)==overflow) {

cout <<"Warning:Stack full,lost number "<<endl;

}

if (numbers.top(p) == underflow) {

cout << "Stack empty." << endl;

} else {

cout << p <<endl;

}

18

More number processingcase '+':

break;

//Add options for further user commands.

} //end switch

} //end do_command

if (numbers.top(p)==underflow){

cout <<"Stack empty "<<endl ;

} else {

numbers.pop();

if (numbers.top(q)==underflow){

cout <<"Stack has just one entry "<<endl ;

numbers.push(p);

} else {

numbers.pop();

if (numbers.push(q + p)==overflow) {

cout <<"Warning:Stack full,lost result "<<endl ;

}

}

}