Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pezy/CppPrimer
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed May 13, 2017
2 parents 01514a5 + afacac5 commit 9305843
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 101 deletions.
4 changes: 2 additions & 2 deletions ch01/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ the loop sums the numbers from -100 to 100. the final value of sum is zero.
int main()
{
int sum = 0;
for (int i=50; i<=100; ++i)
for (int i = 50; i <= 100; ++i)
sum += i;

std::cout << "the sum is: " << sum << std::endl;
Expand All @@ -185,7 +185,7 @@ int main()

int main()
{
for (int i=10; i>=0; --i)
for (int i = 10; i >= 0; --i)
std::cout << i << " ";
std::cout << std::endl;

Expand Down
96 changes: 49 additions & 47 deletions ch04/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
##Exercise 4.1
# Chapter 4. Expressions

## Exercise 4.1

> What is the value returned by 5 + 10 * 20/2?
105

##Exercise 4.2
## Exercise 4.2

> Using Table 4.12 (p. 166), parenthesize the following expressions to indicate the order in which the operands are grouped:
>
Expand All @@ -13,15 +15,15 @@
> * vec.begin() + 1 //=> (*(vec.begin())) + 1
> ```
##Exercise 4.3
## Exercise 4.3
> Order of evaluation for most of the binary operators is left undefined to give the compiler opportunities for optimization. This strategy presents a trade-off between efficient code generation and potential pitfalls in the use of the language by the programmer. Do you consider that an acceptable trade-off? Why or why not?
Yes, I think it necessary to hold the trade-off. Because the speed is always the biggest advantage of C++. Sometimes, we need the compiler's features for efficient work. But if you are not a expert. I have to advice you do not touch the undefined behaviors.
For an instance, `cout << i << ++i <<endl` should never appear in your code.
##Exercise 4.4
## Exercise 4.4
> Parenthesize the following expression to show how it is evaluated. Test your answer by compiling the expression (without parentheses) and printing its result.
>
Expand All @@ -33,7 +35,7 @@ For an instance, `cout << i << ++i <<endl` should never appear in your code.
> // print: 91
> ```
##Exercise 4.5
## Exercise 4.5
> Determine the result of the following expressions.
>
Expand All @@ -44,15 +46,15 @@ For an instance, `cout << i << ++i <<endl` should never appear in your code.
> -30 / 3 * 21 % 4 // -10*21%4 = -210%4 = -2
> ```
##Exercise 4.6
## Exercise 4.6
> Write an expression to determine whether an int value is even or odd.
```cpp
i % 2 == 0 ? "even" : "odd"
```
##Exercise 4.7
## Exercise 4.7
> What does overflow mean? Show three expressions that will overflow.
>
Expand All @@ -62,19 +64,19 @@ i % 2 == 0 ? "even" : "odd"
> unsigned short usvalue = 65535; ++usvalue; // 0
> ```
##Exercise 4.8
## Exercise 4.8
> Explain when operands are evaluated in the logical `AND`, logical `OR`, and equality operators.
from the book:
> The logical `AND` and `OR` operators always evaluate their left operand before the right. Moreover, the right operand is evaluated if and only if the left operand does not determine the result. This strategy is known as **short-circuit evaluation**.
- logical `AND` : the second operand is evaluated if and only if the left side is `true`.
- logical `OR` : the second operand is evaluated if and only if the left side is `false`
- equality operators : `true` only if both operands have the same value, otherwise, it returns `false`.
- logical `AND` : the second operand is evaluated if and only if the left side is `true`.
- logical `OR` : the second operand is evaluated if and only if the left side is `false`
- equality operators : `true` only if both operands have the same value, otherwise, it returns `false`.
##Exercise 4.9
## Exercise 4.9
> Explain the behavior of the condition in the following `if`:
>
Expand All @@ -89,7 +91,7 @@ cp is a pointer to `const char *`, and it's not a `nullptr`. true.
`true && true => true.`
##Exercise 4.10
## Exercise 4.10
> Write the condition for a while loop that would read ints from the standard input and stop when the value read is equal to 42.
Expand All @@ -98,15 +100,15 @@ int i = 0;
while(cin >> i && i != 42)
```
##Exercise 4.11
## Exercise 4.11

> Write an expression that tests four values, a, b, c, and d, and ensures that a is greater than b, which is greater than c, which is greater than d.
```cpp
a > b && b > c && c > d
```

##Exercise 4.12
## Exercise 4.12

> Assuming `i`, `j`, and `k` are all ints, explain what `i != j < k` means.
Expand All @@ -120,11 +122,11 @@ i != j && j < k

---

**reference**
reference:

> It is usually a bad idea to use the boolean literals `true` and `false` as operands in a comparison. These literals should be used only to compare to an object of type `bool`.
##Exercise 4.13
## Exercise 4.13

> What are the values of i and d after each assignment?
>
Expand All @@ -134,7 +136,7 @@ i != j && j < k
> i = d = 3.5; // d = 3.5, i = 3
> ```
##Exercise 4.14
## Exercise 4.14
> Explain what happens in each of the if tests:
>
Expand All @@ -143,7 +145,7 @@ i != j && j < k
> if (i = 42) // true.
> ```
##Exercise 4.15
## Exercise 4.15
> The following assignment is illegal. Why? How would you correct it?
>
Expand All @@ -157,7 +159,7 @@ i != j && j < k
> pi = 0;
> ```
##Exercise 4.16
## Exercise 4.16
> Although the following are legal, they probably do not behave as the programmer expects. Why? Rewrite the expressions as you think they should be.
>
Expand All @@ -170,7 +172,7 @@ i != j && j < k
> if (i == 1024)
> ```
##Exercise 4.17
## Exercise 4.17
> Explain the difference between prefix and postfix increment.
Expand All @@ -180,13 +182,13 @@ The prefix operators return the object itself as an **lvalue**.
The postfix operators return a copy of the object's original value as an **rvalue**.
##Exercise 4.18
## Exercise 4.18
> What would happen if the while loop on page 148 that prints the elements from a vector used the prefix increment operator?
It will print from the second element and dereference `v.end()` at last.(It's undefined and very dangerous)
##Exercise 4.19
## Exercise 4.19
> Given that `ptr` points to an `int`, that `vec` is a `vector<int>`, and that `ival` is an `int`, explain the behavior of each of these expressions. Which, if any, are likely to be incorrect? Why? How might each be corrected?
>
Expand All @@ -200,9 +202,9 @@ It will print from the second element and dereference `v.end()` at last.(It's un
Every value computation and side effect of the first (left) argument of the built-in logical AND operator && and the built-in logical OR operator || is sequenced before every value computation and side effect of the second (right) argument. see [order of evaluation](http://en.cppreference.com/w/cpp/language/eval_order).
##Exercise 4.20
## Exercise 4.20
> Assuming that iter is a vector<string>::iterator, indicate which, if any, of the following expressions are legal. Explain the behavior of the legal expressions and why those that aren’t legal are in error.
> Assuming that iter is a `vector<string>::iterator`, indicate which, if any, of the following expressions are legal. Explain the behavior of the legal expressions and why those that aren’t legal are in error.
>
> ```cpp
> *iter++; // return *iter, then ++iter.
Expand All @@ -213,11 +215,11 @@ Every value computation and side effect of the first (left) argument of the buil
> iter++->empty(); // return iter->empty(), then ++iter.
> ```
##[Exercise 4.21](ex4_21.cpp)
## [Exercise 4.21](ex4_21.cpp)
##[Exercise 4.22](ex4_22.cpp)
## [Exercise 4.22](ex4_22.cpp)
##Exercise 4.23
## Exercise 4.23
> The following expression fails to compile due to operator precedence. Using Table 4.12 (p. 166), explain why it fails. How would you fix it?
>
Expand All @@ -234,7 +236,7 @@ Operator Precedence: `?:` < `+`
string pl = s + (s[s.size() - 1] == 's' ? "" : "s") ;
```
##Exercise 4.24
## Exercise 4.24
> Our program that distinguished between high pass, pass, and fail depended on the fact that the conditional operator is right associative. Describe how that operator would be evaluated if the operator were left associative.
Expand All @@ -252,7 +254,7 @@ finalgrade = ((grade > 90) ? "high pass" : (grade < 60)) ? "fail" : "pass";
if `grade > 90`, first conditional operator's result is `high pass`. so the finalgrade is always fail. It's contradictory obviously.
##Exercise 4.25
## Exercise 4.25
> What is the value of ~'q' \<< 6 on a machine with 32-bit ints and 8 bit chars, that uses Latin-1 character set in which 'q' has the bit pattern 01110001?
Expand All @@ -262,13 +264,13 @@ The bitwise NOT operator (`~`) yields us the Ones' Complement of `0000 0000 0000
We then shift the bits `6` digits to the left, which yields us `1111 1111 1111 1111 1110 0011 1000 0000`. Overflowing bits were discarded. The decimal representation of the binary sequence is `-7296`.
##Exercise 4.26
## Exercise 4.26
> In our grading example in this section, what would happen if we used unsigned int as the type for quiz1?
There are 30 students in the example of this section. `unsigned long` have at least 32 bits on any machine. Thus, it is ensured that the bits start out with well-define values. The standard define that `unsigned int` is guaranteed to hold 16 bits. If `unsigned int` adopted, the result is undefined.
##Exercise 4.27
## Exercise 4.27
> What is the result of each of these expressions?
>
Expand All @@ -280,9 +282,9 @@ There are 30 students in the example of this section. `unsigned long` have at le
> ul1 || ul2 // == ture
> ```
##[Exercise 4.28](ex4_28.cpp)
## [Exercise 4.28](ex4_28.cpp)
##Exercise 4.29
## Exercise 4.29
> Predict the output of the following code and explain your reasoning. Now run the program. Is the output what you expected? If not, figure out why.
>
Expand All @@ -298,7 +300,7 @@ The first result is 10. It returns the number of elements in x. But the second r
reference: [Why the size of a pointer is 4bytes in C++](http://stackoverflow.com/a/2428809)
##Exercise 4.30
## Exercise 4.30
> Using Table 4.12 (p. 166), parenthesize the following expressions to match the default evaluation:
>
Expand All @@ -313,7 +315,7 @@ reference: [Why the size of a pointer is 4bytes in C++](http://stackoverflow.com
reference: [sizeof operator](http://en.cppreference.com/w/cpp/language/sizeof)
##Exercise 4.31
## Exercise 4.31
> The program in this section used the prefix increment and decrement operators. Explain why we used prefix and not postfix. What changes would have to be made to use the postfix versions? Rewrite the program using postfix operators.
Expand All @@ -328,7 +330,7 @@ We use prefix and not postfix, just because of the `Advice: Use Postfix Operator
So, it's just a good habits. And there are no changes if we have to be made to use the postfix versions. Rewrite:
```cpp
for(vector<int>::size_type ix = 0; ix != ivec.size(); ix++, cnt--)
for(vector<int>::size_type ix = 0; ix != ivec.size(); ix++, cnt--)
ivec[ix] = cnt;
```
Expand All @@ -338,7 +340,7 @@ This is not an appropriate example to discuss the difference of prefix and postf
reference: [Usage of the Built-in Comma Operator](http://stackoverflow.com/questions/22591387/usage-of-the-built-in-comma-operator)
##Exercise 4.32
## Exercise 4.32
> Explain the following loop.
>
Expand All @@ -352,7 +354,7 @@ reference: [Usage of the Built-in Comma Operator](http://stackoverflow.com/quest
`ptr` and `ix` have the same function. The former use a pointer, and the latter use the index of array. we use the loop to through the array.(just choose one from `ptr` and `ix`\)
##Exercise 4.33
## Exercise 4.33
> Using Table 4.12 (p. 166) explain what the following expression does:
>
Expand All @@ -374,13 +376,13 @@ someValue ? (++x, y) : (--x, --y);
Even though the result has nothing to do with `x`, the evaluation of `someValue` does effect the operation on `x`.
##Exercise 4.34
## Exercise 4.34
> Given the variable definitions in this section, explain what conversions take place in the following expressions:
>
> - (a) if (fval)
> - (b) dval = fval + ival;
> - (c) dval + ival * cval;
> (a) if (fval)
> (b) dval = fval + ival;
> (c) dval + ival * cval;
Remember that you may need to consider the associativity of the operators.
Expand All @@ -390,7 +392,7 @@ dval = fval + ival; // ival converted to fval, then the result of fval add ival
dval + ival * cval; // cval converted to int, then that int and ival converted to double.
```
##Exercise 4.35
## Exercise 4.35
> Given the following definitions,
>
Expand All @@ -407,15 +409,15 @@ dval + ival * cval; // cval converted to int, then that int and ival converted t
> cval = ival + fval + dval; // ival converted to float, then that float and fval converted to double. At last, that double converted to char(by truncation).
> ```
##Exercise 4.36
## Exercise 4.36
> Assuming i is an int and d is a double write the expression `i *= d` so that it does integral, rather than floating-point, multiplication.
```cpp
i *= static_cast<int>(d);
```
##Exercise 4.37
## Exercise 4.37
> Rewrite each of the following old-style casts to use a named cast:
>
Expand All @@ -427,7 +429,7 @@ i *= static_cast<int>(d);
> pc = (char*)pv; // pc = reinterpret_cast<char*>(pv);
> ```
##Exercise 4.38
## Exercise 4.38
> Explain the following expression:
>
Expand Down
Loading

0 comments on commit 9305843

Please sign in to comment.