Computer Science 2 Data Structures V22.0102 section 2 Recitation 1.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Prefix, Postfix, Infix Notation
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
CS Data Structures Chapter 3 Stacks and Queues.
CS 206 Introduction to Computer Science II 10 / 28 / 2009 Instructor: Michael Eckmann.
COMPUTER SCIENCE I C++ INTRODUCTION
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
SUNY Oneonta Data Structures and Algorithms Visualization Teaching materials Generation Group.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Input, Output, and Processing
Linear Data Structures LIFO – Polish notation Context Saving.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
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.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Review Use of Stack Introduction Stack in our life Stack Operations
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Chapter Topics The Basics of a C++ Program Data Types
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Data Structures and Algorithms
Introduction to C++ Programming Language
Basic Elements of C++.
CSC 172 DATA STRUCTURES.
Data Structures and Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
Basic Elements of C++ Chapter 2.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Lecture No.07 Data Structures Dr. Sohail Aslam
Infix to Postfix Conversion
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Computer Science 2 5/17/2016 Finish the Queue Program
Arithmetic Operations
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
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.
Python Creating a calculator.
Presentation transcript:

Computer Science 2 Data Structures V section 2 Recitation 1

Plan for Today General Questions/Confusions Assignment #2 – Overview & Questions –The Calculator Class –The Converter Class –Sample Input/Output Assignment #2 – The Command-line & Keyboard Input 2

3 Assignment #2: Calculator Introduction Your project is to design a program to implement a calculator. The calculator will take an infix expression convert it to a postfix expression and then evaluate it. Review: infix and postfix notation

4 Assignment #2: Calculator You should implement the calculator in two parts: A Converter class that will convert the input string to postfix. A Calculator class that will evaluate the postfix expression.

5 Assignment #2: Calculator The text contains explanations of the algorithms you will need. See pages for the algorithm you should use for the Calculator class See pages for the Converter algorithm (Another explanation of the two algorithms can be found here).here You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment.PostCalc

6 Assignment #2: Calculator You can also use the ideas from the PostCalc class we looked at in lecture for your Calculator class. However, it will need to be modified to work for this assignment. PostCalc Both the Converter and Calculator classes, should use the StackLI class for the Stack.StackLI Are the methods in StackLI & PostCalc.java clear to everyone? StackLI

7 Assignment #2: Calculator The Calculator class Basically this class will be the same as the PostCalc class shown in lecture. The main differences will be: –The input will be an infix expression instead of a postfix expression. –It will be entered by the user instead of hard- coded by you. –The input may have operands which consist of multiple digits -- not just single digits like PostCalc

8 Assignment #2: Calculator –Parentheses are legal in the infix expression (Note: The parentheses are needed in the infix expression. After your Converter class' algorithm converts the expression to postfix, it will no longer have parentheses because they are not necessary in postfix expressions.) –The Calculator class will instantiate an object of the Converter class in order to have the infix expression converted to a postfix expression. –You may have to modify the other methods in the PostCalc class (and/or add additional ones).

9 Assignment #2: Calculator The Converter class The Converter class will use a Stack in order to implement the algorithm in the book. When the Converter class is instantiated, a String is passed representing the infix expression entered. This should be saved as an instance variable. The method that does all the work in the Converter class should be called toPostFix(). It will convert the infix expression to a postfix expression. (It should return a Queue (use the queue from the book) of Strings representing the postfix expression to the Calculator class. The details and helper methods used in this class are left up to you.

10 Assignment #2: Calculator The Converter class: Hint You will probably want to implement a method that compares two operators for precedence: /** 1 if opA has higher precedence * -1 if opB has higher precedence * 0 if they have equal precedence */ int comparePrecedence(char opA, char opB);

11 Assignment #2: Calculator Sample inputs –3+4*5/6 –(300+23)*(43-21)/(84+7) –(4+8)*(6-5)/((3-2)*(2+2)) Your output should show the converted postfix string and the result of the calculation.

12 Assignment #2: Calculator For instance, here is a sample run of the program (user input in italic, program output in courier ): The input infix expression: $ (4+8)*(6-5)/((3-2)*(2+2)) INFIX: (4+8)*(6-5)/((3-2)*(2+2)) Converted to postfix: POSTFIX: * * / And the answer: RESULT: 3.0

13 Assignment #2: Calculator For a given input at prompt $ $ (4+8)*(6-5)/((3-2)*(2+2)) INFIX: (4+8)*(6-5)/((3-2)*(2+2)) POSTFIX: * * / RESULT: 3.0 Questions?