background preloader

HW1: Postfix Calculator

Facebook Twitter

ECS 40 Homework 1: Postfix Calculator. Due: Thursday, January 19 at 2200 hours.

ECS 40 Homework 1: Postfix Calculator

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. Only some CSIF machines are x86-64. Check in: Makefile and all the necessary source files (do NOT check in executable or object files!). Note: This is an individual homework. Description Create a simple postfix calculator. Reverse Polish notation. During the 1970s and 1980s, RPN was known to many calculator users, as it was used in some handheld calculators of the time designed for advanced users: for example, the HP-10C series and Sinclair Scientific calculators.

Reverse Polish notation

Most of what follows is about binary operators. Postfix Calculator. This calculator uses postfix notation.

Postfix Calculator

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. " Postfix Evaluation. Infix Expression : Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.

Postfix Evaluation

Postfix Expression : The Postfix(Postorder) form of the above expression is "23*45/-". Linked list. A linked list whose nodes contain two fields: an integer value and a link to the next node.

Linked list

The last node is linked to a terminator used to signify the end of the list. The principal benefit of a linked list over a conventional array is that the list elements can easily be inserted or removed without reallocation or reorganization of the entire structure because the data items need not be stored contiguously in memory or on disk, while an array has to be declared in the source code, before compiling and running the program. Linked lists allow insertion and removal of nodes at any point in the list, and can do so with a constant number of operations if the link previous to the link being added or removed is maintained during list traversal. On the other hand, simple linked lists by themselves do not allow random access to the data, or any form of efficient indexing. Advantages[edit] Disadvantages[edit] History[edit] Stack (abstract data type)

Simple representation of a stack.

Stack (abstract data type)

Classes (I) Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.

Classes (I)

An object. 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.

Introduction to C++ Classes

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. C++ classes. Differences between struct and classes in C++[edit] In C++, a structure is a class defined with the struct keyword.[1] Its members and base classes are public by default.

C++ classes

A class defined with the class keyword has private members and base classes by default. Object-Oriented C++ Class Design - CProgramming.com. Understanding Interfaces When you're designing a class in C++, the first thing you should decide is the public interface for the class.

Object-Oriented C++ Class Design - CProgramming.com

The public interface determines how your class will be used by other programmers (or you), and once designed and implemented it should generally stay pretty constant. You may decide to add to the interface, but once you've started using the class, it will be hard to remove functions from the public interface (unless they aren't used and weren't necessary in the first place).

But that doesn't mean that you should include more functionality in your class than necessary just so that you can later decide what to remove from the interface. If you do this, you'll just make the class harder to use. Friendship and inheritance. 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 apply to "friends". Friends are functions or classes declared with the friend keyword. Classes (II) [NOTE: This chapter requires proper understanding of dynamically allocated memory] Special member functions are member functions that are implicitly defined as member of classes under certain circumstances. 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. That means that, for example, five values of type int can be declared as an array without having to declare 5 different variables (each with its own identifier).

Instead, using an array, the five int values are stored in contiguous memory locations, and all five can be accessed using the same identifier, with the proper index. For example, an array containing 5 integer values of type int called foo could be represented as: where each blank panel represents an element of the array. In this case, these are values of type int. Like a regular variable, an array must be declared before it is used. Therefore, the foo array, with five elements of type int, can be declared as: Initializing arrays By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. Control Structures. A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon (;), and are executed in the same order in which they appear in a program.

But programs are not limited to a linear sequence of statements. During its process, a program may repeat segments of code, or take decisions and bifurcate. For that purpose, C++ provides flow control statements that serve to specify what has to be done by our program, when, and under which circumstances. Many of the flow control statements explained in this section require a generic (sub)statement as part of its syntax. . { statement1; statement2; statement3; } The entire block is considered a single statement (composed itself of multiple substatements).

C++ Operators. Logical OR Operator: Logical-or-expression || logical-and-expression 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. Delete an array element. Istream. Public member function Get characters. Using cin.get, cin.getline, & cin.ignore. Using >>, cin.get, cin.getline, and cin.ignore Using the >> operator (with cin) Getline. Istream. Istream. Public member function istream& ignore (streamsize n = 1, int delim = EOF); Extract and discard characters Extracts characters from the input sequence and discards them, until either characters have been extracted, or one compares equal to . Fgets. Function char * fgets ( char * str, int num, FILE * stream ); Get string from stream. How to use EOF in C.

In article Malcolm McLean >the function fgetc() will return an integer, not a character as you may haveimagined, in the range 0-255 except some very odd systems that don't use 8bit bytes. Ios. Public member function Check whether eofbit is set. Atoi. Function int atoi (const char * str); Convert string to integer. C Strings in C and C. How to concatenate two integer values. Infix, Postfix and Prefix.