Skip to content

Commit

Permalink
perfection for pezy#19
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Mar 4, 2015
1 parent 48487e8 commit b163ecc
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 146 deletions.
107 changes: 58 additions & 49 deletions ch05/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ while (val <= 10)

##Exercise 5.3
>Use the comma operator (§ 4.10, p. 157) to rewrite the while loop from § 1.4.1 (p. 11)
so that it no longer requires a block.
so that it no longer requires a block.
Explain whether this rewrite improves or diminishes the readability of this code.

```cpp
Expand All @@ -39,9 +39,9 @@ int main()
int sum = 0, val = 1;
while (val <= 10)
sum += val, ++val;
std::cout << "Sum of 1 to 10 inclusive is "
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;

return 0;
}
```
Expand All @@ -51,7 +51,7 @@ But there are no meaning in this example, however, also are incomprehensible.

##Exercise 5.4
>Explain each of the following examples, and correct anyproblems you detect.
- (a) while (string::iterator iter != s.end()) { /* . . . */ }
- (a) while (string::iterator iter != s.end()) { /* . . . */ }
- (b) while (bool status = find(word)) { /* . . . */ }
if (!status) { /* . . . */ }

Expand All @@ -68,7 +68,7 @@ while ((status = find(word))) {/* ... */}
if (!status) {/* ... */}
```

In fact, the judge `!status` is unnecessary. If the `status=false`, we leave the while, and `!status` is always true.
In fact, the judge `!status` is unnecessary. If the `status=false`, we leave the while, and `!status` is always true.

##[Exercise 5.5](ex5_5.cpp)
##[Exercise 5.6](ex5_6.cpp)
Expand Down Expand Up @@ -116,51 +116,51 @@ Colloquial term used to refer to the problem of how to process nested if stateme
##Exercise 5.13
>Each of the programs in the highlighted text on page 184 contains a common programming error. Identify and correct each error.
```cpp
(a) unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
(a) unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
char ch = next_text();
switch (ch) {
case 'a': aCnt++;
case 'e': eCnt++;
case 'a': aCnt++;
case 'e': eCnt++;
default: iouCnt++;
}
(b) unsigned index = some_value();
(b) unsigned index = some_value();
switch (index) {
case 1:
int ix = get_value();
ivec[ ix ] = index;
int ix = get_value();
ivec[ ix ] = index;
break;
default:
ix = ivec.size()-1;
ix = ivec.size()-1;
ivec[ ix ] = index;
}
(c) unsigned evenCnt = 0, oddCnt = 0;
(c) unsigned evenCnt = 0, oddCnt = 0;
int digit = get_num() % 10;
switch (digit) {
case 1, 3, 5, 7, 9:
case 1, 3, 5, 7, 9:
oddcnt++;
break;
case 2, 4, 6, 8, 10:
evencnt++;
break;
}
(d) unsigned ival=512, jval=1024, kval=4096;
(d) unsigned ival=512, jval=1024, kval=4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt) {
case ival:
bufsize = ival * sizeof(int);
bufsize = ival * sizeof(int);
break;
case jval:
bufsize = jval * sizeof(int);
bufsize = jval * sizeof(int);
break;
case kval:
bufsize = kval * sizeof(int);
bufsize = kval * sizeof(int);
break;
}
```

```cpp
(a) unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
(a) unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
char ch = next_text();
switch (ch) {
case 'a': aCnt++; break;
Expand All @@ -169,59 +169,68 @@ Colloquial term used to refer to the problem of how to process nested if stateme
case 'o':
case 'u': iouCnt++; break;
}
(b) unsigned index = some_value();
(b) unsigned index = some_value();
int ix;
switch (index) {
case 1:
ix = get_value();
ivec[ ix ] = index;
ix = get_value();
ivec[ ix ] = index;
break;
default:
ix = static_cast<int>(ivec.size())-1;
ix = static_cast<int>(ivec.size())-1;
ivec[ ix ] = index;
}
(c) unsigned evenCnt = 0, oddCnt = 0;
(c) unsigned evenCnt = 0, oddCnt = 0;
int digit = get_num() % 10;
switch (digit) {
case 1: case 3: case 5: case 7: case 9:
case 1: case 3: case 5: case 7: case 9:
oddcnt++;
break;
case 2: case 4: case 6: case 8: case 0:
evencnt++;
break;
}
(d) const unsigned ival=512, jval=1024, kval=4096;
(d) const unsigned ival=512, jval=1024, kval=4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt) {
case ival:
bufsize = ival * sizeof(int);
bufsize = ival * sizeof(int);
break;
case jval:
bufsize = jval * sizeof(int);
bufsize = jval * sizeof(int);
break;
case kval:
bufsize = kval * sizeof(int);
bufsize = kval * sizeof(int);
break;
}
```
##[Exercise 5.14](ex5_14.cpp)
## Exercise 5.14
>Write a program to read strings from standard input looking for duplicated words. The program should find places in the input where one word is followed immediately by itself. Keep track of the largest number of times a single repetition occurs and which word is repeated. Print the maximum number of duplicates, or else print a message saying that no word was repeated. For example, if the input is
```sh
how now now now brown cow cow
```
the output should indicate that the word now occurred three times.

- [concise solution](ex5_14.cpp)
- [easy to understand](ex5_14_1.cpp)


##Exercise 5.15
>Explain each of the following loops. Correct any problems you detect.
```cpp
(a) for (int ix = 0; ix != sz; ++ix) { /* ... */ }
(a) for (int ix = 0; ix != sz; ++ix) { /* ... */ }
if (ix != sz)
// . . .
(b) int ix;
(b) int ix;
for (ix != sz; ++ix) { /* ... */ }
(c) for (int ix = 0; ix != sz; ++ix, ++sz) { /*...*/ }
```
```cpp
(a) int ix;
for (ix = 0; ix != sz; ++ix) { /* ... */ }
for (ix = 0; ix != sz; ++ix) { /* ... */ }
if (ix != sz)
// . . .
(b) int ix;
Expand All @@ -237,15 +246,15 @@ Colloquial term used to refer to the problem of how to process nested if stateme
int i;
while ( cin >> i )
// ...

// same as for
for (int i = 0; cin >> i;)
// ...

// for idiomatic
for (int i = 0; i != size; ++i)
// ...

// same as while
int i = 0;
while (i != size)
Expand All @@ -263,17 +272,17 @@ I prefer `for` to `while` in such cases, because it's terse. More importantly, o
>Explain each of the following loops. Correct any problems you detect.
```cpp
(a) do { // added bracket.
int v1, v2;
cout << "Please enter two numbers to sum:" ;
if (cin >> v1 >> v2)
int v1, v2;
cout << "Please enter two numbers to sum:" ;
if (cin >> v1 >> v2)
cout << "Sum is: " << v1 + v2 << endl;
}while (cin);
}while (cin);
(b) int ival;
do {
// . . .
do {
// . . .
} while (ival = get_response()); // should not declared in this scope.
(c) int ival = get_response();
do {
do {
ival = get_response();
} while (ival); // ival is not declared in this scope.
```
Expand All @@ -283,14 +292,14 @@ I prefer `for` to `while` in such cases, because it's terse. More importantly, o
##[Exercise 5.21](ex5_21.cpp)
##Exercise 5.22
>The last example in this section that jumped back to begin could be better written using a loop. Rewrite the code to eliminate the goto.
>The last example in this section that jumped back to begin could be better written using a loop. Rewrite the code to eliminate the goto.
```cpp
// backward jump over an initialized variable definition is okay
begin:
int sz = get_size();
if (sz <= 0) {
goto begin;
}
begin:
int sz = get_size();
if (sz <= 0) {
goto begin;
}
```

use `for` to replace `goto`:
Expand Down
41 changes: 6 additions & 35 deletions ch05/ex5_10.cpp
Original file line number Diff line number Diff line change
@@ -1,64 +1,35 @@
#include <iostream>

using std::cin; using std::cout; using std::endl;

int main()
{
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch)
switch (ch)
switch (tolower(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;
}
//another way to solve it
/* while (cin >> ch)
{
ch = tolower(ch);
switch (ch)
{
case 'a':
++aCnt;
break;
case 'e':
++eCnt;
break;
case 'i':
++iCnt;
break;
case 'o':
++oCnt;
break;
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;
<< "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;
}
50 changes: 0 additions & 50 deletions ch05/ex5_14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,4 @@ int main()
else cout << "the word " << max_duplicated.first << " occurred " << max_duplicated.second + 1 << " times. " << endl;

return 0;
}

//it's a easy way to understand for primary learner
/*
#include<iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> svec;
string str;
while (cin>>str )
{
svec.push_back(str);
}
vector<string>::iterator it = svec.begin();//auto it = svec.begin();
int bigNum = 1, countNum = 1;
string bigStr, countStr;
while (svec.size()>0&&it != svec.end() - 1 )
{
if (*it == *(it + 1))
{
++countNum;
countStr = *it;
}
else
{
countNum = 0;
}
if (countNum>bigNum)
{
bigStr = countStr;
bigNum = countNum;
}
++it;
}
if (bigNum==1)
{
cout << " there is no duplicated string." << endl;
}
else
{
cout << "the word " << bigStr << " occurred " << bigNum << " times." << endl;
}
system("pause");
return 0;
}
*/
Loading

0 comments on commit b163ecc

Please sign in to comment.