Skip to content

Commit aec2b0b

Browse files
committed
review ch01-05
1 parent 5e5e5fc commit aec2b0b

24 files changed

+98
-140
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
##C++ Primer 5th Answers
22

3-
[![GitHub issues](https://img.shields.io/github/issues/Mooophy/Cpp-Primer.svg)](https://github.com/pezy/CppPrimer/issues)
3+
[![GitHub issues](https://img.shields.io/github/issues/pezy/CppPrimer.svg)](https://github.com/pezy/CppPrimer/issues)
44
[![GitHub license](https://img.shields.io/badge/license-CC0-blue.svg)](https://raw.githubusercontent.com/pezy/Cpp-Primer/master/LICENSE)
55
[![](https://img.shields.io/badge/%E4%B8%AD%E6%96%87-%E8%AE%A8%E8%AE%BA%E5%8C%BA-yellowgreen.svg)](https://github.com/ReadingLab/Discussion-for-Cpp)
66

77
### Notes
88

9-
- **g++** and **clang++** are recommended. Visual Studio 2013 may be used as well, but up to June 2015, Visual Studio has not implemented all c++11 features yet.
9+
- Use `GCC 4.9+`, `Clang 3.4+`, `MSVC 14+`, and [others](http://en.cppreference.com/w/cpp/compiler_support).
1010
- Use `-std=c++11`(recommend: `-pedantic -Wall`) flag for compiling.
11-
- If any bug caught, please [let me know](https://github.com/pezy/Cpp-Primer/issues/new), thanks.
11+
- Have you discovered incorrect information? [Submit](https://github.com/pezy/CppPrimer/issues/new).
1212

1313
### Contents
1414

ch01/README.md

+67-86
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1+
# Chapter 1. Getting Started
2+
13
##Exercise 1.1
24

35
> Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the main program from page 2.
46
57
### Windows
68

7-
![windows](https://db.tt/XGeGsg7O)
9+
![windows](https://cloud.githubusercontent.com/assets/1147451/8334465/a87e3528-1aca-11e5-877d-c610f087fc40.png)
10+
811

912
### Linux
1013

11-
![Linux](https://db.tt/2xKWuztU)
14+
![Linux](https://cloud.githubusercontent.com/assets/1147451/8334480/c160e75c-1aca-11e5-92d5-7d0a05fbf493.png)
15+
1216

1317
##Exercise 1.2
1418

1519
> Exercise 1.2: Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main.
1620
1721
###Windows
1822

19-
![windows](https://db.tt/DIJd9eZb)
23+
![image](https://cloud.githubusercontent.com/assets/1147451/8335952/72179d5e-1ad3-11e5-84ff-e924816e64a3.png)
2024

2125
###Linux
2226

23-
![linux](https://db.tt/lhzXhpCt)
27+
![image](https://cloud.githubusercontent.com/assets/1147451/8335963/8debbc5e-1ad3-11e5-9761-013139d291d8.png)
2428

25-
**255**? why? please look at [this](http://www.tldp.org/LDP/abs/html/exitcodes.html)
29+
**255**? why? check [this](http://www.tldp.org/LDP/abs/html/exitcodes.html)
2630

2731
##Exercise 1.3
2832
> Write a program to print Hello, World on the standard output.
@@ -32,8 +36,8 @@
3236

3337
int main()
3438
{
35-
std::cout << "Hello, World" << std::endl;
36-
return 0;
39+
std::cout << "Hello, World" << std::endl;
40+
return 0;
3741
}
3842
```
3943

@@ -111,7 +115,8 @@ int main()
111115

112116
Compiled result(g++):
113117

114-
![result](https://db.tt/CqQKu8GQ)
118+
![ex1_7](https://cloud.githubusercontent.com/assets/1147451/8334581/4fb4a408-1acb-11e5-98e3-54c0929198ec.png)
119+
115120

116121
##Exercise 1.8
117122

@@ -122,12 +127,11 @@ std::cout << "*/";
122127
std::cout << /* "*/" */;
123128
std::cout << /* "*/" /* "/*" */;
124129
```
125-
> After you’ve predicted what will happen, test your answers by compiling a
126-
program with each of these statements. Correct any errors you encounter.
130+
> After you’ve predicted what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.
127131
128132
Compiled result(g++):
129133

130-
![result](https://db.tt/mrL9hDCS)
134+
![ex1_8](https://cloud.githubusercontent.com/assets/1147451/8334603/6aa321e0-1acb-11e5-988a-57e87a53b141.png)
131135

132136
Corrected? just added a quote:
133137
```cpp
@@ -142,13 +146,12 @@ Output:
142146
/**/ */ /*
143147
```
144148

145-
##[Exercise 1.9](ex1_9.cpp)
149+
##[Exercise 1.9](ex1_09.cpp)
146150
##[Exercise 1.10](ex1_10.cpp)
147151
##[Exercise 1.11](ex1_11.cpp)
148152

149153
##Exercise 1.12
150-
> What does the following for loop do? What is the final value
151-
of sum?
154+
> What does the following for loop do? What is the final value of sum?
152155
```cpp
153156
int sum = 0;
154157
for (int i = -100; i <= 100; ++i)
@@ -158,9 +161,9 @@ for (int i = -100; i <= 100; ++i)
158161
the loop sums the numbers from -100 to 100. the final value of sum is zero.
159162

160163
##Exercise 1.13
161-
> Rewrite the exercises from § 1.4.1 (p. 13) using for loops.
164+
> Rewrite the exercises from 1.4.1 (p. 13) using for loops.
162165
163-
Ex1.9:
166+
**Ex1.9**:
164167
```cpp
165168
#include <iostream>
166169

@@ -176,166 +179,144 @@ int main()
176179
}
177180
```
178181

179-
Ex1.10:
182+
**Ex1.10**:
180183
```cpp
181184
#include <iostream>
182185

183186
int main()
184187
{
185188
for (int i=10; i>=0; --i)
186-
std::cout << i << std::endl;
189+
std::cout << i << " ";
190+
std::cout << std::endl;
187191

188192
return 0;
189193
}
190194
```
191195

192-
Ex1.11:
196+
**Ex1.11**:
193197
```cpp
194198
#include <iostream>
195199

196200
int main()
197201
{
198-
int val_small = 0, val_big = 0;
202+
int small = 0, big = 0;
199203
std::cout << "please input two integers:";
200-
std::cin >> val_small >> val_big;
204+
std::cin >> small >> big;
201205

202-
if (val_small > val_big) {
203-
int tmp = val_small;
204-
val_small = val_big;
205-
val_big = tmp;
206+
if (small > big) {
207+
int tmp = small;
208+
small = big;
209+
big = tmp;
206210
}
207211

208-
for (int i = val_small; i <= val_big; ++i)
209-
std::cout << i << std::endl;
212+
for (int i = small; i <= big; ++i)
213+
std::cout << i << " ";
214+
std::cout << std::endl;
210215

211216
return 0;
212217
}
213218
```
214219

215220
##Exercise 1.14
216-
> Compare and contrast the loops that used a for with those
217-
using a while. Are there advantages or disadvantages to using either form?
221+
> Compare and contrast the loops that used a `for` with those using a `while`. Are there advantages or disadvantages to using either form?
222+
223+
- Advantage of `for` and disadvantage of `while`:
224+
- Locality, the variable in the scope of the loop.
225+
- Pattern happens so often: using a variable in a condition and incrementing that variable in the body.
226+
- Advantage of `while` and disadvantage of `for`:
227+
- Clear when there is only one static condition.
228+
- Readable when the global variables incremented in the body.
218229

219-
If you need a pattern which is using a variable in a condition and incrementing that variable in the
220-
body. You should use `for` loop. Else the `while` loop is more simple.
230+
-----
221231

222-
Want to know more? look at [this](http://stackoverflow.com/questions/1600282/guideline-while-vs-for)
232+
[A similar question on Stack Overflow](http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming)
223233

224234
##Exercise 1.15
225-
> Write programs that contain the common errors discussed in
226-
the box on page 16. Familiarize yourself with the messages the compiler
227-
generates.
235+
> Write programs that contain the common errors discussed in the box on page 16. Familiarize yourself with the messages the compiler generates.
228236
229-
**JUST READ IT!**
237+
Self-training.
230238

231239
##Exercise 1.16
232240

233-
> Write your own version of a program that prints the sum of a set of integers read from cin.
234-
235-
Many people confused about this exercise, such as [this](http://www.cplusplus.com/forum/beginner/104169/) and [this](http://stackoverflow.com/questions/17841424/how-to-write-this-while-loop-as-a-for-loop).
236-
237-
In my opinion, the exercise aim to write the program without "**END-OF-FILE**".
238-
239-
**BUT**, the [code](http://www.cplusplus.com/forum/beginner/104169/#msg561450) in first link is not correct.
240-
241-
The following are my own version:
241+
> Write your own version of a program that prints the sum of a set of integers read from `cin`.
242242
243243
```cpp
244244
#include <iostream>
245245

246246
int main()
247247
{
248-
int limit = 0, sum = 0, value = 0;
249-
std::cout << "How many integers would you like to enter?";
250-
std::cin >> limit;
251-
252-
// assume we don't know what is EOF(End-Of-File).
253-
while (std::cin >> value && (--limit != 0))
248+
int sum = 0;
249+
for (int value = 0; std::cin >> value; )
254250
sum += value;
255-
256-
std::cout << sum + value << std::endl;
257-
251+
std::cout << sum << std::endl;
258252
return 0;
259253
}
260254
```
261255

262-
Watch out for "sum + value" in the `cout` line.
263-
264256
##Exercise 1.17
265257

266258
> What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values?
267259
268-
If the input values are all equal, it will print a line which shows the count of the number you input.
269-
270-
If there are no duplicated values, when different values input, a new line will be printed if you click `Enter`.
260+
If all equal, the count will be printed out. If there are no duplicated values, A new line will be printed when `Enter` clicked.
271261

272262
##Exercise 1.18
273263

274264
> Compile and run the program from this section giving it only equal values as input. Run it again giving it values in which no number is repeated.
275265
276-
![run](https://db.tt/F38zExnq)
266+
![ex1_18](https://cloud.githubusercontent.com/assets/1147451/8335404/0861c478-1ad0-11e5-8083-c05a0cd9e758.png)
267+
277268

278269
##Exercise 1.19
279270

280271
> Revise the program you wrote for the exercises in § 1.4.1 (p. 13) that printed a range of numbers so that it handles input in which the first number is smaller than the second.
281272
282-
Yes, we should use `if` to judge which is bigger.
283-
284-
review this [code](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_11.cpp)
273+
check [ex1_11.cpp](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_11.cpp)
285274

286275
##Exercise 1.20
287276

288277
> http://www.informit.com/title/032174113 contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.
289278
290-
[Here](ex1_20.cpp) is the code.
279+
check [code](ex1_20.cpp).
291280

292-
**You need to enable C++11 support in your compiler.
293-
With GCC and Clang, this can be done with the `-std=c++11` option.**
281+
Test it using the `data`/`book.txt`:
294282

295-
**(Never say it again.)**
283+
![ex1_20](https://cloud.githubusercontent.com/assets/1147451/8335638/8f5c2bca-1ad1-11e5-9c51-288382710df2.png)
296284

297-
How to test it? use the `book.txt` in `data` folder. And do it like this:
298-
299-
![run](https://db.tt/fm8iHtkF)
300285

301286
##Exercise 1.21
302-
> Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.
287+
> Write a program that reads two `Sales_item` objects that have the same ISBN and produces their sum.
303288
304-
The program should check whether the objects have the same ISBN.(Have a look at 1.5.2, surprise!)
289+
The program should check whether the objects have the same ISBN.(check 1.5.2)
305290

306291
[Code](ex1_21.cpp)
307292

308293
##Exercise 1.22
309294

310295
> Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read.
311296
312-
Tips: this program will appear in the section 1.6.
297+
Tip: this program will appear in the section 1.6.
313298

314-
[Here](ex1_22.cpp) is the code.
299+
[Code](ex1_22.cpp).
315300

316-
![run](https://db.tt/UlkuvpAS)
301+
![ex1_22](https://cloud.githubusercontent.com/assets/1147451/8335700/d85ee22c-1ad1-11e5-9612-1155145606c1.png)
317302

318303
##Exercise 1.23
319304
> Write a program that reads several transactions and counts
320305
how many transactions occur for each ISBN.
321306

322307
Tip: please review the `1.4.4`.
323308

324-
[Here](ex1_23.cpp) is the code.
309+
[Code](ex1_23.cpp).
325310

326311
##Exercise 1.24
327-
> Test the previous program by giving multiple transactions
328-
representing multiple ISBNs. The records for each ISBN should be grouped
329-
together.
312+
> Test the previous program by giving multiple transactions representing multiple ISBNs. The records for each ISBN should be grouped together.
330313
331-
You can use data/book.txt as the records.
314+
Use `data`/`book.txt` as the records.
332315

333-
![run](https://db.tt/EeDI7lvN)
316+
![ex1_24](https://cloud.githubusercontent.com/assets/1147451/8335734/0fbefbbc-1ad2-11e5-9df3-fa1203dffb42.png)
334317

335-
##Exercise 1.25
336-
> Using the Sales_item.h header from the Web site,
337-
compile and execute the bookstore program presented in this section.
338318

339-
It is the same as Exercise 1.22.
319+
##Exercise 1.25
320+
> Using the `Sales_item.h` header from the Web site, compile and execute the bookstore program presented in this section.
340321
341-
![run](https://db.tt/C6OOPuzA)
322+
![ex1_25](https://cloud.githubusercontent.com/assets/1147451/8335742/1efb475c-1ad2-11e5-9484-69ae44b79385.png)
File renamed without changes.

ch01/ex1_1.cpp

-4
This file was deleted.

ch01/ex1_11.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44

55
int main()
66
{
7-
int val_small = 0, val_big = 0;
7+
int small = 0, big = 0;
88
std::cout << "please input two integers:";
9-
std::cin >> val_small >> val_big;
9+
std::cin >> small >> big;
1010

11-
if (val_small > val_big) {
12-
int tmp = val_small;
13-
val_small = val_big;
14-
val_big = tmp;
11+
if (small > big) {
12+
int tmp = small;
13+
small = big;
14+
big = tmp;
1515
}
1616

17-
while (val_small <= val_big) {
18-
std::cout << val_small << std::endl;
19-
++val_small;
17+
while (small <= big) {
18+
std::cout << small << " ";
19+
++small;
2020
}
21+
std::cout << std::endl;
2122

2223
return 0;
2324
}

ch01/ex1_16.cpp

-16
This file was deleted.

0 commit comments

Comments
 (0)