练习5.1
答:空语句是只含一个分号(;)的语句,若循环体要执行的所有步骤都在循环头包含,则将循环体设为空语句
练习5.2
答:块是指复合语句,当一条简单语句放不下需要表达的内容时,就需要使用块
练习5.3
while(val<=10)
sum+=val,++val;
可读性变低
练习5.4
答:a是指针没有指向尾后元素,则循环不断;b有问题,应将if中的!去掉
练习5.5/5.6
#include <iostream>
using namespace std;
int main()
{
int grades = 85;
if (grades > 90)
cout << "A" << endl;
else if (grades > 60)
cout << "B" << endl;
else
cout << "C" << endl;
cout << ((grades > 90) ? 'A' : (grades > 60) ? 'B' : 'C') ;
return 0;
}
练习5.7
答:a应该在第二行加上分号;b应该将二三行放在块中;c中ival为局部变量,应该把它定义在if外;d括号中应该为==
练习5.8
答:if语句比else语句多的时候,C++中规定else语句与离他最近且没有绑定else的if绑定
练习5.9
#include <iostream>
using namespace std;
int main()
{
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch)
{
if (ch == 'a')
++aCnt;
else if (ch == 'e')
++eCnt;
else if (ch == 'i')
++iCnt;
else if (ch == 'o')
++oCnt;
else if (ch == 'u')
++uCnt;
}
return 0;
}
练习5.10
#include <iostream>
using namespace std;
int main()
{
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch)
{
if (ch == 'a'||ch=='A')
++aCnt;
else if (ch == 'e'||ch=='E')
++eCnt;
else if (ch == 'i'||ch=='I')
++iCnt;
else if (ch == 'o'||ch=='O')
++oCnt;
else if (ch == 'u'||ch=='U')
++uCnt;
}
return 0;
}
练习5.11
#include <iostream>
using namespace std;
int main()
{
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, blankCnt = 0;
char ch;
while (cin >> ch)
{
if (ch == 'a' || ch == 'A')
++aCnt;
else if (ch == 'e' || ch == 'E')
++eCnt;
else if (ch == 'i' || ch == 'I')
++iCnt;
else if (ch == 'o' || ch == 'O')
++oCnt;
else if (ch == 'u' || ch == 'U')
++uCnt;
else if (ch == ' ' || ch == '\t' || ch == '\n')
++blankCnt;
}
return 0;
}
练习5.12
#include <iostream>
#include <string>
using namespace std;
int main()
{
int ffCnt = 0,flCnt = 0,fiCnt = 0;
string s;
while (cin >> s)
{
for (auto i = s.begin(); i != s.end(); ++i)
if (*i == 'f' && *i++ == 'f')
++ffCnt;
for (auto i = s.begin(); i != s.end(); ++i)
if (*i == 'f' && *i++ == 'l')
++flCnt;
for (auto i = s.begin(); i != s.end(); ++i)
if (*i == 'f' && *i++ == 'i')
++fiCnt;
}
return 0;
}
练习5.14
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> s;
string s1;
int count = 1;
while (cin >> s1)
{
s.push_back(s1);
}
for (auto i = s.begin();i!=s.end()-1; )
{
while (*i==*i++)
++count;
}
if (count == 1)
cout << "不存在" << endl;
else
cout << count << endl;
return 0;
}
练习5.15
答:a,ix从0到末尾遍历,if语句判断是否遍历到了末尾,应该将ix定义在函数体外;b,for语句头缺失初始化语句,应该将他设为空语句;c,死循环
练习5.16
答:倾向于选择for语句,for循环更加明了
练习5.17
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> a = {0,1,1,2,3,5,8};
vector<int> b = {0,1,1,2};
int count = 0;
if(a.size()<b.size())
for (int i = 0; i != a.size(); ++i)
{
if (a[i] != b[i])
break;
++count;
}
if (count == a.size())
cout << "a是b的前缀" << endl;
else
for (int i = 0; i != b.size(); ++i)
{
if (a[i] != b[i])
break;
++count;
}
if (count == b.size())
cout << "b是a的前缀" << endl;
return 0;
}
练习5.18
答:a应该将二三四行放在块内;b定义的变量应该放在do前;c应该将定义的变量放在函数体外
练习5.19
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
do
{
cout << "请输入两个字符串" << endl;
cout << (s1.size() < s2.size() ? s1 : s2) << endl;
} while (cin >> s1 >> s2);
return 0;
}
练习5.20
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
int count = 0;
while (cin >> s1 >> s2)
{
if (s1 == s2)
{
++count;
break;
}
}
if (count != 0)
cout << "没有连续出现两个相同的单词" << endl;
else
cout << "单词已经读完" << endl;
return 0;
}
练习5.21
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string s1, s2;
int count = 0;
while (cin >> s1 >> s2)
{
if (!islower(s1[0])&& s1 == s2)
{
++count;
break;
}
}
if (count != 0)
cout << "没有连续出现两个相同的单词" << endl;
else
cout << "单词已经读完" << endl;
return 0;
}
while(sz<0)
sz = get_size();
练习5.23
#include <iostream>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
cout << a / b;
return 0;
}
练习5.24
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b;
if (b == 0)
throw runtime_error("除数不能为0");
cout << a/b;
return 0;
}
练习5.25
#include <iostream>
using namespace std;
int main()
{
int a, b;
while (cin >> a >> b)
{
try
{
if (b == 0)
throw runtime_error("除数不能为0");
}
catch (runtime_error err)
{
cout << err.what() << "输入n可以重新输入数字" << endl;
char c;
cin >> c;
if (c != 'n')
break;
}
cout << a / b;
}
return 0;
}