C++Primer第五版——習題答案詳解(五)



習題答案目錄:https://www.cnblogs.com/Mered1th/p/10485695.html

第6章 函數


練習6.4

#include<iostream>
using namespace std;
int fact(int x) {
	if (x == 1) return x;
	else return x * fact(x - 1);
}

int main() {
	int x;
	cout << "Please input a number:\n";
	while (cin >> x) {
		int ans = fact(x);
		cout << ans << endl;
	}
	system("pause");
	return 0;
}

練習6.5

#include<iostream>
using namespace std;
int abs(int x) {
	return x >= 0 ? x : (-x);
}

int main() {
	int x;
	cout << "Please input a number:\n";
	while (cin >> x) {
		cout << abs(x) << endl;
	}
	system("pause");
	return 0;
}

練習6.7

size_t count_calls(){
    static size_t ctr=0;
    return ctr++;
}

練習6.10

#include<iostream>
using namespace std;
void swap(int *x, int *y) {
	int temp = *x;
	*x = *y;
	*y = temp;
}

int main() {
	int x, y;
	cin >> x >> y;
	cout << x << y << endl;
	swap(x, y);
	cout << x << y << endl;
	system("pause");
	return 0;
}

練習6.12

void swap(int &x, int &y) {
	int temp = x;
	x = y;
	y = temp;
}

練習6.17
不相同,因為轉大寫需要修改傳入的字符串需要加&

#include<iostream>
#include<cctype>
#include<string>
using namespace std;
bool is_upper(const string s) {
	for (auto i : s) {
		if (isupper(i)) return true;
	}
	return false;
}

void to_upper(string &s) {
	for (auto &i : s) {
		i = toupper(i);
	}
}

int main() {
	string s = "abdcedA";
	if (is_upper(s)) cout << "is upper" << endl;
	else cout << "Not upper" << endl;
	to_upper(s);
	cout << s << endl;
	system("pause");
	return 0;
}

練習6.18

bool compare(matrix &a, matrix &b);
vector<int>::iterator change_val(int a, vector<int>::iterator);

練習6.19
a.不合法
b.合法
c.合法
d.合法

練習6.21

#include<iostream>
#include<cctype>
#include<string>
#include<vector>
using namespace std;
int Big(int x, int *y) {
	return x > *y ? x : *y;
}
int main() {
	int x = 3, y = 2;
	cout << Big(x, &y) << endl;
	system("pause");
	return 0;
}

練習6.22
函數傳參必須加引用,不加結果不正確。

#include<iostream>
#include<cctype>
#include<string>
#include<vector>
using namespace std;
void swap(int *&x, int *&y) {
	int *temp = x;
	x = y;
	y = temp;
}
int main() {
	int x = 3, y = 2;
	int *px = &x, *py = &y;
	swap(px, py);
	cout << x << ", " << y << endl;
	cout << *px << ", " << *py << endl;
	system("pause");
	return 0;
}

6.23

#include<iostream>
using namespace std;
void print(const int *pi) {
	cout << *pi << endl;
}
void print(const int *beg, const int *end) {
	while (beg != end) {
		cout << *beg++ << " ";
	}
	cout << endl;
}
void print(const int ia[], size_t size) {
	for (size_t i = 0;i != size;i++) {
		cout << ia[i] << " ";
	}
	cout << endl;
}
void print2(const int (&arr)[2]) {
	for (size_t i = 0;i != 2;i++) {
		cout << arr[i] << " ";
	}
	cout << endl;
}

int main() {
	int i = 0, j[2] = { 0,1 };
	print(&i);
	print(begin(j), end(j));
	print(j, 2);
	print2(j);
	system("pause");
	return 0;
}

練習6.24
如果數組大小小於10,會造成數組越界。除此以外數組不能復制成形參。改成指針或引用。

練習6.25
用命令行當做輸入,輸入的argv[0]保存程序的名字,后面的才是實參

#include<iostream>
#include<string>
using namespace std;

int main(int argc,char const **argv)
{
	cout << argv[1] << endl;
	cout << argv[2] << endl;
	cout << string(argv[1])+string(argv[2]) << endl;
	system("pause");
	return 0;
}

練習6.26

#include<iostream>
#include<string>
using namespace std;

int main(int argc,char const **argv)
{
	for (int i = 0;i < argc;i++) {
		cout << argv[i] << endl;
	}
	system("pause");
	return 0;
}

練習6.27

#include<iostream>
#include<string>
#include<initializer_list>
using namespace std;
int count_Sum(initializer_list<int> il) {
	int sum = 0;
	for (auto beg = il.begin();beg != il.end();beg++) {
		sum += *beg;
	}
	return sum;
}
int main(int argc,char const **argv)
{
	cout << count_Sum({ 1,2,3,4,5 }) << endl;
	system("pause");
	return 0;
}

練習6.28
string類型

練習6.29
如果實參個數多且占用容量大,用引用更好。

練習6.31
返回的引用是局部對象的引用時無效;返回的常量引用是局部常量對象的引用時。

練習6.32
合法,將數組重置為0-9

練習6.33

#include<iostream>
#include<string>
#include<vector>
using namespace std;
void print(vector<int>::iterator vec_begin, vector<int>::iterator vec_end) {
	if (vec_begin != vec_end) {
		cout << *vec_begin << " ";
		return print(++vec_begin, vec_end);
	}
}
int main(int argc,char const **argv)
{	
	vector<int> vec = { 1,2,3,4,6 };
	print(vec.begin(), vec.end());
	system("pause");
	return 0;
}

練習6.34
如果傳入的參數小於0,會一直調用不停止

練習6.35
會一直調用本身,無限循環

練習6.36

string (&fun(int i)[10];

練習6.39
a.合法
b.不合法
c.合法

練習6.40
a.正確
b.錯誤,一旦某個形參被賦予了默認值,它后面的所有形參都必須有默認值

練習6.41
a.非法,第一個形參沒有默認值,必須給實參
b.合法
c.合法,'*'轉換為整形

練習6.42

#include<iostream>
#include<string>
#include<vector>
using namespace std;
string make_plural(size_t ctr, const string &word, const string &ending = "s") {
	return (ctr > 1) ? word + ending : word;
}
int main()
{	
	cout << make_plural(2, "success", "es") << endl;
	cout << make_plural(2, "failure") << endl;
	system("pause");
	return 0;
}

練習6.43
a.放在頭文件中,內聯函數在程序中多個定義必須完全一致。
b.放在頭文件中,函數聲明。

練習6.44

#include<iostream>
#include<string>
#include<vector>
using namespace std;
inline bool isShorter(const string &s1, const string &s2) {
	return s1.size() < s2.size();
}
int main()
{	
	string s1 = "abc", s2 = "abcd";
	cout << isShorter(s1, s2) << endl;
	system("pause");
	return 0;
}

練習6.46
不能,返回值不是常量表達式

練習6.47
在項目屬性中設置命令行/DNDEBUG就不會出現 #ifndef....#endif、assert中的內容。

#include<iostream>
#include<string>
#include<vector>
#include<cassert>
using namespace std;
void read_vi(vector<int>::const_iterator iterator_begin, vector<int>::const_iterator iterator_end) {
#ifndef NDEBUG
	cerr << iterator_end - iterator_begin << __func__ << " " << __FILE__ << " "
		<< __LINE__ << " " << __TIME__ << " " << __DATE__ << endl;
#endif // !NDEBUG
	if (iterator_begin != iterator_end) {
		cout << *iterator_begin << " ";
		return read_vi(++iterator_begin, iterator_end);
	}
	else {
		cout << endl;
		return;
	}
}
int main()
{	
	vector<int> v{ 1,2,3,4,5 };
	read_vi(v.begin(), v.end());
	system("pause");
	return 0;
}

練習6.48
合理,輸入結束時終止程序

練習6.50
a.非法,二義性
b.匹配void f(int)
c.匹配void f(int,int)
d.匹配void f(double,double)

練習6.53
a.合法
b.合法
c.不合法,重復定義

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int calc(int&, int&);
int calc(const int&, const int&);
char calc2(char*, char*);
char calc2(const char*, const char*);
int calc3(char*, char*);
int calc3(char* const, char* const);
int main()
{	
	calc(1, 2);
	system("pause");
	return 0;
}
int calc(int&a, int&b) {
	return a + b;
}
int calc(const int&a, const int&b) {
	return a + b;
}
char calc2(char *a, char *b) {
	return *a;
}
char calc2(const char *a, const char *b) {
	return *a;
}
int calc3(char *a, char *b) {
	return a - b;
}
int calc3(char* const a, char* const b) {
	return a - b;
}

練習6.54

vector<int(*)(int, int)> vf;

練習6.55

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int add(int a, int b) {
	return a + b;
}
int sub(int a, int b) {
	return a - b;
}
int multi(int a, int b) {
	return a * b;
}
int divide(int a, int b) {
	return a / b;
}
int main()
{	
	vector<int(*)(int, int)> vf{ add,sub,multi,divide };
	for (const auto e : vf) cout << e(4, 2) << endl;
	system("pause");
	return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM