From 9cd675af6a6dbd228780cb5f0e93bebef54e672d Mon Sep 17 00:00:00 2001 From: pezy_mbp Date: Fri, 5 May 2017 01:26:31 +0800 Subject: [PATCH] improving markdown of ch04 --- ch04/README.md | 96 ++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/ch04/README.md b/ch04/README.md index 6b0e7250..3e01eb84 100644 --- a/ch04/README.md +++ b/ch04/README.md @@ -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: > @@ -13,7 +15,7 @@ > * 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? @@ -21,7 +23,7 @@ Yes, I think it necessary to hold the trade-off. Because the speed is always the For an instance, `cout << i << ++i < Parenthesize the following expression to show how it is evaluated. Test your answer by compiling the expression (without parentheses) and printing its result. > @@ -33,7 +35,7 @@ For an instance, `cout << i << ++i < // print: 91 > ``` -##Exercise 4.5 +## Exercise 4.5 > Determine the result of the following expressions. > @@ -44,7 +46,7 @@ For an instance, `cout << i << ++i < -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. @@ -52,7 +54,7 @@ For an instance, `cout << i << ++i < What does overflow mean? Show three expressions that will overflow. > @@ -62,7 +64,7 @@ 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. @@ -70,11 +72,11 @@ 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`: > @@ -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. @@ -98,7 +100,7 @@ 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. @@ -106,7 +108,7 @@ while(cin >> i && i != 42) 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. @@ -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? > @@ -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: > @@ -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? > @@ -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. > @@ -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. @@ -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`, 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? > @@ -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::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::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. @@ -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? > @@ -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. @@ -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? @@ -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? > @@ -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. > @@ -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: > @@ -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. @@ -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::size_type ix = 0; ix != ivec.size(); ix++, cnt--) +for(vector::size_type ix = 0; ix != ivec.size(); ix++, cnt--) ivec[ix] = cnt; ``` @@ -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. > @@ -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: > @@ -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. @@ -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, > @@ -407,7 +409,7 @@ 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. @@ -415,7 +417,7 @@ dval + ival * cval; // cval converted to int, then that int and ival converted t i *= static_cast(d); ``` -##Exercise 4.37 +## Exercise 4.37 > Rewrite each of the following old-style casts to use a named cast: > @@ -427,7 +429,7 @@ i *= static_cast(d); > pc = (char*)pv; // pc = reinterpret_cast(pv); > ``` -##Exercise 4.38 +## Exercise 4.38 > Explain the following expression: >