C++ primer 第五版 第六章练习答案

6.3

#include<iostream>
using namespace std;
int fact(int ret) {
	int val = 1;
	while (ret > 1) {
		val *= ret--;
	}
	return val;	
}
int main() {
	int j = fact(5);
	cout << j << endl;
}

6.4

#include<iostream>
using namespace std;
int fact(int ret) {
	int val = 1;
	while (ret > 1) {
		val *= ret--;
	}
	return val;	
}
int main() {
	cout << "请输入一个数" << endl;
	int num = 0;
	cin >> num;
	int j = fact(num);
	cout << "该数的阶乘为" << j << endl;
}

6.5

#include<iostream>
using namespace std;
int abbs(int num) {
	num = abs(num);
	return num;
}
int main() {
	int a = 0;
	cin >> a;
	cout << abbs(a) << endl;
}

6.6

#include<iostream>
using namespace std;//局部变量   形参    局部静态变量
int bbb(int a) {//a为形参   也是局部变量
	int ccc = 0;//ccc为局部静态变量
	++ccc;
	return ccc;
}
int main() {
	int m = 5;
	int j = 0;
	while (m) {
		--m;
		j = bbb(1);
	}
	cout << j << endl;
	//cout << ccc << endl; //局部静态变量不是指在函数外可以调用这个值,而是在这个值存在,与局部变量对比,其值不会被销毁
}

6.7

#include<iostream>
using namespace std;
int bbb() {
	static int cc = -1;
	++cc;
	return cc;
}
int main() {
	int m;
	cin >> m;
	int j = 0;
	while (m) {
		--m;
		j = bbb();
	}
	cout << j;
}

6.8

#pragma once
int f();
int fact();
int change();



6.9

头文件

#pragma once
int fact(int ret);


fact函数
#include "标头.h"
int fact(int ret) {
int S = 1;
while (ret != 1) {
S *= ret;--ret;
}
return S;
}




main函数
#include "标头.h"
#include <iostream>
using namespace std;
int main() {
int n = 0;
cin >> n;
int b;
b=fact(n);
cout << b << endl;
}

6.10

#include<iostream>
using namespace std;
void change(int *a, int*b) {
	int t;
	t = *a;
	*a = *b;
	*b = t;
}
int main() {
	int q = 5;
	int r = 10;
	int *f = &q;
	int *k = &r;
	change(f, k);
	cout << "f的值为" << q;
	cout << "k的值为" <<r;
}

6.11

#include <iostream>
#include <string>
using namespace std;
void reset(int &s) {
	s = 0;
}
int main() {
	int b = 48;
	int &y = b;
	reset(y);
	cout << b << endl;
}

6.12
#include <iostream>
#include <string>
using namespace std;
void change(int &a, int &b) {
	int t = a;
	a = b;
	b = t;
}
int main() {
	int q = 30;
	int p = 20;
	int &qq = q;
	int &pp = p;
	change(qq, pp);
	cout << "q的值为" << q;
	cout << "p的值为" << p;
}

------自定义函数find_char自己的理解

#include <iostream>
#include <string>
using namespace std;
//定义函数返回字符串中某个字母第一次出现的位置以及出现的次数
string::size_type find_char(const string &s, char c, string::size_type &occurs) {
	auto ret = s.size();
	occurs = 0;
	int Cnt = 0;
	for (auto q : s) {
		++Cnt;
		if (q == c) {
			if (ret == s.size())
				ret = Cnt;
			++occurs;
		}
	}
	return ret;
}
int main() {
	string text = "";
	cin >> text;
	const string &ss = text;
	char f = 'q';
	string::size_type occurs = 45;
	string::size_type &t = occurs;
	int b=find_char(ss, f, t);
	cout << "q的出现次数为" << occurs<<endl;
	cout << "q第一次出现的位置为" << b;
}

6.17

#include <iostream>
#include <string>
using namespace std;
bool if_Cap(const string &s) {
	for (auto q : s) {
		int a = q;
		if (a>=65&&a<=90){
			return false;
		}
	}
	return true;
}
void Cap(string &s) {
	for (auto &q : s) {
		int a = q;
		if (a >= 97 && a <= 122) {
			q = a - 32;
		}
	}
}
int main() {
	string text = "";
	cin >> text;
	const string &s = text;
	if (if_Cap(s)) {
		cout << "无大写字母" << endl;
	}
	else {
		cout << "有大写字母" << endl;
	}
	string &f = text;
	Cap(f);
	cout << text << endl;
}

6.18

#pragma once
bool compare(const matrix &x, const matrix &y);
int change_val(int x,vector<int>itrtator it);

6.21

#include <iostream>
using namespace std;
int Max(int a,int*b) {
	if (a>* b) {
		return a;
	}return *b;
}
int main() {
	int a = 10;
	int b = 20;
	int *c = &b;
	cout << Max(a, c) << endl;
}

6.22

#include <iostream>
using namespace std;
void change(int **a, int **b) {
	int **c=&*a;
	*c = *a;
	*a = *b;
	*b = *c;
}
int main() {
	int a = 20;
	int b = 60;
	int *aa = &a;
	int *bb = &b;
	int **aaa = &aa;
	int **bbb = &bb;
	change(aaa, bbb);
	cout << "*aa的值为" << *aa;
	*aa = 98;
	cout << b << endl;
}

6.23

#include <iostream>
using namespace std;
int print(int *f, int n) {
	for (int i = 0; i < n; ++i) {
		cout << f[i] << endl;
	}
}

6.25

#include <iostream>
#include <string>
using namespace std;
int main(int argc,char **argv) {
	string str1 = "";
	string str2 = "";
	str1 = argv[1];
	str2 = argv[2];
	str1 += str2;
	cout << str1;
}

6.26

#include <iostream>
#include <string>
using namespace std;
int main(int argc,char **argv) {
	for (int i = 0; i < argc; ++i) {
		cout << argv[i] << endl;
	}
}

6.27

#include <iostream>
#include <initializer_list>
using namespace std;
int Sum(initializer_list<int>lst) {
	int sum = 0;
	for (auto q : lst) {
		sum += q;
	}
	return sum;
}

6.30

#include <iostream>
#include <string>
using namespace std;
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();//得到较短的string长度
	for (decltype(size)i = 0; i != size; ++i) {
		if (str1[i] != str2[i])
			return 0;
	}
	return 0;
}
int main() {
	string text = "i love you";
	string text1 = "me too";
	cout << str_subrange(text, text1);
}

6.33

#include <iostream>
#include <string>
#include <vector>
using namespace std;
//递归输出vector对象
int cc(vector<int>q) {
	if (q.size() != 0) {
		cout << q.back()<<endl;
		q.pop_back();
		cc(q);
	}
	return 0;
}
int main() {
	vector<int>ivec = { 0,1,2,3,4 };
	cc(ivec);
}

6.36

#include <iostream>
#include <string>
using namespace std;
/*string odd[] = { "1","2","3","4","5" };
string &k(string a[]) {

}*/
string(*fun(string i))[10];




//最内层string i表示调用函数时需要的是string类型的实参,外面的*表示对函数调用的结果执行
解引用操作,后面的[10]表示调用将得到一个维度为10的数组,最前面的string则表示返回数组元素的类型为sting类型

6.37

1----使用尾置返回类型

#include <iostream>
#include <string>
using namespace std;
auto func(string i)->string(*)[10];

2----使用decltype

#include <iostream>
#include <string>
using namespace std;
string arr[10] = {};
decltype(arr)*arrptr(string i);

3----使用类型别名

#include <iostream>
#include <string>
using namespace std;
typedef string arr[10];
//arr m = { "12","re" };
arr(*fun(string i));

6.38

#include <iostream>
#include <string>
using namespace std;
int odd[] = { 1,3,5,7,9 };
int even[] = { 0,2,4,6,8 };
decltype(odd)&arrPtr(int i) {
	return(i % 2) ? odd : even;
}//我他妈就是个天才

函数重载

#include <iostream>
#include <string>
using namespace std;
void print(string i) {
	cout << "!";
}
void print(int i) {
	cout << "?";
}
void print(double q) {
	cout << "#";
}
int main() {
	//string i = "";
	int i = 30;
	print(i);
}

6.42

#include <iostream>
#include <string>
using namespace std;
string make_plural(size_t ctr, const string &word, const string &ending = "s") {
	return (ctr > 1) ? word + ending : word;
}


猜你喜欢

转载自blog.csdn.net/Leslie5205912/article/details/78697252