-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solutions to all exercises in chapter 5.
- Loading branch information
Jaege
committed
Jan 13, 2016
1 parent
0fbeca4
commit baf2b49
Showing
28 changed files
with
577 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
(a) | ||
* vec.begin() ==> * ((vec.begin)()) | ||
(a) `* vec.begin() ==> * ((vec.begin)())` | ||
|
||
The order is: member selector, function call, dereference. | ||
|
||
(b) | ||
* vec.begin() + 1 ==> ( * ((vec.begin)())) + 1 | ||
(b) `* vec.begin() + 1 ==> ( * ((vec.begin)())) + 1` | ||
|
||
The order is: member selector, function call, dereference, add. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A single `;`(semicolon) is a null statement. It is used where the language requires a statement but the program's logic does not. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
|
||
int main() { | ||
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0; | ||
char ch; | ||
while (std::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; | ||
} | ||
} | ||
std::cout << "Number of vowel a: " << aCnt << '\n' | ||
<< "Number of vowel e: " << eCnt << '\n' | ||
<< "Number of vowel i: " << iCnt << '\n' | ||
<< "Number of vowel o: " << oCnt << '\n' | ||
<< "Number of vowel u: " << uCnt << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <iostream> | ||
|
||
int main() { | ||
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0; | ||
unsigned spaceCnt = 0, tabCnt = 0, newlineCnt = 0, otherCnt = 0; | ||
char ch; | ||
while (std::cin.get(ch)) { // `<<` operator will omit whitespace characters | ||
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; | ||
case ' ': | ||
++spaceCnt; | ||
break; | ||
case '\t': | ||
++tabCnt; | ||
break; | ||
case '\n': | ||
++newlineCnt; | ||
break; | ||
default: | ||
++otherCnt; | ||
} | ||
} | ||
std::cout << "Number of vowel a:\t" << aCnt << '\n' | ||
<< "Number of vowel e:\t" << eCnt << '\n' | ||
<< "Number of vowel i:\t" << iCnt << '\n' | ||
<< "Number of vowel o:\t" << oCnt << '\n' | ||
<< "Number of vowel u:\t" << uCnt << '\n' | ||
<< "Number of space:\t" << spaceCnt << '\n' | ||
<< "Number of tab:\t" << tabCnt << '\n' | ||
<< "Number of newline:\t" << newlineCnt << '\n' | ||
<< "Number of others:\t" << otherCnt << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Exercise 5.9: Write a program using a series of if statements to count the number of | ||
vowels in text read from cin. | ||
|
||
Exercise 5.10: There is one problem with our vowel-counting program as we’ve implemented | ||
it: It doesn’t count capital letters as vowels. Write a program that counts | ||
both lower- and uppercase letters as the appropriate vowel—that is, your program | ||
should count both ’a’ and ’A’ as part of aCnt, and so forth. | ||
|
||
Exercise 5.11: Modify our vowel-counting program so that it also counts the number | ||
of blank spaces, tabs, and newlines read. | ||
|
||
Exercise 5.12: Modify our vowel-counting program so that it counts the number of | ||
occurrences of the following two-character sequences: ff, fl, and fi. | ||
|
||
Exercise 5.13: Each of the programs in the highlighted text on page 184 contains a | ||
common programming error. Identify and correct each error. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <iostream> | ||
|
||
int main() { | ||
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0; | ||
unsigned spaceCnt = 0, tabCnt = 0, newlineCnt = 0, otherCnt = 0; | ||
unsigned ffCnt = 0, flCnt = 0, fiCnt = 0; | ||
bool fPresent = false; | ||
char ch; | ||
while (std::cin.get(ch)) { // `<<` operator will omit whitespace characters | ||
switch(ch) { | ||
case 'A': case 'a': | ||
++aCnt; | ||
break; | ||
case 'E': case 'e': | ||
++eCnt; | ||
break; | ||
case 'I': | ||
++iCnt; | ||
break; | ||
case 'i': | ||
++iCnt; | ||
if (fPresent) { | ||
++fiCnt; | ||
fPresent = false; | ||
} | ||
break; | ||
case 'O': case 'o': | ||
++oCnt; | ||
break; | ||
case 'U': case 'u': | ||
++uCnt; | ||
break; | ||
case ' ': | ||
++spaceCnt; | ||
break; | ||
case '\t': | ||
++tabCnt; | ||
break; | ||
case '\n': | ||
++newlineCnt; | ||
break; | ||
case 'f': | ||
if (fPresent) | ||
++ffCnt; | ||
else | ||
fPresent = true; | ||
break; | ||
case 'l': | ||
if (fPresent) { | ||
++flCnt; | ||
fPresent = false; | ||
} | ||
break; | ||
default: | ||
++otherCnt; | ||
} | ||
} | ||
std::cout << "Number of vowel a:\t" << aCnt << '\n' | ||
<< "Number of vowel e:\t" << eCnt << '\n' | ||
<< "Number of vowel i:\t" << iCnt << '\n' | ||
<< "Number of vowel o:\t" << oCnt << '\n' | ||
<< "Number of vowel u:\t" << uCnt << '\n' | ||
<< "Number of space:\t" << spaceCnt << '\n' | ||
<< "Number of tab:\t" << tabCnt << '\n' | ||
<< "Number of newline:\t" << newlineCnt << '\n' | ||
<< "Number of sequence ff:\t" << ffCnt << '\n' | ||
<< "Number of sequence fl:\t" << flCnt << '\n' | ||
<< "Number of sequence fi:\t" << fiCnt << '\n' | ||
<< "Number of others:\t" << otherCnt << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
(a) | ||
|
||
unsigned aCnt = 0, eCnt = 0, iouCnt = 0; | ||
char ch = next_text(); | ||
switch (ch) { | ||
case ’a’: aCnt++; break; // Need `break` | ||
case ’e’: eCnt++; break; // Need `break` | ||
default: iouCnt++; | ||
} | ||
|
||
(b) | ||
|
||
unsigned index = some_value(); | ||
int ix; | ||
switch (index) { | ||
case 1: | ||
//int ix = get_value(); // Cannot bypass initialization | ||
ix = get_value(); | ||
ivec[ ix ] = index; | ||
break; | ||
default: | ||
ix = ivec.size()-1; | ||
ivec[ ix ] = index; | ||
} | ||
|
||
(c) | ||
|
||
unsigned evenCnt = 0, oddCnt = 0; | ||
int digit = get_num() % 10; | ||
switch (digit) { | ||
//case 1, 3, 5, 7, 9: // case label must be integral const expression | ||
case 1: case 3: case 5: case 7: case 9: | ||
oddcnt++; | ||
break; | ||
//case 2, 4, 6, 8, 10: | ||
case 2: case 4: case 6: case 8: case 10: | ||
evencnt++; | ||
break; | ||
} | ||
|
||
(d) | ||
|
||
//unsigned ival=512, jval=1024, kval=4096; | ||
// case label must be integral const expression | ||
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
int main() { | ||
std::string word, duplicatedWord, maxDuplicatedWord; | ||
unsigned cnt = 0, maxCnt = 0; | ||
while (std::cin >> word) { | ||
if (word == duplicatedWord) | ||
++cnt; | ||
else { | ||
if (cnt > maxCnt) { | ||
maxDuplicatedWord = duplicatedWord; | ||
maxCnt = cnt; | ||
} | ||
duplicatedWord = word; | ||
cnt = 1; | ||
} | ||
} | ||
if (cnt > maxCnt) { | ||
maxDuplicatedWord = duplicatedWord; | ||
maxCnt = cnt; | ||
} | ||
std::cout << maxDuplicatedWord << " occurs " << maxCnt | ||
<< " time" << (maxCnt > 1 ? "s" : "") << ". " << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(a) | ||
|
||
int ix; | ||
for (ix = 0; ix != sz; ++ix) { /* ... */ } | ||
//for (int ix = 0; ix != sz; ++ix) { /* ... */ } | ||
// The loop variable is used outside `for` scope, thus should be defined outside. | ||
if (ix != sz) | ||
// ... | ||
|
||
(b) | ||
|
||
int ix; | ||
//for (ix != sz; ++ix) { /* ... */ } | ||
// When the initialization is unnecessary, a null statement should be used. | ||
for (; ix != sz; ++ix) { /* ... */ } | ||
|
||
(c) | ||
|
||
for (int ix = 0; ix != sz; ++ix, ++ sz) { /* ... */ } | ||
// The loop will never end. | ||
for (int ix = 0; ix != sz; ++ix) { /* ... */ } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
int main() { | ||
int i; | ||
while (std::cin >> i) { /* ... */ } | ||
for (int j; std::cin >> j;) { /* ... */ } | ||
|
||
std::vector<int> iv(10, 1); | ||
for (auto it = iv.begin(); it != iv.end(); ++it) { /* ... */ } | ||
auto it2 = iv.begin(); | ||
while (it2 != iv.end()) { | ||
++it2; | ||
/* ... */ | ||
} | ||
|
||
// I would choose `for`-loop, because it can do what a `while`-loop can, but | ||
// not vise versa. | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
#include <sstream> | ||
|
||
void readVector(std::vector<int> &v) { | ||
std::string str; | ||
std::getline(std::cin, str); | ||
std::stringstream ss(str); | ||
int i; | ||
while(ss >> i) | ||
v.push_back(i); | ||
//for (const auto &e : v) | ||
// std::cout << e << " "; | ||
//std::cout << std::endl; | ||
} | ||
|
||
bool isPrefix(const std::vector<int> &v1, const std::vector<int> &v2) { | ||
auto it1 = v1.cbegin(), it2 = v2.cbegin(); | ||
for (; it1 != v1.cend() && it2 != v2.cend(); ++it1, ++it2) | ||
if (*it1 != *it2) | ||
break; | ||
return it1 == v1.cend() || it2 == v2.cend(); | ||
} | ||
|
||
int main() { | ||
std::vector<int> v1, v2; | ||
readVector(v1); | ||
readVector(v2); | ||
std::cout << (isPrefix(v1, v2) ? "true" : "false") << std::endl; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.