Lab 05 – Expressions.

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

Computer Science 2 Data Structures V section 2 Recitation 1.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
CST Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.
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.
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.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
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.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
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.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Reverse Polish Notation Written by J.J. Shepherd.
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.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Programming Abstractions
Review Use of Stack Introduction Stack in our life Stack Operations
Chapter 4 Stacks
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.
Stacks Stacks.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures 5th Week
Expression Trees Eric Roberts CS 106B March 4, 2013.
MEMORY REPRESENTATION OF STACKS
Stack as an ADT.
Homework 4 questions???.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 4.
Stack application: postponing data usage
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Stacks – Calculator Application
Visit for more Learning Resources
Stacks – Calculator Application
PART II STACK APPLICATIONS
106 Data Structure Homework 1
Monday, February 26, 2018 Announcements… For Today…
COMPUTER 2430 Object Oriented Programming and Data Structures I
Lab 04 – Linked List.
5.4 Additional Stack Applications
Stacks, Queues, and Deques
Infix to Postfix Conversion
COMPUTER 2430 Object Oriented Programming and Data Structures I
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Computer Science 2 5/17/2016 Finish the Queue Program
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
Stack.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Lab 03 – Linked List.
Chapter 6 – Queues and Deques
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.
© 2016 Pearson Education, Ltd. All rights reserved.
CHAPTER 3: Collections—Stacks
Presentation transcript:

Lab 05 – Expressions

Requirements Input is an expression string in infix notation. Lab 05 - Expressions Input is an expression string in infix notation. Expression: 43 + 2 * 19 Use an ExpressionManager class to hold your infix, postfix, (and prefix) expressions. Your class should be derived from the abstract interface class ExpressionManagerInterface. Implement member functions: virtual int value(void); virtual string infix(void); virtual string postfix(void); virtual string prefix(void); Output the resulting infix, postfix, (prefix), and integer evaluation value of the expression. Your calculations should perform integer division and produce integer results.

Example Input / Output Input Input Output Output Lab 05 - Expressions Input Input Expression: 43 + 2 * 19 Infix: Postfix: Prefix: Value: Expression: 2 + 3 - 5 Infix: Postfix: Prefix: Value: Output Output Expression: 43 + 2 * 19 Infix: 43 + 2 * 19 Postfix: 43 2 19 * + Prefix: + 43 * 2 19 Value: 81 Expression: 2 + 3 - 5 Infix: 2 + 3 - 5 Postfix: 2 3 + 5 - Prefix: - + 2 3 5 Value: 0

Converting from Infix to Postfix Lab 05 - Expressions

Converting from Infix to Postfix Lab 05 - Expressions

Converting from Infix to Postfix Lab 05 - Expressions

ExpressionManagerInterface Lab 05 - Expressions //YOU MAY NOT MODIFY THIS HEADER INTERFACE #ifndef EXPRESSION_MANAGER_INTERFACE_H #define EXPRESSION_MANAGER_INTERFACE_H #include <string> using std::string; class ExpressionManagerInterface { public: ExpressionManagerInterface(void) {}; virtual ~ExpressionManagerInterface(void) {}; /** Return the integer value of the infix expression */ virtual int value(void) = 0; /** Return the infix items from the expression Throw an error if the expression 1) is not balanced. 2) the number of operators IS NOT one less than the number of operands. 3) there are adjacent operators. */ virtual string infix(void) = 0; /** Return a postfix representation of the infix expression */ virtual string postfix(void) = 0; /** (BONUS) Return a prefix representation of the infix expression */ virtual string prefix(void) { return "NOT IMPLEMENTED"; } /** Return the infix vector'd expression items */ virtual string toString() const = 0; }; #endif // EXPRESSION_MANAGER_INTERFACE_H ExpressionManagerInterface is an interface. DO NOT MODIFY! ExpressionManagerInterface ExpressionManager

Inherits from ExpressionManagerInterface Lab 05 - Expressions class ExpressionManager : public ExpressionManagerInterface { private: string expression_; std::vector<string> inFix_; std::vector<string> postFix_; std::vector<string> preFix_; const string operators = "([{ -+ */% "; public: ExpressionManager() { } ExpressionManager(string exp) : expression_(exp), inFix_(NULL), postFix_(NULL), preFix_(NULL) { } ~ExpressionManager() { } virtual int value(void); /** Return the integer value of the infix expression */ virtual string infix(void); /** Return the infix expression / rejects invalid */ virtual string postfix(void); /** Return a postfix representation */ virtual string prefix(void); /** (BONUS) Return a prefix representation */ virtual string toString() const; /** Return the infix vector'd expression items */ }; #endif // EXPRESSION_MANAGER_H Inherits from ExpressionManagerInterface

inFix to PreFix Given two operands a and b and an operator , Lab 05 - Expressions Given two operands a and b and an operator , The infix notation implies that  will be placed in between a and b. When the operator is placed after both operands , it is called postfix notation. And when the operator is placed before the operands  a b, the expression in prefix notation. Example: To convert “(A + B) * C” to prefix: Reverse the infix expression (A+B)*C becomes C*(B+A). Note: while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘. Obtain the postfix expression (almost) of the modified expression. Push operator on stack is greater than or equal. C*(B+A) becomes CBA+*. Reverse the postfix expression. CBA+* becomes the prefix expression *+ABC.