C++Primer第五章习题

//5.9
#include<iostream>

using namespace std;
int main()
{
	unsigned cnt = 0;
	char s;
	while (cin >> s&&s!='8')
	{
		if (s == 'a')
		{
			++cnt;
		}
		if (s == 'e')
		{
			++cnt;
		}
		if (s == 'i')
		{
			++cnt;
		}
		if (s == 'o')
		{
			++cnt;
		}
		if (s == 'u')
		{
			++cnt;
		}
	}
	cout << "元音字母有: " << cnt << "个"<<endl;
}
//5.10
#include<iostream>

using namespace std;
int main()
{
	unsigned aCnt = 0,eCnt = 0,iCnt = 0,oCnt = 0,uCnt = 0;
	char s;
	while (cin >> s&&s!='8')
	{
		switch (s)
		{
		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:
			break;
		}
	}
	cout << "元音字母a有: " << aCnt << "个"<<endl;
	cout << "元音字母e有: " << eCnt << "个" << endl;
	cout << "元音字母i有: " << iCnt << "个" << endl;
	cout << "元音字母o有: " << oCnt << "个" << endl;
	cout << "元音字母u有: " << uCnt << "个" << endl;
	return 0;
}
//5.11
#include<iostream>

using namespace std;
int main()
{
	unsigned aCnt = 0,eCnt = 0,iCnt = 0,oCnt = 0,uCnt = 0,spaceCnt = 0,tabCnt = 0,newlineCnt = 0;
	char s;
	while (cin >> s&&s!='8')
	{
		switch (s)
		{
		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 ' ':
			++spaceCnt;
			break;
		case '\t':
			++tabCnt;
			break;
		case '\n':
			++newlineCnt;
			break;
		default:
			break;
		}
	}
	cout << "元音字母a有: " << aCnt << "个"<<endl;
	cout << "元音字母e有: " << eCnt << "个" << endl;
	cout << "元音字母i有: " << iCnt << "个" << endl;
	cout << "元音字母o有: " << oCnt << "个" << endl;
	cout << "元音字母u有: " << uCnt << "个" << endl;
	cout << "空格有: " << spaceCnt << "个" << endl;
	cout << "制表符有: " << tabCnt << "个" << endl;
	cout << "换行符有: " << newlineCnt << "个" << endl;
	return 0;
}
//5.12
#include<iostream>
#include<string>

using namespace std;
int main()
{
	unsigned curcnt = 1,max = 0;
	string curstr, prestr = "",maxstr;
	cout << "请输入字符串:" << endl;
	while (cin >> curstr&&curstr != "final")
	{
		if (curstr == prestr)
		{
			++curcnt;
			if (curcnt > max)
			{
				max = curcnt;
				maxstr = curstr;
			}
		}
		else
		{
			curcnt = 1;
		}
		prestr = curstr;
	}
	if (max > 1)
		cout << "相同相邻字符串次数最大为:" << max << endl;
	else
		cout << "每一个字符串出现一次"<<endl;
}
//5.17
#include<iostream>
#include<vector>

using namespace std;
int main()
{
	vector<int> v{ 1,1,2,3,4 };
	vector<int> v1{ 1,1,2 ,3,4,5,4};

	auto it = v.begin();
	auto it1 = v1.begin();

	while (it != v.end() && it1 != v1.end())
	{
		if (*it != *it1)
		{
			cout << "v与v1不存在前缀关系" << endl;
		}
		++it;
		++it1;		
	}
	if (it == v.end())
	{
		cout << "v是v1的前缀" << endl;
	}
	if (it1 == v1.end())
	{
		cout << "v1是v的前缀" << endl;
	}
	return 0;
}
//5.19
#include<iostream>
#include<string>

using namespace std;
int main()
{
	do
	{
		string str1, str2;
		cout << "请输入两个字符串" << endl;
		cin >> str1 >> str2;
		if (str1.size() > str2.size())
		{
			cout << "str1比str2长" << endl;
		}
		if (str1.size() < str2.size())
		{
			cout << "str1比str2短" << endl;
		}
		else
			cout << "str1比str2一样长" << endl;
	} while (cin);
}
//5.20
#include<iostream>
#include<string>

using namespace std;
int main()
{
	string str1, str2 = "";
	bool flag = true;
	cout << "输入一个字符:" << endl;
	while (cin>>str1)
	{
		if (str1 == str2)
		{
			flag = false;
			cout << "连续输出的字符是:" << str1 << endl;
			break;
		}
		str2 = str1;
	}
	if (flag)
	{
		cout << "没有连续相等的字符" << endl;
	}
	return 0;
}
//5.21
#include<iostream>
#include<string>
//#include<cctype>

using namespace std;
int main()
{
	string str1, str2 = "";
	bool flag = true;
	cout << "输入一个字符:" << endl;
	while (cin>>str1)
	{
		if (!isupper(str1[0]))
			continue;
		if (str1 == str2)
		{
			flag = false;
			cout << "连续输出的字符是:" << str1 << endl;
			break;
		}
		str2 = str1;
	}
	if (flag)
	{
		cout << "没有连续相等的字符" << endl;
	}
	return 0;
}
//5.23
#include<iostream>

using namespace std;
int main()
{
	int i, j;
	cout << "请输入两个整数" << endl;
	cin >> i >> j;
	if (j == 0)
	{
		cout << "除数不能为0" << endl;
		return -1;
	}
	cout << i / j;
	return 0;
}
//5.25
#include<iostream>
#include<stdexcept>

using namespace std;
int main()
{
	int i, j;
	cout << "请输入两个整数" << endl;
	while (cin >> i >> j)
	{
		try
		{
			if (j == 0)
			{
				throw runtime_error("除数不能为0");
			}
			cout << i / j;
		}
		catch (runtime_error err)
		{
			cout << err.what() << endl;
			cout << "需要继续吗?y or n" << endl;
			char ch;
			cin >> ch;
			if (ch == 'y')
				break;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38619342/article/details/83064380