C++ Primer Plus(第六版)—— 第五章 循环和关系表达式 笔记和答案

xiaoxiao2021-02-27  273

1.要输出bool,要设置标记,默认是输出0和1的。

[cpp]  view plain  copy cout.setf(ios_base::boolalpha);   cout << true << endl;   2.每个表达式都是值,例如 if(x = 100),相当于x = 100;if(x); 不过我强烈要求不要这么写,容易看错,或者你觉得自己牛逼,但是看你的代码的人未必牛逼。

最后还得加大自己的工作量,成为傻逼

3.这应该是冷知识,在比较旧的C++版本,for循环中的声明(就是下面的int i = 0),是不合法的, [cpp]  view plain  copy for (int i = 0; i < 5; i++)   {   }    for循环中只能使用表达式,而声明不能算是表达式。 后来的一些版本,增加了一个声明表达式,让下面的for循环方式出现。但是这是一个兼容操作, 实际上,还是把i的声明提前的。 cout << i << endl;这个语句在for循环后面是合法的, 再后来,i就真的只能在for循环里面使用了。这不知道多少版本的事了。 跟着再写多一些代码:

[cpp]  view plain  copy int i = 0;   while (i++ < 10)   {       cout << i << endl;   }   int y = (4 + i++) + (6 + i++);//   虽然i是for里面的局部变量了,在外面不能用了,但是在vs的调试工具,鼠标指向的内存居然还是显示i的内容还存在,

然后再看外面面的i,发现当前内存里面有两个i。不知道这算不算bug呢。不过并不影响使用。

4.i++和++i是不同的,不过我建议还是把这个东西独立一行。 [cpp]  view plain  copy int i = 0;   while (i++ < 10)   {       cout << i << endl;   }   int y = (4 + i++) + (6 + i++);//这样是无法判断i自增是在(4+i++)完成之后就自增,还是整个表达式之后就自增。   可能不同的编译器也会有不同效果。所以不要把i++放进复杂的表达式中。

5.++i比i++效率更高。两个表达式都是i先自增,后返回的,只是i++自增前创建一个副本,然后自增,然后返回副本。

对于我们自己的类,效率差别会很明显。

6.基于范围的for循环,C++11新产品

[cpp]  view plain  copy int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };   for (int &i : a)   {       cout << i << endl;   }  

7.模拟eof

[cpp]  view plain  copy char ch;   int count = 0;   cin.get(ch);   while (!cin.fail())   {       cout << ch;       ++count;       cin.get(ch);   }   cout << endl << count << " characters read\n";  

5.8 复习题

1.入口条件就是在进入循环的时候就检测,出口条件是先进入循环,再检测,至少可以执行一次循环。

for、while属于入口条件检测,do while属于出口条件检测。

2. 01234

[cpp]  view plain  copy int i;   for (i = 0; i < 5; i++)   cout << i;   cout << endl;   3. 0369 12

[cpp]  view plain  copy int j;   for (j = 0; j < 11; j += 3)   cout << j;   cout << endl << j << endl;   4. 6 8

[cpp]  view plain  copy int j = 5;   while (++j < 9)   {       cout << j++ << endl;   }  

5.

k = 8

[cpp]  view plain  copy int k = 8;   do       cout << "k = " << k << endl;   while (k++ < 5);   6.

[cpp]  view plain  copy for (int i = 1; i <= 64; i *= 2)   {       cout << i<<" ";   }   cout << endl;   7.使用{},其实就算只有一条语句,也应该使用{},可以是代码更加清晰,而且说不定什么时候需求就要变了,一条语句通常干不了什么的。

8.都有效。

[cpp]  view plain  copy int x = (1, 024);//x为20,()内返回024,0开头是8进制,即是20.   int y;   y = 1, 024;//y=1,之后024.就没了  

9.cin>>ch,跳过空格、换行符、制表符。cin.get(ch) 和 ch = cin.get()可以读取这些字符

5.9编程题

1.

[cpp]  view plain  copy int m = 0;   int n = 0;   cin >> m;   cin >> n;   if (m > n) {       int t = m;       m = n;       n = t;   }   int total = 0;   for (int i = m; i <= n; i++) {       total += i;   }   cout << total <<endl;   2.

5.4程序清单

[cpp]  view plain  copy const int ArSize = 16;   long long factorials[ArSize];   factorials[1] = factorials[0] = 1LL;   for (int i = 2; i < ArSize; i++) {       factorials[i] = i * factorials[i-1];   }   for (int i = 0; i < ArSize; i++) {       cout << i << " = " << factorials[i] << endl;   }   修改后:

[cpp]  view plain  copy const int ArSize = 101;   array<long double, ArSize> factorials;   factorials[1] = factorials[0] = 1;   for (int i = 2; i < ArSize; i++) {       factorials[i] = i * factorials[i-1];   }   for (int i = 0; i < ArSize; i++) {       cout << i << " = " << factorials[i] << endl;   }   结果:

0 = 1

1 = 1

2 = 2

3 = 6

4 = 24

5 = 120

6 = 720

7 = 5040

8 = 40320

9 = 362880

10 = 3.6288e+06

11 = 3.99168e+07

12 = 4.79002e+08

13 = 6.22702e+09

14 = 8.71783e+10

15 = 1.30767e+12

。。。好长

3. [cpp]  view plain  copy cout << "Please enter:" << endl;   int i = -1;   int total = 0;   while (i != 0) {       cin >> i;       total += i;       cout << "current sum: "<< total << endl;   }   4.

[cpp]  view plain  copy const double base = 100;   const double interestA = 0.1;   const double interestB = 0.05;   double moneyA = base;   double moneyB = base;   int year = 0;   while (moneyA >= moneyB) {       ++year;       moneyA += (base * interestA);       moneyB += (moneyB * interestB);   }   cout << "Year:" << year << " A:" << moneyA << " B:" << moneyB << endl;   结果:

[cpp]  view plain  copy <span style="font-family: Arial, Helvetica, sans-serif;">Year:27 A:370 B:373.346</span>  

5.

[cpp]  view plain  copy string month[12] = {       "January","February","March","April",       "May","June","July","August",       "September","October","November","December"};   int total = 0;   int thisYearData[12];   for (int i = 0; i < 12; ++i) {       cout << "Please enter " << month[i] <<"'data:" ;       cin >> thisYearData[i];       total += thisYearData[i];   }   cout << "this year' data: "<< total <<endl;   6. [cpp]  view plain  copy string month[12] = {       "January","February","March","April",       "May","June","July","August",       "September","October","November","December"};   int total = 0;   int yearData[3][12];   int yearTotal[3];   for (int j = 0; j < 3; ++j) {       for (int i = 0; i < 12; ++i) {           cout << "Please enter the " << j + 1 << " year " << month[i] <<"'data:" ;           cin >> yearData[j][i];           total += yearData[j][i];           yearTotal[j] += yearData[j][i];       }   }   for (int i = 0; i < 3; ++i) {       cout << "year:"<< i + 1 << " data:"<< yearTotal[i] << endl;   }   cout << "all year' data: "<< total <<endl;   7.

[cpp]  view plain  copy struct SCar   {       string manufacturer;       int year;   };   int num = 0;   cout << "How many cars do you wish to catalog?";   cin >> num;   cin.get();   SCar* carArr =  new SCar[num];   for (int i = 0; i < num; ++i ) {       cout<< "Car #" << i + 1 << ":"<<endl;       cout<< "Please enter the make:";       getline(cin, carArr[i].manufacturer);       cout << "Please enter the year made:";       cin >> carArr[i].year;       cin.get();   }   cout << "Here is your collection:" << endl;   for (int i = 0 ; i < num; ++i) {       cout << carArr[i].year << " " << carArr[i].manufacturer << endl;   }  

8.

[cpp]  view plain  copy char s[20];   int count = 0;   cout << "Enter words (to stop, type the word done):" <<endl;   while (true) {       cin >> s;       if (0 == strcmp(s, "done")) {           break;       }else{           ++count;       }   }   cout << "You entered a total of " << count << " words" << endl;  

9.

[cpp]  view plain  copy string s;   int count = 0;   cout << "Enter words (to stop, type the word done):" <<endl;   while (true) {       cin >> s;       if (s == "done") {           break;       }else{           ++count;       }   }   cout << "You entered a total of " << count << " words" << endl;   10.

[cpp]  view plain  copy cout << "Enter number of rows: ";   int n = 0;   cin >> n;   for (int i = 0; i < n; ++i) {       for (int j = 0; j < n ; ++j) {           if (j < n - i - 1) {               cout << "." ;           }else{               cout << "*";           }       }       cout << endl;   }  

转载请注明原文地址: https://www.6miu.com/read-8707.html

最新回复(0)