Skip to content

Commit

Permalink
Add solutions to all exercises in chapter 5.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaege committed Jan 13, 2016
1 parent 0fbeca4 commit baf2b49
Show file tree
Hide file tree
Showing 28 changed files with 577 additions and 10 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Cpp-Primer-5th-Exercises
# Solutions to Exercises in **C++ Primer** 5th Edition

This repo is the solutions to exercises in book _C++ Primer_ (5th Edition). All files except explicitly announced are under [Apache License](http://www.apache.org/licenses/LICENSE-2.0).

### Contents

Expand Down Expand Up @@ -39,15 +41,15 @@
[21](ch4/4.21.cpp)|[22](ch4/4.22.cpp)|[23](ch4/4.23.cpp)|[24](ch4/4.24.md)|[25](ch4/4.25.md)|[26](ch4/4.26.md)|[27](ch4/4.27.cpp)|[28](ch4/4.28.cpp)|[29](ch4/4.29.cpp)|[30](ch4/4.30.md)|
[31](ch4/4.31.cpp)|[32](ch4/4.32.cpp)|[33](ch4/4.33.md)|[34](ch4/4.34.md)|[35](ch4/4.35.cpp)|[36](ch4/4.36.cpp)|[37](ch4/4.37.cpp)|[38](ch4/4.38.md)

<!---
#### Chapter 5 [](ch5)
#### Chapter 5 [Statements](ch5)

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
--:|--:|--:|--:|--:|--:|--:|--:|--:|--:
[1](ch5/5.1.cpp)|[2](ch5/5.2.cpp)|[3](ch5/5.3.cpp)|[4](ch5/5.4.cpp)|[5](ch5/5.5.cpp)|[6](ch5/5.6.cpp)|[7](ch5/5.7.cpp)|[8](ch5/5.8.cpp)|[9](ch5/5.9.cpp)|[10](ch5/5.10.cpp)|
[11](ch5/5.11.cpp)|[12](ch5/5.12.cpp)|[13](ch5/5.13.cpp)|[14](ch5/5.14.cpp)|[15](ch5/5.15.cpp)|[16](ch5/5.16.cpp)|[17](ch5/5.17.cpp)|[18](ch5/5.18.cpp)|[19](ch5/5.19.cpp)|[20](ch5/5.20.cpp)|
[21](ch5/5.21.cpp)|[22](ch5/5.22.cpp)|[23](ch5/5.23.cpp)|[24](ch5/5.24.cpp)|[25](ch5/5.25.cpp)
[1](ch5/5.1.md))|[2](ch5/5.2.md)|[3](ch5/5.3.cpp)|[4](ch5/5.4.md)|[5](ch5/5.5.cpp)|[6](ch5/5.6.cpp)|[7](ch5/5.7.md)|[8](ch5/5.8.md)|[9](ch5/5.9.cpp)|[10](ch5/5.10.cpp)|
[11](ch5/5.11.cpp)|[12](ch5/5.12.cpp)|[13](ch5/5.13.md)|[14](ch5/5.14.cpp)|[15](ch5/5.15.md)|[16](ch5/5.16.cpp)|[17](ch5/5.17.cpp)|[18](ch5/5.18.md)|[19](ch5/5.19.cpp)|[20](ch5/5.20.cpp)|
[21](ch5/5.21.cpp)|[22](ch5/5.22.md)|[23](ch5/5.23.cpp)|[24](ch5/5.24.cpp)|[25](ch5/5.25.cpp)

<!---
#### Chapter 6 [](ch6)
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10
Expand Down
6 changes: 2 additions & 4 deletions ch4/4.2.md
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.
1 change: 1 addition & 0 deletions ch5/5.1.md
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.
32 changes: 32 additions & 0 deletions ch5/5.10.cpp
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;
}
48 changes: 48 additions & 0 deletions ch5/5.11.cpp
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;
}
16 changes: 16 additions & 0 deletions ch5/5.11.in
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.
72 changes: 72 additions & 0 deletions ch5/5.12.cpp
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;
}
59 changes: 59 additions & 0 deletions ch5/5.13.md
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;
}

27 changes: 27 additions & 0 deletions ch5/5.14.cpp
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;
}
21 changes: 21 additions & 0 deletions ch5/5.15.md
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) { /* ... */ }
21 changes: 21 additions & 0 deletions ch5/5.16.cpp
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;
}
33 changes: 33 additions & 0 deletions ch5/5.17.cpp
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;
}
Loading

0 comments on commit baf2b49

Please sign in to comment.