C++ Primer第五章习题

习题5-1:
空语句就是什么都不写,只含有一个单独的分号。

;//空语句

使用场景:语法上需要一条语句但是逻辑上不需要。

习题5-2:
块就是复合语句,要用花括号括起来的。
使用场景:语法上需要一条语句,逻辑上需要多条语句。

习题5-3:

#include <iostream>

using namespace std;

int main()
{
		int sum = 0, val = 1;
		while (val <= 10)
				sum +=val, ++val;
		cout << sum << endl;
		return 0;
}

习题5-4:
1、iter没有初始化,如何与s.end()比较
2、while的条件是一个定义语句,编译可以通过,find(word)的返回值为条件值,但是if的条件会报错:没有定义,因为status作用域只在while当中。

习题5-5:

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

using namespace std;

int main()
{
		const vector<string> scores = {"F","D","C","B","A","A++"};
		int grade;
		vector<string> letterScores;
		vector<int> numScores;
		while (cin >> grade)
		{
				if (grade == -1)
						break;
				numScores.push_back(grade);
				if (grade<60)
						letterScores.push_back(scores[0]);
				else 
						letterScores.push_back(scores[(grade-50)/10]);
		}
		for (auto letter : letterScores)
				cout << letter << endl;
		return 0;
}

习题5-6:

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

using namespace std;

int main()
{
		const vector<string> scores = {"F","D","C","B","A","A++"};
		int grade;
		vector<string> letterScores;
		vector<int> numScores;
		while (cin >> grade)
		{
				if (grade == -1)
						break;
				string letter = (grade < 60) ? scores[0] : scores[(grade-50)/10];
				letterScores.push_back(letter);
		}
		for (auto letterScore : letterScores)
				cout << letterScore << endl;
		return 0;
}

习题5-9:
需要注意的是,这里我没有像前面那样设计键盘终止标志,通过流重定向进行的输入。

#include <iostream>

using namespace std;

int main()
{
		unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0;
		char c;
		while (cin >> c)
		{
				if (c == 'a')
						++aCnt;
				if (c == 'e')
						++eCnt;
				if (c == 'i')
						++iCnt;
				if (c == 'o')
						++oCnt;
				if (c == 'u')
						++uCnt;
		}
		cout << aCnt << endl;
		cout << eCnt << endl;
		cout << iCnt << endl;
		cout << oCnt << endl;
		cout << uCnt << endl;

		return 0;
}	

习题5-10:
题目的意思是通过switch case语句来写,我就直接用上面的程序来修改了

#include <iostream>

using namespace std;

int main()
{
		unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0;
		char c;
		while (cin >> c)
		{
				if (c == 'a' || c == 'A')
						++aCnt;
				if (c == 'e' || c == 'E')
						++eCnt;
				if (c == 'i' || c == 'I')
						++iCnt;
				if (c == 'o' || c == 'O')
						++oCnt;
				if (c == 'u' || c == 'U')
						++uCnt;
		}
		cout << aCnt << endl;
		cout << eCnt << endl;
		cout << iCnt << endl;
		cout << oCnt << endl;
		cout << uCnt << endl;

		return 0;
}	

习题5-11:

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

using namespace std;

int main()
{
		unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0,spaceCnt=0, tapCnt=0, enterCnt=0;
		char c;
		while (cin >> noskipws >> c)
		{
				if (c == 'a' || c == 'A')
						++aCnt;
				if (c == 'e' || c == 'E')
						++eCnt;
				if (c == 'i' || c == 'I')
						++iCnt;
				if (c == 'o' || c == 'O')
						++oCnt;
				if (c == 'u' || c == 'U')
						++uCnt;
				if (c == ' ')
						++spaceCnt;
				if (c == '\t')
						++tapCnt;
				if (c == '\n')
						++enterCnt;
		}
		cout << aCnt << endl;
		cout << eCnt << endl;
		cout << iCnt << endl;
		cout << oCnt << endl;
		cout << spaceCnt << endl;
		cout << tapCnt << endl;
		cout << enterCnt << endl;

		return 0;
}	

习题5-12:

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

using namespace std;

int main()
{
		unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0,spaceCnt=0, tapCnt=0, enterCnt=0, ffCnt=0, flCnt=0, fiCnt=0;
		char c, cNext;
		while (cin >> noskipws >> c)
		{
				if (c == 'f')
				{
						cin >> noskipws >> cNext;
						if ( cNext == 'f')
								++ffCnt;
						if ( cNext == 'l')
								++flCnt;
						if ( cNext == 'i')
								++fiCnt;
						c = cNext;
				}
				if (c == 'a' || c == 'A')
						++aCnt;
				if (c == 'e' || c == 'E')
						++eCnt;
				if (c == 'i' || c == 'I')
						++iCnt;
				if (c == 'o' || c == 'O')
						++oCnt;
				if (c == 'u' || c == 'U')
						++uCnt;
				if (c == ' ')
						++spaceCnt;
				if (c == '\t')
						++tapCnt;
				if (c == '\n')
						++enterCnt;
				
		}
		cout << aCnt << endl;
		cout << eCnt << endl;
		cout << iCnt << endl;
		cout << oCnt << endl;
		cout << spaceCnt << endl;
		cout << tapCnt << endl;
		cout << enterCnt << endl;
		cout << "the number of fi" << fiCnt << endl;
		cout << "the number of fl" << flCnt << endl;
		cout << "the number of ff" << ffCnt << endl;

		return 0;
}	

习题5-12的switch写法:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	char c,cNext; 
	int aCnt=0, bCnt=0, ffCnt=0, flCnt=0,fiCnt=0;
	while (cin >> c)
	{
		switch (c)
		{
			case 'f':case 'F' :
			cin >> cNext;
			switch (cNext)
			{
				case 'f':ffCnt++;break;
				case 'l':flCnt++;break;
				case 'i':fiCnt++;break;
				default:c=cNext;break;
			}

			case 'a':case 'A' :aCnt++;break;
			case 'b':case 'B' :bCnt++;break;
		}
	}
	cout << aCnt << endl;
	cout << bCnt << endl;
	cout << ffCnt << endl;
	cout << fiCnt << endl;
	cout << flCnt << endl;

}


习题5-17:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> vec1{0,1,1,2}, vec2{0,1,1,2,3,5,8};
	int size1=vec1.size(), size2=vec2.size(),minSize;
	minSize = size1 < size2? size1 : size2;
	bool head = true;
	for (int i = 0; i != minSize; ++i)
	{
		if (vec1[i] != vec2[i])
		{
			cout << "False" << endl;
			head = false;
			break;
		}
	}
	if (head)
		cout << "True" << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值