Chapter 4 after-school exercise

4.1、105

4.2 (a). * Vec.begin () == * (vec.begin ()); (B) vec.begin * () + 1 == * (vec.begin () + 1)

4.3, I think it is acceptable (personal view is not a standard answer for reference only); this defect will only order of evaluation of the final results of the expression of an impact they occur, and if the order of evaluation if the impact on the final result so avoid this defect itself is the responsibility of the programmer, the compiler does not matter what strategy is the use of

4.4、(((12/3)*4)+(5*15)) + ((24%4)/2) == 91;

//p4_4.cpp

#include <iostream>
using namespace std;

int main ()
{
    int a;
    a = 12/3*4+5*15+24%4/2;
    cout << "a = " << a << endl;

    return 0;
}
 
4.5
a -86;  b -18;  c 0  d -2
 
4.6、 bool is_even = (inum%2 == 0) ?  true:false;
4.7, expression evaluation result obtained exceeds the maximum or minimum value can be stored in a corresponding type
 
4.8, or both logical and logical left to right; and: if and only if the left-evaluate expression result can not determine the right fishes expression evaluated
The order of evaluation equality operator object is not provided
 
4.9 , cp is determined to be a non-null pointer again points null value determination points
 
4.10 while(cin >> num != 42)
 
4.11 (a>b&&b>c&&c>d)
 
4.12 first comparison magnitude j and k i and then returns the value comparison for equality
 
4.13 
(a). d = 3.0; i = 3; (b).i = 3 , d = 3.5;
 
4.14
if (42 = i) being given
if (i = 42) is always true
 
4.15
pi is a pointer: dval = ival = * pi = 0;
 
4.16
. (A) assignment operator with a lower priority if (! (P = getPtr ()) = 0);
. (B) should be checked whether equal ==; if (i == 1024);
 
4.17 Pre-increment operator will inverse operand itself; postincrement not incremented before the operator returns the copy of the object
 
4.18 miss a first output and will dereference the end address.
 
4.19
It is not a null pointer, and the pointer points to the object value is not 0
b ival ival is not 0 is not incremented 1 0 (-1 ival is neither equal nor equal to 0)
c The result is undefined, because the object order of evaluation is uncertain
 
more [ival] <= maximum of [Animals + 1];
++ ival;
 
4.20、
a) * iter ++ legitimate, for the first increase since 1 iter iter again a reference to the solution
b) (* iter) ++, not legal, * iter of type string, the string does not increment operator
c) * iter.empty (), * iter is a string type, not empty () member
d) iter-> empty () legal
e) ++ * iter, illegal,
f) iter ++ -> empty (), legitimate, verify whether iter is empty then incremented by one;
 
4.21
//p4_21.cpp - use conditional operators
#include <the iostream>
#include <Vector>
the using namespace STD;
int main()
{
 vector<int> ivec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 for (auto i = ivec.cbegin(); i != ivec.cend(); ++i)
  cout << *i << " ";
 cout << endl;
 for (auto& c : ivec)
 {
  c = (c % 2 == 0) ? c : 2 * c;
 }
 for (auto i = ivec.cbegin(); i != ivec.cend(); ++i)
  cout << *i << " ";
 cout << endl;
 return 0;
}
 
4.22
finalgrade = (grade > 90) ? "high pass" : (grade > 75) ? "pass": (grade > 60) ? " low pass": "fail";
if(grade>90)
finalgrade = "high pass"
else if(grade > 75)
finalgrade = "pass"
else if(grade > 60)
finalgrade = "low pass"
else
finalgrade = "fail";
 
4.25
先按位取反,再左移6位
 
4.26
位置不够
 
4.27
3 7 true true
 
4.28
 
4.29
 
4.30
(sizeof x) + y;   sizeof( p->mem[i]);  (sizeof a) < b    sizeof(f())
 
4.31
对本例来说,前置和后置都不影响的,得到的结果都是一样的
vector<int>::size_type cnt = ivec.size();
 
for(vector<int>::size_type ix = 0;
ix != ivec.size(); ix++,cnt--)
ivec[ix] = cnt;
 
4.32
遍历数组
 
4.33
如果someValue 为真则执行++x,++y;如果someValue 为假执行--x,--y;
 
4.34
a.  if(fval) fval转换成bool值,非0为true;0 为false
b. dval = fval + ival; //ival转化为float类型与fval相加结果转换为double类型赋给dval;
c. dval + ival * cval //cval 提升为int类型和ival相乘结果转换为double 再与dval 相加
 
4.35
 
a.发生了:'a'提升为int类型与3相加再转化为char 类型赋值给cval
b 发生了: ival提升为float 与1.0相乘,ui转换为float类型再减去乘积
c 发生了: ui提升为float 乘积转换为double 
d 发生了:ival提升为float 与fval相加和再提升为double 与dval相加结果再转换为char 类型
 
4.36
i *=static_cast<int>(d);
4.37
(a) pv = const_cast<void *>(ps);//
(b) i = static_cast<int>(*pc)
(c) pv = static_cast<void *>(&d);
(d) pc = static_cast<char *>(pv)
 
4.38
将j/i的结果强制转换成double 类型初始化slope;
 
 
 

 

Guess you like

Origin www.cnblogs.com/xiaogaogao/p/11755633.html