Professional Documents
Culture Documents
reading:
Programming Abstractions in C++, Chapter 4, 5.1
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, and others.
I/O Streams
reading:
Programming Abstractions in C++, Chapter 4
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, and others.
Description
f.fail()
f.open(filename)
f.close()
f.get()
getline(f&, str&)
f >> var
The second way is wrong; it prints the last line twice. (Why?)
7
Description
make a new directory with the given name
deleteFile(name)
fileExists(name)
getCurrentDirectory()
isDirectory(name),
isFile(name)
openFile(ifstream&, name)
promptUserForFile(
ifstream&, prompt)
readEntireFile(ifstream&, lines&)
renameFile(oldname, newname)
Find errors
data.txt
Donald Knuth
M
76
Stanford U.
B: 1
D: 3
E: all 4
string name;
name = input.getline();
// #1
"Donald Knuth"
char gender;
gender = input.get();
// #2
'M'
int age;
getline(input, age);
stringToInteger(age);
// #3
76
string jobtitle;
input >> jobtitle;
C: 2
d4a57b1d
// #4
"Stanford U."
Grid
reading:
Programming Abstractions in C++, Section 5.1
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, and others.
11
12
Grid (5.1)
grid: a 2-dimensional indexed collection of elements
like a 2-dimensional array, but easier to use, more features
useful for data tables, 2D board games, geometric data, ...
in Stanford C++ lib, a grid can be represented as a Grid object
#include "grid.h"
column
row
75
61
83
71
94
89
98
100
63
54
51
49
14
Constructing a Grid
Grid<type> name;
15
or
g.fill(value);
g.inBounds(r, c)
g.numCols()
g.numRows()
g.resize(nRows, nCols);
g[r][c] = value;
or
g.set(r, c, value);
g.toString()
16
Grid documentation
See detailed documentation
about Grid (and any other
Stanford C++ library feature)
from the link on the
course web site.
stanford.edu/~stepp/cppdoc/
17
Column-major order:
61
83
71
94
89
98
91
63
54
51
49
61
83
71
94
89
98
91
63
54
51
49
18
r\c
D.
12
d4a57b1d
19
Grid as parameter
Whenever a Grid (or any object) is passed by value, C++ makes a
complete copy of its contents, rather than sharing it as in Java.
Objects are passed by value by default, not by reference.
Making copies of objects is slow, and often not what you want.
So, if you declare a Grid or another object as a parameter,
you should usually pass it by reference with &.
int computeSum(Grid<int>& g) {
...
}
20
Grid exercise
Write a function knightCanMove that accepts a grid and two row/column pairs
(r1, c1), (r2, c2) as parameters, and returns true if there is a knight at chess board
square (r1, c1) and he can legally move to empty square (r2, c2).
Recall that a knight makes an "L" shaped move, going 2 squares in one
dimension and 1 square in the other.
knightCanMove(board, 1, 2, 2, 4) returns true
0
"king"
"knight"
2
3
"rook"
4
5
6
7
21
22
Grid solution 2
bool knightCanMove(Grid<string>& board, int r1, int c1,
int r2, int c2) {
int dr = abs(r1 - r2), dc = abs(c1 - c2);
return board.inBounds(r1, c1) && board.inBounds(r2, c2)
&& board[r1][c1] == "knight" && board[r2][c2] == ""
&& ((dr == 1 && dc == 2) || (dr == 2 && dc == 1));
}
23
75
61
83
71
71
94
89
98
91
91
63
54
51
49
49
21
92
68
37
24
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, and others.
Description
open istream to read tokens from string
ostringstream()
26
Formatted I/O
#include <iomanip>
Description
setw(n)
setfill(ch)
sets padding chars inserted by setw to the given char (default ' ')
setbase(b)
left, right
setprecision(d)
fixed
scientific
//
//
//
//
2
20
200
2000
1.41
4.47
14.14
44.72
27
Exercise
Given hours.txt of section leader hours worked, in this format:
123 Alex 3 2 4 1
46 Jessica 8.5 1.5 5 5 10 6
7289 Erik 3 6 4 4.68 4
28
Exercise solution
while (true) {
double hours;
tokens >> hours;
if (tokens.fail()) {
break;
}
totalHours += hours;
days++;
}
// Erik
//
istringstream tokens(line);
int id;
// 7289
string name;
// "Erik"
tokens >> id >> name;
// rest of tokens are days
double totalHours = 0.0;
int days = 0;
...
}
return 0;
}
29