SlideShare a Scribd company logo
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)
Arithmetic Expressions
• Postfix form
– Operator appears after the operands
• (4+3)*5 : 4 3 + 5 *
• 4+(3*5) : 4 3 5 * +
– No precedence rules or parentheses!
• Input expression given in postfix form
– How to evaluate it?
Evaluating Postfix Expressions
• Use a stack, assume binary operators +,*
• Input: postfix expression
• Scan the input
– If operand,
• push to stack
– If operator
• pop the stack twice
• apply operator
• push result back to stack
Example
• Input
5 9 8 + 4 6 * * 7 + *
• Evaluation
push(5)
push(9)
push(8)
push(pop() + pop()) /* be careful for ‘-’ */
push(4)
push(6)
push(pop() * pop())
push(7)
push(pop() + pop())
push(pop() * pop())
print(pop())
• What is the answer?
Exercise
• Input
6 5 2 3 + 8 * + 3 + *
• Input
a b c * + d e * f + g * +
• For each of the previous inputs
– Find the infix expression
Infix to Postfix Conversion
• Observation
– Operands appear in the same order in both
– Output operands as we scan the input
– Must put operators somewhere
• use a stack to hold pending operators
– ‘)’ indicates both operands have been seen
• Will allow only +, *, ‘(‘, and ‘)’, and use
standard precedence rules
• Assume legal (valid) expression
Conversion Algorithm
• Output operands as encountered
• Stack left parentheses
• When ‘)’
– repeat
• pop stack, output symbol
– until ‘(‘
• ‘(‘ is poped but not output
• If symbol +, *, or ‘(‘
– pop stack until entry of lower priority or ‘(‘
• ‘(‘ removed only when matching ‘)’ is processed
– push symbol into stack
• At end of input, pop stack until empty
Exercises
• (5 * (((9 + 8) * (4 * 6)) + 7))
• 6 * (5 + (2 + 3) * 8 + 3)
• a + b * c + (d * e + f) * g

More Related Content

PPT
Stacks
PPTX
Stack_Overview_Implementation_WithVode.pptx
PPTX
week9-prefixinfixandpostfixnotations-191013065821.pptx
PPTX
Prefix, Infix and Post-fix Notations
PPTX
DS-UNIT 3 FINAL.pptx
PPT
PPTX
Unit 3 Stacks and Queues.pptx
PPTX
Unit I - Evaluation of expression
Stacks
Stack_Overview_Implementation_WithVode.pptx
week9-prefixinfixandpostfixnotations-191013065821.pptx
Prefix, Infix and Post-fix Notations
DS-UNIT 3 FINAL.pptx
Unit 3 Stacks and Queues.pptx
Unit I - Evaluation of expression

Similar to Arithmetic notation conversion infix to1 (20)

PPTX
DSA_chapter_04_Stack and data structure and Queue.pptx
PPTX
Stack and queue
PPTX
infix,postfix,prefixavabanwnwnwjjjj.pptx
PPTX
Stack,queue and linked list data structure.pptx
PPTX
My lecture infix-to-postfix
PDF
1.3- infix-ti-postfix.pdf
PPTX
Stack, implementation using array and linked list
PPTX
Unit II - LINEAR DATA STRUCTURES
PPT
data structure and algorithm by bomboat_3
PPT
Stack application
PPT
week 7,8,10,11 alll files included from .ppt
PDF
The concept of stack is extremely important in computer science and .pdf
PPTX
introduction of the Stacks and Queues.pptx
PPTX
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
PPT
MO 2020 DS Stacks 3 AB.ppt
PDF
Chapter 4 stack
PPTX
Infix-Postfix expression conversion
PPTX
DS UNIT1_STACKS.pptx
PPTX
Stack and its Applications : Data Structures ADT
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
DSA_chapter_04_Stack and data structure and Queue.pptx
Stack and queue
infix,postfix,prefixavabanwnwnwjjjj.pptx
Stack,queue and linked list data structure.pptx
My lecture infix-to-postfix
1.3- infix-ti-postfix.pdf
Stack, implementation using array and linked list
Unit II - LINEAR DATA STRUCTURES
data structure and algorithm by bomboat_3
Stack application
week 7,8,10,11 alll files included from .ppt
The concept of stack is extremely important in computer science and .pdf
introduction of the Stacks and Queues.pptx
Application of Stack For Expression Evaluation by Prakash Zodge DSY 41.pptx
MO 2020 DS Stacks 3 AB.ppt
Chapter 4 stack
Infix-Postfix expression conversion
DS UNIT1_STACKS.pptx
Stack and its Applications : Data Structures ADT
Data Structure and Algorithms by Sabeen Memon03.pptx
Ad

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Pre independence Education in Inndia.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Types and Its function , kingdom of life
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Insiders guide to clinical Medicine.pdf
Computing-Curriculum for Schools in Ghana
human mycosis Human fungal infections are called human mycosis..pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPH.pptx obstetrics and gynecology in nursing
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial diseases, their pathogenesis and prophylaxis
Pre independence Education in Inndia.pdf
VCE English Exam - Section C Student Revision Booklet
Cell Types and Its function , kingdom of life
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Insiders guide to clinical Medicine.pdf
Ad

Arithmetic notation conversion infix to1

  • 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)
  • 2. Arithmetic Expressions • Postfix form – Operator appears after the operands • (4+3)*5 : 4 3 + 5 * • 4+(3*5) : 4 3 5 * + – No precedence rules or parentheses! • Input expression given in postfix form – How to evaluate it?
  • 3. Evaluating Postfix Expressions • Use a stack, assume binary operators +,* • Input: postfix expression • Scan the input – If operand, • push to stack – If operator • pop the stack twice • apply operator • push result back to stack
  • 4. Example • Input 5 9 8 + 4 6 * * 7 + * • Evaluation push(5) push(9) push(8) push(pop() + pop()) /* be careful for ‘-’ */ push(4) push(6) push(pop() * pop()) push(7) push(pop() + pop()) push(pop() * pop()) print(pop()) • What is the answer?
  • 5. Exercise • Input 6 5 2 3 + 8 * + 3 + * • Input a b c * + d e * f + g * + • For each of the previous inputs – Find the infix expression
  • 6. Infix to Postfix Conversion • Observation – Operands appear in the same order in both – Output operands as we scan the input – Must put operators somewhere • use a stack to hold pending operators – ‘)’ indicates both operands have been seen • Will allow only +, *, ‘(‘, and ‘)’, and use standard precedence rules • Assume legal (valid) expression
  • 7. Conversion Algorithm • Output operands as encountered • Stack left parentheses • When ‘)’ – repeat • pop stack, output symbol – until ‘(‘ • ‘(‘ is poped but not output • If symbol +, *, or ‘(‘ – pop stack until entry of lower priority or ‘(‘ • ‘(‘ removed only when matching ‘)’ is processed – push symbol into stack • At end of input, pop stack until empty
  • 8. Exercises • (5 * (((9 + 8) * (4 * 6)) + 7)) • 6 * (5 + (2 + 3) * 8 + 3) • a + b * c + (d * e + f) * g