RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Touch and Gestures with Xamarin Forms
Telerik School Academy ASP.NET MVC.
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
C# Fundamentals – Part I
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
When and How to Improve Code Performance? Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Arrays, Lists, Stacks, Queues Static and Dynamic Implementation Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Brief Overview of Combinations, Permutations and Binary Vectors Nikolay Kostov Telerik Corporation Technical Trainer.
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Telerik Software Academy End-to-end JavaScript Applications.
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Subroutines in Computer Programming Telerik School Academy C# Fundamentals – Part 1.
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Building Rock-Solid Software Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
Data Structures and Algorithms Telerik Software Academy
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Lists, Stacks, Queues Svetlin Nakov Telerik Software Academy Manager Technical Trainer
Presentation transcript:

RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant

1. Pre-requirements  List  Stack  Queue 2. Reverse Polish Notation  Explanation  Calculator algorithm 3. Shunting-yard algorithm  Converting expressions to RPN

List, Stack, Queue

 What is "list"?  A data structure (container) that contains a sequence of elements  Can have variable size  Elements are arranged linearly, in sequence  Can be implemented in several ways  Statically (using array  fixed size)  Dynamically (linked implementation)  Using resizable array (the List class)

 Implements the abstract data structure list using an array  All elements are of the same type T  T can be any type, e.g. List, List, List  T can be any type, e.g. List, List, List  Size is dynamically increased as needed  Basic functionality:  Count – returns the number of elements  Add(T) – appends given element at the end

static void Main() { List list = new List () { "C#", "Java" }; List list = new List () { "C#", "Java" }; list.Add("SQL"); list.Add("SQL"); list.Add("Python"); list.Add("Python"); foreach (string item in list) foreach (string item in list) { Console.WriteLine(item); Console.WriteLine(item); } // Result: // Result: // C# // C# // Java // Java // SQL // SQL // Python // Python} Inline initialization: the compiler adds specified elements to the list.

 list[index] – access element by index  Insert(index, T) – inserts given element to the list at a specified position  Remove(T) – removes the first occurrence of given element  RemoveAt(index) – removes the element at the specified position  Clear() – removes all elements  Contains(T) – determines whether an element is part of the list

 IndexOf() – returns the index of the first occurrence of a value in the list (zero-based)  Reverse() – reverses the order of the elements in the list or a portion of it  Sort() – sorts the elements in the list or a portion of it  ToArray() – converts the elements of the list to an array  TrimExcess() – sets the capacity to the actual number of elements

 List keeps a buffer memory, allocated in advance, to allow fast Add(T)  Most operations use the buffer memory and do not allocate new objects  Occasionally the capacity grows (doubles) List : Count = 9 Capacity = 15 Capacity used buffer (Count) unused buffer 9

Live Demo

 LIFO (Last In First Out) structure  Elements inserted (push) at “top”  Elements removed (pop) from “top”  Useful in many situations  E.g. the execution stack of the program  Can be implemented in several ways  Statically (using array)  Dynamically (linked implementation)  Using the Stack class

 Implements the stack data structure using an array  Elements are from the same type T  T can be any type, e.g. Stack  T can be any type, e.g. Stack  Size is dynamically increased as needed  Basic functionality:  Push(T) – inserts elements to the stack  Pop() – removes and returns the top element from the stack

 Basic functionality:  Peek() – returns the top element of the stack without removing it  Count – returns the number of elements  Clear() – removes all elements  Contains(T) – determines whether given element is in the stack  ToArray() – converts the stack to an array  TrimExcess() – sets the capacity to the actual number of elements

 Using Push(), Pop() and Peek() methods static void Main() { Stack stack = new Stack (); Stack stack = new Stack (); stack.Push("1. Ivan"); stack.Push("1. Ivan"); stack.Push("2. Nikolay"); stack.Push("2. Nikolay"); stack.Push("3. Maria"); stack.Push("3. Maria"); stack.Push("4. George"); stack.Push("4. George"); Console.WriteLine("Top = {0}", stack.Peek()); Console.WriteLine("Top = {0}", stack.Peek()); while (stack.Count > 0) while (stack.Count > 0) { string personName = stack.Pop(); string personName = stack.Pop(); Console.WriteLine(personName); Console.WriteLine(personName); }}

Live Demo

 FIFO (First In First Out) structure  Elements inserted at the tail (Enqueue)  Elements removed from the head (Dequeue)  Useful in many situations  Print queues, message queues, etc.  Can be implemented in several ways  Statically (using array)  Dynamically (using pointers)  Using the Queue class

 Implements the queue data structure using a circular resizable array  Elements are from the same type T  T can be any type, e.g. Queue  T can be any type, e.g. Queue  Size is dynamically increased as needed  Basic functionality:  Enqueue(T) – adds an element to the end of the queue  Dequeue() – removes and returns the element at the beginning of the queue

 Basic functionality:  Peek() – returns the element at the beginning of the queue without removing it  Count – returns the number of elements  Clear() – removes all elements  Contains(T) – determines whether given element is in the queue  ToArray() – converts the queue to an array  TrimExcess() – sets the capacity to the actual number of elements in the queue

 Using Enqueue() and Dequeue() methods static void Main() { Queue queue = new Queue (); Queue queue = new Queue (); queue.Enqueue("Message One"); queue.Enqueue("Message One"); queue.Enqueue("Message Two"); queue.Enqueue("Message Two"); queue.Enqueue("Message Three"); queue.Enqueue("Message Three"); queue.Enqueue("Message Four"); queue.Enqueue("Message Four"); while (queue.Count > 0) while (queue.Count > 0) { string message = queue.Dequeue(); string message = queue.Dequeue(); Console.WriteLine(message); Console.WriteLine(message); }}

Live Demo

Postfix visualization of expressions

 Three notation types  Prefix – Example: 5 – (6 * 7) converts to – 5 * 6 7  Infix – Example: 5 – (6 * 7) is 5 – (6 * 7)  Postfix – Example: 5 – (6 * 7) converts to * -  Reverse Polish Notation is postfix  Benefits  No parentheses  Easy to calculate  Easy to use by computers

 While there are input tokens left  Read the next token from input  If the token is a value – push it into the stack  Else the token is an operator (or function)  It is known that the operator takes n arguments.  If stack does not contain n arguments – error  Else, pop n arguments – evaluate the operator  Push the result back into the stack  If stack contains one argument – it is the result  Else - error

 Infix notation: 5 + ((1 + 2) * 4) − 3  RPN: * + 3 –  Step 1 - Token: 5 | Stack: 5  Step 2 - Token: 1 | Stack: 5, 1  Step 3 - Token: 2 | Stack: 5, 1, 2  Step 4 - Token: + | Stack: 5, 3 | Evaluate:  Step 5 - Token: 4 | Stack: 5, 3, 4  Step 6 - Token: * | Stack: 5, 12 | Evaluate: 4 * 3

 Infix notation: 5 + ((1 + 2) * 4) − 3  RPN: * + 3 –  Step 6 - Token: * | Stack: 5, 12 | Evaluate: 3 * 4  Step 7 - Token: + | Stack: 17 | Evaluate:  Step 8 - Token: 3 | Stack: 17, 3  Step 9 - Token: - | Stack: 14 | Evaluate: 17 – 3  Result - 14

Convert from infix to postfix

 Converts from infix to postfix (RPN) notation  Invented by Dijkstra  Stack-based  Two string variables – input and output  A stack holds not yet used operators  A queue holds the output  Reads token by token

 While there are input tokens left  Read the next token from input  If the token is a number – add it into the queue  If the token is a function – push it into the stack  If the token is argument separator (comma)  Until the top of the stack is left parentheses, pop operators from stack and add them to queue  If left parentheses is not reached - error  If the token is left parentheses, push it into the stack

 If the token is an operator A,  While  there is an operator B at the top of the stack and  A is left-associative and its precedence is equal to that of B,  Or A has precedence less than that of B,  Pop B of the stack and add it to the queue  Push A into the stack

 If the token is right parentheses,  Until the top of the stack is a left parenthesis, pop operators off the stack onto the queue  Pop the left parenthesis from the stack, but not onto the queue  If the top of the stack is a function, pop it onto the queue  If left parentheses is not reached – error  If tokens end – while stack is not empty  Pop operators from stack to the queue  If parentheses is found - error

 Infix notation:  Infix notation: * 2 / ( )  Step 1 - Token: 3 | Stack: | Queue: 3  Step 2 - Token: + | Stack: + | Queue: 3  Step 3 - Token: 4 | Stack: + | Queue: 3, 4  Step 4 - Token: * | Stack: +, * | Queue: 3, 4  Step 5 - Token: 2 | Stack: +, * | Queue: 3, 4, 2  Step 6 - Token: / | Stack: +, / | Queue: 3, 4, 2, *

 Infix notation:  Infix notation: * 2 / ( )  Step 6 - Token: / | Stack: +, / | Queue: 3, 4, 2, *  Step 7 - Token: ( | Stack: +, /, ( | Queue: 3, 4, 2, *  Step 7 - Token: 1  Stack: +, /, ( | Queue: 3, 4, 2, *, 1  Step 8 - Token: -  Stack: +, /, (, - | Queue: 3, 4, 2, *, 1  Step 9 - Token: 5  Stack: +, /, (, - | Queue: 3, 4, 2, *, 1, 5

 Infix notation:  Infix notation: * 2 / ( )  Step 9 - Token: 5  Stack: +, /, (, - | Queue: 3, 4, 2, *, 1, 5  Step 9 - Token: )  Stack: +, / | Queue: 3, 4, 2, *, 1, 5, -  Step 9 - Token: None  Stack: | Queue: 3, 4, 2, *, 1, 5, -, /, +  Result – * / +

Combining the knowledge

 Read the input as string  Remove all whitespace  Separate all tokens  Convert the tokens into a queue - Shunting- yard Algorithm  Calculate the final result with the Reverse Polish Notation

Live Demo

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

 “C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com