HW1: Postfix Calculator

TwitterFacebook
Get flash to fully experience Pearltrees
http://cancer.cs.ucdavis.edu/course/ecs40/w12/homework/1/

ECS 40 Homework 1: Postfix Calculator

Due : Thursday, January 19 at 2200 hours. Check out : git clone metastasis@cancer.cs.ucdavis.edu:user/{username}/1/1 Hand out : Reference program: postfix ( SHA-1 : 955dc54f30c2e85215122ade7c75f8001d2f768a ). Example input: postfix.test . Note: the reference program runs on only x86-64 Linux, where the command uname -m should output x86_64 .
Reverse Polish notation ( RPN ) is a mathematical notation in which every operator follows all of its operands , in contrast to Polish notation , which puts the operator in the prefix position. It is also known as postfix notation and is parenthesis-free as long as operator arities are fixed. The description "Polish" refers to the nationality of logician Jan Łukasiewicz , who invented (prefix) Polish notation in the 1920s. http://en.wikipedia.org/wiki/Reverse_Polish_notation

Reverse Polish notation

http://www.uea.ac.uk/~dps/calculator.html

Postfix Calculator

This calculator uses postfix notation. To use the calculator your browser requires JavaScript support. The calculator works a little differently from other calculators you may have used. Suppose you want to add two numbers. Enter the first number. Press "Enter."
http://scriptasylum.com/tutorials/infix_postfix/algorithms/postfix-evaluation/index.htm

Postfix Evaluation

Example : Let us see how the above algorithm will be imlemented using an example. Postfix String : 123*+4- Initially the Stack is empty. Now, the first three characters scanned are 1,2 and 3, which are operands.
In computer science , a linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and a reference (in other words, a link ) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence. A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list.

Linked list

http://en.wikipedia.org/wiki/Linked_list
http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29 Simple representation of a stack In computer science , a stack is a particular kind of abstract data type or collection in which the principal (or only) operations on the collection are the addition of an entity to the collection, known as push and removal of an entity, known as pop . [ 1 ] The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure . In a LIFO data structure, the last element added to the structure must be the first one to be removed. This is equivalent to the requirement that, considered as a linear data structure , or more abstractly a sequential collection, the push and pop operations occur only at one end of the structure, referred to as the top of the stack. Often a peek or top operation is also implemented, returning the value of the top element without removing it. A stack may be implemented to have a bounded capacity.

Stack (abstract data type)

http://www.cplusplus.com/doc/tutorial/classes/ A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are generally declared using the keyword class , with the following format:

Classes (I)

Introduction to C++ Classes

Contents An Example C++ Class C++ classes are similar to Java classes in many ways, but there are also important differences. Below is an example of a C++ class named IntList to be used to represent a list of integers; operations to add a value to the end of the list and to print the list are provided. The implementation uses a dynamically allocated array to store the integers; when the array is full, a new array of twice the size is allocated. In Java, the class definition would all be in a single file. http://pages.cs.wisc.edu/~hasti/cs368/CppTutorial/NOTES/CLASSES-INTRO.html

C++ classes

The C++ programming language allows programmers to separate program-specific data types through the use of classes . Classes define types of data structures and the functions that operate on those data structures. Instances of these data types are known as objects and can contain member variables , constants , member functions , and overloaded operators defined by the programmer. Syntactically, classes are extensions of the C struct , which cannot contain functions or overloaded operators. http://en.wikipedia.org/wiki/C%2B%2B_classes
Friend functions In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends . Friends are functions or classes declared with the friend keyword. If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend :

Friendship and inheritance

Classes (II)

Overloading operators C++ incorporates the option to use standard operators to perform operations with classes in addition to with fundamental types. For example: This is obviously valid code in C++, since the different variables of the addition are all fundamental types. Nevertheless, it is not so obvious that we could perform an operation similar to the following one: In fact, this will cause a compilation error, since we have not defined the behavior our class should have with addition operations.

Arrays

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances. With the introduction of control structures we are going to have to introduce a new concept: the compound-statement or block . A block is a group of statements which are separated by semicolons (;) like all C++ statements, but grouped together in a block enclosed in braces: { } : { statement1; statement2; statement3; } Most of the control structures that we will see in this section require a generic statement as part of its syntax.

Control Structures

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool . Logical OR has left-to-right associativity. The operands to the logical OR operator need not be of the same type, but they must be of integral or pointer type.

Logical OR Operator: ||