C++ Primer 中文第 5 版练习答案 第 5 章 语句(1~13)

C++ Primer 中文版(第 5 版)练习解答合集

自己写的解答,如有错误之处,烦请在评论区指正!


  1. 空语句是只含有一个;的语句。用于语法上需要但是逻辑上不需要一条语句的地方。
  2. 块,即复合语句,是指用花括号括起来的(可能为空的)语句和声明的序列。用于语法上需要一条语句但是逻辑上需要多条语句的地方。
  3. 可读性降低了。
#include <iostream>
using namespace std;
int main() {
	int val = 1, sum = 0;
	while (val <= 10)
		sum += val, ++val;
	cout << sum << endl;
	return 0;
} 
  1. a. 不能在初始化的同时进行判断。
string::iterator iter = s.begin();
while (iter != s.end()) {
	// do something
	++iter;
}

b. status 仅在 while 中可见。

bool status;
while (status = find(word)) { /*...*/ }
if (!status) { /*...*/ }
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
	const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
	int grade;
	string lettergrade;
	cin >> grade;
	if (grade < 60)
		lettergrade = scores[0]; 
	else {
		lettergrade = scores[grade / 10 - 5];
		if (grade < 100) {
			if (grade % 10 > 7)
				lettergrade += '+';
			else if (grade % 10 < 3)
				lettergrade += '-';
		} 
	}
	cout << lettergrade << endl;
	return 0;
} 
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
	const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
	int grade;
	string lettergrade;
	cin >> grade;
	lettergrade = (grade < 60 ? scores[0] : scores[grade / 10 - 5]);
	lettergrade += grade < 100 ? ((grade % 10 > 7) ? "+" : (grade % 10 < 3 ? "-" : "")) : "";
	cout << lettergrade << endl;
	return 0;
} 
// a. 第二行少逗号
if (ival1 != ival2)
	ival1 = ival2;
else ival1 = ival2 = 0;

// b. 少花括号
if (ival < minval) {
	minval = ival;
	occurs = 1;
}

// c. ival 对于第二个 if 不可见
int ival;
if (ival = get_value())
	cout << "ival = " << ival << endl;
if (!ival)
	cout << "ival = 0\n";

// d. 混用相等和赋值
if (!ival)	// if (ival == 0)
	ival = get_value();
  1. “悬垂else”指某个else语句在逻辑上应该与另一个if匹配,而实际上由于错误的写法,编译器会将其与不正确的if匹配。C++ 规定else与最近的尚未匹配的if匹配。
#include <iostream>
using namespace std;
int main() {
	int aCnt, eCnt, iCnt, oCnt, uCnt;
	aCnt = eCnt = iCnt = oCnt = uCnt = 0;
	char ch;
	while (cin >> ch) {
		if (ch == 'a')	++aCnt; 
		if (ch == 'e')	++eCnt;
		if (ch == 'i')	++iCnt;
		if (ch == 'o')	++oCnt;
		if (ch == 'u')	++uCnt;
	} 
	cout << "Number of vowel a: \t" << aCnt << '\n'
         << "Number of vowel e: \t" << eCnt << '\n'
         << "Number of vowel i: \t" << iCnt << '\n'
         << "Number of vowel o: \t" << oCnt << '\n'
         << "Number of vowel u: \t" << uCnt << endl;
	return 0;
}
#include <iostream>
using namespace std;
int main() {
	unsigned 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;
    	}
    }
    cout << "Number of vowel a: \t" << aCnt << '\n'
         << "Number of vowel e: \t" << eCnt << '\n'
         << "Number of vowel i: \t" << iCnt << '\n'
         << "Number of vowel o: \t" << oCnt << '\n'
         << "Number of vowel u: \t" << uCnt << endl;

    return 0;
}
#include <iostream>
using namespace std;
int main() {
	unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
	unsigned spaceCnt = 0, tableCnt = 0, returnCnt = 0;
    char ch;
    while ((ch = getchar()) != EOF) {
    	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':
    			++tableCnt;
    			break;
    		case '\n':
    			++returnCnt;
    			break;
    	}
    }
    cout << "Number of vowel a: \t" << aCnt << '\n'
         << "Number of vowel e: \t" << eCnt << '\n'
         << "Number of vowel i: \t" << iCnt << '\n'
         << "Number of vowel o: \t" << oCnt << '\n'
         << "Number of vowel u: \t" << uCnt << '\n'
         << "Number of space  : \t" << spaceCnt << '\n'
         << "Number of '\\n'  : \t" << returnCnt << '\n'
         << "Number of '\\t'  : \t" << tableCnt << endl;
    return 0;
}
#include <iostream>
using namespace std;
int main() {
	unsigned ffCnt = 0, flCnt = 0, fiCnt = 0;
    char now, last = '\0';
    while ((now = getchar()) != EOF) {
    	switch (now) {
    		case 'f':
    			if (last == 'f')
					++ffCnt;
    			break;
    		case 'l':
    			if (last == 'f')
					++flCnt;
    			break;
    		case 'i':
    			if (last == 'f')
					++fiCnt;
    			break;
    	}
    	last = now;
    }
    cout << "Number of 'ff': \t" << ffCnt << '\n'
         << "Number of 'fl': \t" << flCnt << '\n'
         << "Number of 'fi': \t" << fiCnt << endl;
    return 0;
}
  1. a. 每个case最后都漏了break;
    b. 变量ix必须定义在switch外面,因为定义在里面的话,该定义有可能会被跳过;
    c. 不能这样同时判断,原来的每个case都要分开写成 5 个case
    d. 只有整型常量表达式才能作为case的标签。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值