5.1
最简单的语句时空语句,空语句只含有一个单独的分号;
如果程序的某个地方,语法上需要一条语句但逻辑上不需要,此时应该使用空语句
5.2
复合语句是指用花括号括起来的语句和声明的序列(可能为空),复合语句也被称为块
5.3
#include<iostream>
int main()
{
int val = 1, sum = 0;
while (val <= 10)
sum += val, ++val;
std::cout << sum << std::endl;
return 0;
}
5.4
a) std::string::iterator iter =s.begin();
while(iter!=s.end()) {/…/}
b) bool status;
while(status=find(world)){/…/}
if(!status){/…/}
5.5
#include<iostream>
#include<vector>
#include<string>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
vector<string>scores = { "F","D","C","B","A","A++" };
string lettergrade;;
int grade{};
while (cin >> grade)
{
if (grade < 0 || grade>100)
{
cout << "out of range" << endl;
break;
}
if (grade < 60)
{
lettergrade = scores[0];
}
else
{
lettergrade = scores[(grade - 50) / 10];
if (grade != 100) {
if (grade % 10 > 7)
lettergrade += "+";
else if (grade % 10 < 3)
lettergrade += "-";
}
}
cout << lettergrade << endl;
}
}
5.6
#include<iostream>
#include<vector>
#include<string>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
vector<string>scores = { "F","D","C","B","A","A++" };
string lettergrade;
int grade{};
while (cin >> grade)
{
lettergrade = (grade<0||grade>100)?"out of range": grade < 60 ? scores[0] : scores[(grade - 50) / 10];
lettergrade += (grade >= 100 || grade < 60) ? "" : (grade % 10 > 7) ? "+" : (grade % 10 < 3) ? "-" : "";
cout << lettergrade << endl;
}
}
5.7
(a) if (ival1 != ival2)
ival1 = ival2;
else ival1 = ival2 = 0;
(b) if (ival < minval)
{
minval = ival;
occurs = 1;
}
© int val;
if (ival = get_value())
cout << "ival = " << ival << endl;
if (!ival)
cout << “ival = 0\n”;
(d) if (ival == 0)
ival = get_value();
5.8
当一个if语句嵌套在另一个if语句中,会出现if多余else的情况,如何确定else和哪个if相匹配称为悬浮else
c++规定else与离它最近的未匹配if匹配
5.9
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char c{};
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
while (cin >> c)
{
if (c == 'a')
++aCnt;
if (c == 'e')
++eCnt;
if (c == 'i')
++iCnt;
if (c == 'o')
++ oCnt;
if (c == 'u')
++uCnt;
}
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.10
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char c{};
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
int otherCnt = 0;
while (cin >> c)
{
switch (c)
{
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:
++otherCnt;
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 other:" << otherCnt << endl;
return 0;
}
5.11
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char c{};
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
int otherCnt = 0;
while (cin >> std::noskpws>>c)
{
switch (c)
{
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:
++otherCnt;
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 other:" << otherCnt << endl;
return 0;
}
5.12
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
char c,prec;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
int tabCnt = 0, newlineCnt = 0;
int ffCnt = 0, fiCnt = 0, flCnt = 0;
int otherCnt = 0;
while (cin >> std::noskipws >> c)
{
switch (c)
{
case 'a':
case 'A':
++aCnt;
break;
case 'e':
case 'E':
++eCnt;
break;
case 'i':
if (prec == 'f') ++fiCnt;
case 'I':
++iCnt;
break;
case 'o':
case 'O':
++oCnt;
break;
case 'u':
case 'U':
++uCnt;
break;
case '\n':
++newlineCnt;
break;
case '\t':
case '\v':
++tabCnt;
break;
case 'f':
if (prec == 'f') ++ffCnt;
break;
case 'l':
if (prec == 'f') ++flCnt;
break;
default:
++otherCnt;
break;
}
prec = c;
}
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 ff:" << ffCnt << endl;
cout << "number of fi" << fiCnt << endl;
cout << "number of fl u:" << flCnt << endl;
cout << "number of newline:" << newlineCnt << endl;
cout << "number of tab:" << tabCnt << endl;
cout << "number of other:" << otherCnt << endl;
return 0;
}
5.13
a) 加上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) case1 可能被略过,ix应该定义在switch外
unsigned index = some_value();
int ix;
switch (index) {
case 1:
ix = get_value();
ivec[ ix ] = index;
break;
default:
ix = static_cast<int>(ivec.size())-1;
ivec[ ix ] = index;
}
c)
switch语法问题
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 0:
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);
break;
case kval:
bufsize = kval * sizeof(int);
break;
}
5.14
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
string s,pres,max_string;
unsigned int cnt = 1,max_cnt = 1;
while(cin >> s)
{
if(s == pres)
{
++cnt;
}else
{
if(cnt > max_cnt)
{
max_cnt = cnt;
max_string = pres;
}
cnt = 1;
}
pres = s;
}
if(cnt > max_cnt)
{
max_cnt = cnt;
max_string = pres;
}
(max_cnt > 1) ? (cout << max_string << ":" << max_cnt << endl) : (cout << "no repeat" << endl);
return 0;
}
5.15
a)ix只能在for循环内使用,如果要在外部使用,需要定义在for循环外
int ix;
for (ix = 0; ix != sz; ++ix) { /* ... */ }
if (ix != sz)
// . . .
b)没有初始化语句
int ix;
for (ix = 0; ix != sz; ++ix) { /* ... */ }
c) 如果sz的初始值为0,则不进入循环;如果sz的初始值不为0,则需要循环体内需要有语句退出循环,否则会无休止地执行下去。
5.16
// while idiomatic
int i;
while ( cin >> i )
// ...
// same as for
for (int i = 0; cin >> i;)
// ...
// for idiomatic
for (int i = 0; i != size; ++i)
// ...
// same as while
int i = 0;
while (i != size)
{
// ...
++i;
}
更倾向于while,只需要判断条件,更简洁
5.17
#include<iostream>
#include<vector>
using std::cout;
using std::endl;
using std::vector;
bool func(vector<int>v1, vector<int>v2)
{
for (decltype(v1.size()) i = 0; i != v1.size() && i != v2.size(); ++i)
{
if (v1[i] != v2[i])
{
cout << "false" << endl;
return false;
}
cout << "true" << endl;
return true;
}
}
int main()
{
vector<int>v1{ 0,1,1,2 };
vector<int>v2{ 0,1,1,2,3,5,8 };
func(v1, v2);
return 0;
}
5.18
a)加花括号
b)condition使用的变量应该定义在循环体外
c)condition使用的变量应该定义在循环体外
5.19
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
do
{
string s1, s2;
cin >> s1 >> s2;
(s1.size() > s2.size()) ? (cout << s1 << endl) : (cout << s2 << endl);
} while (cin);
}
5.20
#include<iostream>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string Str, preStr, RepeatStr;
while (cin >> Str)
{
if (Str == preStr)
{
RepeatStr = Str;
cout << RepeatStr << endl;
break;
}
preStr = Str;
}
if (preStr.empty())
{
cout << "no repeat" << endl;
}
return 0;
}
5.22
do
{int sz = get_size()}
while(sz<=0);
5.23
#include<iostream>
int main()
{
int i1, i2;
std::cin >> i1 >> i2;
std::cout << i1 / i2 << std::endl
}
5.24
#include<iostream>
#include<stdexcept>
int main()
{
int i1, i2;
std::cin >> i1 >> i2;
if (i2 == 0)
{
throw std::runtime_error("divisor can not be 0");
}
else
{
std::cout << i1 / i2 << std::endl;
}
return 0;
}
terminate终止,未经处理的异常
5.25
#include<iostream>
#include<stdexcept>
int main()
{
int i1, i2;
while (std::cin >> i1 >> i2)
{
try {
if (i2 == 0)
{
throw std::runtime_error("divisor can not be 0");
}
std::cout << i1 / i2 << std::endl;
}
catch (std::runtime_error e)
{
std::cout << e.what() << "\ntry again? enter y or no" << std::endl;
char c;
std::cin >> c;
if (!std::cin || c == 'n') break;
}
}
return 0;
}
1 0
divisor can not be 0
try again? enter y or no
y
2 0
divisor can not be 0
try again? enter y or no
n