Exercise 5.1

1
2
3
// null statement is a statement just contains a ';'
// applied on certain place which needs a statement required by the rule of syntax, while by logical there is nothing needed to be handled.

Exercise 5.2

1
2
// block is a sequence of statements and declarations surrounded by curly braces, also called as compound statement, may be blank inside curly braces.

Exercise 5.3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int main()
{
  int sum = 0, val = 1;
  while(val <= 10)
    sum += val, ++val;		// less friendly, said in chapter4 that the evaluation of operands in one expression is not ordered, that this expression statement may results undefined behaviour
  std::cout << "Sum of 1 t 10 inclusive is "
	  << sum << std::endl;
  return 0;
}

Exercise 5.4

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
int main()
{
  string s;
  // while(string::iterator iter != s.end()) {/* ... */}, iter is uninitialized
  string::iterator iter = s.begin();
  while(iter != s.end()) ;
  // while (bool status = find(word) {/* ... */}
  // if(!status) {/* ... */}
  // status defined in while loop could only be used inside its scope
  bool status;
  while (status = find(word)) ;
  if(!status) ;
}

Exercise 5.5

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  int grade;
  vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
  cout << "Please enter a grade: " << endl;
  cin >> grade;
  string lettergrade;
  if(grade < 60)
    lettergrade = scores[0];
  else
    {
      lettergrade = scores[(grade - 50) / 10];
      if(grade != 100)
      {
	if(grade % 10 > 7)
	  lettergrade += "+";
	else if(grade % 10 < 3)
	  lettergrade += "-";
      }
    }
  cout << lettergrade << endl;
}

Exercise 5.6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int main()
{
  int grade;
  vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
  cout << "Please enter a grade: " << endl;
  cin >> grade;
  string lettergrade;
  (grade < 60) ? (lettergrade = scores[0]) : (lettergrade = scores[(grade - 50) / 10]);
  (grade != 100) ? ((grade % 10 > 7) ? (lettergrade += "+") : ((grade % 10 < 3) ? (lettergrade += "-") : lettergrade)) : lettergrade;
  cout << lettergrade << endl;
}

Exercise 5.7

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// if(ival1 1= ival2)
//   ival1 = ival2;
// else ival1 = ival2 = 0;
// if(ival < minval)
//   {
//     minval = ival;
//     occurs = 1;
//   }
// if(int ival = get_value())
//   cout << "ival = " << ival << endl;
// else if(!ival)			// ival still in the same scope created by this if-else statement
//   cout << "ival = 0\n";
// if(ival == 0)
//   ival = get_value();

Exercise 5.8

1
2
3
// dangling else means problem of matching else with if in a multipile if and if-else statements, should be considered in programming languages which allow if statements and if-else statements co-exist.
// C++ determines that else should match the nearest unmatched if under such circustance.

Exercise 5.9

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int main()
{
  char c;
  unsigned cnt = 0;
  cout << "Please enter words or specify a text file: " << endl;
  while(cin >> c)
      if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ++cnt;
  cout << "There're " << cnt << " aeiou characters." << endl;
}

Exercise 5.10

 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
int main()
{
  unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
  char ch;
  while (cin >> ch)
    switch (ch)
      {
      case 'a':
      case 'A':
      ++aCnt;
      break;
      case 'e':
      case 'E':
      ++eCnt;
      break;
      case 'i':
      case 'I':
      ++iCnt;
      break;
      case 'o':
      case 'O':
      ++oCnt;
      break;
      case 'u':
      case 'U':
      ++uCnt;
      break;
      }

  cout << "Number of vowel a(A): \t" << aCnt << '\n'
       << "Number of vowel e(E): \t" << eCnt << '\n'
       << "Number of vowel i(I): \t" << iCnt << '\n'
       << "Number of vowel o(O): \t" << oCnt << '\n'
       << "Number of vowel u(U): \t" << uCnt << endl;

  return 0;
}

Exercise 5.11

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
  unsigned cnt = 0;
  string line;
  while(getline(cin, line))
    {
      ++cnt;			// getline reads from cin and stores it in a string object excpets for the tail '\n'
      for(auto &ch : line)
      {
	switch(ch)
	  {
	  case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': case ' ': case '\t':
	    ++cnt;
	    break;
	  }
      }
    }
  cout << cnt << endl;
}

Exercise 5.12

 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
int main()
{
  unsigned cnt = 0;
  char ch;
  bool isf;
  while(cin >> ch)
    {
      switch(ch)
      {
      case 'f':
	{
	  if(!isf) isf = true;
	  else
	    {
	      ++cnt;
	      isf = false;
	    }
	}
	break;
      case 'l':
	if(isf)
	  {
	    ++cnt;
	    isf = false;
	  }
	break;
      case 'i':
	if(isf)
	  {
	    ++cnt;
	    isf = false;
	  }
	break;
      default:
	isf = false;
	break;
      }
    }
  cout << cnt<< endl;
}

Exercise 5.13

 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
int main()
{
  unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
  char ch = next_text();
  switch(ch)
    {
    case 'a':
      aCnt++;
      break;
    case 'e':
      eCnt++;
      break;
    case 'i': case 'o': case 'u':
      iouCnt++;
      break;
    }
  int ix = get_value();
  unsigned index = some_vale();
  switch(index)
    {
    case 1:
      ivec[ix] = index;
      break;
    default:
      ix = ivec.size() - 1;
      ivec[ix] = index;
    }
  unsigned evenCnt = 0, oddCnt = 0;
  int digit = get_num() % 10;
  switch(digit)
    {
    case 1: case 3: case 5: case 7: case 9:
      oddCnt++;
      break;
    case 2: case 4: case 6: case 8: case 10:
      evenCnt++;
      break;
    }
  constexpr unsigned ival = 512, jval = 1024, kval = 4096;
  unsigned bufsize;
  unsigned swt = get_bufCnt();
  switch(swt)
    {
    case ival:
      bufsize = ival * sizeof(int);
      break;
    case jval:
      bufsize = jval * sizeof(int);
      break;
    case kval:
      bufsize = kval * sizeof(int);
      break;
    }
}

Exercise 5.14

 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
int main()
{
  string s, repeat, maxrepeat;
  unsigned cnt = 1, maxcnt = 1;
  while(cin >> s)
    {
      if(repeat == s) ++cnt;
      else
      {
	if(cnt >= 2 && cnt >= maxcnt)
	  {
	    maxrepeat = repeat;
	    maxcnt = cnt;
	  }
	cnt = 1;
	repeat = s;
      }
    }
  if(cnt >= 2 && cnt >= maxcnt)
    {
      maxrepeat = repeat;
      maxcnt = cnt;
    }
  if(maxcnt != 1)
    cout << maxrepeat << " repeated " << maxcnt << " times." << endl;
  else
    cout << "No word has repeated." << endl;
}

Exercise 5.15

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// for(int ix = 0; ix != sz; ++ix) { /* ... */ }
// if(ix != sz) // ... variable ix defined in for statement could not be used outside of for statement
// int ix = 0;
// for(: i != sz; ++ix) { /* ... */ }
// if(ix != sz) // ...
// int ix; // variable ix not initialized, should be int ix = 0;
// for(ix != sz; ++ix) { /* ... */ } // missing initializer, which should be an empty statement when the initialize could be ignored.
// for(; ix != sz; ++ix) { /* ... */ }
// for(int ix = 0; ix != sz; ++ix, ++sz) { /* ... */ } // possibely a infinite loop
// for(int ix = 0; ix != sz; ++ix) { /* ... */ }

Exercise 5.16

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
  int i;
  // while(cin >> i)
  //   cout << i << endl;                 // while better in a loop whose loop cnt is unclear
  for(; cin >> i; )
    cout <<i << endl;
  vector<int> iv = {1, 2, 3, 4, 5, 6};
  auto length = iv.size();
  decltype(length) index = 0;
  while(index != iv.size())
    {
      cout << iv[index];
      ++index;
    }
  for(decltype(iv.size()) index = 0; index != iv.size(); ++index) // for better in a loop whose loop cnt is clear
    cout << iv[index];
  // which is preffered based on certain situation
}

Exercise 5.17

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int main()
{
  vector<int> iv1 = {1, 2, 3, 4}, iv2 = {1, 2, 3, 4, 5, 6};
  bool isPrefix = true;
  for(decltype(iv1.size()) index = 0; index != iv1.size() && index != iv2.size(); ++index)
    if(iv1[index] != iv2[index])
      {
      isPrefix = false;
      break;
      }
  cout << "Is one vector as another vector's prefix? " << isPrefix << endl;
}

Exercise 5.18

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  do
    {
      int v1, v2;		// requires a statement or a compound statement.
      cout << "Please Enter two numbers to sum:";
      if(cin >> v1 >> v2)
      cout << "Sum is : " << v1 + v2 <<endl;
    }
  while(cin);
  int ival = get_response();
  do
    {
      // statement that could modify ival
    }
  while(ival);
  // do
  //   {
  //     int ival = get_response();
  //   }
  // while(ival);              // variable used by condition statement must be defined outside of loop statement, same correctness as qustion b.
}

Exercise 5.19

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int main()
{
  do
    {
      cout << "Please enter two strings: ";
      string s1, s2;
      cin >> s1 >> s2;
      cout << "Shorter one: " << (s1.size() >= s2.size() ? s2 : s1) << endl;
    }
  while(cin);
}

Exercise 5.20

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
  bool repeated = false;
  string s1, s2;
  while(cin >> s1)
    {
      if(s2 == s1)
      {
	repeated = true;
	break;
      }
      else
      s2 = s1;
    }
  if(!repeated)
    cout << "No repeat detected." << endl;
  else
    cout << s1 << " repeats 2 times." << endl;
}

Exercise 5.21

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
  bool repeated = false;
  string s1, s2;
  while(cin >> s1)
    {
      if(islower(s1[0]))
       continue;
      if(s2 == s1)
      {
	repeated = true;
	break;
      }
      else
      s2 = s1;
    }
  if(!repeated)
    cout << "No repeat detected." << endl;
  else
    cout << s1 << " repeats 2 times." << endl;
}

Exercise 5.22

1
2
3
4
// int sz = get_size();
// while(sz <= 0)
//   sz = get_size();

Exercise 5.23

1
2
3
4
5
6
7
8
int main()
{
  int i1, i2;
  cout << "Please enter two integer numbers: ";
  cin >> i1 >> i2;
  cout << i1 / i2 << endl;
}