CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials...

Preview:

Citation preview

CS 106X – Programming Abstractions in C++Cynthia Bailey Lee

              

CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee

 is licensed under a Creative Commons Attribution-

NonCommercial-ShareAlike 4.0 International License.

Permissions beyond the scope of this license may be available at 

http://peerinstruction4cs.org.

2

Today’s Topics

1. ADTs/Containers: what are they?2. Stack3. Compare Stack and Vector

implementations of the same code4. [Maybe Monday] Stack example: Reverse

Polish Notation evaluator

ADTs

ADTs Programming language independent

models of common containers They encompass not only the nature of

the data, but ways of accessing it

They form a rich vocabulary of nouns and verbs, often drawing on analogies to make their use intuitive, and to give code written in them a certain literary quality

"Hope" is the thing with feathersBY EMILY DICKENSON

“Hope” is the thing with feathers -That perches in the soul -And sings the tune without the words -And never stops - at all -

And sweetest - in the Gale - is heard -And sore must be the storm -That could abash the little BirdThat kept so many warm -

I’ve heard it in the chillest land -And on the strangest Sea -Yet - never - in Extremity,It asked a crumb - of me.

Some ADTs implemented in the Stanford Libraries Vector Grid Graph Map (and HashMap) Set (and HashSet) PriorityQueue Queue Stack

ADT: Grid

static void clearBoard(Grid<bool>& board);

int main(){ Grid<bool> board(8,8); clearBoard(board); //not strictly necessary // …more code to come… return 0;}

static void clearBoard(Grid<bool>& board){ for (int i=0; i<board.numRows(); i++){ for (int j=0; j<board.numCols(); j++){ board[i][j] = false; } }}

queensafety.cpp

Stacks

New ADT: Stacktemplate <typename ValueType> class Stack {

public: Stack(); virtual ~Stack(); int size() const; bool isEmpty() const; void clear(); void push(ValueType value); ValueType pop(); ValueType peek() const; std::string toString();

private:

};

stack.h

-Redacted-

New ADT: Stacktemplate <typename ValueType> class Stack {

public: Stack(); virtual ~Stack(); int size() const; bool isEmpty() const; void clear(); void push(ValueType value); ValueType pop(); ValueType peek() const; std::string toString();

private:

};

stack.h

-Redacted- This image was released by the National Cancer Institute, an agency part of the National Institutes of Health, with the ID 8368

Using a Stack to buffer file inputvoid mystery(ifstream& infile) { Stack<string> lines; string line; while (getline(infile,line)) { lines.push(line); } infile.close(); while (!lines.isEmpty()) { cout << lines.pop() << endl; }}

What does this code do?A. Prints all lines of a

file to cout B. Prints only the first

line of a file to coutC. Prints only the last

line of a file to coutD. Prints all lines of a

file to cout in reverse

E. All/ none/ more than one of the above

void mystery(ifstream& infile) { Stack<string> lines; string line; while (getline(infile,line)) { lines.push(line); } infile.close(); while (!lines.isEmpty()) { cout << lines.pop() << endl; }}

Using a Stack to buffer file inputWhat does this code do?A. Prints all lines of a

file to cout B. Prints only the first

line of a file to coutC. Prints only the last

line of a file to coutD. Prints all lines of a

file to cout in reverse

E. All/ none/ more than one of the above

Why do I need Stack? I could have done that with a Vector!

Stack or Vector?

Vector<string> lines;

lines.insert(lines.size(), line);

cout << lines[lines.size()-1] << endl;lines.remove(lines.size()-1);

void mystery(ifstream& infile) { Stack<string> lines; string line; while (getline(infile,line)) { lines.push(line); } infile.close(); while (!lines.isEmpty()) { cout << lines.pop() << endl; }}

Vector versionvoid mystery2(ifstream& infile) { Vector<string> lines; string line; while (getline(infile,line)) { lines.insert(lines.size(),line); } infile.close(); while (!lines.isEmpty()) { cout << lines[lines.size()-1] << endl; lines.remove(lines.size()-1); }}

Applications of StacksWe’ve seen one (buffering input and giving it back in reverse—LIFO—order). What else are Stacks good for?

Operator Precedence and Syntax Trees Ignoring operator precedence rules, how

many distinct results are there to the following arithmetic expression? 3 * 3 + 3 * 3

A. 1B. 2C. 3D. 4E. More than 4

Reverse Polish Notation Ambiguities don’t exist in RPN Also called “postfix” because the

operator goes after the operands

Postfix (RPN): 4 3 * 4 3 * +

Equivalent Infix: (4*3) + (4*3)

http://commons.wikimedia.org/wiki/File:Hewlett-Packard_48GX_Scientific_Graphing_Calculator.jpg

Reverse Polish Notation This postfix expression:

4 3 * 7 2 5 * + + Is equivalent to this infix expression:A. ((4*3) + (7*2)) + 5B. (4*3) + ((7+2) + 5)C. (4*3) + (7 + (2*5))D. Other/none/more than one

http://commons.wikimedia.org/wiki/File:Hewlett-Packard_48GX_Scientific_Graphing_Calculator.jpg

Stacks and RPN Evaluate this expression with the help of a

stack Encounter a number: PUSH it Encounter an operator: POP two numbers and

PUSH result 4 3 * 7 2 5 * + +

4 3

4

12

*7

12

2

7

12

* ?

?

?

?

?

?

Contents of the stack, reading from top down:

A. 7, 12B. 2, 7, 12C. 10, 7,12D. 10, 5, 2,

7, 12E. Other

5

2

7

12

Stacks and RPN:What does that look like in code?

Evaluate this expression with the help of a stack Encounter a number: PUSH it Encounter an operator: POP two numbers and PUSH result

43*725*++

Recommended