C++ Primer 中文版(第 5 版)练习解答合集
自己写的解答,如有错误之处,烦请在评论区指正!
- 空语句是只含有一个
;
的语句。用于语法上需要但是逻辑上不需要一条语句的地方。 - 块,即复合语句,是指用花括号括起来的(可能为空的)语句和声明的序列。用于语法上需要一条语句但是逻辑上需要多条语句的地方。
- 可读性降低了。
#include <iostream>
using namespace std;
int main() {
int val = 1, sum = 0;
while (val <= 10)
sum += val, ++val;
cout << sum << endl;
return 0;
}
- 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();
- “悬垂
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;
}
- a. 每个
case
最后都漏了break
;
b. 变量ix
必须定义在switch
外面,因为定义在里面的话,该定义有可能会被跳过;
c. 不能这样同时判断,原来的每个case
都要分开写成 5 个case
;
d. 只有整型常量表达式才能作为case
的标签。