我是靠谱客的博主 合适冬瓜,最近开发中收集的这篇文章主要介绍C++Primer 第五版 第五章 语句,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

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

5.3
可读性降低了

  1 #include <iostream>
  2 using namespace std;
  3 
  4 int main()
  5 {
  6         int val = 0, sum;
  7 
  8         while (val <= 10)
  9                 sum += val, ++val;
 10 }   

5.8
悬垂else:if分支多余else分支;
C++规定else与离他最近地尚未匹配地if匹配,从而消除了程序地二义性;

5.9

  1 #include <iostream>
  2 using namespace std;
  3 
  4 int main()
  5 {
  6         char ch;
  7         int cst;
  8         while (cin >> ch)
  9         {
 10                 switch (ch)
 11                 {
 12                         case 'a': case'e': case 'i': case 'o': case 'u': ++cst;
 13                 }
 14         }
 15         cout << cst << endl;
 16 }
~     

5.12
解法一:

  1 #include <iostream>
  2 using namespace std;
  3 
  4 int main()
  5 {
  6         char cur_char, bef_char = '';
  7         int cun_ff = 0, cun_fi = 0, cun_fl = 0;
  8         while (cin >> std::noskipws >> cur_char)
  9         {
 10                 switch (cur_char)
 11                 {
 12                         case 'f':
 13                                if (bef_char == 'f')
 14                                        ++cun_ff;
 15                                break;
 16                         case 'i':
 17                                if (bef_char == 'f')
 18                                        ++cun_fi;
 19                                break;
 20                         case 'l':
 21                                if (bef_char == 'f')
 22                                        ++cun_fl;
 23                                break;
 24                         default :break;
 25                 }
 26                 bef_char = cur_char;
 27         }
 28         cout << endl;
 29 
 30         cout << "the number of ff is : " << cun_ff << endl;
 31         cout << "the number of fi is : " << cun_fi << endl;
 32         cout << "the number of fl is : " << cun_fl << endl;
 33 
 34 }
 35 

解法二:

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 int main()
  6 {
  7         unsigned ffCnt = 0, flCnt = 0, fiCnt = 0;
  8         char str;
  9         int flag = 0;
 10         while (cin >> noskipws >> str)
 11         {
 12                 switch (str)
 13                 {
 14                         case 'f':
 15                                 if (flag == 1)
 16                                         ++ffCnt;
 17                                 else
 18                                         flag = 1;
 19                                 break;
 20                         case 'l':
 21                                 if (flag == 1)
 22                                         ++flCnt;
 23                                 flag = 0;
 24                                 break;
 25                         case 'i':
 26                                 if (flag == 1)
 27                                         ++fiCnt;
 28                                 flag = 0;
 29                                 break;
 30                         default:
 31                                 flag = 0;
 32                                 break;
 33                 }
 34         }
 35         cout << "the number of ff are : " << ffCnt << endl;
 36         cout << "the number of fi are : " << fiCnt << endl;
 37         cout << "the number of fl are : " << flCnt << endl;
 38 

5.13

  1. break;
  2. 如果在某处一个带有初值的变量位于作用域之外,在另一处该变量位于作用域之内,则从前一处跳转到后一处的行为是非法的;
  3. 每个case标签只能对应一个值;
  4. case标签必须是整数型常量表达式;

5.14

  1 #include <iostream>
  2 #include <string>
  3 #include <vector>
  4 using namespace std;
  5 
  6 int main()
  7 {
  8         vector<string> v;
  9         string cur_string;
 10         //读取若干string对象
 11         while (cin >> cur_string)
 12                 v.push_back(cur_string);
 13         cout << endl;
 14         //定义存储不重复string对象的vector和相应联系重复出现的次数vector
 15         vector<int> cnt_string;
 16         vector<string> sig_string;
 17         auto temp = v.begin();//不重复string对象的迭代器
 18         auto beg = v.begin();
 19         //统计相应string对象和连续重复出现的次数
 20         while (temp != v.end())
 21         {
 22                 int i = 0;
 23                 for (beg = temp; beg != v.end(); ++beg)
 24                 {
 25                         if (*temp == *beg)
 26                                 ++i;
 27                         else
 28                                 break;
 29                 }
 30                 cnt_string.push_back(i);
 31                 sig_string.push_back(*temp);
 32                 temp = beg;
 33         }
 34         //遍历输出
 35         for (auto &c : sig_string)
 36                 cout << c << ' ';
 37         cout << endl;
 38         for (auto &c : cnt_string)
 39                 cout << c <<  ' ';
 40         cout << endl;
 41         int max, max_i;
 42         max = cnt_string[0];
 43         max_i = 0;
 44 
 45         for (decltype(cnt_string.size()) i = 1; i < cnt_string.size(); ++i)
 46         {
 47                 if (max < cnt_string[i])
 48                 {
 49                         max = cnt_string[i];
 50                         max_i = i;
 51                 }
 52         }
 53         cout << sig_string[max_i] << ", has been appear continuely " << max << " times." << endl;
 54 
 55 
 56         return 0;
 57 }
           

5.15
1.if语句必为假;
2.for语句语法错误,缺少init-statement;
3.死循环;

5.16
如果知道循环次数,选择for,否则while;

5.17

  1 #include <iostream>
  2 #include <vector>
  3 using namespace std;
  4 
  5 template <typename T>
  6 bool subvector(vector<T> vc0, vector<T> vc1)
  7 {       for (decltype(vc0.size()) i = 0; i < ((vc0.size() > vc1.size()) ? vc1.size() : vc0.size()); ++i)
  8                 if (vc0[i] != vc1[i])
  9                         return false;
 10         return true;
 11 }
 12 int main()
 13 {
 14         vector<int> vc0{0, 1, 1, 2, 3};
 15         vector<int> vc1{0, 1, 1, 2, 3, 5, 8};
 16         cout << boolalpha << subvector(vc0, vc1) << endl;
 17 
 18         return 0;
 19 }
~        

5.18

  1. 语句块,需要{};
  2. ==
  3. ival块作用域,在函数体外面失效;

5.19

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 int main()
  6 {
  7         do
  8         {
  9                 string s1, s2;
 10                 cout << "Please enter two string : ";
 11                 cin >> s1 >> s2;
 12                 cout  << ((s1 > s2) ? s2 : s1) << endl;
 13         }while (cin);
 14 }
 15 

5.20

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 int main()
  6 {
  7         string cur_string, bef_string{};
  8         bool flag = 1;
  9         while (cin >> cur_string)
 10         {
 11                 if (cur_string == bef_string)
 12                 {
 13                         cout << cur_string << " appear continuely." << endl;
 14                         flag = 0;
 15                         break;
 16                 }
 17                 bef_string = cur_string;
 18         }
 19 
 20         if (flag)
 21         {
 22                 cout << "Not have anyone words appear continuely" << endl;
 23         }
 24 
 25 
 26 }

5.21

  1 #include <iostream>
  2 #include <string>
  3 #include <cctype>
  4 using namespace std;
  5 
  6 int main()
  7 {
  8         string cur_string, bef_string{};
  9         bool flag = 1;
 10         while (cin >> cur_string)
 11         {
 12                 if (cur_string == bef_string)
 13                 {
 14                         if (isupper(cur_string[0]))
 15                         {
 16                                 cout << cur_string << " appear continuely." << endl;
 17                                 flag = 0;
 18                                 break;
 19                         }
 20                 }
 21                 bef_string = cur_string;
 22         }
 23 
 24         if (flag)
 25         {
 26                 cout << "Not have anyone words appear continuely" << endl;
 27         }
 28 
 29 
 30 }

5.22
continue

5.23-5.25

  1 #include <iostream>
  2 #include <stdexcept>
  3 using namespace std;
  4 
  5 int main()
  6 {
  7         int it1, it2;
  8         while (cin >> it1 >> it2)
  9         {
 10                 try{
 11                 if (it2 == 0)
 12                         throw runtime_error("it2 = 0");
 13                 else
 14                         cout << it1 / it2 << endl;
 15                 }
 16                 catch (runtime_error err){
 17                         cout << err.what()
 18                              << "nTry Again? Enter y or n?" << endl;
 19                         char c;
 20                         cin >> c;
 21                         if (!cin || c == 'n')
 22                                 break;
 23                 }
 24          
 25         }
 26 }

最后

以上就是合适冬瓜为你收集整理的C++Primer 第五版 第五章 语句的全部内容,希望文章能够帮你解决C++Primer 第五版 第五章 语句所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部