Exercises Section 5.1
Ex5.1
; // null statement
Ex5.2
// block
{
}
Exercises Section 5.2
Ex5.4
a) while (string::iterator iter != s.end()) { /*... */ } // 未初始化 iter 的值
// 修改如下
string::iterator iter = s.begin();
while (iter != s.end()) { /*... */ }
b) while (bool status = find(word)) { /*... */ }
if (!status) { /*... */ } // status 不能在 while 循环外获取
// 修改如下
while (bool status = find(word))
{
if (!status) { /*... */ }
/*... */
}
Exercises Section 5.3.1
Ex5.7
a) if (ival1 != ival2)
ival1 = ival2
else ival1 = ival2 = 0;
// 修改如下
if (ival1 != ival2)
ival1 = ival2;
else
ival1 = ival2 = 0;
b) if (ival < minval)
minval = ival;
occurs = 1;
// 修改如下
if (ival < minval)
minval = ival;
occurs += 1;
c) if (int ival = get_value())
cout << "ival = " << ival << endl;
if (!ival)
cout << "ival = 0\n";
// 修改如下
int ival = get_value();
if (!ival)
cout << "ival = 0\n";
else
cout << "ival = " << ival << endl;
d) if (ival = 0)
ival = get_value();
// 修改如下
if (ival == 0)
ival = get_value();
Ex5.8
"dangling else":当有多个 if 嵌套时,如果只有一个 else,那么该 else 在没有明确表示匹配哪个 if 时,会自动匹配离它最近的 if。
Exercises Section 5.3.2
Ex5.9
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a_count = 0;
int e_count = 0;
int i_count = 0;
int o_count = 0;
int u_count = 0;
string s = "";
cout << "Please enter a string: " << endl;
getline(cin, s);
for (char c : s)
{
if (c == 'a' || c == 'A')
++a_count;
if (c == 'e' || c == 'E')
++e_count;
if (c == 'i' || c == 'I')
++i_count;
if (c == 'o' || c == 'O')
++o_count;
if (c == 'u' || c == 'U')
++u_count;
}
cout << a_count << " " << e_count << " " << i_count << " " << o_count << " " << u_count << endl;
system("pause");
return 0;
}
Ex5.10
#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 << endl;
cout << "Number of vowel e: \t" << eCnt << endl;
cout << "Number of vowel i: \t" << iCnt << endl;
cout << "Number of vowel o: \t" << oCnt << endl;
cout << "Number of vowel u: \t" << uCnt << endl;
system("pause");
return 0;
}
Ex5.11
#include<iostream>
using namespace std;
int main()
{
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, blackCnt = 0;
char ch;
while ((ch = cin.get()) != EOF) // cin 会自动忽略所有空白字符,故而使用cin.get()
{
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 ' ': case '\t': case '\n':
++blackCnt;
break;
}
}
cout << "Number of vowel a: \t" << aCnt << endl;
cout << "Number of vowel e: \t" << eCnt << endl;
cout << "Number of vowel i: \t" << iCnt << endl;
cout << "Number of vowel o: \t" << oCnt << endl;
cout << "Number of vowel u: \t" << uCnt << endl;
cout << "Number of blank spaces, tabs and newlines: \t" << blackCnt << endl;
system("pause");
return 0;
}
Ex5.13
a)
unsigned aCnt = 0, eCnt = 0, iouCnt = 0;
char ch = next_text();
switch(ch) // 缺少 break
{
case 'a': aCnt++;
case 'e': eCnt++;
default: iouCnt++;
}
// 修改如下
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)
unsigned index = some_value();
switch(index)
{
case 1:
int ix = get_value();
ivec[ix] = index;
break;
default:
ix = ivec.size() - 1; // 上个 case 初始化的变量不能在下一个case 中使用
ivec[ix] = index;
}
// 修改如下
unsigned index = some_value();
switch(index)
{
case 1:
int ix = get_value();
ivec[ix] = index;
break;
default:
int ix = ivec.size() - 1;
ivec[ix] = index;
break;
}
c)
unsigned evenCnt = 0, oddCnt = 0;
int digit = get_num() % 10;
switch(digit)
{
case 1, 3, 5, 7, 9: // 必须分开写,不能使用逗号
oddCnt++;
break;
case 2, 4, 6, 8, 10:
evenCnt++;
break;
}
// 修改如下
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)
unsigned ival = 512, jval = 1024, kval = 4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt)
{
case ival: // case 后面不能跟变量
bufsize = ival + sizeof(int);
break;
case jval:
bufsize = jval + sizeof(int);
break;
case kval:
bufsize = kval + sizeof(int);
break;
}
// 修改如下
unsigned ival = 512, jval = 1024, kval = 4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt)
{
case 512:
bufsize = ival + sizeof(int);
break;
case 1024:
bufsize = jval + sizeof(int);
break;
case 4096:
bufsize = kval + sizeof(int);
break;
}
Exercises Section 5.4.1
Ex5.14
#include<iostream>
#include<string>
using namespace std;
int main()
{
int max_cnt = 0, cnt = 0;
string max_word, word, front_word;
while (cin >> word)
{
if (front_word.empty())
{
max_word = word;
front_word = word;
++cnt;
max_cnt = cnt;
}
else
{
if (front_word == word)
{
++cnt;
front_word = word;
if (max_cnt < cnt)
{
max_cnt = cnt;
max_word = word;
}
}
else
{
front_word = word;
cnt = 1;
}
}
}
if (max_cnt > 1)
cout << "The " << max_word << " occurred " << max_cnt << " times" << endl;
else
cout << "No word occurred more than two times." << endl;
system("pause");
return 0;
}
Exercises Section 5.4.2
Ex5.15
a)
for (int ix = 0; ix != sz; ++ix) { /*... */ }
if (ix != sz) // ix 不能在 for 循环外使用
// ...
// 修改如下
int ix = 0;
for (; ix != sz; ++ix) { /* ... */ }
if (ix != sz)
// ...
b)
int ix;
for (ix != sz; ++ix) { /*... */ } // for 循环少了一条语句且 ix 未被初始化
// 修改如下
for (int ix = 0; ix != sz; ++ix) { /*... */ }
c)
for (int ix = 0; ix != sz; ++ix, ++sz) { /*...*/ } // for 循环永远不会停止
// 修改如下
for (int ix = 0; ix != sz; ++ix) { /*...*/ }
Ex5.17
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v1,v2;
int a=0,b=0;
while(cin>>a)
v1.push_back(a);
cin.clear(); // 清除ctrl z 状态 让 cin >> b 能进行下去
while(cin>>b)
v2.push_back(b);
if(v1.size()<v2.size())
{
int i=0;
while (i != v1.size() && v1[i] == v2[i])
{
++i;
}
if(i==v1.size())
{
cout<<" v1 is prefix of v2 "<<endl;
cout<<" true "<<endl;
}
else
cout<<" false "<<endl;
}
else
{
int i=0;
while (i != v2.size() && v1[i] == v2[i])
{
++i;
}
if(i==v2.size())
{
cout<<"v2 is prefix of v1"<<endl;
cout<<" false "<<endl;
}
else
cout<<" flase "<<endl;
}
system("pause");
return 0;
}
Exercises Section 5.4.4
Ex5.18
a) // 忘记花括号了;修正如下
do
{
int v1, v2;
cout << "Please enter two numbers to sum:";
if (cin >> v1 >> v2)
cout << "Sum is: " << v1 + v2 << endl;
} while (cin);
b) // while 括号后面的变量必须是事先声明了的; 修改如下
int ival = get_response();
do
{
// ...
} while (ival);
c) // ival 不能在 while 循环外使用;修改如下
int ival = get_response();
do
{
} while (ival);
Ex5.19
#include<iostream>
#include<string>
using namespace std;
int main()
{
do
{
string s1, s2;
cout << "Please enter two string :" << endl;
if (cin >> s1 >> s2)
{
if (s1 > s2)
cout << "s2 is less than s1" << endl;
else if (s1 < s2)
cout << "s1 is less than s2" << endl;
else
cout << "two string are equal" << endl;
}
} while (cin);
system("pause");
return 0;
}
Exercises Section 5.5.1
Ex5.20
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str, tmpstr;
unsigned cnt = 0;
cin >> str;
tmpstr = str;
while (cin >> str)
{
if (str == tmpstr)
{
++cnt;
cout << str << " occurs twice in succession" << endl;
break;
}
tmpstr = str;
}
if (cnt == 0)
{
cout << "No word occurs twice in succession" << endl;
}
system("pause");
return 0;
}
Exercises Section 5.5.2
Ex5.21
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str, tmpstr;
unsigned cnt = 0;
cin >> str;
tmpstr = str;
while (cin >> str)
{
if (str[0] >= 'A' && str[0] <= 'Z' && str == tmpstr)
{
++cnt;
cout << str << " occurs twice in succession" << endl;
break;
}
tmpstr = str;
}
if (cnt == 0)
{
cout << "No word occurs twice in succession" << endl;
}
system("pause");
return 0;
}
Exercises Section 5.5.3
Ex5.22
int sz = get_size();
while (sz <= 0)
{
sz = get_size();
}
Exercises Section 5.6.3
Ex5.23
#include<iostream>
using namespace std;
int main()
{
int v1, v2;
cout << "Please enter two numbers: ";
cin >> v1 >> v2;
while (v2 == 0)
{
cout << "The second number is zero, please enter another value: ";
cin >> v2;
}
cout << "The result is " << v1 / v2 << endl;
system("pause");
return 0;
}
Ex5.24
#include<iostream>
using namespace std;
int main()
{
int v1, v2;
cout << "Please enter two numbers: ";
cin >> v1 >> v2;
if (v2 != 0)
{
cout << "The result is " << v1 / v2 << endl;
system("pause");
return 0;
}
else
{
cerr << "The second number is zero!" << endl;
system("pause");
return -1;
}
}
Ex5.25
#include<iostream>
using namespace std;
int main()
{
int v1, v2;
cout << "Please enter two numbers: ";
cin >> v1 >> v2;
try
{
if (v2 == 0)
throw runtime_error("The second number is zero!");
}
catch(runtime_error err)
{
cout << err.what() << " Please enter another value: ";
cin >> v2;
}
cout << "The result is " << v1 / v2 << endl;
system("pause");
return 0;
}