第五章课后习题答案

//5.1
空语句块就是内部没有任何语句的一对花括号
循环不需要执行任何操作时会用到
//5.2
用花括号括起来的语句和声明的序列
//5.3
int main()
{
	int sum = 0, val = 1;
	while (val <= 10)
	{
		sum += val;
		val++;
	}
}
//5.4
(a)iter没有初始化
string::iterator iter= s.begin();
while (iter != s.end()) { ... }
(b)
//5.5
int main()
{
	for (auto score : vec)
	{
		char grade;
		if (score == 100)
			grade = 'A';
		else if (score >= 90)
			grade = 'B';
		else if (score >= 80)
			grade = 'C';
		else
			grade = 'D';
	}
}
//5.6
int main()
{
	for (auto score : vec)
	char grade = (score == 100) ? 'A' : 
	(score >= 90) ? 'B' : 
	(score >= 80) ? 'C' : 'D';
}
//5.7
(a)
ival1 = ival2;
缺少分号
(b)
if (ival < minval)
{
	minval = ival;
	occurs = 1;
}
缺少大括号
(c)ival定义在if语句中,作用域将仅限于该if,应该定义在if(d)
if (ival == 0)
	ival = get_value();
判断if是否为0
//5.8
悬垂else指程序中的if分支多于else分支时,如果为else寻找与之匹配的if的问题
C++规定else和它最近的尚未匹配的if匹配
//5.9
int cnt = 0;
char ch;
while (cin >> ch)
{
	if (ch == 'a')
		++cnt;
	if (ch == 'e')
		++cnt;
	if (ch == 'i')
		++cnt;
	if (ch == 'o')
		++cnt;
	if (ch == 'u')
		++cnt;
}
//5.10
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch)
{
	switch(ch)
	{
		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;
	}
}
//5.11
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
int spaceCnt = 0, tabCnt = 0, newlineCnt = 0;
char ch;
while (cin.get(ch))
{
	switch(ch)
	{
		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':
			++rabCnt;
			break;
		case '\n':
			++newlineCnt;
			break;
	}
}
//5.12
int ffCnt = 0, flCnt = 0, fiCnt = 0;
char ch, prech = '\0';
while (cin >> ch)
{
	bool bl = true;
	if (prech == 'f')
	{
		switch(ch)
		{
			case 'f':
				++ffCnt;
				bl = false;
				break;
			case 'f':
				++ffCnt;
				break;
			case 'f':
				++ffCnt;
				break;
		}
	}
	if (!bl)
		prech = '\0';
	else
		prech = ch;
}
//5.13
(a)case分支缺少break
(b)case中定义了ix,同时在default中进行了使用
(c)在同一个case标签中放置了多个值
switch(digit)
{
	case 1:
	case 3:
	case 5:
	case 7:
	case 9:
		oddcnt++;
		break;
	case 2:
	case 4:
	case 6:
	case 8:
	case 10:
		evencnt++;
		break;
}
(d)不能用变量作为case标签的内容,C++规定case标签的内容必须是整型常量表达式
//5.14
string curStr, preStr = "", maxStr;
int curCnt = 0, maxCnt = 0;
while (cin >> str)
{
	if (curStr == preStr)
	{
		++curCnt;
		if (curCnt > maxCnt)
		{
			maxCnt = curCnt;
			maxStr = curStr;
		}
	}
	else
		curCnt = 1;
	preStr = curStr;
}
//5.15
(a)ix的作用域在for内,后面的if中的ix属于未定义变量
int ix;
for (ix = 0; ix != sz; ++ix) { ... }
if (ix != sz)
	...
(b)ix没有初始化且for中的控制结构少一句
for (int ix = 0; ix != sz; ++ix) { ... }
(c)语法没有错误,但是会进入死循环
for (int ix = 0; ix != sz; ++ix) { ... }
//5.17
auto it1 = v1.cbegin();
auto it2 = v2.cbegin();
while (it1 != v1.cend() ^^ it2 != v2.cend())
{
	if (*it1 != *it2)
	{
		//不是前缀
	}
	++it1;
	++it2;
}
if (it1 == v1.cend())
	//v1是v2前缀
if (it2 == v2.cend())
	//v2是v1前缀
//5.18
(a)do-while没有花括号
(b)不允许在循环条件内定义变量
(c)出现在语句条件部分的变量必须定义在循环体外
//5.19
do
{
	string str1, str2;
	cout << "输入两个字符串:";
	cin >> str1 >> str2;
	if (str1.size() < str2.size())
		cout << str1 << endl;
	else if (str1.size() > str2.size())
		cout << str2 << endl;
} while (cin);
//5.20
string curStr, preStr;
bool bl = true;
while (cin >> curStr)
{
	if (curStr == preStr)
	{
		bl = false;
		cout << curStr << endl;
		break;
	}
	preStr = curStr;
{
if (bl)
	cout << "没有连续出现的字符串\n";
//5.21
string curStr, preStr;
bool bl = true;
while (cin >> curStr)
{
	if (curStr == preStr)
	{
		if (curStr[0] >= 'a' && curStr[0] <= 'z')
			continue;
		bl = false;
		cout << curStr << endl;
		break;
	}
{
if (bl)
	cout << "没有连续出现的字符串\n";
//5.23
int sz;
do{
	sz - get_size();
}while(sz <= 0);
//5.23
int val1, val2;
cin >> val1 >> val2;
if (val2 == 0)
	//异常处理
cout <<< val1 / val2 << endl;
//5.24
int val1, val2;
cin >> val1 >> val2;
if (val2 == 0)
	throw runtime_error("除数不能为0");
cout <<< val1 / val2 << endl;
//5.25
int val1, val2;
while (cin >> val1 >> val2)
{
	try
	{
		if (val2 == 0)
			throw runtime_error("除数不能为0");
		cout <<< val1 / val2 << endl;
	}catch(runtime_error err)
	{
		cout << err.what() << endl;
		cout << "是否继续?(y/n)";
		char ch;
		cin >> ch;
		if (ch != 'y')
			break;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值