C++ Primer習題解答 Chapter 3

3-2

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

int main()
{
	int option;
	while (cout<<"1 讀入整行\n2 讀入單詞\n", cin >> option) {
		if (option == 1) {
			string s;
			//循環讀取,每次讀取一行,getline會讀取到輸入流的換行符時停止,並且將換行符以外的全部字符保存
			while (getline(cin, s)) {
				cout << s << endl;
			}
			//清空輸入流,不然EOF會對下次輸入造成影響
			cin.clear();
		}
		else if (option == 2) {
			string s;
			//循環讀取,每次讀取一個單詞,通過cin讀取字符串會自動忽略開頭空白字符,並持續讀取直到下一個空白字符
			while (cin >> s) {
				cout << s << endl;
			}
			//清空輸入流,不然EOF會對下次輸入造成影響
			cin.clear();
		}
	}
	system("pause");
	return 0;
}

3-4

#include<iostream>
#include<string>
using namespace std;
int main() 
{
	string s1, s2;
	cin >> s1 >> s2;
	if (s1 >= s2) {
		cout << s1 << endl;
	}
	else {
		cout << s2 << endl;
	}
	cin >> s1 >> s2;
	if (s1.size() >= s2.size()) {
		cout << s1 << endl;
	}
	else {
		cout << s2 << endl;
	}
	system("pause");
	return 0;
}

3-5

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string sum, temp;

	while (cin >> temp) {
		sum += temp;
	}
	cout << sum << endl;
	cin.clear();
	sum = "";
	while (cin >> temp) {
		if (!sum.size()) {
			sum += temp;
		}
		else {
			sum = sum + " " + temp;
		}	
	}
	cout << sum << endl;
	system("pause");
	return 0;
}

3-6

#include<string>
using namespace std;
int main()
{
	string str;
	getline(cin, str);
	for (auto &c : str) {
		c = 'X';
	}
	cout << str << endl;
	system("pause");
	return 0;
}

3-8

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	cout << "please input a string:" << endl;
	getline(cin, str);
	//字符串索引
	decltype(str.size()) i = 0;
	while (i < str.size()) {
		str[i] = 'X';
		i++;
	}
	cout << str << endl;
	cout << "please input a string:" << endl;
	getline(cin, str);
	for (i = 0; i < str.size(); i++) {
		str[i] = 'X';
	}
	cout << str << endl;
	system("pause");
	return 0;
}

3-9

#include<iostream>
#include<vector>
using namespace std;
int main() 
{
	
	system("pause");
	return 0;
}

3-10

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
	string str, output;
	getline(cin, str);
	for (auto &ch : str) {
		if (!ispunct(ch)) {
			output += ch;
		}
	}
	cout << output << endl;
	system("pause");
	return 0;
}

3-14

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> v;
	int tmp;
	while (cin >> tmp) {
		v.push_back(tmp);
		cout << "繼續嗎(y/n)?"<<endl;
		char ans = 'y';
		cin >> ans;
		if (ans == 'n' or ans == 'N') {
			break;
		}
	}
	for (auto i : v) {
		cout << i << endl;
	}
	system("pause");
	return 0;
}

3-15

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
	vector<string> v;
	string tmp;
	while (cin >> tmp) {
		v.push_back(tmp);
		cout << "繼續嗎(y/n)?" << endl;
		char ans = 'y';
		cin >> ans;
		if (ans == 'n' or ans == 'N') {
			break;
		}
	}
	for (auto i : v) {
		cout << i << endl;
	}
	system("pause");
	return 0;
}

3-16

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	vector<int> v1;
	vector<int> v2(10);
	vector<int> v3(10, 42);
	vector<int> v4{ 10 };
	vector<int> v5{ 10, 42 };
	vector<string> v6{ 10 };
	vector<string> v7{10, "hi"};

	cout << "第1個容器:" << endl;
	cout << v1.size() << endl;
	for (auto c : v1) {
		cout << c << endl;
	}

	cout << "第2個容器:" << endl;
	cout << v2.size() << endl;
	for (auto c : v2) {
		cout << c << " ";
	}
	cout << endl;
	cout << "第3個容器:" << endl;
	cout << v3.size() << endl;
	for (auto c : v3) {
		cout << c << " ";
	}
	cout << endl;

	cout << "第4個容器:" << endl;
	cout << v4.size() << endl;
	for (auto c : v4) {
		cout << c << " ";
	}
	cout << endl;

	cout << "第5個容器:" << endl;
	cout << v5.size() << endl;
	for (auto c : v5) {
		cout << c << " ";
	}
	cout << endl;

	cout << "第6個容器:" << endl;
	cout << v6.size() << endl;
	for (auto c : v6) {
		cout << c << " ";
	}
	cout << endl;

	cout << "第7個容器:" << endl;
	cout << v7.size() << endl;
	for (auto c : v7) {
		cout << c << " ";
	}
	cout << endl;
	system("pause");
}

3-17

#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main()
{
	vector<string> v;
	string tmp;
	while (cin>>tmp) {
		v.push_back(tmp);
		char opt = 'y';
		cout << "繼續輸入嗎(y/n)?" << endl;
		cin >> opt;
		if (opt == 'n' or opt == 'N') {
			break;
		}
	}
	for (auto &item : v) {
		for (auto &ch : item) {
			ch = toupper(ch);
		}
	}

	for (auto item : v) {
		cout << item << endl;
	}
	system("pause");
}

3-20

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> vInt;
	int iVal;
	cout << "請輸入一組數字:" << endl;
	while (cin >> iVal) {
		vInt.push_back(iVal);
	}
	cout << "相鄰兩項和:" << endl;
	for (decltype(vInt.size()) i = 0; i < vInt.size() - 1;i++) {
		cout << vInt[i] + vInt[i + 1] << " ";
	}
	cout << endl;
	cout << "首尾項求和:" << endl;
	for (decltype(vInt.size()) i = 0; (i < vInt.size()) and (i <= vInt.size() - i - 1); i++) {
		//處理奇數情形
		if (i == vInt.size() - i - 1) {
			cout << vInt[i];
			break;
		}
		cout << vInt[i] + vInt[vInt.size()-1-i] << " ";
	}
	cout << endl;
	system("pause");
}

3-21

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	vector<int> v1;
	vector<int> v2(10);
	vector<int> v3(10, 42);
	vector<int> v4{ 10 };
	vector<int> v5{ 10, 42 };
	vector<string> v6{ 10 };
	vector<string> v7{ 10, "hi" };

	cout << "第1個容器:" << endl;
	cout << v1.size() << endl;
	for (auto e = v1.cbegin(); e != v1.cend(); e++) {
		cout << *e << " ";
	}
	cout << endl;

	cout << "第2個容器:" << endl;
	cout << v2.size() << endl;
	for (auto e = v2.cbegin(); e != v2.cend(); e++) {
		cout << *e << " ";
	}
	cout << endl;

	cout << "第3個容器:" << endl;
	cout << v3.size() << endl;
	for (auto e = v3.cbegin(); e != v3.cend(); e++) {
		cout << *e << " ";
	}
	cout << endl;

	cout << "第4個容器:" << endl;
	cout << v4.size() << endl;
	for (auto e = v4.cbegin(); e != v4.cend(); e++) {
		cout << *e << " ";
	}
	cout << endl;

	cout << "第5個容器:" << endl;
	cout << v5.size() << endl;
	for (auto e = v5.cbegin(); e != v5.cend(); e++) {
		cout << *e << " ";
	}
	cout << endl;

	cout << "第6個容器:" << endl;
	cout << v6.size() << endl;
	for (auto e = v6.cbegin(); e != v6.cend(); e++) {
		cout << *e << " ";
	}
	cout << endl;

	cout << "第7個容器:" << endl;
	cout << v7.size() << endl;
	for (auto e = v7.cbegin(); e != v7.cend(); e++) {
		cout << *e << " ";
	}
	cout << endl;
	system("pause");
}

3-22

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

int main()
{
	vector<string> vStr;
	string str;
	//只輸入回車取得空行
	while (getline(cin, str)) {
		vStr.push_back(str);
	}

	//遇到空元素說明段落結束
	for (auto eStr = vStr.begin(); eStr != vStr.end() and !(*eStr).empty(); eStr++) {
		for (auto eCh = (*eStr).begin(); eCh != (*eStr).end(); eCh++) {
			*eCh = toupper(*eCh);
		}
		cout << *eStr << endl;
	}
	system("pause");
}

3-23

#include<vector>
#include<iostream>
using namespace std;
int main()
{
	vector<int> vInt(10);
	for (auto e = vInt.begin(); e != vInt.end(); e++) {
		cout << "input a number:";
		int i;
		cin >> i;
		*e = i;
	}
	for (auto e = vInt.begin(); e != vInt.end(); e++) {
		*e *= 2;
		cout << *e << " ";
	}
	cout << endl;
	system("pause");
}

3-24

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> vInt;
	int iVal;
	while (cin >> iVal) {
		vInt.push_back(iVal);
	}
	if (vInt.cbegin() == vInt.cend()) {
		cout << "向量爲空" << endl;
		return -1;
	}

	auto begin = vInt.cbegin();
	auto end = vInt.cend();
	auto size = vInt.size();

	cout << "鄰項和:" << endl;
	int count = 0;
	for (auto it = begin; it <= end - 2; it++) {
		count++;
		cout << (*it + (*(++it))) << " ";
		//每行輸出5個結果
		if (count % 5 == 0) {
			cout << endl;
		}
	}
	if (size % 2 != 0) {
		cout << *(end - 1);
	}
	cout << endl;
	cout << "首尾和:" << endl;
	count = 0;
	for (auto it = begin; it != (begin + size / 2 + 1); it++) {
		//到中間了
		count++;
		if (it == (end - (it - begin) - 1)) {
			cout << *it << " ";
			break;
		}
		cout << *it + *(end - (it - begin) - 1) << " ";
		if (count % 5 == 0) {
			cout << endl;
		}
	}
	cout << endl;
	system("pause");
}

3-25

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	//分數段向量
	vector<unsigned> vscore(11);
	unsigned int score;
	int count = 0;
	auto it = vscore.begin();
	while (cin >> score) {
		if (score <= 100) {
			(*(it + score / 10))++;
			count++;
		}
	}
	cout << "共" << count << "個有效成績" << endl;
	for (auto e = vscore.begin(); e != vscore.end(); e++) {
		cout << *e << endl;
	}
	system("pause");
}

3-26

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> vInt = {-99, -45, -11, 0, 7, 11, 18, 56, 77, 99, 100};
	int i;
	cout << "input:";
	cin >> i;
	auto begin = vInt.begin();
	auto end = vInt.end();
	auto mid = begin + (end - begin) / 2;
	while (mid!=end and i!=*mid) {
		if (i < *mid) {
			end = mid;
			mid = begin + (end - begin) / 2;
		}
		else {
			begin = mid + 1;
			mid = begin + (end - begin) / 2;
		}
	}
	if (mid != end) {
		cout << *mid << endl;
	}
	else if (i == *mid) {
		cout << *mid << endl;
	}
	else {
		cout << "not found" << endl;
	}
	
	system("pause");
}

3-31

#include<iostream>
using namespace std;
int main()
{
	int array[10], array2[10];
	for (int i = 0; i < 10; i++) {
		array[i] = i;
	}
	cout << "first array:" << endl;
	for (auto i : array) {
		cout << i << " ";
	}
	cout << endl;
	for (int i = 0; i < 10; i++) {
		array2[i] = array[i];
	}
	cout << "a copy of the first array:" << endl;
	for (auto i : array2) {
		cout << i << " ";
	}
	cout << endl;
	system("pause");
}

3-32

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> vint1, vint2;
	//使用了vector.size()的循環裏面不要修改vector的大小
	for (int i = 0; i < 10; i++) {
		vint1.push_back((int)i);
	}
	cout << "first vector:" << endl;
	for (auto i : vint1) {
		cout << i << " ";
	}
	cout << endl;
	//vector支持拷貝操作,不用像數組複製起來那麼繁瑣
	vint2 = vint1;
	cout << "a copy of the first vector:" << endl;
	for (auto i : vint1) {
		cout << i << " ";
	}
	cout << endl;
	system("pause");
}

3-35

#include<iostream>
#include<iterator>
using namespace std;
int main() 
{
	const int sz = 10;
	int arr[sz];
	int *pstart = begin(arr);
	int *pend = end(arr);
	for (int *pnow = pstart; pnow < pend; pnow++) {
		*pnow = pnow - pstart;
		cout << *pnow << " ";
	}
	cout << endl;
	for (int *pnow = pstart; pnow < pend; pnow++) {
		*pnow = 0;
		cout << *pnow << " ";
	}
	cout << endl;
	system("pause");
}

3-36

#include<iostream>
#include<iterator>
using namespace std;
int main()
{
	int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10];
	//第二個數組的首尾指針
	int *pstart2 = begin(arr2);
	int *pend2 = end(arr2);
	cout << "input 10 numbers:" << endl;
	for (int *pnow = pstart2; pnow < pend2; pnow++) {
		cin >> *pnow;
	}
	bool flag = true;
	//第一個數組的首尾指針
	int *pstart1 = begin(arr1);
	int *pend1 = end(arr1);
	for (int *pnow1 = pstart1, *pnow2 = pstart2; 
		pnow1 < pend1, pnow2 < pend2; 
		pnow1++, pnow2++) 
	{
		if (*pnow1 != *pnow2) {
			flag = false;
			break;
		}
	}
	flag ? cout << "equal"<<endl : cout << "not equal"<<endl;

	//對於vector來說方法是一樣的,只不過把指針改爲迭代器而已
	system("pause");
}

3-39

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
	string str1, str2;
	cin >> str1 >> str2;
	if (str1 > str2) {
		cout << str1 << "大於" << str2 << endl;
	}
	else if (str1 == str2) {
		cout << str1 << "等於" << str2 << endl;
	}
	else {
		cout << str1 << "小於" << str2 << endl;
	}

	char cstr1[100], cstr2[100];
	cin >> cstr1 >> cstr2;
	int res = strcmp(cstr1, cstr2);
	if (res > 0) {
		cout << cstr1 << "大於" << cstr2 << endl;
	}
	else if (res == 0) {
		cout << cstr1 << "等於" << cstr2 << endl;
	}
	else {
		cout << cstr1 << "小於" << cstr2 << endl;
	}
	system("pause");
}

3-40

#define  _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<cstring> 
using namespace std;
int main()
{
	char cstr1[10] = "Hello,";
	char cstr2[10] = "C++!";
	char cstr3[20];
	strcat(cstr1, cstr2);
	strcpy(cstr3,cstr1);
	cout << cstr3 << endl;
	system("pause");
}

3-41

#include<iostream>
#include<vector>
#include<iterator>

using namespace std;

int main()
{
	int arr[5] = {1, 2, 3, 4, 5};
	vector<int> v(begin(arr), end(arr));
	for (auto i : v) {
		cout << i << " ";
	}
	cout << endl;
	system("pause");
}

3-42

#include<iostream>
#include<vector>
#include<iterator>

using namespace std;

int main()
{
	const int size = 50;
	vector<int> v;
	for (int i = 0; i < size; i++)
	{
		v.push_back(i);
		cout << i << " ";
	}
	cout << endl;
	//賦值給一個整型數組
	int arr[size];
	auto it = v.cbegin();
	//範圍for語句遍歷數組
	for (auto &i : arr) {
		i = *it;
		cout << *it << " ";
		it++;
	}
	cout << endl;
	system("pause");
}

3-43

#include<iostream>
#include<iterator>
using namespace std;
int main()
{
	//定義並且初始化一個二維數組ia
	int ia[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};
    
	//range-based for loop
	for (int (&p)[4] : ia) {
		for (int i : p) {
			cout << i << " ";
		}
	}
	cout << endl;
    
	//index based ordinary for loop
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			cout << ia[i][j] << " ";
		}
	}
	cout << endl;
    
	//pointer based ordinary for loop
	for (int(*poutter)[4] = begin(ia); poutter < end(ia); poutter++) {
		for (int *pinner = begin(*poutter); pinner < end(*poutter); pinner++) {
			cout << *pinner << " ";
		}
	}
	cout << endl;
	system("pause");
}

3-44

#include<iostream>
#include<iterator>
using namespace std;
int main()
{
	//定義並且初始化一個二維數組ia
	int ia[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};
	using int_array = int[4];
    
	//range-based for loop
	for (int_array &p : ia) {
		for (int i : p) {
			cout << i << " ";
		}
	}
	cout << endl;
    
	//index based ordinary for loop
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 4; j++) {
			cout << ia[i][j] << " ";
		}
	}
	cout << endl;
    
	//pointer based ordinary for loop
	using p_outter_array = int(*)[4];
	//typedef int(*outter_array)[4];
	using p_inner_array = int*;
	//typedef int *p_inner_array;
	for (p_outter_array poutter = begin(ia); poutter < end(ia); poutter++) {
		for (p_inner_array pinner = begin(*poutter); pinner < end(*poutter); pinner++) {
			cout << *pinner << " ";
		}
	}
	cout << endl;
	system("pause");
}

3-45

#include<iostream>
#include<iterator>
using namespace std;
int main()
{
	//定義並且初始化一個二維數組ia
	int ia[3][4] = {
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11}
	};

	//range-based for loop
	for (auto &p : ia) {
		for (auto i : p) {
			cout << i << " ";
		}
	}
	cout << endl;
    
	//index based ordinary for loop
	for (auto i = 0; i < 3; i++) {
		for (auto j = 0; j < 4; j++) {
			cout << ia[i][j] << " ";
		}
	}
	cout << endl;

	for (auto poutter = begin(ia); poutter < end(ia); poutter++) {
		for (auto pinner = begin(*poutter); pinner < end(*poutter); pinner++) {
			cout << *pinner << " ";
		}
	}
	cout << endl;
	system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章