Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
The unorganized person’s data structure
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
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
Yinchong Han Linjia Jiang. Introduction There are three forms of expressions which are prefix, infix and postfix notation. The project will evaluate infix.
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.
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.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
Infix, Postfix, Prefix.
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
The Stack and Queue Types Lecture 10 Hartmut Kaiser
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Operaciones y Variables
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Stacks  Introduction  Applications  Implementations  Complex Applications.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CHP-3 STACKS.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
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.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
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(),
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Review Use of Stack Introduction Stack in our life Stack Operations
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Stacks Chapter 7 introduces the stack data type.
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
Algorithms and Data Structures
Stacks – Calculator Application
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Stack.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stack and Queues Stack implementation using Array
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
(Part 2) Infix, Prefix & Postfix
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures and Algorithms 2/2561
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:

stacks21 Stacks II Adventures in Notation

stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left to right, results of each sub-expression becoming operands to larger expression -- but … All operators are not created equal -- multiplication & division take precedence over addition & subtraction...

stacks23 The trouble with infix... So it isn’t really all that simple -- we must first scan for higher-precedence operations, and evaluate these first -- and that’s not so easy to program -- so … In the calculator program, we rely on parentheses to tell us which operations to perform when -- hence the need for fully- parenthesized expression

stacks24 Alternatives to infix -- prefix Prefix notation, a.k.a. Polish notation Operator precedes operands –infix expression: A + B –prefix expression: +AB Parentheses become unnecessary –infix expression: (A + B) * C –prefix expression: * + A B C

stacks25 Converting from infix to prefix Write out operands in original order Place operators in front of their operands If there’s a compound expression, the prefix expression may have two or more operators in a row If parentheses are not present, pay attention to precedence

stacks26 A + B + C >>>>>>+ + A B C A - B + C >>>>>>+ - A B C A + (B - C) >>>>>+ A - B C A * ((B + C) - D) / E >>> / * A - + B C D E A + B * C / D >>>>+ A / * B C D A * B + C - D / E >>> - + * A B C / D E Conversion examples

stacks27 Prefix evaluation scan left to right until we find the first operator immediately followed by pair of operands evaluate expression, and replace the “used” operator & operands with the result continue until a single value remains

stacks28 Prefix Example + * / // original expression + * 2 3 9// 4/2 evaluated +6 9// 2*3 evaluated 15// 6+9 evaluated

stacks29 Another example * / // original expression * / // 4+3 evaluated * 2 / // 7-5 evaluated * 2 / 6 3// 2+4 evaluated * 2 2// 6/3 evaluated 4// 2*2 evaluated

stacks210 Prefix summary Operands (but often not operators) same order as infix Expression designated unambiguously without parentheses Improvement on infix, but still not quite as simple to evaluate as one might wish -- have to deal with exceptions

stacks211 Alternative II: Postfix Postfix is also known as reverse Polish notation -- widely used in HP calculators In postfix notation, operators appear after the operands they operate on As with prefix, operand order is maintained, and parentheses are not needed notPostfix expression is not merely a reverse of the equivalent prefix expression

stacks212 Postfix expression examples Simple expression: –Original Expression: A + B –Postfix Equivalent: A B + Compound expression with parentheses: –original: (A + B) * (C - D) –postfix: A B + C D - * Compound expression without parentheses: –original: A + B * C - D –postfix: A B C * + D -

stacks213 Postfix expression evaluation Read expression left to right When an operand is encountered, save it & move on When an operator is encountered, evaluate expression, using operator & last 2 operands saved, saving the result When entire expression has been read, there should be one value left -- final result

stacks214 Postfix evaluation using stack Postfix evaluation can easily be accomplished using a stack to keep track of operands As operands are encountered or created (through evaluation) they are pushed on stack When operator is encountered, pop two operands, evaluate, push result

stacks215 Postfix calculator code int main( ) { double answer; cout << "Type a postfix arithmetic expression:" << endl; answer = read_and_evaluate(cin); cout << "That evaluates to " << answer << endl; return EXIT_SUCCESS; }

stacks216 Postfix calculator code double read_and_evaluate(istream& ins) { Stack numstack; double number; char symbol;...

stacks217 Postfix calculator code while (!ins.eof( ) && ins.peek( ) != '\n') { if (isdigit(ins.peek( )) || (ins.peek( ) == ‘.’)) { ins >> number; numstack.push(number); } …

stacks218 Postfix calculator code else if (strchr("+-*/", ins.peek( )) != NULL) { ins >> symbol; evaluate(numstack, symbol); } else cin.ignore( ); } // end of while loop return numstack.pop( ); }

stacks219 Postfix calculator code void evaluate(Stack & numbers, char op) { double operand1, operand2; operand2 = numbers.pop( ); operand1 = numbers.pop( );...

stacks220 Postfix calculator code switch (op) { case '+': numbers.push(operand1 + operand2); break; case '-': numbers.push(operand1 - operand2); break;...

stacks221 Postfix calculator code case '*': numbers.push(operand1 * operand2); break; case '/': numbers.push(operand1 / operand2); break; } // end switch statement } // end function

stacks222 Translating infix to postfix Postfix expression evaluation is easiest type to program Next task is to take an infix expression and translate it into postfix for evaluation Some basic assumptions: –all operations are binary (no unary negative) –expressions are fully parenthesized

stacks223 Translating infix to postfix General method: –move each operator to the right of its corresponding right parenthesis –eliminate parentheses Example: (((A + B) * C) - (E * (F + G))) (((A B) + C) * (E (F G) +) * ) - A B+ C * E F G + * -

stacks224 Pseudocode for translation program Do { if (left parenthesis) read & push else if (operand) read & write to file else if (operator) read & push else if (right parenthesis) read & discard pop operand & write to file pop & discard left parenthesis } while (expression to read)

stacks225 Translation program code void main( ) { Stack opstack; char ch; double num; ofstream outfile;...

stacks226 Translation program code outfile.open(“EXPRESS.TXT”); cout << “Enter a fully-parenthesized infix “; cout << “expression below: “ << endl; while (cin && cin.peek != ‘\n’) {...

stacks227 Translation program code // check for operand -- write to file if found if (isdigit (cin.peek( ) || cin.peek( ) == ‘.’) { cin >> num; outfile << num; }

stacks228 Translation program code // check for operator or left parenthesis; // if found, push on stack else if (strchr( “+-*/(“, cin.peek( ))!=NULL) { cin >> ch; opstack.push(ch); }

stacks229 Translation program code // check for right parenthesis -- if found, // pop stack & send operator to file else if (cin.peek( ) == ‘)’) { cin.ignore( );// discard right paren outfile << opstack.pop( ); opstack.pop( );// discard left paren }

stacks230 Translation program code else // it’s some other character - who cares? cin.ignore( ); } // end of while loop cin.ignore( ); // discard newline character return; }

stacks231 OK -- but what if expression isn’t fully parenthesized? We have to fall back on the rules for expression evaluation we know & love –do expression in parentheses first –do other operations in order of precedence -- in case of tie, leftmost sub-expression wins Example: A - ( B + C) * D - E order: Postfix: A B C + D * - E -

stacks232 Algorithm for expression conversion Do if (left parenthesis) read & push else if (operand) read & write to file else if (arithmetic operator) // continued on next slide...

stacks233 Conversion algorithm continued while (stack not empty && stack.peek( ) != ‘(‘ && op precedence lower than stack.peek( )) pop stack & write to file read op push op else // character should be ‘)’ // next slide...

stacks234 Conversion algorithm continued read & discard right paren do pop stack & write to file while (stack.peek( ) != ‘(‘) pop & discard left paren while (expression to read) // ends outer loop while (stack not empty) pop & write to file