C++Prime 第五章
练习5.1
以分号结尾成为空语句,语法上需要一条语句,逻辑上不需要时.
练习5.2
用花括号括起来的语句语句,可以为空,称为复合语句,或者块.语法上需要一条语句,逻辑上需要多条语句时.
练习5.3 略
练习5.4
(a) while(string::iterator iter != s.end()) //iter未初始化直接用,while判断语句里不允许定义变量(for可以,while不可以!!!)
(b) while(bool status = find(word)) if(!status) {/*...*/} //有问题,if永远不会被执行
练习5.5
#include <iostream>
#include <vector>
using namespace std;
int main()
{
const vector<string> score{ "F","D","C","B","A","A++" };
cout << "Please enter a score:";
string ans;
int grade;
cin >> grade;
if (grade / 10 < 6)
ans = score[0];
else//及格了
{
ans = score[(grade - 50) / 10];
if (grade != 100)
{
if (grade % 10 >= 7)//个位大于等于7,认为是该分段优秀学生
ans += "+";
else if (grade % 10 <= 3)//个位小于等于3,认为是该分段较差学生
ans += "-";
else
;//空语句,不做改变,该分段普通学生
}
else
;//空语句,100就是A++
}
cout << ans << endl;
return 0;
}
练习5.6
#include <iostream>
#include <vector>
using namespace std;
int main()
{
const vector<string> score{ "F","D","C","B","A","A++" };
cout << "Please enter a score:";
string ans;
int grade;
cin >> grade;
ans = grade / 10 < 6 ? score[0] : grade == 100 ? "A++" : grade % 10 >= 7 ? score[(grade - 50) / 10] + "+" : grade % 10 >= 3 ? score[(grade - 50) / 10] : score[(grade - 50) / 10] + "-";
cout << ans << endl;
return 0;
}
练习5.7
(a) 缺少分号
(b) 缩进不帅,或者变成复合语句均符合答案
(c) if 语句里不能定义变量
(d) ival == 0
练习5.8
悬垂else: if比else多,与 就近的 尚未匹配的 if语句匹配.
练习5.9
#include <iostream>
#include <string>
using namespace std;
int main()
{
char alpha;
int aCnt = 0, oCnt = 0, eCnt = 0, iCnt = 0, uCnt = 0;
int otherCnt = 0;
while (cin >> alpha)
{
alpha = tolower(alpha);
if (alpha == 'a')
aCnt++;
else if (alpha == 'o')
oCnt++;
else if (alpha == 'e')
eCnt++;
else if (alpha == 'i')
iCnt++;
else if (alpha == 'u')
uCnt++;
else
otherCnt++;
}
cout << "a或者A有:" << aCnt << "个\n";
cout << "o或者O有:" << oCnt << "个\n";
cout << "e或者E有:" << eCnt << "个\n";
cout << "i或者I有:" << iCnt << "个\n";
cout << "u或者U有:" << uCnt << "个\n";
cout << "共计有:" << aCnt + oCnt + eCnt + iCnt + uCnt << "个\n";
return 0;
}
练习5.10
解法一:同上题.直接调用tolower函数.
解法二:每个if语句里判断一次,如if(c == 'a' || c == A") aCnt++;
解法三:switch语句里,大小写标签共用一段代码.如case 'a': case 'A': aCnt++;break;
练习5.11
int white_cnt = 0;
if(alpha == ' ' || alpha == '\t' || alpha == '\n')
white_cnt++;
练习5.12
和上面一模一样.
练习5.13
(a) break;
(b) switch内部的变量定义,不要初始化,非要初始化就加括号,缩小作用域.本体修改:
case 1:
int ix;
ix = get_value(); 其余相同
(c) 表示范围时,写在一行,不可省略case.修改: case 1:case 3:case 5 类推.
(d) case标签必须是常量表达式.
练习5.14
#include <iostream>
#include <string>
using namespace std;
int main()
{
int maxn = 0,tmpn = 0;
string maxstr,tmp,pre;//tmp是当前处理子串,pre是上一个子串
cin >> pre;//首先读取一个子串
tmpn = 1;//目前子串数量为1
maxn = 1;
bool find = false;//未找到最大连续子串
while (cin >> tmp)
{
if (tmp == pre)//是连续子串
tmpn++;
else
{
if (tmpn > maxn)//该子串出现次数大于当前最大的子串出现次数
{ //更新数据
maxn = tmpn;
maxstr = pre;
find = true;
}
tmpn = 1;//刷新子串出现次数和上一个子串
pre = tmp;
}
}
if (tmpn > maxn)//这里处理最后一个数据不会进入else的特殊情况
{
maxn = tmpn;
maxstr = pre;
find = true;
}
if (find)
cout << maxstr << "出现了" << maxn << "次!\n";
else
cout << "未出现连续重复" << endl;
return 0;
}
练习5.15
(a) 非必要的if语句判断
(b) ix未初始化,没有condition.
(c) ++sz ,中间不能有空格
练习5.16
具体情况具体选择
练习5.17
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vi1, vi2;
int num;
cout << "请输入第一组测试数据,以负数结束:\n";
while (cin >> num && num >= 0)
vi1.push_back(num);
cout << "请输入第二组测试数据,以负数结束:\n";
while (cin >> num && num >= 0)
vi2.push_back(num);
auto p1 = vi1.begin(), p2 = vi2.begin();
for (; p1 != vi1.end() && p2 != vi2.end(); ++p1, ++p2)
if (*p1 != *p2)
break;
if (p1 == vi1.end() || p2 == vi2.end())
cout << "是前缀" << endl;
else
cout << "不是" << endl;
return 0;
}
练习5.18
(a)花括号. (b)while里不允许定义变量 (c)do里不要定义while会用到的变量
练习5.19
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string s1, s2;
cout << "Please enter two strings:" << endl;
cin >> s1 >> s2;
do
{
cout << (s1.size() < s2.size() ? s1 : s2) << endl;
cout << "Please enter two strings:" << endl;
} while (cin >> s1 >> s2);
return 0;
}
练习5.20
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string cur_word,pre_word;
bool find = false;
while (cin >> cur_word)
{
if (cur_word == pre_word)
{
find = true;
break;
}
pre_word = cur_word;
}
cout << (find ? cur_word : "No word continuelly show." )<< endl;
return 0;
}
练习5.21
#include <iostream>
#include <vector>
using namespace std;
int main()
{
string cur_word, pre_word;
bool find = false;
while (cin >> cur_word)
{
if (!isupper(cur_word[0]))
continue;
if (cur_word == pre_word)
{
find = true;
break;
}
pre_word = cur_word;
}
cout << (find ? cur_word : "No word continuelly show.") << endl;
return 0;
}
练习5.23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n1, n2;
cout << "输入两个数字:" << endl;
while (cin >> n1 >> n2)
{
if (n2 != 0)
{
cout << n1 << " 除以 "<<n2<<" = "<<n1 / n2 << endl;
break;
}
else
cout << "除数不能为0.继续输入两个数字:" << endl;
}
return 0;
}
练习5.24
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "输入两个数字:" << endl;
cin >> n1 >> n2;
if (n2 == 0)
throw runtime_error("The divide can't be zero!\n");
cout << n1 << " 除以 "<<n2<<" = "<<n1 / n2 << endl;
return 0;
}
故意除数输入0,会显示有未经处理的运行时异常
练习5.25
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "输入两个数字:" << endl;
while (cin >> n1 >> n2)
{
try
{
if (n2 == 0)
throw runtime_error("The divide can't be zero!\n");
cout << n1 << " 除以 " << n2 << " = " << n1 / n2 << endl;
}
catch (runtime_error err)
{
cout << err.what() << "再次输入两个数:" << endl;
}
}
return 0;
}