Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
Shunting-yard algorithm Infix to postfix conversion Based on
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.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
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.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
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.
Topic 15 Implementing and Using Stacks
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
Evaluation of Expressions
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching materials Generation Group.
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.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
BCA II Data Structure Using C
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
Stacks – Calculator Application
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Infix to Postfix Conversion
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
CSC 143 Java Applications of Trees.
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
More About Stacks: Stack Applications
Data Structures and Algorithms 2/2561
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
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.
Presentation transcript:

Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5

Workshop sign-up Still time : Dave Feil-Seifer Ross Carmara

Infix to postfix * 3 == 7 (because multiplication has higher precidence) 10 – 4 – 3 == 3 (because subtraction proceeds left to right)

Infix to postfix 4 ^ 3 ^ 2 == != 4096 Generally, Rather than:

Precidence A few simple rules: () > ^ > * / > + - Subtraction associates left-to-right Exponentiation associates right to left

Infix Evaluation 1 – 2 – 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 2 == -8 (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) Could you write a program to evaluate stuff like this?

Postfix  If we expressed (1 – 2) – ( ( ( ( 4 ^ 5) * 3) * 6) / (7 ^ ( 2 ^ 2 ) ) ) As 1 2 – 4 5 ^ 3 * 6 * ^ ^ / - Then, we could use the postfix stack evaluator

Postfix evaluation using a stack 1. Make an empty stack 2. Read tokens until EOF a. If operand push onto stack b. If operator i. Pop two stack values ii. Perform binary operation iii. Push result 3. At EOF, pop final result

1 2 – 4 5 ^ 3 * 6 * ^ ^ / -

1

2121

1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 4

1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 5 4

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ /

1 2 – 4 5 ^ 3 * 6 * ^ ^ / - 7

1 2 – 4 5 ^ 3 * 6 * ^ ^ / - -8

But how to go from infix to postfix?  Could you write a program to do it?  What data structures would you use  Stack  Queue  How about a simple case just using “+”    Operands send on to output?  Operator push on stack?  Pop ‘em all at the end?

More complex 2 ^ 5 – 1 == 2 5 ^ 1 – Modify the simple rule? If you are an operator, pop first, then push yourself? ok

Even more complex 3 * 2 ^ ^ * 1 – If you are an operator: Pop if the top of the stack is higher precedence than

Infix to postfix Stack Algorithm Operands : Immediately output Close parenthesis: Pop stack until open parenthesis Operators: 1. Pop all stack symbols until a symbol of lower precedence (or a right-associative symbol of equal precedence) appears. 2. Push operator EOF: pop all remaining stack symbols

1 – 2 ^ 3 ^ 3 – ( * 6) * 7

1

- 1

- 1 2

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^- ^- 1 2

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^- ^

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^^- ^^

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 ^^- ^^

1 – 2 ^ 3 ^ 3 – ( * 6) * ^ ^ -

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 (- ( ^ ^ -

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 (- ( ^ ^ - 4

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 +(- +( ^ ^ - 4

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 +(- +( ^ ^ - 4 5

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 *+(- *+( ^ ^ - 4 5

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 *+(- *+( ^ ^

1 – 2 ^ 3 ^ 3 – ( * 6) * ^ ^ * +

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 *- * ^ ^ * +

1 – 2 ^ 3 ^ 3 – ( * 6) * 7 *- * ^ ^ * + 7

1 – 2 ^ 3 ^ 3 – ( * 6) * ^ ^ * + 7 * -

1 – 2 ^ 3 ^ 3 – ( * 6) * ^ ^ * + 7 * - ((1 – (2 ^ (3 ^ 3))) – (( 4 + (5 * 6)) * 7)) Input To evaluation stack