C++ Primer习题解答 Chapter 5

5-5

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

int main()
{
	string table[] = { "F", "D", "C", "B", "A", "A++"};
	int grade;
	string level;
	cout << "please input your grade:";
	cin >> grade;
	cout << endl;
	if (grade < 0 or grade > 100) {
		cout << "illegal!" << endl;
		system("pause");
		return -1;
	}
	if (grade == 100) {
		level = table[size(table) - 1];
		cout << level << endl;
		system("pause");
		return 0;
	}
	if (grade < 60) {
		level = table[0];
		cout << level << endl;
		system("pause");
		return 0;
	}
	level = table[(grade - 50) / 10];
	if (grade % 10 < 3) {
		level = "-" + level;
	}
	else if (grade % 10 > 7) {
		level = "+" + level;
	}
	cout << level << endl;
	system("pause");
}

5-6

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

int main()
{
	string table[] = { "F", "D", "C", "B", "A", "A++" };
	int grade;
	string level;
	
	cout << "please input your grade:";
	cin >> grade;
	cout << endl;
	if (grade < 0 or grade > 100) {
		cout << "illegal" << endl;
	}
	if (grade == 100) {
		level = table[size(table) - 1];
		cout << level << endl;
		system("pause");
		return 0;
	}
	if (grade < 60) {
		level = table[0];
		cout << level << endl;
		system("pause");
		return 0;
	}
	int i = grade % 10;
	int j = grade / 10;
	level = (i == 6) ? table[1]
			: (i == 7) ? table[2]
			: (i == 8) ? table[3] : table[4];
	//cout << level << endl;
	level = ((j <= 2) ? "-"
		: (j >= 8) ? "+" : " ") + level;
	cout << level << endl;
	system("pause");
}

5-9

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int opt = 1;
	string s;
	int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, others = 0;
	while (opt) {
		cout << "input some text:" << endl;
		getline(cin, s);
		for (auto i : s) {
			switch (i) {
			case 'a':
				acnt++;
				break;
			case 'e':
				ecnt++;
				break;
			case 'i':
				icnt++;
				break;
			case 'o':
				ocnt++;
				break;
			case 'u':
				ucnt++;
				break;
			default:
				others++;
				break;
			}
		}
		cout << "a\t\t" << acnt << endl;
		cout << "e\t\t" << ecnt << endl;
		cout << "i\t\t" << icnt << endl;
		cout << "o\t\t" << ocnt << endl;
		cout << "u\t\t" << ucnt << endl;
		cout << "others\t" << others << endl;
		cout << "input 0 to stop:" << endl;
		cin >> opt;
	}
	system("pause");
}

5-10

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int opt = 1;
	string s;
	int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, others = 0;
	while (opt) {
		cout << "input some text:" << endl;
		getline(cin, s);
		for (auto i : s) {
			switch (i) {
			case 'A':
			case 'a':
				acnt++;
				break;
			case 'E':
			case 'e':
				ecnt++;
				break;
			case 'I':
			case 'i':
				icnt++;
				break;
			case 'O':
			case 'o':
				ocnt++;
				break;
			case 'U':
			case 'u':
				ucnt++;
				break;
			default:
				others++;
				break;
			}
		}
		cout << "a\t\t" << acnt << endl;
		cout << "e\t\t" << ecnt << endl;
		cout << "i\t\t" << icnt << endl;
		cout << "o\t\t" << ocnt << endl;
		cout << "u\t\t" << ucnt << endl;
		cout << "others\t" << others << endl;
		cout << "input 0 to stop:" << endl;
		cin >> opt;
	}
	system("pause");
}

5-11

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int opt = 1;
	string s;
	int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, others = 0;
	int space, table, line;
	space = table = line = 0;
	while (opt) {
		cout << "input some text:" << endl;
		getline(cin, s);
		cout << s << endl;
		for (auto i : s) {
			//cout << static_cast<int>(i) << " ";
			switch (i) {
			case 'A':
			case 'a':
				acnt++;
				break;
			case 'E':
			case 'e':
				ecnt++;
				break;
			case 'I':
			case 'i':
				icnt++;
				break;
			case 'O':
			case 'o':
				ocnt++;
				break;
			case 'U':
			case 'u':
				ucnt++;
				break;
			case ' ':
				++space;
				break;
			case '\t':
				++table;
				break;
			case '\n':
				++line;
				break;
			default:
				others++;
				break;
			}
		}
		cout << "a\t\t" << acnt << endl;
		cout << "e\t\t" << ecnt << endl;
		cout << "i\t\t" << icnt << endl;
		cout << "o\t\t" << ocnt << endl;
		cout << "u\t\t" << ucnt << endl;
		cout << "space\t" << space << endl;
		cout << "table\t" << table << endl;
		cout << "line feed\t" << line << endl;
		cout << "others\t" << others << endl;

		acnt = ecnt = icnt = ocnt = ucnt = space = table = line = 0;
		cout << "input 0 to stop:" << endl;
		cin >> opt;
		cin.get();
	}
	system("pause");
}

5-12

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int opt = 1;
	string s;
	int acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, others = 0;
	int space, table, line;
	space = table = line = 0;
	int ffcnt = 0, ficnt = 0, flcnt = 0;
	char prech = '\0';
	while (opt) {
		cout << "input some text:" << endl;
		getline(cin, s);
		cout << s << endl;
		for (auto i : s) {
			//cout << static_cast<int>(i) << " ";
			switch (i) {
			case 'f':
				if (prech == 'f') {
					++ffcnt;
					prech = '\0';
				}
				else {
					prech = 'f';
				}
				break;
			case 'l':
				if (prech == 'f') {
					++flcnt;
					prech = '\0';
				}
				else {
					prech = '\0';
				}
				break;
			case 'A':
			case 'a':
				acnt++;
				break;
			case 'E':
			case 'e':
				ecnt++;
				break;
			case 'I':
			case 'i':
				icnt++;
				if (prech == 'f') {
					++ficnt;
					prech = '\0';
				}
				else {
					prech = '\0';
				}
				break;
			case 'O':
			case 'o':
				ocnt++;
				break;
			case 'U':
			case 'u':
				ucnt++;
				break;
			case ' ':
				++space;
				break;
			case '\t':
				++table;
				break;
			case '\n':
				++line;
				break;
			default:
				others++;
				break;
			}
		}
		cout << "a\t\t" << acnt << endl;
		cout << "e\t\t" << ecnt << endl;
		cout << "i\t\t" << icnt << endl;
		cout << "o\t\t" << ocnt << endl;
		cout << "u\t\t" << ucnt << endl;
		cout << "space\t\t" << space << endl;
		cout << "table\t\t" << table << endl;
		cout << "line feed\t" << line << endl;
		cout << "ff\t\t" << ffcnt << endl;
		cout << "fl\t\t" << flcnt << endl;
		cout << "fi\t\t" << ficnt << endl;
		cout << "others\t" << others << endl;

		//还原初始值
		acnt = ecnt = icnt = ocnt = ucnt = space = table = line = 0;
		ffcnt = flcnt = ficnt = 0;
		prech = '\0';
		cout << "input 0 to stop:" << endl;
		cin >> opt;
		cin.get();
	}
	system("pause");
}

5-14

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string currStr, prevStr, maxDupStr;
	int count = 1, maxCount = 1;
	while (cin >> currStr) {
		//如果和前一次一样,重复次数+1
		if (prevStr == currStr) {
			++count;
		}
		//如果重复停止
		else {
			count = 1;
		}
		if (count > maxCount) {
			maxCount = count;
			maxDupStr = currStr;
		}
		prevStr = currStr;
	}
	cout << maxDupStr << "重复次数最多," << "共计" << maxCount << "次" << endl;
	system("pause");
}

5-16

#include<iostream>
using namespace std;

int main()
{
	//while
	int i;
	while (cin >> i) {
		cout << i << " ";
	}
	cout << endl;
	cin.clear();
	//while 累加
	int sum = 0;
	i = 0;
	while (i <= 100) {
		sum += i;
		++i;
	}
	cout << "sum: " << sum << endl;
	//for
	i = 0;
	for (; cin >> i;) {
		cout << i << " ";
	}
	cout << endl;
	//for 累加
	sum = 0;
	for (i = 0; i <= 100;i++) {
		sum += i;
	}
	cout << "sum: " << sum << endl;
	system("pause");
}

5-17

#include<vector>
#include<iostream>
using namespace std;
int main()
{
	bool result = true;
	vector<int> v1 = { 1, 2, 3, 4, 6, 9 };
	vector<int> v2 = { 1, 2, 3, 4, 6, 9, 11, 45, 23};
	vector<int> v3 = { 1, 2, 3, 99, 6, 9, 11, 45, 23 };
	for (auto it1 = v1.begin(), it2 = v2.begin();
		it1 != v1.end() and it2 != v2.end();
		it1++, it2++) {
		if (*it1 != *it2) {
			result = false;
		}
	}
	cout << (result ? "包含" : "不包含") << endl;
	for (auto it2 = v2.begin(), it3 = v3.begin();
		it2 != v2.end() and it3 != v3.end();
		it2++, it3++) {
		if (*it2 != *it3) {
			result = false;
		}
	}
	cout << (result ? "包含" : "不包含") << endl;
	system("pause");
}

5-19

#include<iostream>
#include<string>
using namespace std;
int main() {
	do {
		string str1, str2;
		cout << "请输入两个字符串" << endl;
		cin >> str1 >> str2;
		if (str1.length() >= str2.length()) {
			cout << str1.length() << endl;
		}
		else {
			cout << str2.length() << endl;
		}
	} while (cin);
	system("pause");
}

5-20

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	string preStr;
	bool flag = false;
	while (cin>>str and !str.empty()) {
		if (preStr == str) {
			flag = true;
			break;
		}
		preStr = str;
	}
	cout << (flag ? str + "重复" : "没有重复单词") << endl;
	cin.clear();
	system("pause");
}

5-21

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
	string str;
	string preStr;
	bool flag = false;
	while (cin >> str and !str.empty()) {
		if (!isupper(str[0])) {
			continue;
		}
		else {
			if (preStr == str) {
				flag = true;
				break;
			}
		}
		preStr = str;
	}
	cout << (flag ? str + "重复" : "没有重复单词") << endl;
	cin.clear();
	system("pause");
}

5-23

#include<iostream>
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	cout << a / b << endl;
	cin.get();
	system("pause");
}

5-24

#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
	int a, b, result;
	cin >> a >> b;
	if (b == 0) {
		throw runtime_error("除数不能为0");
	}
	cout << a / b << endl;
	cin.get();
	system("pause");
}

5-25

#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
	int a, b, result;
	cin >> a >> b;
	try {
		if (!b) {
			throw runtime_error("除数不能为0!");
		}
		result = a / b;
	}
	catch (runtime_error error) {
		cout << error.what() << endl;
		cout << "继续吗(y/n)?" << endl;
		char ch;
		cin >> ch;
		if (ch == 'y' or ch == 'Y') {
			cout << "0" << endl;
		}
		return 0;
	}
	cout << a / b << endl;
	cin.get();
	system("pause");
}
发布了22 篇原创文章 · 获赞 36 · 访问量 6075

猜你喜欢

转载自blog.csdn.net/cxm2643199642/article/details/104363391