打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++Primer第五章语句习题
C++第五章语句习题
5.1
空语句就是一个;当for循环的初始条件为空或者for,while语句循环体为空时都可以使用空语句。
5.2
块就是用花括号括起来的语句序列。for,while,if语句有需要执行多条语句时。
5.3
5.4
a)没有初始化iter为iter= s.begin()
b)应该在while之前为word赋值
5.5
int grades;
char score;
cin >> grades;
if (grades > 90)
score = 'A';
else if (grades > 60)
score = 'B';
else
score = 'C';

5.6
score = grades > 90?'A':grades > 60 ? 'B':'C';
5.7
a)if(ival1 != ival2)
{
ival1 = ival2;
}
else
{
ival1 = ival2 = 0;
}
b)  if (ival < minval)
{
minval = ival;
occurs = 1;
}
c)  int ival = get_value();
if (ival)
cout << "ival = " << ival << endl;
if (!ival)
cout << "ival = 0\n";
d)  if (ival == 0)
ival = get_value();
5.8
悬垂else是指在一个语句包含的if子语句多余else子语句的时候,多余的else应该与那一个if匹配
5.9
int main()
{
unsigned aCnt = 0,eCnt = 0,iCnt = 0,oCnt = 0,uCnt = 0;
char ch;
while (cin >> ch)
{
if(ch == 'a')
aCnt++;
if(ch == 'e')
eCnt++;
if(ch == 'i')
iCnt++;
if(ch == 'o')
iCnt++;
if(ch == 'u')
uCnt++;
}
return 0;
}
5.10
int main()
{
unsigned aCnt = 0,eCnt = 0,iCnt = 0,oCnt = 0,uCnt = 0;
char ch;
while (cin >> ch)
{
if(ch == 'a' || ch == 'A')
aCnt++;
if(ch == 'e' || ch == 'E')
eCnt++;
if(ch == 'i' || ch == 'I')
iCnt++;
if(ch == 'o' || ch == 'O')
iCnt++;
if(ch == 'u' || ch == 'U')
uCnt++;
}
return 0;
}
5.11
int main()
{
unsigned aCnt = 0,eCnt = 0,iCnt = 0,oCnt = 0,uCnt = 0;
unsigned sCnt = 0,nCnt = 0,tCnt = 0;
char ch;
while (cin >> ch)
{
if(ch == 'a' || ch == 'A')
aCnt++;
if(ch == 'e' || ch == 'E')
eCnt++;
if(ch == 'i' || ch == 'I')
iCnt++;
if(ch == 'o' || ch == 'O')
iCnt++;
if(ch == 'u' || ch == 'U')
uCnt++;
if(ch == ' ')
sCnt++;
if(ch == '\n')
nCnt++;
if(ch == '\t')
tCnt++;
}
return 0;
}
5.12
int main()
{
char ch,c = '\0';
unsigned ffCnt = 0,flCnt = 0,fiCnt = 0;
while (cin >> ch)
{
if(c == 'f')
{
switch (ch)
{
case 'f':
ffCnt++;
break;
case 'l':
flCnt++;
break;
case 'i':
fiCnt++;
break;
default:
break;
}
}
c = ch;
}
return 0;
}
5.13
a)每个case标签都没有break
b)在case标签中定义了ix但是在default中又使用了ix
c)case中出现多个常量时应该用case1:case2:casen
d)case语句中不能用变量应该是常量和常量表达式
5.14
int main()
{
string beforeWord,newWord,resultWord;
int cnt = 0;maxCnt = 1;
while (cin >> newWord)
{
if (newWord == beforeWord)
{
cnt ++;
}
else
{
if(cnt > maxCnt)
{
maxCnt = cnt;
resultWord = beforeWord;
}
cnt = 1;
}
beforeWord = newWord;
}
return 0;
}
5.15
a)
ix是在for循环中定义的不能再for外使用
b)
int ix;//应该赋初值
for(;ix != sz ++ix)
c)
可能会是死循环
5.16
在循环次数确定的时候用for,不确定的时候用while
5.17
bool contain(vector<int>&v1,vector<int>&v2)
{
int size = v1.size() > v2.size()?v2.size():v1.size();
for (int i = 0; i < size; i ++)
{
if(v1[i] != v2[i])
return false;
}
return true;
}
5.18
a)do while之间缺少花括号
b)不能在while条件判断中定义变量
c)正确
5.19
do
{
string str1,str2;
cin >> str1 >> str2;
str1.size() < str2.size()?cout << str1 << endl:cout << str2 << endl;
}
while(cin);
5.20
int main()
{
string s1,s2;
while(cin >> s2)
{
if(s2 == s1)
break;
else
s1 = s2;
}
if((!s1.empty()) && s1 == s2)
{
cout << "重复的单词是: "<< s1;
}
else
{
cout << "没有重复的单词"<<endl;
}
return 0;
}
5.21
(s1==s2) && isupper(s2[0])
5.22
int sz = get_value();
while(sz <= 0)
{
sz = get_value();
}
5.23
int main()
{
int ival1,ival2;
cin >> ival1 >> ival2;
cout << ival1/ival2 << endl;
return 0;
}
5.24
int main()
{
int ival1,ival2;
cin >> ival1 >> ival2;
if (ival2 == 0)
{
throw runtime_error("第二个数是0");
}
cout << ival1/ival2 << endl;
return 0;
}
5.25
int main()
{
int ival1,ival2;
while(cin >> ival1 >> ival2)
{
try
{
if (ival2 == 0)
{
throw runtime_error("第二个数是0");
}
cout << ival1/ival2 << endl;
}
catch(runtime_error err){
cout <<err.what()<<"\nTry Again? enter y or n"<<endl;
char c
cin >> c;
if(!cin|| c == 'n')
break;
}
}
return 0;
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C++ 学习笔记1 - 日志 - 邱泳天 - 血与荣誉SNS平台 - Powered by ...
流式IO(一)
VC++中的string
VS2008调试技巧收集备用
判断输入的是大写字母还是小写,并转换
71 f0301
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服