Skip to content

Commit bfc4461

Browse files
committed
improving markdown format.
1 parent 49291a5 commit bfc4461

File tree

17 files changed

+1001
-903
lines changed

17 files changed

+1001
-903
lines changed

Diff for: README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
##C++ Primer 5th Answers
1+
# C++ Primer 5th Answers
2+
3+
---
24

35
[![GitHub issues](https://img.shields.io/github/issues/pezy/CppPrimer.svg)](https://github.com/pezy/CppPrimer/issues)
46
[![GitHub license](https://img.shields.io/badge/license-CC0-blue.svg)](https://raw.githubusercontent.com/pezy/Cpp-Primer/master/LICENSE)
57
[![](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)
68
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/pezy/0.99)
9+
[![Donate](https://img.shields.io/badge/Donate-%E6%94%AF%E4%BB%98%E5%AE%9D-blue.svg)](http://devnotes.org/img/alipay.jpg)
710

8-
### Notes
11+
## Notes
912

1013
- Use `GCC 4.9+`, `Clang 3.4+`, `MSVC 14+`, and [others](http://en.cppreference.com/w/cpp/compiler_support).
1114
- Use `-std=c++11`(recommend: `-pedantic -Wall`) flag for compiling.
1215
- Have you discovered incorrect information? [Submit](https://github.com/pezy/CppPrimer/issues/new).
1316

14-
### Contents
17+
## Contents
1518

1619
- [Chapter 1. Getting Started](ch01/README.md)
1720
- Part I: The Basics

Diff for: ch01/README.md

+51-35
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
# Chapter 1. Getting Started
22

3-
##Exercise 1.1
3+
## Exercise 1.1
44

55
> Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the main program from page 2.
66
7-
### Windows
7+
- Windows
88

99
![windows](https://cloud.githubusercontent.com/assets/1147451/8334465/a87e3528-1aca-11e5-877d-c610f087fc40.png)
1010

11-
12-
### Linux
11+
- Linux
1312

1413
![Linux](https://cloud.githubusercontent.com/assets/1147451/8334480/c160e75c-1aca-11e5-92d5-7d0a05fbf493.png)
1514

16-
17-
##Exercise 1.2
15+
## Exercise 1.2
1816

1917
> 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.
2018
21-
###Windows
19+
- Windows
2220

2321
![image](https://cloud.githubusercontent.com/assets/1147451/8335952/72179d5e-1ad3-11e5-84ff-e924816e64a3.png)
2422

25-
###Linux
23+
- Linux
2624

2725
![image](https://cloud.githubusercontent.com/assets/1147451/8335963/8debbc5e-1ad3-11e5-9761-013139d291d8.png)
2826

2927
**255**? why? check [this](http://www.tldp.org/LDP/abs/html/exitcodes.html)
3028

31-
##Exercise 1.3
29+
## Exercise 1.3
30+
3231
> Write a program to print Hello, World on the standard output.
3332
3433
```cpp
@@ -41,7 +40,8 @@ int main()
4140
}
4241
```
4342

44-
##Exercise 1.4
43+
## Exercise 1.4
44+
4545
> Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.
4646
4747
```cpp
@@ -58,7 +58,7 @@ int main()
5858
}
5959
```
6060

61-
##Exercise 1.5
61+
## Exercise 1.5
6262

6363
> We wrote the output in one large statement. Rewrite the program to use a separate statement to print each operand.
6464
@@ -81,12 +81,13 @@ int main()
8181
}
8282
```
8383

84-
##Exercise 1.6
84+
## Exercise 1.6
85+
8586
> Explain whether the following program fragment is legal.
8687
8788
It's illegal.
8889

89-
**[Error] expected primary-expression before '<<' token**
90+
**`[Error] expected primary-expression before '<<' token`**
9091

9192
Fixed it: remove the spare semicolons.
9293

@@ -96,11 +97,12 @@ std::cout << "The sum of " << v1
9697
<< " is " << v1 + v2 << std::endl;
9798
```
9899

99-
##Exercise 1.7
100+
## Exercise 1.7
100101

101102
> Compile a program that has incorrectly nested comments.
102103
103104
Example:
105+
104106
```cpp
105107
/*
106108
* comment pairs /* */ cannot nest.
@@ -117,23 +119,25 @@ Compiled result(g++):
117119

118120
![ex1_7](https://cloud.githubusercontent.com/assets/1147451/8334581/4fb4a408-1acb-11e5-98e3-54c0929198ec.png)
119121

120-
121-
##Exercise 1.8
122+
## Exercise 1.8
122123

123124
> Indicate which, if any, of the following output statements are legal:
125+
124126
```cpp
125127
std::cout << "/*";
126128
std::cout << "*/";
127129
std::cout << /* "*/" */;
128130
std::cout << /* "*/" /* "/*" */;
129131
```
132+
130133
> After you’ve predicted what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.
131134
132135
Compiled result(g++):
133136

134137
![ex1_8](https://cloud.githubusercontent.com/assets/1147451/8334603/6aa321e0-1acb-11e5-988a-57e87a53b141.png)
135138

136139
Corrected? just added a quote:
140+
137141
```cpp
138142
std::cout << "/*";
139143
std::cout << "*/";
@@ -142,16 +146,21 @@ std::cout << /* "*/" /* "/*" */;
142146
```
143147

144148
Output:
149+
145150
```sh
146151
/**/ */ /*
147152
```
148153

149-
##[Exercise 1.9](ex1_09.cpp)
150-
##[Exercise 1.10](ex1_10.cpp)
151-
##[Exercise 1.11](ex1_11.cpp)
154+
## [Exercise 1.9](ex1_09.cpp)
155+
156+
## [Exercise 1.10](ex1_10.cpp)
157+
158+
## [Exercise 1.11](ex1_11.cpp)
159+
160+
## Exercise 1.12
152161

153-
##Exercise 1.12
154162
> What does the following for loop do? What is the final value of sum?
163+
155164
```cpp
156165
int sum = 0;
157166
for (int i = -100; i <= 100; ++i)
@@ -160,10 +169,12 @@ for (int i = -100; i <= 100; ++i)
160169

161170
the loop sums the numbers from -100 to 100. the final value of sum is zero.
162171

163-
##Exercise 1.13
172+
## Exercise 1.13
173+
164174
> Rewrite the exercises from 1.4.1 (p. 13) using for loops.
165175
166176
**Ex1.9**:
177+
167178
```cpp
168179
#include <iostream>
169180

@@ -180,6 +191,7 @@ int main()
180191
```
181192

182193
**Ex1.10**:
194+
183195
```cpp
184196
#include <iostream>
185197

@@ -194,6 +206,7 @@ int main()
194206
```
195207

196208
**Ex1.11**:
209+
197210
```cpp
198211
#include <iostream>
199212

@@ -217,7 +230,8 @@ int main()
217230
}
218231
```
219232

220-
##Exercise 1.14
233+
## Exercise 1.14
234+
221235
> Compare and contrast the loops that used a `for` with those using a `while`. Are there advantages or disadvantages to using either form?
222236
223237
- Advantage of `for` and disadvantage of `while`:
@@ -231,12 +245,13 @@ int main()
231245

232246
[A similar question on Stack Overflow](http://stackoverflow.com/questions/2950931/for-vs-while-in-c-programming)
233247

234-
##Exercise 1.15
248+
## Exercise 1.15
249+
235250
> Write programs that contain the common errors discussed in the box on page 16. Familiarize yourself with the messages the compiler generates.
236251
237252
Self-training.
238253

239-
##Exercise 1.16
254+
## Exercise 1.16
240255

241256
> Write your own version of a program that prints the sum of a set of integers read from `cin`.
242257
@@ -253,44 +268,43 @@ int main()
253268
}
254269
```
255270

256-
##Exercise 1.17
271+
## Exercise 1.17
257272

258273
> What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values?
259274
260275
If all equal, the count will be printed out. If there are no duplicated values, A new line will be printed when `Enter` clicked.
261276

262-
##Exercise 1.18
277+
## Exercise 1.18
263278

264279
> 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.
265280
266281
![ex1_18](https://cloud.githubusercontent.com/assets/1147451/8335404/0861c478-1ad0-11e5-8083-c05a0cd9e758.png)
267282

268-
269-
##Exercise 1.19
283+
## Exercise 1.19
270284

271285
> 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.
272286
273287
check [ex1_11.cpp](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_11.cpp)
274288

275-
##Exercise 1.20
289+
## Exercise 1.20
276290

277-
> 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.
291+
> <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.
278292
279293
check [code](ex1_20.cpp).
280294

281295
Test it using the `data`/`book.txt`:
282296

283297
![ex1_20](https://cloud.githubusercontent.com/assets/1147451/8335638/8f5c2bca-1ad1-11e5-9c51-288382710df2.png)
284298

299+
## Exercise 1.21
285300

286-
##Exercise 1.21
287301
> Write a program that reads two `Sales_item` objects that have the same ISBN and produces their sum.
288302
289303
The program should check whether the objects have the same ISBN.(check 1.5.2)
290304

291305
[Code](ex1_21.cpp)
292306

293-
##Exercise 1.22
307+
## Exercise 1.22
294308

295309
> Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read.
296310
@@ -300,23 +314,25 @@ Tip: this program will appear in the section 1.6.
300314

301315
![ex1_22](https://cloud.githubusercontent.com/assets/1147451/8335700/d85ee22c-1ad1-11e5-9612-1155145606c1.png)
302316

303-
##Exercise 1.23
317+
## Exercise 1.23
318+
304319
> Write a program that reads several transactions and counts
305320
how many transactions occur for each ISBN.
306321

307322
Tip: please review the `1.4.4`.
308323

309324
[Code](ex1_23.cpp).
310325

311-
##Exercise 1.24
326+
## Exercise 1.24
327+
312328
> Test the previous program by giving multiple transactions representing multiple ISBNs. The records for each ISBN should be grouped together.
313329
314330
`data`/`book.txt` may be used as the records.
315331

316332
![ex1_24](https://cloud.githubusercontent.com/assets/1147451/8335734/0fbefbbc-1ad2-11e5-9df3-fa1203dffb42.png)
317333

334+
## Exercise 1.25
318335

319-
##Exercise 1.25
320336
> Using the `Sales_item.h` header from the Web site, compile and execute the bookstore program presented in this section.
321337
322338
![ex1_25](https://cloud.githubusercontent.com/assets/1147451/8335742/1efb475c-1ad2-11e5-9484-69ae44b79385.png)

0 commit comments

Comments
 (0)