C++ Primer(第五版) 第六章练习答案
目录
- C++ Primer(第五版) 第六章练习答案
-
-
- 6.1
- 6.2
- 6.3
- 6.4
- 6.5
- 6.6
- 6.7
- 6.8
- 6.9cc
- 6.9h
- 6.9cpp
- 6.10
- 6.11
- 6.12
- 6.13
- 6.14
- 6.15
- 6.16
- 6.17
- 6.18
- 6.19
- 6.20
- 6.21
- 6.22
- 6.23
- 6.24
- 6.25
- 6.26
- 6.27
- 6.28
- 6.29
- 6.30
- 6.31
- 6.32
- 6.33
- 6.34
- 6.35
- 6.36
- 6.37
- 6.38
- 6.39
- 6.40
- 6.41
- 6.42
- 6.43
- 6.44
- 6.45
- 6.46
- 6.47
- 6.48
- 6.49
- 6.50
- 6.51
- 6.52
- 6.53
- 6.54
- 6.55
- 6.56
6.1
6.2
6.3
#include
using std::cout; using std::endl;
int fact(int val)
{
int ret = 1;
while (val)
ret *= val--;
return ret;
}
int main()
{
cout << fact(5) << endl;
return 0;
}
6.4
#include
using std::cout; using std::endl; using std::cin;
int fact()
{
int val;
int ret = 1;
cin >> val;
if (val > 0)
while (val)
ret *= val--;
else if (val < 0)
while (val)
ret *= val++;
else
return 0;
return ret;
}
int main()
{
cout << fact() << endl;
return 0;
}
6.5
#include
using std::cout; using std::endl; using std::cin;
int absolute(int x)
{
if (x >= 0)
return x;
return -x;
}
int main()
{
int x;
while (cin >> x)
cout << absolute(x) << endl;
return 0;
}
6.6
int func(int x)
{
int y;
static int z;
return ++z;
}
6.7
#include
int func() {
static int x; return x++; }
int main()
{
for (int i = 0; i < 10; ++i)
printf("%d\n", func());
return 0;
}
6.8
int fact(int val);
int fact();
int absolute(int x)
6.9cc
#include "6_9.h"
int func()
{
static int x;
return ++x;
}
6.9h
#ifndef _6_9_h
#define _6_9_h
int func();
#endif
6.9cpp
#include
#include "6_9.h"
int main()
{
std::cout << func() << std::endl;
return 0;
}
6.10
#include
using namespace std;
void func(int *x, int *y);
int main()
{
int n = 10, m = 20;
func(&n, &m);
cout << n << " " << m << endl;
return 0;
}
void func(int *x, int *y)
{
int p = *x;
*x = *y;
*y = p;
}
6.11
#include
using namespace std;
void reset(int &x);
int main()
{
int x = 30;
reset(x);
cout << x << endl;
return 0;
}
void reset(int &x)
{
x = 20;
}
6.12
#include
using namespace std;
void func(int &x, int &y);
int main()
{
int n = 10, m = 20;
func(n, m);
cout << n << " " << m << endl;
return 0;
}
void func(int &x, int &y)
{
int p = x;
x = y;
y = p;
}
6.13
6.14
6.15
6.16
bool is_empty(const string &s) {
return s.empty(); }
6.17
#include
#include
using namespace std;
void toupper(string &s);
bool isupper(const string &s);
int main()
{
string s("AZng XIUU");
if (isupper(s))
toupper(s);
cout << s << endl;
return 0;
}
void toupper(string &s)
{
for (auto &c : s)
if (c >= 'A' && c <= 'Z')
c += 32;
}
bool isupper(const string &s)
{
for (const auto &c : s)
if (c >= 'A' && c <= 'Z')
return true;
return false;
}
6.18
bool compare(const matrix&, const matrix&);
vector<int>::iterator change_val(int, vector<int>::iterator);
6.19
6.20
6.21
int func(int x, const int *y)
{
if (x > *y)
return x;
return *y;
}
6.22
#include
using namespace std;
void func(int **x, int **y)
{
int *p = *x;
*x = *y;
*y = p;
}
void func(int *&x, int *&y)
{
int *p = x;
x = y;
y = p;
}
int main()
{
int n = 10, m = 20, *x = &n, *y = &m;
cout << x << " " << y << endl;
func(x, y);
cout << x << " " << y << endl;
return 0;
}
6.23
#include
using namespace std;
void print(int x)
{
cout << x << endl;
}
void print(int *arr, size_t size)
{
for (size_t i = 0; i != size; ++i)
cout << arr[i] << endl;
}
int main()
{
int i = 0, j[] = {
0, 1, 2, 3};
print(i);
print(j, sizeof(j) / sizeof(int));
return 0;
}
6.24
void print(const int ia[10])
{
for (size_t i = 0; i != 10; ++i)
cout << ia[i] << endl;
}
void print(const int ia[], size_t size)
{
for (decltype(size) i = 0; i != size; ++i)
cout << ia[i] << endl;
}
6.25
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
if (argc > 1)
{
string s;
for (int i = 1; i < argc; ++i)
s += argv[i];
cout << s << endl;
}
return 0;
}
6.26
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
if (argc > 1)
for (int i = 1; i < argc; ++i)
cout << argv[i] << endl;
return 0;
}
6.27
#include
#include
#include
using namespace std;
int sum(const initializer_list<int> &il);
int main(int argc, char *argv[])
{
initializer_list<int> il{
1, 2, 3, 4, 5};
cout << sum({
1, 2, 3, 4, 5, 6, 7, 8, 9}) << endl;
return 0;
}
int sum(const initializer_list<int> &il)
{
int sum = 0;
for (auto &i : il)
sum += i;
return sum;
}
6.28
6.29
6.30
#include
#include
#include
using namespace std;
bool str_subrange(const string &str1, const string &str2);
int main()
{
return 0;
}
bool str_subrange(const string &str1, const string &str2)
{
if (str1.size() == str2.size())
return str1 == str2;
auto size = (str1.size() < str2.size()) ? str1.size() : str2.size();
for (decltype(size) i = 0; i != size; ++i)
{
if (str1[i] != str2[i])
return;
}
}
6.31
6.32
6.33
#include
#include
using namespace std;
void putvec(const vector<int> &vec, vector<int>::size_type size);
int main()
{
vector<int> vec{
1, 2, 3, 4, 5};
putvec(vec, vec.size());
return 0;
}
void putvec(const vector<int> &vec, vector<int>::size_type size)
{
if (size != 1)
putvec(vec, size - 1);
cout << vec[size - 1] << endl;
}
6.34
6.35
6.36
#include
using std::string;
string (&func())[10];
6.37
#include
using std::string;
string (&func1())[10];
using arrS = string[10];
arrS &func2();
auto func3() -> string(&)[10];
int arr[10];
decltype(arr) &func4();
6.38
decltype(odd) &arrPtr(int i)
6.39
6.40
6.41
6.42
#include
#include
using namespace std;
string make_plural(size_t ctr, const string &word, const string &ending = "s");
int main()
{
cout << make_plural(1, "success") << " "
<< make_plural(2, "failure") << endl;
return 0;
}
string make_plural(size_t ctr, const string &word, const string &ending)
{
return (ctr > 1) ? word + ending : word;
}
6.43
6.44
inline
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
6.45
6.46
6.47
#include
#include
#include
using namespace std;
void putvec(const vector<int> &vec, vector<int>::size_type size);
int main()
{
vector<int> vec{
1, 2, 3, 4, 5};
putvec(vec, vec.size());
return 0;
}
void putvec(const vector<int> &vec, vector<int>::size_type size)
{
#ifdef DEBUG
cout << "size: "<< size << endl;
#endif
if (size != 1)
putvec(vec, size - 1);
cout << vec[size - 1] << endl;
}
6.48
6.49
6.50
6.51
#include
using namespace std;
void f() {
puts("void f()"); }
void f(int) {
puts("void f(int)"); }
void f(int, int) {
puts("void f(int, int)"); }
void f(double, double = 3.14) {
puts("void f(double, double = 3.14)"); }
int main()
{
f();
f(true);
f(1, 'a');
f(3.34);
return 0;
}
6.52
6.53
6.54
#include
#include
using namespace std;
int func(int, int) {
static int i; cout << ++i << endl; return 0; }
int main()
{
vector<decltype(func)*> vec;
return 0;
}
6.55
#include
#include
using namespace std;
int func1(int a, int b) {
return a + b; }
int func2(int a, int b) {
return a - b; }
int func3(int a, int b) {
return a * b; }
int func4(int a, int b) {
return a / b; }
int func5(int a, int b) {
return a % b; }
int main()
{
vector<int(*)(int, int)> vec{
func1, func2, func3, func4, func5};
return 0;
}
6.56
#include
#include
using namespace std;
int func1(int a, int b) {
return a + b; }
int func2(int a, int b) {
return a - b; }
int func3(int a, int b) {
return a * b; }
int func4(int a, int b) {
return a / b; }
int func5(int a, int b) {
return a % b; }
int main()
{
vector<int(*)(int, int)> vec{
func1, func2, func3, func4, func5};
for (int i = 0; i < vec.size(); ++i)
cout << vec[i](10, 10) << endl;
return 0;
}