C++ Primer 5th的练习—第五章

Ex_5.5

/*
把数字成绩转换成字母成绩,100分对应A++  90分对应A  80分对应B
							70分对应C  60分对应D  <60分对应F
			除100分以外,末尾8,9的添加+号,末尾0,1,2的添加-号
*/

#include 
#include 
#include 
#include 
using namespace std;
int main()
{
	const vector scores = { "F","D","C","B","A","A++" };
	//这里之所以倒过来写,便于后面的下标计算
	int grade;
	string lettergrade;
	while (cin >> grade && grade >= 0 && grade <= 100)
	{
		if (grade < 60)
			lettergrade = scores[0];
		else
			//使用计算下标的方式打打减少代码量
			lettergrade = scores[(grade - 50) / 10];
		
		if (grade >= 60 && grade != 100)	//不及格的分数不讨论加减号
			if (grade % 10 >= 3) {
				if (grade % 10 >= 8)
					lettergrade += '+';		//这里可用"" 也可用''
			}
			else
				lettergrade += "-";
			
		cout << lettergrade << endl;
	}
	return 0;
}

Ex_5.6

//使用条件运算符代替上题中的if else语句
#include 
#include 
#include 
using namespace std;
int main()
{
	const vector scores = { "F","D","C","B","A","A++" };
	int grade;
	string lettergrade;
	while (cin >> grade && grade >= 0 && grade <= 100)
	{
		grade < 60 ? lettergrade = scores[0] : lettergrade 
= scores[(grade - 50) / 10];			

		if (grade >= 60 && grade != 100)
			grade % 10 >= 8 ? lettergrade += '+' : 
grade % 10 <= 2 ? lettergrade += '-' : lettergrade += "";

		cout << lettergrade << endl;
	}
	return 0;
}

Ex_5.9

//使用if语句,统计从cin读入文本中有多少个元音字母
#include 
using namespace std;
int main()
{
	char ch;
	int cnt = 0;
	while (cin >> ch)
		switch (ch){
			case'a':	case'A':
			case'e':	case'E':
			case'i':	case'I':
			case'o':	case'O':
			case'u':	case'U':
				++cnt; break;
			default:
			;
		}
	cout << "The vowel numbers: " << cnt << endl;

	return 0;
}

Ex_5.11

//修改程序,也能统计制表符,空格,换行符的数量
#include 
using namespace std;
int main()
{
	char ch;
	int cnt = 0, spacecnt = 0, tabcnt = 0, linecnt = 0;
	//使用cin.get()获取字符,因为cin会忽略\t \n和space
	while (cin.get(ch))
		switch (ch) {
			case'a':	case'A':
			case'e':	case'E':
			case'i':	case'I':
			case'o':	case'O':
			case'u':	case'U':
				++cnt; break;
			case' ':
				++spacecnt; break;
			case'\t':
				++tabcnt; break;
			case'\n':
				++linecnt; break;
			default:
				;
		}
	cout << "The vowel numbers: " << cnt << endl;
	cout << "The space character numbers: " << spacecnt << endl;
	cout << "The tab character numbers: " << tabcnt << endl;
	cout << "The line character numbers: " << linecnt << endl;

	return 0;
}

Ex_5.12

/*
修改程序,使其能统计含有2个字符的字符序列 ff,fl,fi
一个字符只会被统计一次,例如xxxxxxxfflxxxxxxxx
我们统计的结果是		ff一次,fl零次,fi零次
*/
#include 
using namespace std;
int main()
{
	char prech = '\0', ch;	//定义prech记录前一个字符
	int ffcnt = 0, flcnt = 0, ficnt = 0;
	//使用cin.get()获取字符,因为cin会忽略\t \n和space
	while (cin >> ch)
	{
		//设置一个标记,仅当flag为真时,ch赋给prech
		bool flag = true;
		if (prech == 'f')
			switch (ch)
			{
				case'f':
					++ffcnt; 
					break;
				case'l':
					++flcnt; 
					break;
				case'i':
					++ficnt; 
					break;
				default:
					;
			}
		if (!flag)
			prech = '\0';
		else
			prech = ch;		
	}
		
	cout << "The ff charcater numbers: " << ffcnt << endl;
	cout << "The fl character numbers: " << flcnt << endl;
	cout << "The fi character numbers: " << ficnt << endl;
	
	return 0;
}

Ex_5.14

//查找重复出现单词的最大次数
#include 
#include 
using namespace std;
int main()
{
	//当前字符串,前一个字符串,重复出现次数最多字符串
	string currString, preString, maxString;
	//当前字符串的次数,次数最多的次数
	int currCnt = 0, maxCnt = 0;

	while (cin >> currString) {
		if (currString == preString) {
			++currCnt;
			if (currCnt > maxCnt) {
				maxCnt=currCnt;
				maxString = currString;
			}
		}
		else
			currCnt = 1;
		preString = currString;
	}
	if (maxCnt > 1)
		cout << "The max repeated string is " << maxString << endl
		<< "And the count of the string is " << maxCnt << endl;
	else
		cout << "No any strings have ever appeared in succession." << endl;

	return 0;
}

Ex_5.17

//实现两个vector的逐元素比较,检查一个是否是另一个的前缀

#include 
#include 
using namespace std;
int main()
{
	vector v1 = { 0,1,1,2 };
	vector v2 = { 0,1,1,3 };
	auto it1 = v1.begin();
	auto it2 = v2.begin();
	//使用迭代器遍历v1和v2
	while (it1 != v1.end() && it2 != v2.end())
	{
		if (*it1 != *it2) {		//当前两个元素不相等,不可能是前缀,直接跳出循环
			cout << "v1 and v2 are not prefixes to each other." << endl;
			break;
		}
		++it1;
		++it2;
	}

	if (it1 == v1.end() && it2 != v2.end())		//v1.size() < v2.size()
		cout << "v1 is v2's prefix." << endl;
	if (it2 == v2.end() && it2 != v2.end())		//v1.size() > v2.size()
		cout << "v2 is v1's prefix." << endl;
	if (it2 == v2.end() && it2 == v2.end())		//v1.size() = v2.size()
		cout << "v1 is equal to v2, and they are prefixes to each other." << endl;

	return 0;
}

Ex_5.19

#include 
#include 
using namespace std;
int main()
{
	do {
		string str1, str2;
		cout << "Please enter two strings:";
		cin >> str1 >> str2;
		if (str1.size() > str2.size())
			cout << "The smaller string is " << str2 << endl;
		else
			cout << "The smaller string is " << str1 << endl;
	} while (cin);

	return 0;
}

//或者使用条件运算符,代码更简洁,不过可读性低
	do {
		string str1, str2;
		cin >> str1 >> str2;
		cout << ((str1.size() > str2.size()) ? str2 : str1) 
<< endl;				//注意把整个条件运算符括起来
	} while (cin);

Ex_5.20

//从标准输入中读取string对象,直到连续出现两个相同的单词
//或所有单词都读完

#include 
#include 
using namespace std;
int main()
{
	string currStr, preStr;
	bool flag = false;		//设置一个标记,控制输出流

	while (cin >> currStr){
		if (currStr == preStr){
			flag = true;
			cout << currStr << "连续重复出现了2次。" << endl;
			break;
		}
		preStr = currStr;		
	}
	if (!flag)
		cout << "没有任何单词是连续重复出现的。" << endl;

	return 0;
}

Ex_5.21

//修改上题程序,使其找到的重复单词必须以大写字母开头
#include 
#include 
#include 
using namespace std;
int main()
{
	string currStr, preStr;
	bool flag = false;		

	while (cin >> currStr) {
		if (!isupper(*currStr.begin()))
			continue;		//如果首字母不是大写字母,进行下一次迭代
		if (currStr == preStr) {
			flag = true;
			cout << currStr << "连续重复出现了2次。" << endl;
			break;
		}
		preStr = currStr;
	}
	if (!flag)
		cout << "没有任何单词是连续重复出现的。" << endl;

	return 0;
}

Ex_5.23

#include 
using namespace std;
int main()
{
	int v1, v2;
	while (cin >> v1 >> v2) {
		if (v2 == 0) {
			cout << "The divisor can't be zero!" << endl;
			return -1;
		}			
		cout << v1 / v2 << endl;	//若除数为0,这句并不会被执行
	}
	return 0;
}

Ex_5.25

#include 
#include 
using namespace std;
int main()
{
	int v1, v2;
	while (cin >> v1 >> v2) {
		try {
			if (v2 == 0) {
				throw runtime_error("The divisor can't be zero!");
			}
			cout << v1 / v2 << endl;
		}
		catch(runtime_error err){
			cout << err.what() << endl;		//返回初始化的字符串初始值
			cout << "Again? Enter y or n" << endl;
			char c;
			cin >> c;
			if (!cin || c == 'n')
				break;
		}
	}
	return 0;
}

你可能感兴趣的:(C++,Primer,练习)