45
Stack Stack Mobile Game Programming Mobile Game Programming [email protected] Division of Digital Contents, Dongseo Univ. September 8, 2016

01 stack 20160908_jintaek_seo

Embed Size (px)

Citation preview

Page 1: 01 stack 20160908_jintaek_seo

StackStackMobile Game ProgrammingMobile Game Programming

[email protected] of Digital Contents, Dongseo Univ.

September 8, 2016

Page 2: 01 stack 20160908_jintaek_seo

Presentation Outline ADT Data Structure Stack Implementation of a stack

– KStack– template KStack

Algorithms with stack– Base conversion of numeral system– Evaluation of postfix notation– Conversion from infix to postfix notation

2

Page 3: 01 stack 20160908_jintaek_seo

Abstract Data Type(ADT) an abstract data type (ADT) is a mathematical model

 for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data.

possible values, possible operations on data of this type, and the behavior of these operations.

Example: integers– Values …, -2, -1, 0, 1, 2, …– Operations

• Addition• Subtraction• Multiplication• Division• Greater than, etc…

3

Page 5: 01 stack 20160908_jintaek_seo

Data Structure(s) a data structure is a particular way of organizing data in

a computer so that it can be used efficiently. Data Structures implement one or more particular 

abstract data types (ADT). Data Structure is a concrete implementation of the

contract provided by an ADT. Examples:

– Array, Stack, List– Associative array, Record(struct)– Union– Set– Graph, Tree– Class– …

5

Page 6: 01 stack 20160908_jintaek_seo

Stack an abstract data type that serves as a collection of

elements. two principal operations:

– push, which adds an element to the collection.– pop, which removes the most recently added element that was

not yet removed. LIFO (for last in, first out) the push and pop operations occur only at one end of

the structure, referred to as the top of the stack. may be implemented to have a bounded capacity. Overflow

– when the stack is full and does not contain enough space to accept an entity to be pushed.

6

Page 7: 01 stack 20160908_jintaek_seo

Stack runtime Simple representation of a stack runtime.

7

Page 8: 01 stack 20160908_jintaek_seo

Hardware stacks#include "stdafx.h"

void Test( int i ){ i = 4;}void main(){ int i; int j; i = 3; j = i; Test( i ); printf( "%d\r\n", i );}

8

Page 9: 01 stack 20160908_jintaek_seo

Stack Settings in Visual Studio 2013 Property pageLinkerSystemStack Reserve Size

9

Page 10: 01 stack 20160908_jintaek_seo

Working Example#include <stdio.h>void Swap(int a, int b) {

int t=a;a=b;b=t;

}//Swapvoid main() {

int a=2;int b=3;Swap(a,b);printf("%d,%d\n",a,b);

}//main

10

Page 11: 01 stack 20160908_jintaek_seo

#include <stdio.h>void Swap(int a, int b) {

int t=a;a=b;b=t;

}//Swapvoid main() {

int a=2;int b=3;Swap(a,b);printf("%d,%d\n",a,b);

}//main

11

Page 12: 01 stack 20160908_jintaek_seo

#include <stdio.h>void Swap(int a, int b) {

int t=a;a=b;b=t;

}//Swapvoid main() {

int a=2;int b=3;Swap(a,b);printf("%d,%d\n",a,b);

}//main

12

Page 13: 01 stack 20160908_jintaek_seo

#include <stdio.h>void Swap(int a, int b) {

int t=a;a=b;b=t;

}//Swapvoid main() {

int a=2;int b=3;Swap(a,b);printf("%d,%d\n",a,b);

}//main

13

Page 14: 01 stack 20160908_jintaek_seo

Revised working example#include <stdio.h>void Swap(int *a,int *b) {

int t=*a;//assigns the contents of a to t*a=*b;//assigns the contents of b to the location of contents of a.*b=t;//assigns the t to the location of contents of b

}//Swapvoid main() {

int a=2,b=3;Swap(&a,&b);//pass addresses of a and bprintf("%d,%d\n",a,b);

}//main

14

Page 15: 01 stack 20160908_jintaek_seo

15

#include <stdio.h>void Swap(int *a,int *b) {

int t=*a;//assigns the contents of a to t*a=*b;//assigns the contents of b to the location of contents of a.*b=t;//assigns the t to the location of contents of b

}//Swapvoid main() {

int a=2,b=3;Swap(&a,&b);//pass addresses of a and bprintf("%d,%d\n",a,b);

}//main

Page 16: 01 stack 20160908_jintaek_seo

16

#include <stdio.h>void Swap(int *a,int *b) {

int t=*a;//assigns the contents of a to t*a=*b;//assigns the contents of b to the location of contents of a.*b=t;//assigns the t to the location of contents of b

}//Swapvoid main() {

int a=2,b=3;Swap(&a,&b);//pass addresses of a and bprintf("%d,%d\n",a,b);

}//main

Page 17: 01 stack 20160908_jintaek_seo

17

#include <stdio.h>void Swap(int *a,int *b) {

int t=*a;//assigns the contents of a to t*a=*b;//assigns the contents of b to the location of contents of a.*b=t;//assigns the t to the location of contents of b

}//Swapvoid main() {

int a=2,b=3;Swap(&a,&b);//pass addresses of a and bprintf("%d,%d\n",a,b);

}//main

Page 18: 01 stack 20160908_jintaek_seo

18

#include <stdio.h>void Swap(int *a,int *b) {

int t=*a;//assigns the contents of a to t*a=*b;//assigns the contents of b to the location of contents of a.*b=t;//assigns the t to the location of contents of b

}//Swapvoid main() {

int a=2,b=3;Swap(&a,&b);//pass addresses of a and bprintf("%d,%d\n",a,b);

}//main

Page 19: 01 stack 20160908_jintaek_seo

C++ implementations of a stackclass KStack{public: KStack() : m_sp( 0 ) { } void Push( int data_ ); bool Pop( int& outData_ ); bool IsEmpty() const;private: int m_sp; int m_data[ 100 ];};//class KStack

19

Page 20: 01 stack 20160908_jintaek_seo

void KStack::Push( int data_ ){ m_data[ m_sp ] = data_; m_sp += 1;}bool KStack::Pop( int& outData_ ){ if( m_sp <= 0 ) return false; m_sp -= 1; outData_ = m_data[ m_sp ]; return true;}bool KStack::IsEmpty() const{ return m_sp == 0;}

20

Page 21: 01 stack 20160908_jintaek_seo

Template KStacktemplate<typename T, int STACK_SIZE>class KStack{public: KStack() : m_sp( 0 ) { } void Push( T data_ ); bool Pop( T& outData_ ); bool IsEmpty() const;private: int m_sp; T m_data[ STACK_SIZE ];};//class KStack

21

Page 22: 01 stack 20160908_jintaek_seo

template<typename T, int STACK_SIZE>void KStack<T,STACK_SIZE>::Push( T data_ ){ m_data[ m_sp ] = data_; m_sp += 1;}template<typename T, int STACK_SIZE>bool KStack<T, STACK_SIZE>::Pop( T& outData_ ){ if( m_sp <= 0 ) return false; m_sp -= 1; outData_ = m_data[ m_sp ]; return true;}

22

Page 23: 01 stack 20160908_jintaek_seo

void main(){ KStack<int,100> s; s.Push( 3 ); s.Push( 5 ); int data; const bool bIsPop = s.Pop( data ); printf( "%d\r\n", data );}

23

Page 24: 01 stack 20160908_jintaek_seo

Actual usage of stack in C++ std::stack

template<    class T,    class Container = std::deque<T>

> class stack;

a container adapter that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure.

24

Page 25: 01 stack 20160908_jintaek_seo

#include <stack>

void main(){ std::stack<int> s; int n;

printf( "Enter decimal digit:" ); scanf( "%d", &n ); while( n >= 5 ) { s.push( n % 5 ); n /= 5; }//while

25

printf( "Equivalent Quintett is %d", n );

while( s.empty() == false ) { n = s.top(); printf( "%d", n ); s.pop(); }//while}//main

Page 26: 01 stack 20160908_jintaek_seo

Algorithms with Stack Convert Decimal Number to Pentamal Number Reverse Polish Notation: Evaluating Postfix Notation Conversion from Infix to Postfix Notation

26

Page 27: 01 stack 20160908_jintaek_seo

Convert Decimal Number to Pentamal Number divide the number by 5. write quotient and remainder.

now quotient is dividend, continue till dividend is less than 5.

the reverse order of the remainder is a pentamal number.

27

Page 28: 01 stack 20160908_jintaek_seo

void main(){ KStack<int, 100> s; int n;

printf( "Enter decimal number:" ); scanf( "%d", &n ); while( n >= 5 ) { s.Push( n % 5 ); n /= 5; }//while printf( "Equivalent pentamal is ", n ); while( s.Pop( n ) ) printf( "%d", n );}//main

28

Page 29: 01 stack 20160908_jintaek_seo

Practice Write a class that convert decimal number to n-ary

number.– binary– trimal– tetramal– pentamal– hexamal– heptamal– octal– nonamal

there must a getter that gets the converted number in string.

29

Page 30: 01 stack 20160908_jintaek_seo

Reverse Polish Notation: Postfix Notation the operators follow their operands; for instance, to add 3 and 4, one would write "3 4 +"

rather than "3 + 4". "3 − 4 + 5“ "3 4 − 5 +"

– if there are multiple operations, the operator is given immediately after its second operand.

it removes the need for parentheses that are required by infix.

"3 − (4 × 5)“ is quite different from "(3 − 4) × 5". In postfix, it could be written "3 4 5 × −", which

unambiguously means "3 (4 5 ×) −" which reduces to "3 20 −“.

30

Page 31: 01 stack 20160908_jintaek_seo

Evaluation postfix expression While there are input tokens left

– Read the next token from input.– If the token is a value

• Push it onto the stack.– Otherwise, the token is an operator (operator here includes both

operators and functions). • It is known a priori that the operator takes n arguments.• If there are fewer than n values on the stack

– (Error) The user has not input sufficient values in the expression.• Else, Pop the top n values from the stack.• Evaluate the operator, with the values as arguments.• Push the returned results, if any, back onto the stack.

If there is only one value in the stack – That value is the result of the calculation.

Otherwise, there are more values in the stack – (Error) The user input has too many values.

31

Page 32: 01 stack 20160908_jintaek_seo

example: "5 + ((1 + 2) × 4) − 3“ 5 1 2 + 4 × + 3 −

32

Input Operation Stack Comment5 Push 5 

1Push 1

 5

2

Push 2

 1

5

+Add 3

Pop two values (1, 2) and push result (3)5

4

Push 4

 3

5

×Multiply 12

Pop two values (3, 4) and push result (12)5

+ Add 17Pop two values (5, 12) and push result (17)

3Push value 3

 17

− Subtract 14Pop two values (17, 3) and push result (14)

  Result 14 

Page 33: 01 stack 20160908_jintaek_seo

Practice Write a class that evaluates a postfix notation.

33

Page 34: 01 stack 20160908_jintaek_seo

Conversion from Infix to Postfix NotationWhile there are tokens to be read:•Read a token.•If the token is a number, then add it to the output queue.•If the token is an operator, o1, then:

• while there is an operator token o2, at the top of the operator stack and either

• o1 is left-associative and its precedence is less than or equal to that of o2, or

• o1 is right associative, and has precedence less than that of o2,• pop o2 off the operator stack, onto the output queue;

• at the end of iteration push o1 onto the operator stack.•If the token is a left parenthesis (i.e. "("), then push it onto the stack.

34

Page 35: 01 stack 20160908_jintaek_seo

• If the token is a left parenthesis (i.e. "("), then push it onto the stack.

• If the token is a right parenthesis (i.e. ")"):• Until the token at the top of the stack is a left parenthesis, pop

operators off the stack onto the output queue.• Pop the left parenthesis from the stack, but not onto the output

queue.• If the stack runs out without finding a left parenthesis, then there

are mismatched parentheses.When there are no more tokens to read:• While there are still operator tokens in the stack:

• If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses.

• Pop the operator onto the output queue.Exit.

35

Page 36: 01 stack 20160908_jintaek_seo

Detailed example: 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 predefined operator properties table.

How can we implement this properties table?

36

Page 37: 01 stack 20160908_jintaek_seo

Detailed example: 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3

37

Page 38: 01 stack 20160908_jintaek_seo

Detailed example: 3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3

38

Page 39: 01 stack 20160908_jintaek_seo

Practice Write a class that converts Infix notation to Postfix

notation. Expand the features of existing class(written in previous

learning).

39

Page 40: 01 stack 20160908_jintaek_seo

int priority( char a ) { int temp = 0;

if( a == '^' ) temp = 4; else if( a == '*' || a == '/' ) temp = 3; else if( a == '+' || a == '-' ) temp = 2; return temp;}

int main() { std::string infix; infix = "3+4*2/(1-5)^2^3";

std::stack<char> operator_stack; std::string postfix;

40

Page 41: 01 stack 20160908_jintaek_seo

for( unsigned i = 0; i < infix.length(); i++ ) { if( infix[ i ] == '+' || infix[ i ] == '-' || infix[ i ] == '*' || infix[ i ] == '/' ) { // } else if( infix[ i ] == '^' ) { // } else if( infix[ i ] == '(' ) { operator_stack.push( infix[ i ] ); } else if( infix[ i ] == ')' ) { while( operator_stack.top() != '(' ) { postfix += operator_stack.top(); operator_stack.pop(); } operator_stack.pop(); } else { postfix += infix[ i ]; } }

41

Page 42: 01 stack 20160908_jintaek_seo

if( infix[ i ] == '+' || infix[ i ] == '-' || infix[ i ] == '*' || infix[ i ] == '/' ) { while( !operator_stack.empty() && priority( infix[ i ] ) <= priority( operator_stack.top() ) ) { postfix += operator_stack.top(); operator_stack.pop(); } operator_stack.push( infix[ i ] ); } else if( infix[ i ] == '^' ) { while( !operator_stack.empty() && priority( infix[ i ] ) < priority( operator_stack.top() ) ) { postfix += operator_stack.top(); operator_stack.pop(); } operator_stack.push( infix[ i ] ); } else if( infix[ i ] == '(' ) {

42

Page 43: 01 stack 20160908_jintaek_seo

while( !operator_stack.empty() ) { postfix += operator_stack.top(); operator_stack.pop(); }//while

std::cout << postfix << std::endl;

return 0;}

43

Page 44: 01 stack 20160908_jintaek_seo

Actual usage of equation evaluation in C++ boost::math boost::spirit

44

Page 45: 01 stack 20160908_jintaek_seo

End https://en.wikipedia.org/wiki/Abstract_data_type https://en.wikipedia.org/wiki/Reverse_Polish_notation https://en.wikipedia.org/wiki/Shunting-yard_algorithm http://www.cplusplus.com/forum/beginner/16722/

45