以下程序由 Teddy van Jerry (我自己)编写并运行,基本保证正确性。(有时可能会为优化程序超前使用某些内容)
Contents
- Before we comb through the codes
- Exercise 3.4
- Exercise 3.5(a)
- Exercise 3.5(b)
- Exercise 3.6
- Exercise 3.7
- Exercise 3.8(a)
- Exercise 3.8(b)
- Exercise 3.10
- Exercise 3.14
- Exercise 3.15
- Exercise 3.16
- Exercise 3.17
- Exercise 3.19
- Exercise 3.20(a)
- Exercise 3.20(b)
- Exercise 3.21
- Exercise 3.22
- Exercise 3.23
- Exercise 3.24(a)
- Exercise 3.24(b)
- Exercise 3.25
- Exercise 3.26
- Exercise 3.31
- Exercise 3.32(a)
- Exercise 3.32(b)
- Exercise 3.35
- Exercise 3.36(a)
- Exercise 3.36(b)
- Exercise 3.39
- Exercise 3.40
- Exercise 3.41
- Exercise 3.42
- Exercise 3.43(a)
- Exercise 3.43(b)
- Exercise 3.43(c)
- Exercise 3.44(a)
- Exercise 3.44(b)
- Exercise 3.44(c\)
- Exercise 3.45(a)
- Exercise 3.45(b)
- Exercise 3.45(c\)
- Next Chapter
- See also
Before we comb through the codes
在第二章扎实基础之上,本章学习会相对轻松,若有知识遗忘可以方便地查看之前所学内容。第三章相比第二章更多强调运用。
Review:
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 1(第一章)
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 2(第二章)
TIP:标有(C++/11)者为C++/11标准下可以使用的题,若为老版本应加以修改。
Exercise 3.4
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
cin >> s1 >> s2;
if (s1 == s2) cout << "The two strings are equal." << endl;
else
{
if (s1.size() == s2.size()) cout << "The two strings have the same length." << endl;
else
{
string S1 = "‘" + s1 + "’";
string S2 = "‘" + s2 + "’";
if (s1.size() > s2.size()) cout << S1 << " is longer than " << S2 << "." << endl;
else cout << S2 << " is longer than " << S1 << "." << endl;
}
}
return 0;
}
Exercise 3.5(a)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, p;
cin >> p;
while (cin >> s)
{
if (s > p) p = s;
}
cout << p << endl;
}
Exercise 3.5(b)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
while (cin >> s)
cout << s << " ";
cout << endl;
return 0;
}
Exercise 3.6
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
for (auto &c : s)
c = toupper(c);
cout << s << endl;
return 0;
}
Exercise 3.7
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
for (char &c : s)
c = toupper(c);
cout << s << endl;
return 0;
}
Exercise 3.8(a)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
if (!s.empty())
{
unsigned t = s.size();
unsigned i = 0;
while (i < t)
{
s[i] = toupper(s[i]);
i++;
}
}
else
{
cerr << "Empty!";
return -1;
}
cout << s << endl;
return 0;
}
Exercise 3.8(b)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
getline(cin, s);
if (!s.empty())
{
for (int i = 0; i < s.size(); i++)
s[i] = toupper(s[i]);
cout << s << endl;
}
else
{
cerr << "Empty" << endl;
return -1;
}
return 0;
}
Exercise 3.10
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s, p;
getline(cin, s);
for (auto& c : s)
{
if (!ispunct(c))
p += c;
}
cout << p << endl;
return 0;
}
Exercise 3.14
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
int i;
while (cin >> i)
v.push_back(i);
return 0;
}
Exercise 3.15
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string i;
vector<string> v;
while (cin >> i)
v.push_back(i);
return 0;
}
Exercise 3.16
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10, 42);
vector<int> v4{10};
vector<int> v5{10, 42};
vector<string> v6{10};
vector<string> v7{10, "Hi"};
//v1
cout << "The size of v1 is " << v1.size() << ".\n" << "The content(s) of v1:" << endl;
if (v1.size() != 0)
{
for (auto c : v1)
cout << c << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
//v2
cout << "The size of v2 is " << v2.size() << ".\n" << "The content(s) of v2:" << endl;
if (v2.size() != 0)
{
for (auto c : v2)
cout << c << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
//v3
cout << "The size of v3 is " << v3.size() << ".\n" << "The content(s) of v3:" << endl;
if (v3.size() != 0)
{
for (auto c : v3)
cout << c << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
//v4
cout << "The size of v4 is " << v4.size() << ".\n" << "The content(s) of v4:" << endl;
if (v4.size() != 0)
{
for (auto c : v4)
cout << c << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
//v5
cout << "The size of v5 is " << v5.size() << ".\n" << "The content(s) of v5:" << endl;
if (v5.size() != 0)
{
for (auto c : v5)
cout << c << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
//v6
cout << "The size of v6 is " << v6.size() << ".\n" << "The content(s) of v6:" << endl;
if (v6.size() != 0)
{
for (auto c : v6)
cout << c << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
//v7
cout << "The size of v7 is " << v7.size() << ".\n" << "The content(s) of v7:" << endl;
if (v7.size() != 0)
{
for (auto c : v7)
cout << c << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
return 0;
}
Output:
The size of v1 is 0.
The content(s) of v1:
None!
The size of v2 is 10.
The content(s) of v2:
0*0*0*0*0*0*0*0*0*0*
The size of v3 is 10.
The content(s) of v3:
42*42*42*42*42*42*42*42*42*42*
The size of v4 is 1.
The content(s) of v4:
10*
The size of v5 is 2.
The content(s) of v5:
10*42*
The size of v6 is 10.
The content(s) of v6:
**********
The size of v7 is 10.
The content(s) of v7:
Hi*Hi*Hi*Hi*Hi*Hi*Hi*Hi*Hi*Hi*
Exercise 3.17
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> text;
string word;
while (cin >> word)
{
for (auto &c : word)
c = toupper(c);
text.push_back(word);
}
for (auto i = 0; i < text.size(); i++)
{
if ((i + 1) % 8 == 0)
cout << text[i] << endl;
else cout << text[i] << " ";
}
return 0;
}
Exercise 3.19
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec(10);
for (auto &c : ivec)
c = 42;
for (auto output : ivec)
cout << output << " ";
cout << endl;
//This is Way 3 (of 4) according to my note.
return 0;
}
Exercise 3.20(a)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int i;
while (cin >> i)
ivec.push_back(i);
const auto s = ivec.size();
for (auto j = 0; j <= s / 2 - 1; j++)
cout << ivec[2 * j] + ivec[2 * j + 1] << endl;
if (s % 2 == 1)
cout << ivec[s-1] << endl;
return 0;
}
Exercise 3.20(b)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int i;
while (cin >> i)
ivec.push_back(i);
const auto s = ivec.size();
for (auto j = 0; j <= s / 2 - 1; j++)
cout << ivec[j] + ivec[s - j - 1] << endl;
if (s % 2 == 1)
cout << ivec[s / 2] << endl;
return 0;
}
Exercise 3.21
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10, 42);
vector<int> v4{ 10 };
vector<int> v5{ 10, 42 };
vector<string> v6{ 10 };
vector<string> v7{ 10, "Hi" };
vector<vector<int>> vint{ v1,v2,v3,v4,v5 };
vector<vector<string>> vstr{ v6,v7 };
unsigned i = 1, j = 6;
for (auto c = vint.cbegin(); c!= vint.cend(); ++c)
{
cout << "The size of v" << i << " is " << c->size() << ".\n" << "The content(s) of v" << i << ":" << endl;
if (c->cbegin() != c->cend())
{
// equivalent to vector<int>::const_iterator it
for (auto it = c->cbegin(); it != c->cend(); ++it)
cout << *it << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
i++;
}
for (auto c = vstr.cbegin(); c != vstr.cend(); ++c)
{
cout << "The size of v" << j << " is " << c->size() << ".\n" << "The content(s) of v" << j << ":" << endl;
if (c->cbegin() != c->cend())
{
// equivalent to vector<int>::const_iterator it
for (auto it = c->cbegin(); it != c->cend(); ++it)
cout << *it << "*";
cout << endl;
}
else cout << "None!" << endl;
cout << endl;
j++;
}
return 0;
}
Output:(同Exercise 3.16)
Exercise 3.22
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string line;
vector<string> text;
while (getline(cin, line))
text.push_back(line);
for (auto it = text.begin(); it != text.end() && !it->empty(); ++it)
for (auto &c : *it)
c = toupper(c);
for (auto c : text)
cout << c << " ";
cout << endl;
return 0;
}
Exercise 3.23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vint(10,42);
for (auto it = vint.begin(); it != vint.end(); ++it)
*it = 2 * *it;
for (auto c : vint)
cout << c << " ";
cout << endl;
return 0;
}
Exercise 3.24(a)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
int i;
while (cin >> i)
ivec.push_back(i);
for (auto it = ivec.begin(); it != ivec.end() && it != ivec.end() - 1; it += 2)
cout << (*it) + (*it + 1) << " ";
if (ivec.size() % 2 == 0) cout << endl;
else cout << i << endl;
return 0;
}
Exercise 3.24(b)
程序及思考见我的博客 关于 C++ Primer 中 Iterator Arithmatic(迭代器)的思考。
Exercise 3.25
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<unsigned> scores(11, 0);
unsigned grade;
cout << "Full mark is 100." << endl;
while (cin >> grade)
{
if (grade <= 100)
++ *(scores.begin() + grade / 10);
else
{
cerr << "Error!" << endl;
return -1;
}
}
for (auto it = scores.begin(); it != scores.end() - 1; ++it)
cout << "[" << 10 * (it - scores.begin()) << "," << 10 * (it - scores.begin() + 1) << ")" << ": " << *it << endl;
cout << "100: " << *(--scores.end()) << endl;
return 0;
}
Example 1:
42 65 95 100 39 67 95 76 88 76 83 92 76 93
Output:
[0,10): 0
[10,20): 0
[20,30): 0
[30,40): 1
[40,50): 1
[50,60): 0
[60,70): 2
[70,80): 3
[80,90): 2
[90,100): 4
100: 1
Example 2:
56 86 78 90 58 131 89 55 100 98
Output:
Error!
Exercise 3.26
程序以及调试本Exercise的相关内容可见我的【心得体会】。
Exercise 3.31
#include <iostream>
#include <cstddef>
using namespace std;
int main()
{
int a1[10] = { 0,1,2,3,4,5,6,7,8,9 };
// another way
int a2[10];
for (size_t i = 0; i != 10; i++)
a2[i] = i;
for (auto c : a1)
cout << c << " ";
cout << endl;
for (auto c : a2)
cout << c << " ";
cout << endl;
return 0;
}
Exercise 3.32(a)
#include <iostream>
#include <cstddef>
using namespace std;
int main()
{
int a1[10] = { 0,1,2,3,4,5,6,7,8,9 };
int a3[10];
for (size_t i = 0; i != 10; i++)
a3[i] = a1[i];
for (auto c : a1)
cout << c << " ";
cout << endl;
for (auto c : a3)
cout << c << " ";
cout << endl;
return 0;
}
Exercise 3.32(b)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a1(10);
for (unsigned i = 0; i != 10; i++)
a1[i] = i;
vector<int> a2 = a1;
for (auto c : a1)
cout << c << " ";
cout << endl;
for (auto c : a2)
cout << c << " ";
cout << endl;
return 0;
}
Exercise 3.35
#include <iostream>
using namespace std;
int main()
{
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int* p = a;
while (*p != a[10])
{
*p = 0;
++p;
}
for (auto c : a)
cout << c << " ";
cout << endl;
}
Exercise 3.36(a)
#include <iostream>
using namespace std;
int main()
{
int a1[5] = { 0,1,2,3,4 };
int a2[5] = { 0,1,2,3,4 };
int a3[6] = { 0,1,2,3,4,5 };
int a4[5] = { 0,2,2,3,4 };
if (sizeof(a1) != sizeof(a2)) //Change the array when needed.
cout << "a1 is unequal to a2." << endl;
else
{
int* pointer1 = begin(a1);
int* pointer2 = begin(a2);
unsigned s = 0;
int array_size = sizeof(a1) / sizeof(int); // In this compiler, sizeof(int) is 4.
cout << array_size << endl;
for (int i = 0; i != array_size; i++)
{
cout << *pointer1 << " " << *pointer2 << endl;
if (*pointer1 == *pointer2)
s++;
++pointer1;
++pointer2;
}
if(s== array_size)
cout<< "a1 is equal to a2." << endl;
else cout << "a1 is unequal to a2." << endl;
}
return 0;
}
Exercise 3.36(b)
程序及思考见我的博客 关于 C++中 输入多行不定数量数字 的思考。
Exercise 3.39
Way I:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1 = "A string example";
string s2 = "A different string";
if (s1 == s2) cout << "s1 is equal to s2." << endl;
else
{
if (s1 > s2) cout << "s1 is greater than s2." << endl;
else cout << "s2 is greater than s1." << endl;
}
return 0;
}
Way II:
#include <iostream>
#include <string.h> // C-Style Strings
using namespace std;
int main()
{
const char ca1[] = "A string example";
const char ca2[] = "A different string";
if (strcmp(ca1, ca2) == 0) cout << "ca1 is equal to ca2." << endl;
else
{
if (strcmp(ca1, ca2) > 0) cout << "ca1 is greater than ca2." << endl;
else cout << "ca2 is greater than ca1." << endl;
}
return 0;
}
Exercise 3.40
Way I:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1("Teddy");
string s2("Bear");
string s3 = s1 + " " + s2;
cout << s3 << endl;
return 0;
}
Way II:
此程序等详情见我的博客 【笔记】 C++中 strcpy_s 和 strcat_s(C++ Primer Exercise 3.40)
Exercise 3.41
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
int a[] = { 0,1,2,3,4,5,6,7,8,9 };
vector<int> vint(begin(a), end(a));
for (auto c : vint)
cout << c << " ";
cout << endl;
return 0;
}
Exercise 3.42
Way I:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int num;
vector<int> vint;
cout << "This vector should contain no more than 1000 numbers and no 0 is allowed." << endl;
while (cin >> num)
vint.push_back(num);
int arr[1000];
for (auto& c : arr)
c = 0;
for (unsigned i = 0; i != vint.size(); i++)
arr[i] = vint[i];
for (auto c : arr)
{
if (c != 0)
cout << c << " ";
}
cout << endl;
return 0;
}
Way II:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int num;
vector<int> vint;
cout << "This vector should contain no more than 1000 numbers and no 0 is allowed." << endl;
while (cin >> num)
vint.push_back(num);
int arr[1000];
for (auto& c : arr)
c = 0;
for (auto p = vint.begin(); p != vint.end(); ++p)
arr[p - vint.begin()] = *p;
for (auto c : arr)
{
if (c != 0)
cout << c << " ";
}
cout << endl;
return 0;
}
Exercise 3.43(a)
#include <iostream>
using namespace std;
int main()
{
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (int (&c1)[4] : a1)
for (int& c2 : c1)
cout << c2 << " ";
cout << endl;
return 0;
}
关于Multidimentional Arrays 的 initialization(初始值)见我的【笔记】
Exercise 3.43(b)
#include <iostream>
#include <cstddef>
using namespace std;
int main()
{
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (size_t i = 0; i != 3; i++)
for (size_t j = 0; j != 4; j++)
cout << a1[i][j] << " ";
cout << endl;
return 0;
}
Exercise 3.43(c)
#include <iostream>
using namespace std;
int main()
{
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (int(*p)[4] = a1; p != a1 + 3; ++p)
for (int* q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}
有时觉得第9行判断条件有些绕。事实上,q是p的pointer,※p就是与q同级,q完全可以用※p+k(k为int距离)代替,如是便比较好理解其中有无※的区别了。(此处用※代替pointer的标记*)
Exercise 3.44(a)
#include <iostream>
using namespace std;
int main()
{
using int_array_reference = int(&) [4];
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (int_array_reference c1 : a1)
for (int c2 : c1)
cout << c2 << " ";
cout << endl;
return 0;
}
Exercise 3.44(b)
同3.43(b)
Exercise 3.44(c)
#include <iostream>
using namespace std;
int main()
{
using int_array = int[4];
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (int_array* p = a1; p != a1 + 3; ++p)
for (int* q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}
Exercise 3.45(a)
#include <iostream>
using namespace std;
int main()
{
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (auto& c1 : a1)
for (auto& c2 : c1)
cout << c2 << " ";
cout << endl;
return 0;
}
Exercise 3.45(b)
#include <iostream>
using namespace std;
int main()
{
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (auto i = 0; i != 3; i++)
for (auto j = 0; j != 4; j++)
cout << a1[i][j] << " ";
cout << endl;
return 0;
}
Exercise 3.45(c)
#include <iostream>
using namespace std;
int main()
{
int a1[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
for (auto* p = a1; p != a1 + 3; ++p)
for (auto* q = *p; q != *p + 4; ++q)
cout << *q << " ";
cout << endl;
return 0;
}
Next Chapter
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 4(第四章)
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。