C++ Primer(5e)第5章习题

5.1

只含一个单独的分号的语句叫空语句;如果在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该
使用空语句。

5.2

用花括号括起来的语句和声明的序列叫块也叫复合语句;如果在程序的某个地方,语法上需要一条语句,
但是逻辑上需要多条语句,则应使用复合语句。

5.3

for (int sum = 0, val = 1; val <= 10; ++val)
    sum += val;

5.4

(a) while (string::iterator iter != s.end()) { /* */ }
当迭代器所指对象不是字符串最后一个字符时,进行块内语句
(b)while (bool status = find(word)) { /* */ }
         if (!status) { /* */ }
  由于if也需要访问status,故status必须定义在语句的外部。
  修改如下:
bool status;
while ( status = find(word)) { /* */ }
	if (!status) { /* */ }

5.5

#include
#include
#include
using namespace std;

int main()
{
	const vector<string> scores = { "F", "D", "C", "B", "A","A++" };
	int grade = 0;
	cout << "Enter a grade(0~100):" << endl;
	cin >> grade;
	string lettergrade;
	if (grade < 60)
		lettergrade = scores[0];
	else
		lettergrade = scores[(grade - 50) / 10];
	cout << lettergrade << endl;
	return 0;
}

5.6

#include
#include
#include
using namespace std;

int main()
{
	const vector<string> scores = { "F", "D", "C", "B", "A","A++" };
	int grade = 0;
	cout << "Enter a grade(0~100):" << endl;
	cin >> grade;
	string lettergrade = (grade < 60) ? scores[0] : scores[(grade - 50) / 10];
	cout << lettergrade << endl;
	return 0;
}

5.7

(a) if (ival1 != ival2)
		ival1 = ival2;
	else
		ival1 = ival2 = 0;
		
(b)if (ival < minval)
	{
		minval = ival;
		occurs = 1;
	}
	
(c) int ival;
	if (ival = get_value())
		cout << "ival = " << ival << endl;
	if (!ival)
		cout << "ival = 0\n";

(d) if (int ival = 0)
		ival = get_value();

5.8

悬垂else:如何知道某个给定的else是和哪个if匹配
解决方法:else与离它最近的尚未匹配的if匹配

5.9

#include
using namespace std;

int main()
{
	unsigned vowelCnt = 0, otherCnt = 0;
	char ch;
	while (cin >> ch)
	{
		switch (ch)
		{
		case 'a': case 'e': case 'i': case 'o': case 'u':
			++vowelCnt;
			break;
		default:
			++otherCnt;
			break;
		}
	}
	cout << "Number of vowels: " << vowelCnt << endl;
	cout << "Number of others: " << otherCnt << endl;
	return 0;
}

5.10

#include
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;
		default:
			break;
		}
	}
	cout << "Number of vowel a:" << aCnt << endl;
	cout << "Number of vowel e:" << eCnt << endl;
	cout << "Number of vowel i:" << iCnt << endl;
	cout << "Number of vowel o:" << oCnt << endl;
	cout << "Number of vowel u:" << uCnt << endl;
	return 0;
}

5.11 5.12

#include
using namespace std;

int main()
{
	unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
	unsigned nCnt = 0, sCnt = 0, tCnt = 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;
		case '\n':
			++nCnt;
			break;
		case '\t':
			++tCnt;
			break;
		case ' ':
			++sCnt;
			break;
		default:
			break;
		}
	}
	cout << "Number of vowel a:" << aCnt << endl;
	cout << "Number of vowel e:" << eCnt << endl;
	cout << "Number of vowel i:" << iCnt << endl;
	cout << "Number of vowel o:" << oCnt << endl;
	cout << "Number of vowel u:" << uCnt << endl;
	cout << "Number of '\\n:" << nCnt << endl;
	cout << "Number of '\\t:" << tCnt << endl;
	cout << "Number of ' ':" << sCnt << endl;
	return 0;
}

这两题建议参考这位博主: https://blog.csdn.net/fengzhanghao23/article/details/48444377

5.13

(a)在每个case后面忘记加上break
修改如下:
unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
char ch = next_text();
switch (ch)
{
	case 'a': aCnt++; break;
	case 'e': eCnt++; break;
	default: iouCnt++; break;
}

(b)case 1中定义的ix不能在default中使用
修改如下:
	unsigned index = some_value();
	int ix;
	switch (index)
	{
	case 1:
		ix = get_value();
		ivec[ix] = index;
		break;
	default:
		ix = ivec.size() - 1;
		ivec[ix] = index;
		break;
	}
	
(c)每条case语句后面只能由一个常量
修改如下:
	unsigned evenCnt = 0, oddCnt = 0;
	int digit = get_num() % 10;
	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语句后面是个常量
修改如下:
	const unsigned ival = 512, jval = 1024, kval = 4096;
	unsigned bufsize;
	unsigned swt = get_bufCnt();
	switch (swt)
	{
	case ival:
		bufsize = ival * sizeof(int);
		break;
	case jval:
		bufsize = jval * sizeof(int);
	case kval:
		bufsize = kval * sizeof(int);
		break;
	default:
		break;
	}

5.14

#include
#include
using namespace std;

int main()
{
	string word, lastword, maxword;   // 当前输入的单词、上一个单词、连续次数最多的单词
	unsigned maxcnt = 1, cnt = 1;     // 最大连续次数、 次数
	cin >> word;
	maxword = word;        // 把第一个输入的单词当作连续次数最多的单词
	lastword = word;       // 把第一个输入的单词当作上一个单词
	while (cin >> word)    // 输入下一个单词
	{
		if (word == lastword)
			++cnt;
		else
			cnt = 1;
		if (cnt > maxcnt)     // 比较连续单词的次数是否是最大的
		{
			maxcnt = cnt;
			maxword = word;
		}
		lastword = word;
	}
	if (maxcnt > 1)
		cout << maxword << " occurs " << maxcnt << " times." << endl;
	else
		cout << "nothing!" << endl;    // 最后结束输入按Ctrl + Z
	return 0;
}

5.15

(a) 
int ix = 0;
for (; ix != sz; ++ix) { /*...*/ }
if (ix != sz)
	// ...
(b)
for (int ix = 0; ix != sz; ++ix) { /*...*/ }
(c)
for (int ix = 0; ix != sz; ++ix) { /*...*/ }

5.18

(a)当输入不为空时,输入2个整数计算它们的和。
修改如下:
	do
	{
		int v1, v2;
		cout << "Please enter two numbers to sum:";
		if (cin >> v1 >> v2)
			cout << "Sum is: " << v1 + v2 << endl;
	} while (cin);
	
(b)当ival不为0时进行循环。
修改如下:
	int ival;
	do
	{
	  // ....	
	} while (ival = get_response());
	
(c)当ival不为0时进行循环。
感觉没错。。。

5.19

#include
#include
using namespace std;

int main()
{
	string rsp;
	do {
		cout << "Enter two strings: ";
		string s1, s2;
		cin >> s1 >> s2;
		decltype(s1) s3 = (s1.size() < s2.size()) ? s1 : s2;
		cout << s3 << endl;
		cout << "More? Enter Y or N: ";
		cin >> rsp;
	} while (!rsp.empty() && rsp[0] != 'n');
	return 0;
}

5.20

#include
#include
using namespace std;

int main()
{
	string word, nextword;
	int cnt = 1;
	cout << "Enter a string:" << endl;
	if (cin >> word)
	{
		while (cin >> nextword)
		{
			if (nextword == word)
			{
				cout << nextword << endl;
				++cnt;
				break;
			}
			else
			{
				word = nextword;
				cnt = 1;
			}
		}
		if (cnt == 1)
			cout << "nothing!" << endl;
	}
	return 0;
}

5.21

#include
#include
using namespace std;

int main()
{
	string word, nextword;
	int count = 1;
	cout << "Enter a string:" << endl;
	if (cin >> word && word[0] >= 'A' && word[0] <= 'Z')
	{
		while (cin >> nextword)
		{
			if (nextword == word)
			{
				cout << nextword << endl;
				++count;
				break;
			}
			else
			{
				word = nextword;
				count = 1;
			}
		}
		if (count == 1)
			cout << "nothing!" << endl;
	}
	else
		cout << "nothing!" << endl;
	return 0;
}

[注]:goto语句、try语句块和异常处理可以大致游览,没必要精读。

你可能感兴趣的:(C++)