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) { /*... */ }
/*... */
}
#include<iostream>#include<string>usingnamespace std;
intmain(){
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");
return0;
}
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>usingnamespace std;
intmain(){
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");
return0;
}
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>usingnamespace std;
intmain(){
do
{
string s1, s2;
cout << "Please enter two string :" << endl;
if (cin >> s1 >> s2)
{
if (s1 > s2)
cout << "s2 is less than s1" << endl;
elseif (s1 < s2)
cout << "s1 is less than s2" << endl;
else
cout << "two string are equal" << endl;
}
} while (cin);
system("pause");
return0;
}
Exercises Section 5.5.1
Ex5.20
#include<iostream>#include<string>usingnamespace std;
intmain(){
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");
return0;
}
int sz = get_size();
while (sz <= 0)
{
sz = get_size();
}
Exercises Section 5.6.3
Ex5.23
#include<iostream>usingnamespace std;
intmain(){
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");
return0;
}
Ex5.24
#include<iostream>usingnamespace std;
intmain(){
int v1, v2;
cout << "Please enter two numbers: ";
cin >> v1 >> v2;
if (v2 != 0)
{
cout << "The result is " << v1 / v2 << endl;
system("pause");
return0;
}
else
{
cerr << "The second number is zero!" << endl;
system("pause");
return-1;
}
}
Ex5.25
#include<iostream>usingnamespace std;
intmain(){
int v1, v2;
cout << "Please enter two numbers: ";
cin >> v1 >> v2;
try
{
if (v2 == 0)
throwruntime_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");
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端