C++Primer--第三章练习题自做

练习3.2

//读入一整行
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;
int i = 42;
int main() {
	string line;
	while (getline(cin, line))
	{
		if (line.empty())	break;
		cout << line << endl;
	}

	return 0;
}
//读入一个词
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;
int i = 42;
int main() {
	string world;
	while (cin >> world)
	{
		//ending flag word "end"
		if (world == "end")	break;
		cout << world << endl;
	}

	return 0;
}

练习3.4

//字符串相等不相等
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;
int i = 42;
int main() {
	string str1, str2;
	getline(cin, str1);
	getline(cin, str2);
	if (str1 == str2) {
		cout << "str1 = str2" << endl;
	}
	else {
		string result{ (str1 > str2) ? str1 : str2 };
		cout << "the bigger one is: " << result << endl;
	}

	return 0;
}
//字符串等长不等长
#include <iostream>
#include <string>

using std::string;
using std::cin;
using std::cout;
using std::endl;
int i = 42;
int main() {
	string str1, str2;
	getline(cin, str1);
	getline(cin, str2);
	if (str1.size() == str2.size()) {
		cout << "same length" << endl;
	}
	else {
		string result{ (str1.size() > str2.size()) ? str1 : str2 };
		cout << "the bigger one is: " << result << endl;
	}

	return 0;
}

练习3.6-练习3.11

练习3.14

#include <iostream>
#include <vector>

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

int main() {
	vector<int> vecint;
	int item;
	while (cin >> item) {
		if (item == -1)	break;
		vecint.push_back(item);
	}
	for (auto i : vecint)	cout << i << endl;

	return 0;
}

练习3.15

#include <iostream>
#include <vector>
#include <string>

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

int main() {
	vector<string> vecstr;
	string item;
	while (getline(cin, item)) {
		if (item.empty())	break;
		vecstr.push_back(item);
	}
	for (auto i : vecstr)	cout << i << endl;

	return 0;
}

练习3.16

练习3.17

#include <iostream>
#include <vector>
#include <string>
#include "cctype"

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

int main() {
	vector<string> vecstr;
	string item;
	while (cin >>item) {
		if (item == "end")	break;
		vecstr.push_back(item);
	}
	for (auto str : vecstr) {
		for (auto& ch : str) {
			if (islower(ch)) {
				ch -= 32;
			}
		}
		cout << str << endl;
	}

	return 0;
}

练习3.20

//相邻
#include <iostream>
#include <vector>

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

int main() {
	vector<int> vecint;
	int item;
	while (cin >> item) {
		if (item == -1)	break;
		vecint.push_back(item);
	}
	bool flag{ false };
	for (int i = 0; i < (vecint.size() - 1); i++) {
		if (flag)	cout << " ";
		cout << vecint[i] + vecint[i + 1];
		flag = true;
	}

	return 0;
}
//首尾
#include <iostream>
#include <vector>

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

int main() {
	vector<int> vecint;
	int item;
	while (cin >> item) {
		if (item == -1)	break;
		vecint.push_back(item);
	}
	bool flag{ false };
	for (int i = 0; i < (vecint.size() / 2); i++) {
		if (flag)	cout << " ";
		cout << vecint[i] + vecint[vecint.size() - i - 1];
		flag = true;
	}

	return 0;
}

练习3.23

#include <iostream>
#include <vector>

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

int main() {
	vector<int> vecint;
	int item, number{ 0 };
	while (number < 10){
		number++;
		cin >> item;
		vecint.push_back(item);
	}

	vector<int>::iterator cend = vecint.end();

	while (number > 0) {
		(*--cend) *= 2;
		number--;
	}
	bool flag{ false };
	for (; number < 10; number++) {
		if (flag)	cout << " ";
		cout << (*cend++);	
		flag = true;
	}

	return 0;
}

练习3.32

#include <iostream>
#include <vector>

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

int main() {
	int original[10];
	vector<int> latterone;
	
	for (int i = 0; i < 10; i++) {
		original[i] = i;
		latterone.push_back(original[i]);
	}
	bool flag{ false };
	cout << "the original array is:" << endl;
	for (int i = 0; i < 10; i++) {
		if (flag)	cout << " ";
		cout << original[i];
		flag = true;
	}
	cout << endl;
	flag = false;
	
	auto cbegin = latterone.begin();
	while (cbegin != latterone.end())
	{
		if (flag)	cout << " ";
		cout << *cbegin++;
		flag = true;
	}

	return 0;
}

练习3.40

#include <iostream>
#include "cstring"

using std::cout;
using std::endl;

int main() {
	char str0[12]{ "hello " };
	char str1[]{ "world" };
	strcat_s(str0, str1);
	char str3[12];
	strcpy_s(str3, str0);
	cout << str3 << endl;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比巧克力巧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值