Exercise 2.3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int main()
{
  unsigned u = 10, u2 = 42;
  std::cout << u2 - u << std::endl; // 32
  std::cout << u - u2 << std::endl; // -32 的二进制补码表示,此次输出该二进制表示被当作无符号数
  int i = 10, i2 = 42;
  std::cout << i2 - i << std::endl; // 32
  std::cout << i - i2 << std::endl; // -32
  std::cout << i - u << std::endl;  // 0
  std::cout << u - i << std::endl;  // 0
  return 0;
}

Exercise 2.8

1
2
3
4
5
6
int main()
{
  std::cout << "\x32\x4d\n" << "\x32\t\x4d\n" << std::endl;
  return 0;
}

Exercise 2.9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
int main()
{
  // std::cin >> int input_value;
  // first there should be declaration
  int input_value;
  std::cin >> input_value;
  // int i = { 3.14 } precision loss
  double i = {3.14};
  // double salary = wage = 9999.99
  double salary = 9999.99, wage = salary;
  int i = 3.14;			// truncated
  return 0;
}

Exercise 2.10

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
std::string global_str;		// ""
int global_int;			// 0
int main()
{
  int local_int;		// undefined
  std::string local_str;	// initialized by class string
  std::cout << global_str << '\n'
	  << global_int << '\n'
	  << local_int  << '\n'
	  << local_str  << std::endl;
  return 0;
}

Exercise 2.11

1
2
3
4
5
6
7
int main()
{
  extern int ix = 1024;		// definition
  int iy;			// definition
  extern int iz;		// declaration
}

Exercise 2.12

1
2
3
4
5
6
7
8
9
int main()
{
  int double = 3.14;		// false, using c++ keyword as name
  int _;			// right, starts with '_' is ok
  int catch-22;			// false, '-' not allowed
  int 1_or_2 = 1;		// could not starts with number
  double Double = 3.14;		// uppercase sensitive
}

Exercise 2.13

1
2
3
4
5
6
7
int i = 42;
int main()
{
  int i = 100;
  int j = i;			// 100
}

Exercise 2.14

1
2
3
4
5
6
7
8
int main()
{
  int i = 100, sum = 0;
  for(int i = 0; i != 10; ++i)
    sum += i;
  std::cout << i << " " << sum << std::endl; // i = 100, sum = 45
}

Exercise 2.15

1
2
3
4
5
6
7
8
int main()
{
  int ival = 1.01;		// right, precision loss
  int &rvall = 1.01;		// false, reference must be binded to an object
  int &rval2 = ival;		// right
  int &rval3;			// false, reference must be initialized
}

Exercise 2.16

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int main()
{
  int i, &r1 = i;
  double d = 0, &r2 = d;
  r2 = 3.14159;			// right
  r2 = r1;			// right
  i = r2;			// right, maybe precision loss
  r1 = d;			// right, maybe precision loss
}

Exercise 2.17

1
2
3
4
5
6
7
int main()
{
  int i, &ri = i;
  i = 5; ri = 10;
  std::cout << i << " " << ri << std::endl; // 10 10
}

Exercise 2.18

1
2
3
4
5
6
7
8
int main()
{
  int i = 3, *p = &i;
  *p = 4;
  std::cout << *p << " " << i << std::endl;
  p = nullptr;
}

Exercise 2.19

1
2
3
// pointer object, stores a address of one object.
// reference not an object, bind to another object, a alias name.

Exercise 2.20

1
2
3
4
5
6
7
8
int main()
{
  int i = 42;
  int *p1 = &i;
  *p1 = *p1 * *p1;		// i = 42 * 42
  std::cout << *p1 << std::endl;
}

Exercise 2.21

1
2
3
4
5
6
7
8
int main()
{
  int i = 0;
  double* dp = &i;		// false, pointer and its target's type must match
  int *ip = i;			// false, directly assign a variable to a pointer
  int *p = &i;			// right
}

Exercise 2.22

1
2
3
4
5
6
7
8
int main()
{
  int i = 4;
  int *p = &i;
  if(p) return 0;			// if p not a nullptr
  if(*p) return 0;			// if p points to a value which converted to bool whose value is true;
}

Exercise 2.23

1
2
// it is not possible to determine whether a given pointer points to a valid object, due to the fact that this object may be uninitialized.

Exercise 2.24

1
2
3
4
5
6
7
int main()
{
  int i = 42;
  void *p = &i;
  long *lp = &i;		// types of pointer and the object be pointed to must match
}

Exercise 2.25

1
2
3
4
5
6
7
int main()
{
  int* ip, i, &r = i;		// ip int*, i int, r int&;
  int i, *ip = 0;		// i int, ip int* nullptr;
  int* ip, ip2;			// ip int*, ip2 int;
}

Exercise 2.26

1
2
3
4
5
6
7
8
int main()
{
  const int buf;		// const variable must be initialized
  int cnt = 0;			// right
  const int sz = cnt;		// right, iust copy the value
  ++cnt, ++sz;			// first is right, second is false, could not change a constant value
}

Exercise 2.27

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int main()
{
  int i = -1, &r = 0;		// false, reference must be binded to an object
  int *const p2 = &i2;		// right
  const int i = -1, &r = 0;	// right, reference to constant could be binded to literal
  const int *const p3 = &i2;	// right, whether i2 is constant or not has no effect
  const int *p1 = &i2;		// right
  const int &const_r2;		// false, reference must be initialized
  const int i2 = i, &r = i;	// right
}

Exercise 2.28

1
2
3
4
5
6
7
8
9
int main()
{
  int i, *const cp;		// false, const pointer must be initialized
  int *p1, *const p2;		// false, same reason as before
  const int ic, &r = ic;	// false, const int object must be initialized
  const int *const p3;		// false, ...
  const int *p;			// right, defines a pointer which points to const int object
}

Exercise 2.29

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int main()
{
  i = ic;			// right, just copy the value
  p1 = p3;			// false, if passed the const object pointed by p3 could be modified through p1
  p1 = &ic;			// false, same reason as the former one
  p3 = &ic;			// false, const pointer could not be modified
  p2 = p1;			// false, same reason as the former one
  ic = *p3;			// false, const int object could not be modified
}

Exercise 2.30

1
2
3
4
5
6
7
8
int main()
{
  const int v2 = 0;		// top const
  int v1 = v2;
  int *p1 = &v1, &r1 = v1;
  const int *p2 = &v2, *const p3 = &i, &r2 = v2; // p2 low const, p3 top low const, r2 low const
}

Exercise 2.31

1
2
3
4
5
6
7
8
9
int main()
{
  r1 = v2;			// right, top-level const of v2 is not affected
  p1 = p2;			// false, low-level const of p2 is affected
  p2 = p1;			// right, no const is affected
  p1 = p3;			// false, low-level const of p3 is affected
  p2 = p3;			// right, no const is affedted
}

Exercise 2.32

1
2
3
4
5
6
int main()
{
  // int null = 0, *p = null;	 false, could not assign a int variable to pointer
  int null = 0, *p = nullptr;
}

Exercise 2.33

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{
  int i = 0, &r = i;
  auto a = r;
  const int ci = i, &cr = ci;
  auto b = ci;
  auto c = cr;
  auto d = &i;
  auto e = &ci;
  const auto f = ci;
  auto &g = ci;
  std::cout << "a " << a;
  a = 42;			// a = 42
  std::cout << " " << a << std::endl;
  std::cout << "b " << b;
  b = 42;			// b = 42
  std::cout << " " << b << std::endl;
  std::cout << "c " << c;
  c = 42;			// c = 42
  std::cout << " " << c << std::endl;
  d = 42;			// false, types not match;
  e = 42;			// false, types not match;
  g = 42;			// g is a reference to const int
}

Exercise 2.35

1
2
3
4
5
6
7
8
9
int main()
{
  const int i = 42;
  auto j = i;			// j int, 4
  const auto &k = i;		// k const int&, bind to i, 42
  auto *p = &i;			// p const int*, points to i
  const auto j2 = i, &k2 = i;   // j2 const int, 42, k2 const int&, bind to i, 42
}

Exercise 2.36

1
2
3
4
5
6
7
8
9
int main()
{
  int a = 3, b = 4;
  decltype(a) c = a;		// c int, 3
  decltype((b)) d = a;		// d int&, 3
  ++c;				// 4
  ++d;				// 4
}

Exercise 2.37

1
2
3
4
5
6
7
int main()
{
  int a = 3, b = 4;
  decltype(a) c = a;		// c int, 3
  decltype((a = b)) d = a;	// d int&, bind to a, 3
}

Exercise 2.38

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int main()
{
  int i = 0, &r = i;
  // same
  auto a = i;
  decltype(i) b = i;
  // different
  auto c = r;			// c int
  decltype(r) d = r;		// d int&
}

Exercise 2.42

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 1
int main()
{
  Sales_data item;
  while(std::cin >> item.bookNo >> item.units_sold >> item.revenue)
    {
      std::cout << item.bookNo << " "
	      << item.units_sold << " "
	      << item.revenue << std::endl;
    }
  return 0;
}
// 2
int main()
{
  Sales_data item1, item2;
  std::cin >> item1.bookNo >> item1.units_sold >> item1.revenue
	 >> item2.bookNo >> item2.units_sold >> item2.revenue;
  std::cout << item1.bookNo << " "
	  << item1.units_sold + item2.units_sold << " "
	  << item1.revenue + item2.revenue << std::endl;
  return 0;
}
// 3
int main()
{
  unsigned sold_sum = 0;
  double revenue_sum = 0.0;
  Sales_data item;
  while(std::cin >> item.bookNo >> item.units_sold >> item.revenue)
    {
      sold_sum += item.units_sold;
      revenue_sum += item.revenue;
    }
  std::cout << item.bookNo << " "
	  << sold_sum << " "
	  << revenue_sum << std::endl;
  return 0;
}
// 4
int main()
{
  int cnt = 1;
  Sales_data item1, item2;
  if(std::cin >> item1.bookNo >> item1.units_sold >> item1.revenue)
    {
      while(std::cin >> item2.bookNo >> item2.units_sold >> item2.revenue)
      {
	// don't worry, Sales_item item is initialized when created
	if(item1.bookNo == item2.bookNo)
	  cnt += 1;
	else
	  {
	    std::cout << item1.bookNo << " recorded " << cnt << std::endl;
	    item1 = item2;
	    cnt = 1;
	  }
      }
      // the last one
      std::cout << item1.bookNo << " recorded " << cnt << std::endl;
    }
  else
    {
      std::cerr << "No data?" << std::endl;
      return -1;
    }
  return 0;
}