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

来源:互联网 发布:香港网络电视直播软件 编辑:程序博客网 时间:2024/06/10 20:16


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

[cpp] view plain copy
  1. cout.setf(ios_base::boolalpha);  
  2. cout << true << endl;  

2.每个表达式都是值,例如
if(x = 100),相当于x = 100;if(x);
不过我强烈要求不要这么写,容易看错,或者你觉得自己牛逼,但是看你的代码的人未必牛逼。

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


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

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

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


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


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

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


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

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

7.模拟eof

[cpp] view plain copy
  1. char ch;  
  2. int count = 0;  
  3. cin.get(ch);  
  4. while (!cin.fail())  
  5. {  
  6.     cout << ch;  
  7.     ++count;  
  8.     cin.get(ch);  
  9. }  
  10. cout << endl << count << " characters read\n";  

5.8 复习题

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

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


2.
01234

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

3.
0369
12

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

4.
6
8

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

5.

k = 8

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

6.

[cpp] view plain copy
  1. for (int i = 1; i <= 64; i *= 2)  
  2. {  
  3.     cout << i<<" ";  
  4. }  
  5. cout << endl;  

7.使用{},其实就算只有一条语句,也应该使用{},可以是代码更加清晰,而且说不定什么时候需求就要变了,一条语句通常干不了什么的。


8.都有效。

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

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


5.9编程题

1.

[cpp] view plain copy
  1. int m = 0;  
  2. int n = 0;  
  3. cin >> m;  
  4. cin >> n;  
  5. if (m > n) {  
  6.     int t = m;  
  7.     m = n;  
  8.     n = t;  
  9. }  
  10. int total = 0;  
  11. for (int i = m; i <= n; i++) {  
  12.     total += i;  
  13. }  
  14. cout << total <<endl;  

2.

5.4程序清单

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

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

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
  1. cout << "Please enter:" << endl;  
  2. int i = -1;  
  3. int total = 0;  
  4. while (i != 0) {  
  5.     cin >> i;  
  6.     total += i;  
  7.     cout << "current sum: "<< total << endl;  
  8. }  

4.

[cpp] view plain copy
  1. const double base = 100;  
  2. const double interestA = 0.1;  
  3. const double interestB = 0.05;  
  4. double moneyA = base;  
  5. double moneyB = base;  
  6. int year = 0;  
  7. while (moneyA >= moneyB) {  
  8.     ++year;  
  9.     moneyA += (base * interestA);  
  10.     moneyB += (moneyB * interestB);  
  11. }  
  12. cout << "Year:" << year << " A:" << moneyA << " B:" << moneyB << endl;  
结果:

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


5.

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


6.
[cpp] view plain copy
  1. string month[12] = {  
  2.     "January","February","March","April",  
  3.     "May","June","July","August",  
  4.     "September","October","November","December"};  
  5. int total = 0;  
  6. int yearData[3][12];  
  7. int yearTotal[3];  
  8. for (int j = 0; j < 3; ++j) {  
  9.     for (int i = 0; i < 12; ++i) {  
  10.         cout << "Please enter the " << j + 1 << " year " << month[i] <<"'data:" ;  
  11.         cin >> yearData[j][i];  
  12.         total += yearData[j][i];  
  13.         yearTotal[j] += yearData[j][i];  
  14.     }  
  15. }  
  16. for (int i = 0; i < 3; ++i) {  
  17.     cout << "year:"<< i + 1 << " data:"<< yearTotal[i] << endl;  
  18. }  
  19. cout << "all year' data: "<< total <<endl;  

7.

[cpp] view plain copy
  1. struct SCar  
  2. {  
  3.     string manufacturer;  
  4.     int year;  
  5. };  
  6. int num = 0;  
  7. cout << "How many cars do you wish to catalog?";  
  8. cin >> num;  
  9. cin.get();  
  10. SCar* carArr =  new SCar[num];  
  11. for (int i = 0; i < num; ++i ) {  
  12.     cout<< "Car #" << i + 1 << ":"<<endl;  
  13.     cout<< "Please enter the make:";  
  14.     getline(cin, carArr[i].manufacturer);  
  15.     cout << "Please enter the year made:";  
  16.     cin >> carArr[i].year;  
  17.     cin.get();  
  18. }  
  19. cout << "Here is your collection:" << endl;  
  20. for (int i = 0 ; i < num; ++i) {  
  21.     cout << carArr[i].year << " " << carArr[i].manufacturer << endl;  
  22. }  

8.

[cpp] view plain copy
  1. char s[20];  
  2. int count = 0;  
  3. cout << "Enter words (to stop, type the word done):" <<endl;  
  4. while (true) {  
  5.     cin >> s;  
  6.     if (0 == strcmp(s, "done")) {  
  7.         break;  
  8.     }else{  
  9.         ++count;  
  10.     }  
  11. }  
  12. cout << "You entered a total of " << count << " words" << endl;  

9.

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

10.

[cpp] view plain copy
  1. cout << "Enter number of rows: ";  
  2. int n = 0;  
  3. cin >> n;  
  4. for (int i = 0; i < n; ++i) {  
  5.     for (int j = 0; j < n ; ++j) {  
  6.         if (j < n - i - 1) {  
  7.             cout << "." ;  
  8.         }else{  
  9.             cout << "*";  
  10.         }  
  11.     }  
  12.     cout << endl;  
  13. }  


阅读全文
0 0
原创粉丝点击