C++ Primer(第五版)第5章答案

第5章 语句


练习5.1

只含有一个单独的分号的语句就是空语句。如果在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该使用空语句。

练习5.2

块也叫复合语句,它是指用花括号括起来的语句和声明序列。如果在程序的某个地方,语法上需要一条语句,但是逻辑上需要多条语句,则应该使用复合语句。

练习5.3

while (val<=10)
sum+=val,++val;

代码的可读性降低了

练习5.4

  • (a)非法,iter应该定义在while语句外部
  • (b)if语句无法访问到while语句中的status,if语句应该内嵌在while语句中

练习5.5

#include <iostream>
using namespace std;
int main()
{
 int grade = 0;
 while (cin >> grade && grade > 100)
  cout << "error!please again:" << endl;
 if (grade == 100)
  cout << "A++";
 else if (grade >= 90)
  cout << "A";
 else if (grade >= 80)
  cout << "B";
 else if (grade >= 70)
  cout << "C";
 else if (grade >= 60)
  cout << "D";
 else
  cout << "F";
 return 0;
}

练习5.6

#include <iostream>
#include <string>
using namespace std;
int main()
{
 int grade = 0;
 while (cin >> grade && grade > 100)
  cout << "error!please again:" << endl;
 string finalgrade;
 (grade == 100) ? finalgrade = "A++" :
  (grade >= 90) ? finalgrade = "A" :
  (grade >= 80) ? finalgrade = "B" :
  (grade >= 70) ? finalgrade = "C" :
  (grade >= 60) ? finalgrade = "D" : finalgrade = "F";
 cout << finalgrade << endl;
 return 0;
}

练习5.7

  • (a)ival1=ival2末尾加上分号
  • (b)用花括号括起来
  • (c)将if (!ival)改为else
  • (d)将ival改为ival==0

练习5.8

某个else与哪个if匹配的问题称为垂悬else,C++语言规定else与它最近的尚未匹配的if匹配

练习5.9

#include <iostream>
using namespace std;
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')
   ++ocnt;
  if (ch == 'u')
   ++ucnt;
 }
 cout << "a: " << acnt << endl;
 cout << "e: " << ecnt << endl;
 cout << "i: " << icnt << endl;
 cout << "o: " << ocnt << endl;
 cout << "u: " << ucnt << endl;
 return 0;
}

练习5.10

#include <iostream>
using namespace std;
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')
   ++ocnt;
  if (ch == 'u' || ch == 'U')
   ++ucnt;
 }
 cout << "a: " << acnt << endl;
 cout << "e: " << ecnt << endl;
 cout << "i: " << icnt << endl;
 cout << "o: " << ocnt << endl;
 cout << "u: " << ucnt << endl;
 return 0;
}

练习5.11

#include <iostream>
using namespace std;
int main()
{
 unsigned acnt = 0, ecnt = 0, icnt = 0, ocnt = 0, ucnt = 0, spaceCnt = 0, tabCnt = 0, newLineCnt = 0;
 char ch;
 while (cin >> std::noskipws >> 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;
  case ' ':
   ++spaceCnt;break;
  case '\t':
   ++tabCnt;break;
  case '\n':
   ++newLineCnt;break;
  }
 }
 cout << "a: " << acnt << endl;
 cout << "e: " << ecnt << endl;
 cout << "i: " << icnt << endl;
 cout << "o: " << ocnt << endl;
 cout << "u: " << ucnt << endl;
 cout << "space: " << spaceCnt << endl;
 cout << "tab: " << tabCnt << endl;
 cout << "newLine: " << newLineCnt << endl;
 return 0;
}

练习5.12

#include <iostream>
using std::cin; using std::cout; using std::endl;
int main()
{
 unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, spaceCnt = 0, tabCnt = 0, newLineCnt = 0, ffCnt = 0, flCnt = 0, fiCnt = 0;
 char ch, prech = '\0';
 while (cin >> std::noskipws >> ch)
 {
  switch (ch)
  {
  case 'a':
  case 'A':
   ++aCnt;
   break;
  case 'e':
  case 'E':
   ++eCnt;
   break;
  case 'i':
   if (prech == 'f') ++fiCnt;
  case 'I':
   ++iCnt;
   break;
  case 'o':
  case 'O':
   ++oCnt;
   break;
  case 'u':
  case 'U':
   ++uCnt;
   break;
  case ' ':
   ++spaceCnt;
   break;
  case '\t':
   ++tabCnt;
   break;
  case '\n':
   ++newLineCnt;
   break;
  case 'f':
   if (prech == 'f') ++ffCnt;
   break;
  case 'l':
   if (prech == 'f') ++flCnt;
   break;
  }
  prech = ch;
 }

 cout << "Number of vowel a(A): \t" << aCnt << '\n'
  << "Number of vowel e(E): \t" << eCnt << '\n'
  << "Number of vowel i(I): \t" << iCnt << '\n'
  << "Number of vowel o(O): \t" << oCnt << '\n'
  << "Number of vowel u(U): \t" << uCnt << '\n'
  << "Number of space: \t" << spaceCnt << '\n'
  << "Number of tab char: \t" << tabCnt << '\n'
  << "Number of new line: \t" << newLineCnt << '\n'
  << "Number of ff: \t" << ffCnt << '\n'
  << "Number of fl: \t" << flCnt << '\n'
  << "Number of fi: \t" << fiCnt << endl;

 return 0;
}

练习5.13

  • (a)缺少break,改为
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)应该在外部定义ix,改为
unsigned index = some_value();
int ix=0;
switch (index)
{
  case 1:
    ix = get_value();
    ivec[ ix ] = index;
    break;
  default:
    ix = static_cast<int>(ivec.size())-1;
    ivec[ ix ] = index;
}
  • (c)用:隔开而非,,改为
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 0:
    evencnt++;
    break;
}
  • (d)case 标签必须是整型常量表达式,改为
const unsigned ival=512, jval=1024, kval=4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt) 
{
  case ival:
    bufsize = ival * sizeof(int);
    break;
  case jval:
    bufsize = jval * sizeof(int);
    break;
  case kval:
    bufsize = kval * sizeof(int);
    break;
  }

练习5.14

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
 vector<string> text;//按顺序存储出现的单词,重复的只存一个
 unsigned index = 0, times = 1, maxTimes = 1;//索引和次数,最大次数
 unsigned temp = 0;//存储次数最大的单词的索引
 string word;
 cin >> word;//输入第一个单词
 text.push_back(word);
 while (cin >> word)
 {
  if (word == text[index])
  {
   ++times;
   if (times > maxTimes)
   {
    maxTimes = times;
    temp = index;
   }
  }
  else
  {
   text.push_back(word);
   ++index;
   times = 1;
  }
 }
 if (maxTimes == 1)
  cout << "NO" << endl;
 else
  cout << text[temp] << " " << maxTimes;
 return 0;
}

练习5.15

  • (a)
int ix=0;
for (int ix=0;ix!=sz;++ix) {/*...*/}
if (ix!=sz)
//...
  • (b)
int ix;
for (;ix!=sz;++ix) {/*..*/}
  • (c)
for (int ix=0;ix!=sz;++ix) {/*...*/}

练习5.16

int i;
while ( cin >> i )
  // ...


for (int i = 0; cin >> i;)
  // ...


for (int i = 0; i != size; ++i)
  // ...


int i = 0;
while (i != size)
{
  // ...
  ++i;
}

我会更倾向使用 while,因为while 显得简洁,代码可读性强

练习5.17

#include <iostream>
#include <vector>
using namespace std;
int main()
{
 vector<int> v2{ 1,0,1,0,1,1,1 };
 vector<int> v1{ 1,0,1 };
 bool flag = true;
 int n = 0;
 if (v1.size() >= v2.size())
 {
  for (int i : v2)
  {
   if (i != v1[n++])
    flag = false;
  }
 }
 else
 {
  for (int i : v1)
  {
   if (i != v2[n++])
    flag = false;
  }
 }
 if (flag)
  cout << "YES";
 else
  cout << "NO";
 return 0;
}

练习5.18

  • (a)
do{
  //...
} while (cin);//应该用花括号括起来
  • (b)
int ival=0;
do{
  //...
} while (ival=get_response());//ival应该定义在循环外
  • (c)
int ival=get_response();
do{
  //...
} while (ival);//ival应该定义在循环外

练习5.19

#include <iostream>
#include <string>
using namespace std;
int main()
{
 string s1, s2, s;
 do
 {
  cout << "Please enter two string" << endl;
  cin >> s1 >> s2;
  if (s1.size() > s2.size())
   cout << s2 << endl;
  else
   cout << s1 << endl;
  cout << "enter yes continue" << endl;
  cin >> s;

 } while (s=="yes");
 return 0;
}

练习5.20

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
 string word;
 vector<string> text;
 while (cin >> word)
  text.push_back(word);
 bool flag = true;
 auto it = text.begin() + 1;
 while (it != text.end())
 {
  if (*it == *(it - 1))
  {
   flag = false;
   break;
  }
  else
   ++it;
 }
 if (flag)
  cout << "No word repeat" << endl;
 else
  cout << *it << endl;
 return 0;
}

练习5.21

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
 string word;
 vector<string> text;
 while (cin >> word)
  text.push_back(word);
 bool flag = true;
 auto it = text.begin() + 1;
 while (it != text.end())
 {
  if (*it == *(it - 1) && (*it)[0] == toupper((*it)[0]))
  {
   flag = false;
   break;
  }
  else
   ++it;
 }
 if (flag)
  cout << "No big word repeat" << endl;
 else
  cout << *it << endl;
 return 0;
}

练习5.22

#include <iostream>
using namespace std;
int main()
{
 int i = 0;
 while (cin >> i && i != 100)
 {
  cout << "No!" << endl;
 }
 cout << "Yes! is " << i << endl;
 return 0;
}

练习5.23

#include <iostream>
using namespace std;
int main()
{
 int m = 0, n = 0;
 cout << "Enter two numbers:" << endl;
 cin >> m >> n;
 cout << m << " / " << n << " = " << m / n;
 return 0;
}

练习5.24

#include <iostream>
using namespace std;
int main()
{
 int m = 0, n = 0;
 cout << "Enter two numbers:" << endl;
 cin >> m >> n;
 if (n == 0)
  throw runtime_error("The divisor cannot be 0");
 cout << m << " / " << n << " = " << m / n;
 return 0;
}

练习5.25

#include <iostream>
using namespace std;
int main()
{
 int m = 0, n = 0;
 cout << "Enter two numbers:" << endl;
 while (cin >> m >> n)
 {
  try
  {
   if (n == 0)
    throw runtime_error("The divisor cannot be 0");
  }
  catch (runtime_error err)
  {
   cout << err.what() << endl;
   cout << "Try Agin? Enter y or n" << endl;
   char c;
   cin >> c;
   if (!cin || c == 'n')
    break;
   cout << "Enter two numbers:" << endl;
  }
 }
 cout << m << " / " << n << " = " << m / n;
 return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邻家的阿飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值