C++ Primer(5e)第3章习题

3.1

/* 编写程序,使用while循环将50到100的整数相加 */
#include
using std::cout;
using std::endl;
int main()
{
	int sum = 0, val = 50;
	while (val <= 100)
	{
		sum += val;
		++val;
	}
	cout << "Sum of 50 to 100 inclusive is " << sum << endl;
	return 0;

/* 按递减顺序打印出10到0之间的整数 */
#include
using std::cout;
using std::endl;
int main()
{
	int val = 10;
	while (val >= 0)
	{
		cout << val << " ";
		val--;
	}
	cout << endl;
	return 0;
}
/* 输入两个数,打印出两个数之间的所有整数 */
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
	cout << "Enter two numbers:" << endl;
	int v1 = 0, v2 = 0;
	cin >> v1 >> v2;
	if (v1 > v2)
	{
		int temp = v1;
		v1 = v2;
		v2 = temp;
	}
	while (v1 <= v2)
	{
		cout << v1 << " ";
		v1++;
	}
	cout << endl;
	return 0;
}

3.2

/* 一次读入一行 */
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
	string line;
	while (getline(cin, line))
		if (!line.empty())
			cout << line << endl;
	return 0;
}
/* 一次读入一个词 */
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
	string word;
	cin >> word;
	cout << word << endl;
	return 0;

3.3

string类的输入运算符:遇到空白字符就结束
getline函数:保留输入时的空白字符

3.4

/* 比较两个字符串是否相等并输出结果,不等则输出较大字符串 */
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
	cout << "Enter two strings:" << endl;
	string s1, s2;
	getline(cin, s1);
	getline(cin, s2);
	if (s1 == s2)
		cout << "They are equal" << endl;
	else
	{
		string temp = s1;
		if (temp < s2)
			temp = s2;
		cout << "The max string is " << temp << endl;
	}
	return 0;
}
/* 比较两个字符串是否等长,不等则输出较长字符串 */
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
	cout << "Enter two strings:" << endl;
	string s1, s2;
	getline(cin, s1);
	getline(cin, s2);
	if (s1.size() == s2.size())
		cout << "Their lengths are equal" << endl;
	else
	{
		string temp = s1;
		if (temp.size() < s2.size())
			temp = s2;
		cout << "The longer string is " << temp << endl;
	}
	return 0;
}

3.5

/* 输入多个字符串并将它们连接在一起,输入连接成的大字符串 */
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
	string s1, s2;
	cout << "请输入一个字符串:" << endl;
	getline(cin, s1);
	cout << "输入需要拼接的字符串:" << endl;
	while (getline(cin, s2))
		s1 += s2;              
	cout << "连接成的大字符串为:" << s1 << endl;  // 不想输入字符串了记得按Ctrl+Z
	return 0;
}

3.6

#include
#include
using std::cout;
using std::endl;
using std::string;
int main()
{
	string s = ("Hello, World!");
	for (auto &c : s)
		c = 'X';
	cout << s << endl;
	return 0;
}

3.7

#include
#include
using std::cout;
using std::endl;
using std::string;
int main()
{
	string s = ("Hello, World!");
	for (char &c : s)
		c = 'X';
	cout << s << endl;
	return 0;
}

3.8

#include
#include
using std::cout;
using std::endl;
using std::string;
int main()
{
	string s = ("Hello, World!");
	string::size_type n = 0;
	while (n < s.size())
	{
		s[n] = 'X';
		++n;
	}
	cout << s << endl;
	return 0;
}

3.9

string s;
cout << s[0] << endl;
不合法,如果s为空字符串,则会出现不可预知的结果。

3.10

#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
	string s1;    // s1用来接收输入的字符串
	string s2("\0");    // s2用来存储处理后的字符串
	cout << "请输入一个字符串:" << endl;
	while (getline(cin, s1)) // 不断读取字符串
	{
		for (decltype(s1.size()) i = 0; i < s1.size(); i++)     // 遍历字符串的每一个元素
		{
			if (!ispunct(s1[i]))     // 如果该元素不为标点符号
				s2 += s1[i];         // 将该元素赋给s2
		}
		cout << "处理后的字符串:" << s2 << endl;   // 输出处理后的字符串
		s2 = "\0";      // 将s2清空
		cout << "请输入一个字符串:" << endl;
		string s3;
		getline(cin, s3);
	}
	return 0;
}

3.11

const string s = "Keep out!";
for (auto &c : s){/*...*/ }
合法,char类型

3.12

(a)vector<vector<int>> ivec;
// 该向量的元素是vector的对象
(b)vector<string> svec = ivec;
// 把ivec的元素拷贝给svec
(c)vector<string> svec(10, "null");
// 10个string类型元素,每个都被初始化为“null”

3.13

(a)初始状态为空
(b)包含10个元素,每个被初始化为0
(c)包含10个元素,每个被初始化为42
(d)包含一个元素,值为10
(e)包含2个元素,值分别为1042
(f)包含10个元素,初始值为空string
(g)包含10个元素,每个被初始化为“hi"

3.14

#include
#include
using std::vector;
using std::cin;
using std::cout;
using std::endl;

int main()
{
	int number;
	vector<int> text;
	while (cin >> number)
	{
		text.push_back(number);
	}
	return 0;
}

3.15

#include
#include
#include
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main()
{
	string word;
	vector<string> text;
	while (cin >> word)
	{
		text.push_back(word);
	}
	return 0;
}

3.17

#include
#include
#include
#include

using namespace std;

int main()
{
	vector<string> s;
	string word;
	while (cin >> word)
		s.push_back(word);
	for (auto c : s)
	{
		for (auto &v : c)
		{
			v = toupper(v);
		}
		cout << c << endl;
	}
	return 0;
}

3.18

vector<int> ivec;
ivec[0] = 42;
不合法,修改如下:
vector<int> ivec{42};

3.19

vector<int> i(10, 42);
vector<int> i{42,42,42,42,42,42,42,42,42,42};
vector<int> i = {42,42,42,42,42,42,42,42,42,42};
肯定少的好,打的快

3.20

/* 相邻相加 */
#include
#include
#include

using namespace std;

int main()
{
	int number;
	vector<int> text;
	while (cin >> number)
		text.push_back(number);
	for (decltype(text.size()) i = 0; i < text.size() - 1; i++)
	{
		int sum = 0;
		sum = text[i] + text[i + 1];
		cout << sum << " ";
	}
	cout << endl;
	return 0;
}

/* 首尾相加 */
#include
#include
#include
using namespace std;

int main()
{
	int number;
	vector<int> text;
	while (cin >> number)
		text.push_back(number);
	for (decltype(text.size()) i = 0; i < text.size() / 2; i++)
	{
		int sum = 0;
		sum = text[i] + text[text.size() - i - 1];
		cout << sum << endl;
	}
	if (text.size() % 2 != 0)        
	{
		int temp = text[text.size() / 2];
		cout << temp << endl;
	}
	return 0;
}

3.22

#include
#include
#include
using namespace std;

int main()
{
	string s("Hello World!!!");
	for (auto it = s.begin(); it != s.end(); it++)
	{
		*it = toupper(*it);
		cout << *it;
	}
	cout << endl;
	return 0;	
}

3.23

#include
#include
using namespace std;

int main()
{
	vector<int> num{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	for (auto it = num.begin(); it != num.end(); it++)
	{
		*it *= 2;
		cout << *it << " ";
	}
	cout << endl;
	return 0;
}

3.24

/* 相邻相加 */            
#include    
#include
using namespace std;

int main()
{
	int a;
	vector<int> b;
	while (cin >> a)
		b.push_back(a);
	for (auto it = b.begin(); it != b.end() - 1; it++)
	{
		int  sum = 0;
		sum = *it + *(it + 1);
		cout << sum << " ";
	}
	cout << endl;
	return 0;
}
/* 首尾相加 */
#include      
#include
using namespace std;

int main()
{
	int a;
	vector<int> b;
	while (cin >> a)
		b.push_back(a);
	auto beg = b.begin();
	auto end = b.end();
	auto mid = b.begin() + (end - beg) / 2;
	while (beg != mid)
	{
		int sum = 0;
		sum = *beg + *(end - 1);
		cout << sum << " ";
		beg++;
		end--;
	}
	if ((end - beg) % 2 != 0)
	{
		cout << *mid;
	}
	cout << endl;
	return 0;
}

3.25

vector<int> scores(11, 0);
	unsigned grade;
	auto it = scores.begin();
	while (cin >> grade)
	{
		if (grade <= 100 && it != scores.end())
			*it = grade;
			it++;
	}

3.26

如果使用mid = (beg + end) / 2不能保证迭代器类型一致。

3.27

unsigned buf_size = 1024;
(a)int ia[buf_size];        (b)int ia[4 * 7 - 14];
(c)int ia[buf_size()];      (d)char st[11] = "fundamental";
a,b都合法,c不能确定,如果buf_size()返回的是个负数,则不合法,d不合法,没有空间放空字符。

3.28

string sa[10];     // 空
int ia[10];          // 10个值为0的数
int main()
{
	string sa2[10];  // 空
	int ia2[10];   // 自行体会~
}

3.29

数组不能添加元素;数组的大小必须确定。

3.30

constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 1; ix <= array_size; ix++)
    ia[ix] = ix;
  数组下标从0开始,末尾应为元素个数减一.

3.31

#include
using namespace std;

int main()
{
	constexpr int num = 10;
	int ia[num];
	for (int ix = 0; ix < num; ix++)
		ia[ix] = ix;
	return 0;
}

3.32

#include
using namespace std;

int main()
{
	constexpr int num = 10;
	int ia[num];
	int ib[num];
	for (int ix = 0; ix < num; ix++)
		ia[ix] = ix;
	for (int ix = 0; ix < num; ix++)
		ib[ix] = ia[ix];
	return 0;
}

3.33

会进行默认初始化,默认初始化可能会有未定义的值。

3.34

把p2指向的元素的地址赋给p1
当p1和p2不指向同一个数组同一个元素时是非法的

3.35

#include
using namespace std;

int main()
{
	int ia[] = { 1, 2, 3, 4, 5 };
	int *p = begin(ia);
	int *q = end(ia);
	while (p != q)
	{
		*p = 0;
		cout << *p << " ";
		p++;
	}
	cout << endl;
	return 0;
}

3.37

当数组ca的元素不为0时,输出。

输出结果:

C++ Primer(5e)第3章习题_第1张图片

3.38

两个指针相加后所得到的地址不知道指向什么东西。

3.39

/* 两个string对象 */
#include
#include
using namespace std;

int main()
{
	string s1 ("Hello Wordl!");
	string s2 ("A string");
	if (s1 > s2)
		cout << s1 << endl;
	else if (s1 < s2)
		cout << s2 << endl;
	else
		cout << "equal" << endl;
	return 0;
}
/* C风格 */
#include
#include
using namespace std;

int main()
{
	const char s1[] = "Hello World!";
	const char s2[] = "A string";
	if (strcmp(s1, s2) < 0)
		cout << s2 << endl;
	else if (strcmp(s1, s2) > 0)
		cout << s1 << endl;
	else
		cout << "equal" << endl;
	return 0;
}

[注]:给定字符随意

3.40

如果编译器出现如下问题,请在第一行加上#define _CRT_SECURE_NO_WARNINGS
这是因为windows方面编译器认为这些函数不安全,提示让你用他们提供的库函数。
C++ Primer(5e)第3章习题_第2张图片

3.41

#include
#include
using namespace std;

int main()
{
	int arr[] = { 0, 1, 2, 3, 4, 5 };
	vector<int> ivec(begin(arr), end(arr));
	for (auto i = ivec.begin(); i != ivec.end(); i++)
	{
		cout << *i << " ";
	}
	cout << endl;
	return 0;
}

3.42

#include
#include
using namespace std;

int main()
{
	vector<int> ivec(5, 5);
	int num[5];
	int j = 0;
	for (auto i = ivec.begin(); i != ivec.end(); i++)
	{
		num[j] = *i;
		cout << num[j] << " ";
		j++;
	}
	cout << endl;
	return 0;
}

3.43

/* 使用范围for语句 */
#include
using namespace std;

int main()
{
	int ia[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	for (int (&row)[4] : ia)
	{
		for (int &col : row)
			cout << col << " ";
		cout << endl;
	}
	return 0;
}
/* 下标运算符版本 */
#include
using namespace std;

int main()
{
	int ia[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 4; j++)
			cout << ia[i][j] << " ";
		cout << endl;
	}
	return 0;
}
/* 指针版本 */
#include
using namespace std;

int main()
{
	int ia[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	for (int (*p)[4] = ia; p != ia + 3; p++)
	{
		for (int *q = *p; q != *p + 4; q++)
			cout << *q << " ";
		cout << endl;
	}
	return 0;
}

3.44

/* 太懒了,只改指针版本 */
#include
using namespace std;

int main()
{
	int ia[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	using int_array = int[4];
	typedef int int_array[4];
	for (int_array *p = ia; p != ia + 3; p++)
	{
		for (int *q = *p; q != *p + 4; q++)
			cout << *q << " ";
		cout << endl;
	}
	return 0;
}

3.45

/* 太懒了,只改范围for语句版本 */
#include
using namespace std;

int main()
{
	int ia[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	for (auto &row : ia)
	{
		for (auto col : row)
			cout << col << " ";
		cout << endl;
	}
	return 0;
}

你可能感兴趣的:(C++)