CS 211. Project 5 Infix Expression Evaluation

Similar documents
CS 211 Programming Practicum Spring 2018

CS 211 Programming Practicum Fall 2018

CS 211 Programming Practicum Spring 2017

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CS 206 Introduction to Computer Science II

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stacks Calculator Application. Alexandra Stefan

Data Structures Week #3. Stacks

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Stack Abstract Data Type

March 13/2003 Jayakanth Srinivasan,

Stacks and their Applications

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

STACKS AND QUEUES. Problem Solving with Computers-II

Project 1: Implementation of the Stack ADT and Its Application

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Infix to Postfix Conversion

Lecture No.04. Data Structures

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. Revised based on textbook author s notes.

Lecture 8: Simple Calculator Application

Linear Data Structure

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Data Structures and Algorithms

Lecture 12 ADTs and Stacks

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

ADT Sorted List Operations

Stack and Its Implementation

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

CSE 230 Intermediate Programming in C and C++

An Introduction to Trees

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

Top of the Stack. Stack ADT

Stacks, Queues (cont d)

Name CPTR246 Spring '17 (100 total points) Exam 3

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Formal Languages and Automata Theory, SS Project (due Week 14)

Top of the Stack. Stack ADT

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Dynamic Data Structures

Parsing Scheme (+ (* 2 3) 1) * 1

CMPS 390 Data Structures

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

Monday, November 13, 2017

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

CS 206 Introduction to Computer Science II

1. Stack Implementation Using 1D Array

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

CSE 12 Week Eight, Lecture One

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Lists are great, but. Stacks 2

CS 2604 Minor Project 1 Summer 2000

Discussion of project # 1 permutations

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Expressions and Assignment

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

Exercise 6.2 A generic container class

Linked Lists, Stacks, and Queues

PA3 Design Specification

CS24 Week 4 Lecture 2

CHAPTER 8: Central Processing Unit (CPU)

Today's Topics. CISC 458 Winter J.R. Cordy

Data Structure (CS301)

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS W3134: Data Structures in Java

8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks

Stacks CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008

BBM 201 DATA STRUCTURES

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Sample Question Paper

BBM 201 DATA STRUCTURES

Project 3: RPN Calculator

Lecture Data Structure Stack

4.3 Stacks and Queues

CS 261: Data Structures. Dynamic Array Queue

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Stacks Fall 2018 Margaret Reid-Miller

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community


! Set of operations (add, remove, test if empty) on generic data. ! Intent is clear when we insert. ! Which item do we remove? Stack.

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Disclaimer. A Bit About Forth. No Syntax. Outline. I don't know Forth Typical Forth tutorial. Dave Eckhardt

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CSE 214 Computer Science II Stack

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Transcription:

CS 211 Project 5 Infix Expression Evaluation

Part 1 Create you own Stack Class Need two stacks one stack of integers for the operands one stack of characters for the operators Do we need 2 Stack Classes? (perhaps not) one Stack Class of the Token class from the proj5base.cpp file will work one Stack Class of integers will also work since characters are a sub-type of integers (conversion with type cast may be needed) Use C++ templating is not allowed!! First: An INSANE idea for your first C++ Object program Second: You are to write this code yourself not copy it from the internet

Part 1 Create your own Stack Class Requires implementation with a Dynamic Array Data Members: pointer to a dynamic array amount of space currently allocated amount of space currently in use Methods Constructor/Initialize Reset (clear array) IsEmpty() Push() Pop() Top()

Part 1 Create your own Stack Class Naming your class MyStack rather than Stack is suggested avoids confusion with any system provided stack class Dynamic array initialize to only hold 2 values grow by an increase of 2 values each time just like Project 2 Data Members MUST be private In-Class Use Only Methods should also be private ex: grow() method called by push()

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN If token is Value: Push on If token is Operator: Follow Algorithm Current Token:

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: VALUE 3

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: VALUE 3 Push on 3

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: OPERATOR * 3

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: OPERATOR * Since is Empty Push on OP Stack 3 *

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: VALUE 4 3 *

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: VALUE 4 Push on VALUE Stack 4 3 *

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: OPERATOR + 4 3 *

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: OPERATOR + Since Top OP has higher precedence PopAndEval( ) then 4 3 *

PopAndEval ( ) Result = Val1 Op Val2 4 3 *

PopAndEval ( ) Result = Val1 * Val2 Step 1: Op = Top of OP Stack 4 3 *

PopAndEval ( ) Result = Val1 * Val2 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack 4 3

PopAndEval ( ) Result = Val1 * 4 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of 4 3

PopAndEval ( ) Result = Val1 * 4 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of Step 4: Pop 3

PopAndEval ( ) Result = 3 * 4 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of Step 4: Pop Step 5: Val1 = Top of 3

PopAndEval ( ) Result = 3 * 4 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of Step 4: Pop Step 5: Val1 = Top of Step 6: Pop

PopAndEval ( ) Result = 12 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of Step 4: Pop Step 5: Val1 = Top of Step 6: Pop Step 7: Evaluate Result

PopAndEval ( ) Result = 12 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of Step 4: Pop Step 5: Val1 = Top of Step 6: Pop Step 7: Evaluate Result Step 8: Push Result on 12

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: OPERATOR + Since Top OP has higher precedence PopAndEval( ) then 12

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: OPERATOR + Since Top OP has higher precedence PopAndEval( ) then Push on OP Stack 12 +

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: Value 5 Push on 5 12 +

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: EOLN PopAndEval( ) until OP Stack is Empty 5 12 +

PopAndEval ( ) Result = 12 + 5 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of Step 4: Pop Step 5: Val1 = Top of Step 6: Pop Step 7: Evaluate Result Step 8: Push Result on 5 12 +

PopAndEval ( ) Result = 12 + 5 = 17 Step 1: Op = Top of OP Stack Step 2: Pop OP Stack Step 3: Val2 = Top of Step 4: Pop Step 5: Val1 = Top of Step 6: Pop Step 7: Evaluate Result Step 8: Push Result on 17

Step 1. Create/Clear and Operator Stack Step 2. Get Tokens until End Of Line is Reached 3 * 4 + 5 EOLN ^ If token is Value: Push on If token is Operator: Follow Algorithm Current Token: EOLN When OP Stack is Empty Expression Result on Top of 17