1. 程式人生 > >C++ Primer 第五版第六章習題答案

C++ Primer 第五版第六章習題答案

書籍版本:2019年9月第一版;王剛 楊巨峰譯;電子工業出版社

編譯器 : win10  && VS2015

6.1

形參是定義在函式中的虛擬值,實參是形參的初始值。

6.2

a.  返回值是int型別,不能return一個string

b.  沒有定義返回值型別

c.  兩個引數名不能一樣

d.  沒有加{}

6.3

 

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int fact(int val)
{
	int sum = 1;
	for (int i = 1; i <= val; i++)
	{
		sum = sum * i;
	}
	return sum;
}

int main()
{
	int s = fact(5);
	cout << s << endl;

	system("pause");
}



6.4

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int fact(int val)
{
	int sum = 1;
	for (int i = 1; i <= val; i++)
	{
		sum = sum * i;
	}
	return sum;
}

int main()
{
	cout << "請輸入一個數字:";
	int a;
	cin >> a;
	int s = fact(a);
	cout << a << "的階乘值為:" << s << endl;

	system("pause");
}



6.5

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int numfab(int val)
{
	return fabs(val);
}

int main()
{
	cout << "請輸入一個數字:";
	int a;
	cin >> a;
	int s = numfab(a);
	cout << a << "的絕對值為:" << s << endl;

	system("pause");
}



6.6
區域性變數包括 形參和靜態區域性變數。 
形參屬於自動物件,在函式開始時為形參申請儲存空間,在函式終止時形參被銷燬。 
區域性靜態變數,在程式第一次經過物件定義語句時初始化,直到程式終止時才被銷燬。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int numfab(int val)//形參
{
	static int count = 0;// 區域性靜態變數
	count++;
	int num = fabs(val);// 區域性變數
	return num;
}

int main()
{
	cout << "請輸入一個數字:";
	int a;
	cin >> a;
	int s = numfab(a);
	cout << a << "的絕對值為:" << s << endl;

	system("pause");
}



6.7

#include "stdafx.h"
#include <iostream>
using namespace std;

int num()
{
	static int count = 0;
	if (count<1)
	{
		return count++;
	}
	else
		return count;
}

int main()
{
	for (int i = 0; i < 10; i++)
	{
		cout << num() << endl;
	}

	system("pause");
}

6.8

//這是Chapter6.h檔案的內容
#pragma once
class CChapter6
{
public:
	CChapter6();
	~CChapter6();

private:
	int f();
	int f2(int i);
	int calc(int v1, int v2);
	double square(double x);
};

 

// 這是Chapter6.cpp檔案中的內容
#include "stdafx.h"
#include "Chapter6.h"
#include <iostream>
using namespace std;

CChapter6::CChapter6()
{
}


CChapter6::~CChapter6()
{
}

int CChapter6::f()
{
	string s;
	return 0;
}

int CChapter6::f2(int i)
{

}

int CChapter6::calc(int v1, int v2)
{

}

double CChapter6::square(double x)
{

}

6.9

如上題

6.10

#include "stdafx.h"
#include <iostream>
using namespace std;

void ExChangeNum(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

int main()
{
	int a = 111, b = 999;
	cout << "交換之前: a: " << a << ", b: " << b << endl;
	ExChangeNum(&a, &b);
	cout << "交換之後: a: " << a << ", b: " << b << endl;

	system("pause");
}

6.11

#include "stdafx.h"
#include <iostream>
using namespace std;

void ExChangeNum(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void Reset(int& a, int& b)
{
	int tmp = a;
	a = b;
	b = tmp;
}

int main()
{
	int a = 111, b = 999;
	cout << "交換之前: a: " << a << ", b: " << b << endl;
	Reset(a, b);
	cout << "交換之後: a: " << a << ", b: " << b << endl;

	system("pause");
}

6.12

程式碼同上。個人覺得引用更好用。

6.13

void f(T)是傳入一個T型別的變數

void f(&T)是傳入一個T型別的引用

6.14

當形參的值不能改就不能傳引用;當形參是一個很難拷貝的型別並且可能對其值修改時傳引用;

6.15

s是常量引用是表明函式中不會改變其值;

s與occurs是引用是為了取到對應的物件;

s是普通引用可能會造成其值被改變;

occurs是常量引用則無法改變其值,就無法得到想要的結果。

6.16

只能接受非常量string,改引數為const string&

6.17

#include "stdafx.h"
#include <iostream>
using namespace std;

bool IsUpExist(const string& s)
{
	for (int i = 0; i < s.size(); i++)
	{
		if (isupper(s[i]))
		{
			return true;
		}
	}
	return false;
}

void StringToLower(string& s)
{
	for (int i = 0; i < s.size(); i++)
	{
		if ( (s[i]) )
		{
			s[i] = tolower(s[i]);
		}
	}
}

int main()
{
	string a = "aaaaaHaaaa";
	string b = "AHFBDIALFH";
	cout << "修改之前a: " << a << endl;
	cout << "修改之前b: " << b << endl;
	bool hasUp = IsUpExist(a);
	StringToLower(b);
	cout << hasUp << endl;
	cout << "修改之後b: " << b << endl;

	system("pause");
}

6.18

a.  bool Compare(matrix& m1, matrix& m2);

b.  vectoe<int> change_val(int a, vector<int> iter);

6.19

a不合法,只能傳一個引數

b合法,但第二個引數最好不要傳常量

c合法

d不合法,最後一個引數不是整形

6.20

傳入引數的值不能被改變時傳入常量;

本該是常量設成了普通引用,有可能會改變其值與原目的不符,且無法傳入常量引用作為引數

6.21