Skip to content

Commit

Permalink
improving markdown format.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed May 16, 2017
1 parent 49291a5 commit bfc4461
Show file tree
Hide file tree
Showing 17 changed files with 1,001 additions and 903 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
##C++ Primer 5th Answers
# C++ Primer 5th Answers

---

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

### Notes
## Notes

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

### Contents
## Contents

- [Chapter 1. Getting Started](ch01/README.md)
- Part I: The Basics
Expand Down
86 changes: 51 additions & 35 deletions ch01/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
# Chapter 1. Getting Started

##Exercise 1.1
## Exercise 1.1

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

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


### Linux
- Linux

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


##Exercise 1.2
## Exercise 1.2

> 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.
###Windows
- Windows

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

###Linux
- Linux

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

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

##Exercise 1.3
## Exercise 1.3

> Write a program to print Hello, World on the standard output.
```cpp
Expand All @@ -41,7 +40,8 @@ int main()
}
```

##Exercise 1.4
## Exercise 1.4

> Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.
```cpp
Expand All @@ -58,7 +58,7 @@ int main()
}
```

##Exercise 1.5
## Exercise 1.5

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

##Exercise 1.6
## Exercise 1.6

> Explain whether the following program fragment is legal.
It's illegal.

**[Error] expected primary-expression before '<<' token**
**`[Error] expected primary-expression before '<<' token`**

Fixed it: remove the spare semicolons.

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

##Exercise 1.7
## Exercise 1.7

> Compile a program that has incorrectly nested comments.
Example:

```cpp
/*
* comment pairs /* */ cannot nest.
Expand All @@ -117,23 +119,25 @@ Compiled result(g++):

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


##Exercise 1.8
## Exercise 1.8

> Indicate which, if any, of the following output statements are legal:
```cpp
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;
```

> After you’ve predicted what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.
Compiled result(g++):

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

Corrected? just added a quote:

```cpp
std::cout << "/*";
std::cout << "*/";
Expand All @@ -142,16 +146,21 @@ std::cout << /* "*/" /* "/*" */;
```

Output:

```sh
/**/ */ /*
```

##[Exercise 1.9](ex1_09.cpp)
##[Exercise 1.10](ex1_10.cpp)
##[Exercise 1.11](ex1_11.cpp)
## [Exercise 1.9](ex1_09.cpp)

## [Exercise 1.10](ex1_10.cpp)

## [Exercise 1.11](ex1_11.cpp)

## Exercise 1.12

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

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

##Exercise 1.13
## Exercise 1.13

> Rewrite the exercises from 1.4.1 (p. 13) using for loops.
**Ex1.9**:

```cpp
#include <iostream>

Expand All @@ -180,6 +191,7 @@ int main()
```

**Ex1.10**:

```cpp
#include <iostream>

Expand All @@ -194,6 +206,7 @@ int main()
```

**Ex1.11**:

```cpp
#include <iostream>

Expand All @@ -217,7 +230,8 @@ int main()
}
```

##Exercise 1.14
## Exercise 1.14

> Compare and contrast the loops that used a `for` with those using a `while`. Are there advantages or disadvantages to using either form?
- Advantage of `for` and disadvantage of `while`:
Expand All @@ -231,12 +245,13 @@ int main()

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

##Exercise 1.15
## Exercise 1.15

> Write programs that contain the common errors discussed in the box on page 16. Familiarize yourself with the messages the compiler generates.
Self-training.

##Exercise 1.16
## Exercise 1.16

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

##Exercise 1.17
## Exercise 1.17

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

##Exercise 1.18
## Exercise 1.18

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


##Exercise 1.19
## Exercise 1.19

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

##Exercise 1.20
## Exercise 1.20

> 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.
> <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.
check [code](ex1_20.cpp).

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

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

## Exercise 1.21

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

[Code](ex1_21.cpp)

##Exercise 1.22
## Exercise 1.22

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

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

##Exercise 1.23
## Exercise 1.23

> Write a program that reads several transactions and counts
how many transactions occur for each ISBN.

Tip: please review the `1.4.4`.

[Code](ex1_23.cpp).

##Exercise 1.24
## Exercise 1.24

> Test the previous program by giving multiple transactions representing multiple ISBNs. The records for each ISBN should be grouped together.
`data`/`book.txt` may be used as the records.

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

## Exercise 1.25

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

0 comments on commit bfc4461

Please sign in to comment.