Soumettre la recherche
Mettre en ligne
Polish nootation
•
2 j'aime
•
3,017 vues
Srikanth Chennupati
Suivre
polish notation
Lire moins
Lire la suite
Signaler
Partager
Signaler
Partager
1 sur 40
Télécharger maintenant
Télécharger pour lire hors ligne
Recommandé
Virtual Memory and Paging
Virtual Memory and Paging
Emery Berger
Flowchart and algorithem
Flowchart and algorithem
ehsanullah786
computer Architecture
computer Architecture
umardanjumamaiwada
Algorythmes et programmation
Algorythmes et programmation
Mohamed Latifi
Python
Python
Edureka!
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
National Cheng Kung University
Machine language
Machine language
鍾誠 陳鍾誠
DBMS unit-5.pdf
DBMS unit-5.pdf
Prof. Dr. K. Adisesha
Recommandé
Virtual Memory and Paging
Virtual Memory and Paging
Emery Berger
Flowchart and algorithem
Flowchart and algorithem
ehsanullah786
computer Architecture
computer Architecture
umardanjumamaiwada
Algorythmes et programmation
Algorythmes et programmation
Mohamed Latifi
Python
Python
Edureka!
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
National Cheng Kung University
Machine language
Machine language
鍾誠 陳鍾誠
DBMS unit-5.pdf
DBMS unit-5.pdf
Prof. Dr. K. Adisesha
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
Mohamed El Desouki
GNU ld的linker script簡介
GNU ld的linker script簡介
Wen Liao
Virtual memory
Virtual memory
Muhammad Farooq
MIPS Assembly Language I
MIPS Assembly Language I
LiEdo
Python programs - PPT file (Polytechnics)
Python programs - PPT file (Polytechnics)
SHAMJITH KM
Implementation of absolute loader
Implementation of absolute loader
Jaya Krishnan P
Computer registers
Computer registers
DeepikaT13
Virtual Memory
Virtual Memory
Archith777
乱数調整概説
乱数調整概説
Blastoise_X
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
Md. Imran Hossain Showrov
MACRO PROCESSOR
MACRO PROCESSOR
Bhavik Vashi
20101016 嵌入式系統開發之道講座
20101016 嵌入式系統開發之道講座
黑狗 大
Parallel Processing
Parallel Processing
Pierre Vigneras
How to execute a C program
How to execute a C program
Leela Koneru
JVM JIT compilation overview by Vladimir Ivanov
JVM JIT compilation overview by Vladimir Ivanov
ZeroTurnaround
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
kiki utagawa
Steps for c program execution
Steps for c program execution
Rumman Ansari
Chapter 1
Chapter 1
Ashhad Kamal
Cache memory
Cache memory
Prasenjit Dey
02 Computer Evolution And Performance
02 Computer Evolution And Performance
Jeanie Delos Arcos
Conversion from infix to prefix using stack
Conversion from infix to prefix using stack
Haqnawaz Ch
Infix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
Contenu connexe
Tendances
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
Mohamed El Desouki
GNU ld的linker script簡介
GNU ld的linker script簡介
Wen Liao
Virtual memory
Virtual memory
Muhammad Farooq
MIPS Assembly Language I
MIPS Assembly Language I
LiEdo
Python programs - PPT file (Polytechnics)
Python programs - PPT file (Polytechnics)
SHAMJITH KM
Implementation of absolute loader
Implementation of absolute loader
Jaya Krishnan P
Computer registers
Computer registers
DeepikaT13
Virtual Memory
Virtual Memory
Archith777
乱数調整概説
乱数調整概説
Blastoise_X
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
Md. Imran Hossain Showrov
MACRO PROCESSOR
MACRO PROCESSOR
Bhavik Vashi
20101016 嵌入式系統開發之道講座
20101016 嵌入式系統開發之道講座
黑狗 大
Parallel Processing
Parallel Processing
Pierre Vigneras
How to execute a C program
How to execute a C program
Leela Koneru
JVM JIT compilation overview by Vladimir Ivanov
JVM JIT compilation overview by Vladimir Ivanov
ZeroTurnaround
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
kiki utagawa
Steps for c program execution
Steps for c program execution
Rumman Ansari
Chapter 1
Chapter 1
Ashhad Kamal
Cache memory
Cache memory
Prasenjit Dey
02 Computer Evolution And Performance
02 Computer Evolution And Performance
Jeanie Delos Arcos
Tendances
(20)
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
GNU ld的linker script簡介
GNU ld的linker script簡介
Virtual memory
Virtual memory
MIPS Assembly Language I
MIPS Assembly Language I
Python programs - PPT file (Polytechnics)
Python programs - PPT file (Polytechnics)
Implementation of absolute loader
Implementation of absolute loader
Computer registers
Computer registers
Virtual Memory
Virtual Memory
乱数調整概説
乱数調整概説
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
MACRO PROCESSOR
MACRO PROCESSOR
20101016 嵌入式系統開發之道講座
20101016 嵌入式系統開發之道講座
Parallel Processing
Parallel Processing
How to execute a C program
How to execute a C program
JVM JIT compilation overview by Vladimir Ivanov
JVM JIT compilation overview by Vladimir Ivanov
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Pythonの処理系はどのように実装され,どのように動いているのか? 我々はその実態を調査すべくアマゾンへと飛んだ.
Steps for c program execution
Steps for c program execution
Chapter 1
Chapter 1
Cache memory
Cache memory
02 Computer Evolution And Performance
02 Computer Evolution And Performance
En vedette
Conversion from infix to prefix using stack
Conversion from infix to prefix using stack
Haqnawaz Ch
Infix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
8.binry search tree
8.binry search tree
Chandan Singh
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
Infix to-postfix examples
Infix to-postfix examples
mua99
Infix prefix postfix expression -conversion
Infix prefix postfix expression -conversion
Syed Mustafa
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
Archie Jamwal
2 1 expressions
2 1 expressions
Ken Kretsch
C applications
C applications
faizankhan260690
The Stack And Recursion
The Stack And Recursion
Ashim Lamichhane
Queues
Queues
Ashim Lamichhane
Syntactic structures
Syntactic structures
Gil Cabaltican
Tree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
Evaluation of postfix expression
Evaluation of postfix expression
Akhil Ahuja
Circular queues
Circular queues
Ssankett Negi
Syntactic categories, by dr. shadia yousef banjar.ppt [compatibility mode]
Syntactic categories, by dr. shadia yousef banjar.ppt [compatibility mode]
Dr. Shadia Banjar
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
sethuraman R
Syntax & syntactic analysis,lec.1, dr. shadia.ppt [compatibility mode]
Syntax & syntactic analysis,lec.1, dr. shadia.ppt [compatibility mode]
Dr. Shadia Banjar
(Binary tree)
(Binary tree)
almario1988
Stacks Implementation and Examples
Stacks Implementation and Examples
greatqadirgee4u
En vedette
(20)
Conversion from infix to prefix using stack
Conversion from infix to prefix using stack
Infix to postfix conversion
Infix to postfix conversion
8.binry search tree
8.binry search tree
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Infix to-postfix examples
Infix to-postfix examples
Infix prefix postfix expression -conversion
Infix prefix postfix expression -conversion
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
2 1 expressions
2 1 expressions
C applications
C applications
The Stack And Recursion
The Stack And Recursion
Queues
Queues
Syntactic structures
Syntactic structures
Tree and Binary Search tree
Tree and Binary Search tree
Evaluation of postfix expression
Evaluation of postfix expression
Circular queues
Circular queues
Syntactic categories, by dr. shadia yousef banjar.ppt [compatibility mode]
Syntactic categories, by dr. shadia yousef banjar.ppt [compatibility mode]
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Syntax & syntactic analysis,lec.1, dr. shadia.ppt [compatibility mode]
Syntax & syntactic analysis,lec.1, dr. shadia.ppt [compatibility mode]
(Binary tree)
(Binary tree)
Stacks Implementation and Examples
Stacks Implementation and Examples
Similaire à Polish nootation
Fundamentals of Engineering Probability Visualization Techniques & MatLab Cas...
Fundamentals of Engineering Probability Visualization Techniques & MatLab Cas...
Jim Jenkins
random test
random test
kapilonweb
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.ir
nabati
Storing and manipulating graphs in HBase
Storing and manipulating graphs in HBase
Dan Lynn
HBaseCon 2012 | Storing and Manipulating Graphs in HBase
HBaseCon 2012 | Storing and Manipulating Graphs in HBase
Cloudera, Inc.
famous placement papers
famous placement papers
Ramanujam Ramu
Lecture 6 operators
Lecture 6 operators
eShikshak
Spring 2003
Spring 2003
butest
L7 pointers
L7 pointers
Kathmandu University
C# p2
C# p2
Renas Rekany
Labsheet2
Labsheet2
rohassanie
RDataMining slides-r-programming
RDataMining slides-r-programming
Yanchang Zhao
Ch4.mapreduce algorithm design
Ch4.mapreduce algorithm design
AllenWu
DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019
Sabrina Marechal
R studio
R studio
Kinza Irshad
Reconsidering Custom Memory Allocation
Reconsidering Custom Memory Allocation
Emery Berger
R vectorization
R vectorization
臻博 Zhenbo 李 Li
1. basics of python
1. basics of python
PhD Research Scholar
Distributive property
Distributive property
DebbyChamberlain
R05010303 C O M P U T E R P R O G R A M M I N G A N D N U M E R I C A L M E ...
R05010303 C O M P U T E R P R O G R A M M I N G A N D N U M E R I C A L M E ...
guestd436758
Similaire à Polish nootation
(20)
Fundamentals of Engineering Probability Visualization Techniques & MatLab Cas...
Fundamentals of Engineering Probability Visualization Techniques & MatLab Cas...
random test
random test
Introduction to R r.nabati - iausdj.ac.ir
Introduction to R r.nabati - iausdj.ac.ir
Storing and manipulating graphs in HBase
Storing and manipulating graphs in HBase
HBaseCon 2012 | Storing and Manipulating Graphs in HBase
HBaseCon 2012 | Storing and Manipulating Graphs in HBase
famous placement papers
famous placement papers
Lecture 6 operators
Lecture 6 operators
Spring 2003
Spring 2003
L7 pointers
L7 pointers
C# p2
C# p2
Labsheet2
Labsheet2
RDataMining slides-r-programming
RDataMining slides-r-programming
Ch4.mapreduce algorithm design
Ch4.mapreduce algorithm design
DataWeave 2.0 - MuleSoft CONNECT 2019
DataWeave 2.0 - MuleSoft CONNECT 2019
R studio
R studio
Reconsidering Custom Memory Allocation
Reconsidering Custom Memory Allocation
R vectorization
R vectorization
1. basics of python
1. basics of python
Distributive property
Distributive property
R05010303 C O M P U T E R P R O G R A M M I N G A N D N U M E R I C A L M E ...
R05010303 C O M P U T E R P R O G R A M M I N G A N D N U M E R I C A L M E ...
Polish nootation
1.
Chapter 13 CASE STUDY: THE
POLISH NOTATION 1. The Problem 2. The Idea (a) Expression Trees (b) Polish Notation 3. Evaluation of Polish Expressions 4. Translation from Infix Form to Polish Form 5. Application: An Interactive Expression Evaluator Outline Data Structures and Program Design In C++ Transp. 1, Chapter 13, Case Study: The Polish Notation 482 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
2.
Priorities of Operators The
quadratic formula: x = (−b + (b ↑ 2 − (4 × a) × c) ↑ 1 )/(2 × a) 2 Order of doing operators: x = (−b + (b ↑ 2 − (4 × a) × c) ↑ 1 ) / (2 × a) 2 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 10 1 7 2 5 3 4 6 9 8 Priorities of operators: Operators Priority ↑ , all unary operators 6 ∗ / % 5 + − (binary) 4 == != < > <= >= 3 not 2 && || 1 = 0 Priorities of Operators Data Structures and Program Design In C++ Transp. 2, Sect. 13.1, The Problem 483 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
3.
Evaluation of an
Expression Tree × × + ! + ! – / – –2.9 / – 2.9 2.7 3.0 5.5 2.5 2.7 3.0 5.5 2.5 (a) (b) × × + ! –2.0 ! – –2.9 0.9 – 5.5 2.5 5.5 2.5 (c) (d) × × –12.0 –2.0 ! –2.0 6.0 3.0 (e) (f) (g) Evaluation of an Expression Tree Data Structures and Program Design In C++ Transp. 3, Sect. 13.2, The Idea 484 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
4.
Polish Notation The Polish
mathematician JAN ŁUKASIEWICZ made the observation that, by writing all operators either before their operands or after them, it is not necessary to keep track of priorities, of parentheses bracketing expressions, or to make repeated scans to evaluate an expression. Polish forms: When the operators are written before their operands, it is called the prefix form. When the operators come after their operands, it is called the postfix form, or, sometimes, reverse Polish form or suffix form. The infix form means writing binary operators between their operands. Example, Quadratic Formula: Prefix: = x / + ∼ b ↑ − ↑ b 2 × × 4 a c 1 × 2 a 2 1 Postfix: x b ∼ b 2 ↑ 4 a × c × − 2 ↑ + 2 a × / = In these expressions, ‘ − ’ denotes binary subtraction and ‘ ∼ ’ denotes unary negation. Polish Notation Data Structures and Program Design In C++ Transp. 4, Sect. 13.2, The Idea 485 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
5.
Evaluation of an
Expression in Prefix Form Error code Expression :: evaluate prefix(Value &result) /* Outline of a method to perform prefix evaluation of an Expression. The details depend on further decisions about the implementation of expressions and values. */ { if (the Expression is empty) return fail; else { remove the first symbol from the Expression, and store the value of the symbol as t; if (t is a unary operation) { Value the argument; if (evaluate prefix(the argument) == fail) return fail; else result = the value of operation t applied to the argument; } else if (t is a binary operation) { Value first argument, second argument; if (evaluate prefix(first argument) == fail) return fail; if (evaluate prefix(second argument) == fail) return fail; result = the value of operation t applied to first argument and second argument; } else // t is a numerical operand. result = the value of t; } return success; } Evaluation of an Expression in Prefix Form Data Structures and Program Design In C++ Transp. 5, Sect. 13.3, Evaluation of Polish Expressions 486 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
6.
C++ Conventions and
Class Declaration class Expression { public: Error code evaluate prefix(Value &result); Error code get token(Token &result); // Add other methods. private: // Add data members to store an expression }; We define a token to be a single operator or operand from the expression. In our programs, we shall represent tokens as objects of a struct Token. We employ a method Token Expression :: get token( ); that will move through an expression removing and returning one to- ken at a time. We assume the existence of a Token method kind() that will return one of the values of the following enumerated type: enum Token type {operand, unaryop, binaryop}; // Add any other legitimate token types. We assume that all the operands and the results of evaluating the operators are of the same type, which we call Value. We assume the existence of three auxiliary functions that re- turn a result of type Value: Value do unary(const Token &operation, const Value &the argument); Value do binary(const Token &operation, const Value &first argument, const Value &second argument); Value get value(const Token &operand); C++ Conventions and Class Declaration Data Structures and Program Design In C++ Transp. 6, Sect. 13.3, Evaluation of Polish Expressions 487 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
7.
C++ Function for
Prefix Evaluation Error code Expression :: evaluate prefix(Value &result) /* Post: If the Expression does not begin with a legal prefix expression, a code of fail is returned. Otherwise a code of success is returned, and the Expression is evaluated, giving the Value result. The initial tokens that are evaluated are removed from the Expression. */ { Token t; Value the argument, first argument, second argument; if (get token(t) == fail) return fail; switch (t.kind( )) { case unaryop: if (evaluate prefix(the argument) == fail) return fail; else result = do unary(t, the argument); break; case binaryop: if (evaluate prefix(first argument) == fail) return fail; if (evaluate prefix(second argument) == fail) return fail; else result = do binary(t, first argument, second argument); break; case operand: result = get value(t); break; } return success; } C++ Function for Prefix Evaluation Data Structures and Program Design In C++ Transp. 7, Sect. 13.3, Evaluation of Polish Expressions 488 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
8.
Evaluation of Postfix
Expressions typedef Value Stack entry; // Set the type of entry to use in stacks. Error code Expression :: evaluate postfix(Value &result) /* Post: The tokens in Expression up to the first end expression symbol are re- moved. If these tokens do not represent a legal postfix expression, a code of fail is returned. Otherwise a code of success is returned, and the removed sequence of tokens is evaluated to give Value result. */ { Token t; // Current operator or operand Stack operands; // Holds values until operators are seen Value the argument, first argument, second argument; do { if (get token(t) == fail) return fail; // No end expression token switch (t.kind( )) { case unaryop: if (operands.empty( )) return fail; operands.top(the argument); operands.pop( ); operands.push(do unary(t, the argument)); break; case binaryop: if (operands.empty( )) return fail; operands.top(second argument); operands.pop( ); if (operands.empty( )) return fail; operands.top(first argument); operands.pop( ); operands.push(do binary(t, first argument, second argument)); break; Evaluation of Postfix Expressions Data Structures and Program Design In C++ Transp. 8, Sect. 13.3, Evaluation of Polish Expressions 489 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
9.
case operand:
operands.push(get value(t)); break; case end expression: break; } } while (t.kind( ) != end expression); if (operands.empty( )) return fail; operands.top(result); operands.pop( ); if (!operands.empty( )) return fail; // surplus operands detected return success; } Evaluation of Postfix Expressions Data Structures and Program Design In C++ Transp. 9, Sect. 13.3, Evaluation of Polish Expressions 490 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
10.
Proof of the
Program: Counting Stack Entries RUNNING SUM CONDITION. For a sequence E of operands, unary operators, and binary operators, form a running sum by starting at the left end of E and counting +1 for each operand, 0 for each unary operator, and −1 for each binary operator. E satisfies the running-sum condition provided that this running sum never falls below 1 and is exactly 1 at the right-hand end of E . a=2 b = –7 c=3 2 3 1 2 4 4 8 8 24 2 2 –7 –7 49 49 49 49 49 49 25 25 5 2 2 4 –7 7 7 7 7 7 7 7 7 7 7 7 7 12 12 12 12 3 1 b ~ b 2 4 a × c × – 2 2 a × 1 1 2 3 2 3 4 3 4 3 2 3 2 1 2 3 2 1 THEOREM 13.1 If E is a properly formed expression in postfix form, then E must satisfy the running sum condition. THEOREM 13.2 A properly formed expression in postfix form will be correctly evaluated by method evaluate postfix. THEOREM 13.3 If E is any sequence of operands and opera- tors that satisfies the running-sum condition, then E is a properly formed expression in postfix form. THEOREM 13.4 An expression in postfix form that satisfies the running-sum condition corresponds to exactly one fully bracketed expression in infix form. Hence no parentheses are needed to achieve the unique representation of an ex- pression in postfix form. Proof of the Program: Counting Stack Entries Data Structures and Program Design In C++ Transp. 10, Sect. 13.3, Evaluation of Polish Expressions 491 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
11.
Syntax Diagrams for
Polish Expressions Operand Prefix expression Unary Prefix operator expression Binary Prefix Prefix operator expression expression Operand Postfix expression Postfix Unary expression operator Postfix Postfix Binary expression expression operator Unary operator Postfix expression Operand Binary Postfix operator expression Syntax Diagrams for Polish Expressions Data Structures and Program Design In C++ Transp. 11, Sect. 13.3, Evaluation of Polish Expressions 492 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
12.
Recursive Evaluation of
Postfix Expressions Error code Expression :: evaluate postfix(Value &result) /* Post: The tokens in Expression up to the first end expression symbol are re- moved. If these tokens do not represent a legal postfix expression, a code of fail is returned. Otherwise a code of success is returned, and the removed sequence of tokens is evaluated to give Value result. */ { Token first token, final token; Error code outcome; if (get token(first token) == fail | first token.kind( ) != operand) | outcome = fail; else { outcome = recursive evaluate(first token, result, final token); if (outcome == success && final token.kind( ) != end expression) outcome = fail; } return outcome; } Recursive Evaluation of Postfix Expressions Data Structures and Program Design In C++ Transp. 12, Sect. 13.3, Evaluation of Polish Expressions 493 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
13.
Recursive Function for
Postfix Evaluation Error code Expression :: recursive evaluate(const Token &first token, Value &result, Token &final token) /* Pre: Token first token is an operand. Post: If the first token can be combined with initial tokens of the Expression to yield a legal postfix expression followed by either an end expression symbol or a binary operator, a code of success is returned, the legal postfix subexpression is evaluated, recorded in result, and the terminating Token is recorded as final token. Otherwise a code of fail is returned. The initial tokens of Expression are removed. Uses: Methods of classes Token and Expression, including recursive evaluate and functions do unary, do binary, and get value. */ { Value first segment = get value(first token), next segment; Error code outcome; Token current token; Token type current type; do { outcome = get token(current token); if (outcome != fail) { switch (current type = current token.kind( )) { case binaryop: // Binary operations terminate subexpressions. case end expression: // Treat subexpression terminators together. result = first segment; final token = current token; break; Recursive Function for Postfix Evaluation Data Structures and Program Design In C++ Transp. 13, Sect. 13.3, Evaluation of Polish Expressions 494 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
14.
Recursive Postfix Evaluation,
Continued case unaryop: first segment = do unary(current token, first segment); break; case operand: outcome = recursive evaluate(current token, next segment, final token); if (outcome == success && final token.kind( ) != binaryop) outcome = fail; else first segment = do binary(final token, first segment, next segment); break; } } } while (outcome == success && current type != end expression && current type != binaryop); return outcome; } Recursive Postfix Evaluation, Continued Data Structures and Program Design In C++ Transp. 14, Sect. 13.3, Evaluation of Polish Expressions 495 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
15.
Translation from Infix
Form to Postfix Form Delay each operator until its right-hand operand has been translated. A simple operand goes to output with no delay. Infix form: x+y ~x x+ y × z Postfix form: x y + x~ xyz × + Infix form: x = (~b + ( b 2 − 4 × a × c) 1 2 ) / (2 × a) Postfix form: 1 x b~b 2 4a × c × − 2 +2a× / = Problem: What token will terminate the right-hand operand of a given operator and thereby mark the place at which that operator should be placed? To find this token, we must take both parentheses and prior- ities of operators into account. Translation from Infix Form to Postfix Form Data Structures and Program Design In C++ Transp. 15, Sect. 13.4, Translation from Infix to Polish Form 496 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
16.
Tokens Terminating a
Right-Hand Operand If op is an operator in an infix expression, then its right- hand operand contains all tokens on its right until one of the following is encountered: 1. the end of the expression; 2. an unmatched right parenthesis ‘ ) ’; 3. an operator of priority less than or equal to that of op , not within a bracketed sub-expression, if op has priority less than 6; or 4. an operator of priority strictly less than that of op , not within a bracketed subexpression, if op has priority 6. Stack Processing 1. At the end of the expression, all operators are output. 2. A right parenthesis causes all operators found since the corresponding left parenthesis to be output. 3. An operator of priority not 6 causes all other operators of greater or equal priority to be output. 4. An operator of priority 6 causes all other operators of strictly greater priority to be output, if such operators exist. Stack Processing Data Structures and Program Design In C++ Transp. 16, Sect. 13.4, Translation from Infix to Polish Form 497 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
17.
Infix to Postfix
Translation Expression Expression :: infix to postfix( ) /* Pre: The Expression stores a valid infix expression. Post: A postfix expression that translates the infix expression is returned. */ { Expression answer; Token current, prior; Stack delayed operations; while (get token(current) != fail) { switch (current.kind( )) { case operand: answer.put token(current); break; case leftparen: delayed operations.push(current); break; case rightparen: delayed operations.top(prior); while (prior.kind( ) != leftparen) { answer.put token(prior); delayed operations.pop( ); delayed operations.top(prior); } delayed operations.pop( ); break; Infix to Postfix Translation Data Structures and Program Design In C++ Transp. 17, Sect. 13.4, Translation from Infix to Polish Form 498 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
18.
Infix to Postfix
Translation, Continued case unaryop: case binaryop: // Treat all operators together. bool end right = false; // End of right operand reached? do { if (delayed operations.empty( )) end right = true; else { delayed operations.top(prior); if (prior.kind( ) == leftparen) end right = true; else if (prior.priority( ) < current.priority( )) end right = true; else if (current.priority( ) == 6) end right = true; else answer.put token(prior); if (!end right) delayed operations.pop( ); } } while (!end right); delayed operations.push(current); break; } } while (!delayed operations.empty( )) { delayed operations.top(prior); answer.put token(prior); delayed operations.pop( ); } answer.put token(";"); return answer; } Infix to Postfix Translation, Continued Data Structures and Program Design In C++ Transp. 18, Sect. 13.4, Translation from Infix to Polish Form 499 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
19.
Input
Contents of Stack Output Token (rightmost token is on top) Token(s) x x = = ( = ( ∼ = ( ∼ b = ( ∼ b + = ( + ∼ ( = ( + ( b = ( + ( b ↑ = ( + ( ↑ 2 = ( + ( ↑ 2 − = ( + ( − ↑ 4 = ( + ( − 4 × = ( + ( − × a = ( + ( − × a × = ( + ( − × × c = ( + ( − × c ) = ( + × − ↑ = ( + ↑ 1 1 2 = ( + ↑ 2 ) = ↑+ / = / ( = / ( 2 = / ( 2 × = / ( × a = / ( × a ) = / × end of expression / = Translation of quadratic formula into postfix Data Structures and Program Design In C++ Transp. 19, Sect. 13.4, Translation from Infix to Polish Form 500 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
20.
An Interactive Expression
Evaluator Goal: Develop a program that can evaluate a function that is typed in interactively while the program is running. One such appli- cation is a program that will draw the graph of a mathematical function. First task: Take as input an expression involving constants, variable(s), arithmetic operators, and standard functions, with bracket- ing allowed, as typed in from the terminal, and translate the expression into postfix form. Second task: Evaluate this postfix expression for values of the variable(s) given as its calling parameter(s) and return the answer, which can then be graphed. Overall structure: int main( ) /* Pre: None Post: Acts as a menu-driven graphing program. Uses: Classes Expression and Plot, and functions introduction, get command, and do command. */ { introduction( ); Expression infix; // Infix expression from user Expression postfix; // Postfix translation Plot graph; char ch; while ((ch = get command( )) != q ) do command(ch, infix, postfix, graph); } An Interactive Expression Evaluator Data Structures and Program Design In C++ Transp. 20, Sect. 13.5, An Interactive Expression Evaluator 501 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
21.
void do command(char
c, Expression &infix, Expression &postfix, Plot &graph) /* Pre: None Post: Performs the user command represented by char c on the Expression infix, the Expression postfix, and the Plot graph. Uses: Classes Token, Expression and Plot. */ { switch (c) { case r : // Read an infix expression from the user. infix.clear( ); infix.read( ); if (infix.valid infix( ) == success) postfix = infix.infix to postfix( ); else cout < "Warning: Bad expression ignored. " < endl; break; < < case w : // Write the current expression. infix.write( ); postfix.write( ); break; case g : // Graph the current postfix expression. if (postfix.size( ) <= 0) cout < "Enter a valid expression before graphing!" < endl; < < else { graph.clear( ); graph.find points(postfix); graph.draw( ); } break; case l : // Set the graph limits. if (graph.set limits( ) != success) cout < "Warning: Invalid limits" < endl; break; < < case p : // Print the graph parameters. Token :: print parameters( ); break; case n : // Set new graph parameters. Token :: set parameters( ); break; case h : // Give help to user. help( ); break; } } Allocation of tasks, expression evaluator Data Structures and Program Design In C++ Transp. 21, Sect. 13.5, An Interactive Expression Evaluator 502 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
22.
Representation of the
Data In addition to the (independent) variable x used for plotting, an expression may contain further variables that we call pa- rameters for the graph. The program must establish the definitions of the predefined tokens (such as the operators +, − , and *), the operand x used to represent a coordinate in the graphing, and perhaps some constants. For each different token we must remember: Its name (as a String), so that we can recognize it in an input expression; Its kind, one of operand, unary operator, binary operator, and right unary operator (like factorial ‘!’), left parenthe- sis, or right parenthesis; For operators, a priority; For operands, a value. We associate an integer code with each token and place this code in the expression, rather than the full record. We shall thus set up a lexicon for the tokens, which will include an array indexed by the integer codes; this array will hold the full records for the tokens. Expressions are lists of token codes. Translation from infix to postfix uses a stack of token codes. Evaluation of the expression uses a recursive function. Given a token code, the lexicon provides information about the token. A hash table finds the code associated with a token name. Representation of the Data Data Structures and Program Design In C++ Transp. 22, Sect. 13.5, An Interactive Expression Evaluator 503 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
23.
Transp. 23, Sect.
13.5, An Interactive Expression Evaluator Data structures for tokens and expressions Priority Name Kind or value input expression (instring): 1 ( leftparen – ( s + x ) * ( – t ) – 7 2 ) rightparen – 3 ~ unaryop 6 infix: 1 24 17 22 2 19 1 3 25 2 18 23 17 + binaryop 4 18 – binaryop 4 exprlength 504 postfix: 19 * binaryop 5 24 22 17 25 3 19 23 18 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458 Data Structures and Program Design In C++ 22 x operand 0.0 parameter: 23 7 operand 7.0 24 s operand 0.0 24 25 25 t operand 0.0 Lexicon
24.
The Lexicon in
C++ Implementation Token records: struct Token record { String name; double value; int priority; Token type kind; }; Lexicon class: struct Lexicon { Lexicon( ); int hash(const String &x) const; void set standard tokens( ); // Set up the predefined tokens. int count; // Number of records in the Lexicon int index code[hash size]; // Declare the hash table. Token record token data[hash size]; }; Token class: class Token { public: // Add methods here. private: int code; static Lexicon symbol table; static List< int > parameters; }; List< int > Token :: parameters; // Allocate storage for static members. Lexicon Token :: symbol table; The Lexicon in C++ Implementation Data Structures and Program Design In C++ Transp. 24, Sect. 13.5, An Interactive Expression Evaluator 505 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
25.
Token Class class Token
{ public: Token( ) {} Token (const String &x); Token type kind( ) const; int priority( ) const; double value( ) const; String name( ) const; int code number( ) const; static void set parameters( ); static void print parameters( ); static void set x(double x val); private: int code; static Lexicon symbol table; static List< int > parameters; }; Token Class Data Structures and Program Design In C++ Transp. 25, Sect. 13.5, An Interactive Expression Evaluator 506 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
26.
Token Constructor Token ::
Token(const String &identifier) /* Post: A Token corresponding to String identifier is constructed. It shares its code with any other Token object with this identifier. Uses: The class Lexicon. */ { int location = symbol table.hash(identifier); if (symbol table.index code[location] == − 1) { // Create a new record. code = symbol table.count ++ ; symbol table.index code[location] = code; symbol table.token data[code] = attributes(identifier); if (is parameter(symbol table.token data[code])) parameters.insert(0, code); } else code = symbol table.index code[location]; // Code of an old record } The auxiliary function attributes examines the name of a token and sets up an appropriate Token record according to our conventions. Token Constructor Data Structures and Program Design In C++ Transp. 26, Sect. 13.5, An Interactive Expression Evaluator 507 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
27.
Parameter Values void Token
:: set parameters( ) /* Post: All parameter values are printed for the user, and any changes specified by the user are made to these values. Uses: Classes List, Token record, and String, and function read num. */ { int n = parameters.size( ); int index code; double x; for (int i = 0; i < n; i ++ ) { parameters.retrieve(i, index code); Token record &r = symbol table.token data[index code]; cout < "Give a new value for parameter " < (r.name).c str( ) < < < " with value " < r.value < endl; < < < cout < "Enter a value or a new line to keep the old value: " < < flush; < if (read num(x) == success) r.value = x; } } Parameter Values Data Structures and Program Design In C++ Transp. 27, Sect. 13.5, An Interactive Expression Evaluator 508 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
28.
The Lexicon Constructor Lexicon
:: Lexicon( ) /* Post: The Lexicon is initialized with the standard tokens. Uses: set standard tokens */ { count = 0; for (int i = 0; i < hash size; i ++ ) index code[i] = − 1; // code for an empty hash slot set standard tokens( ); } void Lexicon :: set standard tokens( ) { int i = 0; String symbols = (String) "; ( ) ˜ abs sqr sqrt exp ln lg sin cos arctan round trunc ! % + − * / ˆ x pi e"; String word; while (get word(symbols, i ++ , word) != fail) { Token t = word; } token data[23].value = 3.14159; token data[24].value = 2.71828; } The Lexicon Constructor Data Structures and Program Design In C++ Transp. 28, Sect. 13.5, An Interactive Expression Evaluator 509 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
29.
Predefined Tokens for
Expression Evaluation Token Name Kind Priority/Value 0 ; end expression 1 ( leftparen 2 ) rightparen 3 ∼ unaryop 6 negation 4 abs unaryop 6 5 sqr unaryop 6 6 sqrt unaryop 6 7 exp unaryop 6 8 ln unaryop 6 natural logarithm 9 lg unaryop 6 base 2 logarithm 10 sin unaryop 6 11 cos unaryop 6 12 arctan unaryop 6 13 round unaryop 6 14 trunc unaryop 6 15 ! right unary 6 factorial 16 % right unary 6 percentage 17 + binaryop 4 18 − binaryop 4 19 ∗ binaryop 5 20 / binaryop 5 21 ^ binaryop 6 22 x operand 0.00000 23 pi operand 3.14159 24 e operand 2.71828 Predefined Tokens for Expression Evaluation Data Structures and Program Design In C++ Transp. 29, Sect. 13.5, An Interactive Expression Evaluator 510 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
30.
Hash Table Processing int
Lexicon :: hash(const String &identifier) const /* Post: Returns the location in table Lexicon :: index code that corresponds to the String identifier. If the hash table is full and does not contain a record for identifier, the exit function is called to terminate the program. Uses: The class String, the function exit. */ { int location; const char *convert = identifier.c str( ); char first = convert[0], second; // First two characters of identifier if (strlen(convert) >= 2) second = convert[1]; else second = first; location = first % hash size; int probes = 0; while (index code[location] >= 0 && identifier != token data[index code[location]].name) { if ( ++ probes >= hash size) { cout < "Fatal Error: Hash Table overflow. Increase its sizen"; < exit(1); } location += second; location %= hash size; } return location; } Hash Table Processing Data Structures and Program Design In C++ Transp. 30, Sect. 13.5, An Interactive Expression Evaluator 511 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
31.
Class Expression: Token
Lists class Expression { public: Expression( ); Expression(const Expression &original); Error code get token(Token &next); void put token(const Token &next); Expression infix to postfix( ); Error code evaluate postfix(Value &result); void read( ); void clear( ); void write( ); Error code valid infix( ); int size( ); void rewind( ); // resets the Expression to its beginning private: List< Token > terms; int current term; Error code recursive evaluate(const Token &first token, Value &result, Token &final token); }; Error code Expression :: get token(Token &next) /* Post: The Token next records the current term of the Expression; current term is incremented; and an error code of success is returned, or if there is no such term a code of fail is returned. Uses: Class List. */ { if (terms.retrieve(current term, next) != success) return fail; current term ++ ; return success; } Class Expression: Token Lists Data Structures and Program Design In C++ Transp. 31, Sect. 13.5, An Interactive Expression Evaluator 512 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
32.
Reading an Expression void
Expression :: read( ) /* Post: A line of text, entered by the user, is split up into tokens and stored in the Expression. Uses: Classes String, Token, and List. */ { String input, word; int term count = 0; int x; input = read in(cin, x); add spaces(input); // Tokens are now words of input. bool leading = true; for (int i = 0; get word(input, i, word) != fail; i ++ ) { // Process next token if (leading) if (word == "+") continue; // unary + else if (word == " − ") word = "˜"; // unary − Token current = word; // Cast word to Token. terms.insert(term count ++ , current); Token type type = current.kind( ); if (type == leftparen | type == unaryop | type == binaryop) | | leading = true; else leading = false; } } Reading an Expression Data Structures and Program Design In C++ Transp. 32, Sect. 13.5, An Interactive Expression Evaluator 513 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
33.
Leading and Non-Leading
Positions The value of Boolean variable leading detects whether an operator has a left argument. We shall show that if leading is true, then the current token can have no left argument and therefore cannot legally be a binary operator. In this way, our method is able to distinguish between unary and binary versions of the operators + and − . Tokens Legal in Leading and Non-Leading Positions Previous token Legal tokens any one of: any one of: Leading position: start of expression operand binary operator unary operator unary operator left parenthesis left parenthesis Nonleading position: operand binary operator right unary operator right unary operator right parenthesis right parenthesis end of expression Tokens Legal in Leading and Non-Leading Positions Data Structures and Program Design In C++ Transp. 33, Sect. 13.5, An Interactive Expression Evaluator 514 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
34.
Error Checking for
Correct Syntax Error code Expression :: valid infix( ) /* Post: A code of success or fail is returned according to whether the Expression is a valid or invalid infix sequence. Uses: Class Token. */ { Token current; bool leading = true; int paren count = 0; while (get token(current) != fail) { Token type type = current.kind( ); if (type == rightparen | type == binaryop | | type == rightunaryop) { | if (leading) return fail; } else if (!leading) return fail; if (type == leftparen) paren count ++ ; else if (type == rightparen) { paren count −− ; if (paren count < 0) return fail; } if (type == binaryop | type == unaryop | type == leftparen) | | leading = true; else leading = false; } if (leading) return fail; // An expected final operand is missing. if (paren count > 0) return fail; // Right parentheses are missing. rewind( ); return success; } Error Checking for Correct Syntax Data Structures and Program Design In C++ Transp. 34, Sect. 13.5, An Interactive Expression Evaluator 515 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
35.
Auxiliary Evaluation Functions Value
get value(const Token ¤t) /* Pre: Token current is an operand. Post: The Value of current is returned. Uses: Methods of class Token. */ { return current.value( ); } Value do binary(const Token &operation, const Value &first argument, const Value &second argument) /* Pre: Token operation is a binary operator. Post: The Value of operation applied to the pair of Value parameters is returned. Uses: Methods of class Token. */ { switch (operation.code number( )) { case 17: return first argument + second argument; case 18: return first argument − second argument; case 19: return first argument * second argument; case 20: return first argument/second argument; case 21: return exp(first argument, second argument); } } Auxiliary Evaluation Functions Data Structures and Program Design In C++ Transp. 35, Sect. 13.5, An Interactive Expression Evaluator 516 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
36.
Graphing the Expression:
The Class Plot class Plot { public: Plot( ); Error code set limits( ); void find points(Expression &postfix); void draw( ); void clear( ); int get print row(double y value); int get print col(double x value); private: Sortable list< Point > points; // records of points to be plotted double x low, x high; // x limits double y low, y high; // y limits double x increment; // increment for plotting int max row, max col; // screen size }; int Plot :: get print row(double y value) /* Post: Returns the row of the screen at which a point with y-coordinate y value should be plotted, or returns − 1 if the point would fall off the edge of the screen. */ { double interp y = ((double) max row) * (y high − y value)/(y high − y low) + 0.5; int answer = (int) interp y; if (answer < 0 | answer > max row) answer = − 1; | return answer; } Graphing the Expression: The Class Plot Data Structures and Program Design In C++ Transp. 36, Sect. 13.5, An Interactive Expression Evaluator 517 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
37.
Points struct Point {
int row, col; Point( ); Point(int a, int b); bool operator == (const Point &p); bool operator != (const Point &p); bool operator >= (const Point &p); bool operator <= (const Point &p); bool operator > (const Point &p); bool operator < (const Point &p); }; bool Point :: operator < (const Point &p) {return (row < p.row) | (col < p.col && row == p.row); } | void Plot :: find points(Expression &postfix) /* Post: The Expression postfix is evaluated for values of x that range from x low to x high in steps of x increment. For each evaluation we add a Point to the Sortable list points. Uses: Classes Token, Expression, Point, and List. */ { double x val = x low; double y val; while (x val <= x high) { Token :: set x(x val); postfix.evaluate postfix(y val); postfix.rewind( ); Point p(get print row(y val), get print col(x val)); points.insert(0, p); x val += x increment; } } Points Data Structures and Program Design In C++ Transp. 37, Sect. 13.5, An Interactive Expression Evaluator 518 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
38.
Drawing the Graph void
Plot :: draw( ) /* Post: All screen locations represented in points have been marked with the character # to produce a picture of the stored graph. Uses: Classes Point and Sortable list and its method merge sort. */ { points.merge sort( ); int at row = 0, at col = 0; // cursor coordinates on screen for (int i = 0; i < points.size( ); i ++ ) { Point q; points.retrieve(i, q); if (q.row < 0 | q.col < 0) continue; // off the scale of the graph | if (q.row < at row | (q.row == at row && q.col < at col)) continue; | if (q.row > at row) { // Move cursor down the screen. at col = 0; while (q.row > at row) { cout < endl; < at row ++ ; } } if (q.col > at col) // Advance cursor horizontally. while (q.col > at col) { cout < " "; < at col ++ ; } cout < "#"; < at col ++ ; } while (at row ++ <= max row) cout < endl; < } Drawing the Graph Data Structures and Program Design In C++ Transp. 38, Sect. 13.5, An Interactive Expression Evaluator 519 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
39.
A Graphics-Enhanced Plot
Class void Plot :: find points(Expression &postfix) /* Post: The Expression postfix is evaluated for values of x that range from x low to x high in steps of x increment. For each evaluation we add a Point of the corresponding data point to the Sortable list points. Uses: Classes Token, Expression, Point, and List and the library < graphics.h >. */ { int graphicdriver = DETECT, graphicmode; initgraph( &graphicdriver, &graphicmode, ""); // screen detection and graphresult( ); // initialization max col = getmaxx( ); // with graphics.h library max row = getmaxy( ); double x val = x low; double y val; while (x val <= x high) { Token :: set x(x val); postfix.evaluate postfix(y val); postfix.rewind( ); Point p(get print row(y val), get print col(x val)); points.insert(0, p); x val += x increment; } } A Graphics-Enhanced Plot Class Data Structures and Program Design In C++ Transp. 39, Sect. 13.5, An Interactive Expression Evaluator 520 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
40.
Plotting the Points void
Plot :: draw( ) /* Post: All pixels represented in points have been marked to produce a picture of the stored graph. Uses: Classes Point and Sortable list and the library <graphics.h> */ { for (int i = 0; i < points.size( ); i ++ ) { Point q; points.retrieve(i, q); if (q.row < 0 | q.col < 0) continue; // off the scale of the graph | if (q.row > max row | q.col > max col) continue; | putpixel(q.col, q.row, 3); // graphics.h library function } cout "Enter a character to clear screen and continue: " < flush; < char wait for; cin > wait for; > restorecrtmode( ); // graphics.h library function } Plotting the Points Data Structures and Program Design In C++ Transp. 40, Sect. 13.5, An Interactive Expression Evaluator 521 © 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Télécharger maintenant