Stacks – Calculator Application

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 & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
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 2 Data Structures V section 2 Recitation 1.
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
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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
Department of Technical Education Andhra Pradesh
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
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.
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.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
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
Objectives of these slides:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.
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.
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(),
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.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures 5th Week
CSC 172 DATA STRUCTURES.
COMPUTER 2430 Object Oriented Programming and Data Structures I
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
Stack Data Structure, Reverse Polish Notation, Homework 7
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
CSC 143 Java Applications of Trees.
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Applications of Stacks and Queues
More About Stacks: Stack Applications
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 – Calculator Application Alexandra Stefan Last modified 2/6/2018

Infix and Postfix Notation The standard notation we use for writing mathematical expressions is called infix notation. The operators are between the operands. There are two alternative notations: prefix notation: the operator comes before the operands. postfix notation: the operator comes after the operands. Example: infix: 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) - 7 ) prefix: (* 5 (- (* (+ 9 8) (* 4 6)) 7)) postfix: 5 9 8 + 4 6 * * 7 - * No parentheses needed. Can be easily evaluated using a stack. operator operand

Processing a Symbolic Expression How do we process an expression such as: 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) - 7 ) postfix: 5 9 8 + 4 6 * * 7 - * Think of the input as a list of tokens. Assume it is already tokenized A token is a logical unit of input, such as: A number An operator A parenthesis.

Tokens A token is a logical unit of input, such as: A number An operator A parenthesis. What are the tokens in: 51 * (((195 + 8 ) * (4 - 6)) + 7) Answer: 51, *, (, (, (, 195, +, 8, ), *, (, 4, -, 6, ), ), +, 7, ) 19 tokens. Note that a token is NOT a character. For example 195 is one token, but it contains 3 charatcters. We will not discuss how to build tokens from characters. The numbers are the difficult part.

Converting Infix to Postfix Input: a list/stream of tokens in infix order. Output: a list of tokens in postfix order. Assumptions: Each operator has two operands. The input is fully parenthesized. Every operation (that contains an operator and its two operands) is enclosed in parentheses. Fully parenthesized Not fully parenthesized (not allowed as input) (3+5) 3+5 (2+(5-4)) (2+5-4) 2+(5-4) 2+((5-4)) ((2 + 9) - (4 + 5)) (2 + 9) - (4 + 5)

input: a stream of tokens in infix order. output: a list, result, of tokens in postfix order. (Uses a stack: op_stack ) result = empty list op_stack = empty stack while(the input stream is not empty) T = next token If T is left parenthesis, ignore. If T is a number, insertAtEnd(result, T) If T is an operator, push(op_stack, T). If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) T op_stack result list 5 * 9 5 9 + * + 8 5 9 8 ) 5 9 8 + / * / 4 5 9 8 + 4 * / * 6 5 9 8 + 4 6 5 9 8 + 4 6 * 5 9 8 + 4 6 * / - * - 7 5 9 8 + 4 6 * / 7 5 9 8 + 4 6 * / 7 - 5 9 8 + 4 6 * / 7 - * Infix  Postfix ( 5 * ( ( ( 9 + 8 ) / ( 4 * 6 ) ) - 7 ) ) - Numbers go in the result list - Operators go on the op_stack (the stack shown grows to the right) - Left parenthesis, (, are ignored. - At right parenthesis, ), pop operator from op_stack and add it to the result list.

Evaluating Expressions in Postfix Notation while(token list is not empty) T = remove next token (number or operator) from list. If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B (order: B T A, e.g.: B-A) push(stack, C) final_result = pop(stack) Input: a list tokens in infix order. Output: the result of the calculation (a number). Assumption: the list of tokens is be provided as input. Postfix: 5 9 8 + 4 6 * * 7 - * Token list: 5, 9, 8, +, 4, 6, *, *, 7, -, * 6 4 17 5 4*6 = 24 8 9 5 9+8 = 17 24 17 5 17/24 = 408 7 408 5 408-7 = 401 17 5 408 5 401 5 5*401=2005 2005 2005 5,9,8 + 4,6 * / 7 - *

A student pointed out that in the previous example, slide 7 was not evaluating the same postfix expression we got in slide 6. This is what was shown in class. Below I have modified the original infix expression slightly and redid the steps so that the same expression is evaluated. (2:40pm)

input: a stream of tokens in infix order. output: a list, result, of tokens in postfix order. (Uses a stack: op_stack ) result = empty list op_stack = empty stack while(the input stream is not empty) T = next token If T is left parenthesis, ignore. If T is a number, insertAtEnd(result, T) If T is an operator, push(op_stack, T). If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) T op_stack result list 5 * 2 + * + 8 5 2 8 ) 5 2 8 + / * / 6 5 2 8 + 6 - * / - 4 5 2 8 + 6 4 5 2 8 + 6 4 - 5 2 8 + 6 4 - / * - 7 5 2 8 + 6 4 - / 7 5 2 8 + 6 4 - / 7 - 5 2 8 + 6 4 - / 7 - * Infix  Postfix ( 5 * ( ( ( 2 + 8 ) / ( 6 - 4 ) ) - 7 ) ) - Numbers go in the result list - Operators go on the op_stack (the stack shown grows to the right) - Left parenthesis, (, are ignored. - At right parenthesis, ), pop operator from op_stack and add it to the result list.

Evaluating Expressions in Postfix Notation while(token list is not empty) T = remove next token (number or operator) from list. If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B (order: B T A, e.g.: B-A) push(stack, C) final_result = pop(stack) Input: a list tokens in infix order. Output: the result of the calculation (a number). Assumption: the list of tokens is be provided as input. Postfix: 5 2 8 + 6 4 - / 7 - * Token list: 5, 2, 8, +, 6, 4, -, /, 7, -, * 4 6 10 5 6-4 = 2 8 2 5 2+8 = 10 2 10 5 10/2 = 5 7 5 5-7 = -2 10 5 5 -2 5 5*(-2)=-10 -10 -10 5,2,8 + 4,6 - / 7 - * Here the * indicates the multiplication operator, not a pop() operation on the stack. We do not explicitly show the pop operations. Instead, for each operator we pop, pop, calculate, push.