/* 正常注释 /* 嵌套注释 */ 正常注释*/ ^ ch1.cpp:97:37: error: stray ‘\255’ in program ch1.cpp:97:37: error: stray ‘\243’ in program ch1.cpp:97:37: error: stray ‘\345’ in program ch1.cpp:97:37: error: stray ‘\270’ in program ch1.cpp:97:37: error: stray ‘\270’ in program ch1.cpp:97:37: error: stray ‘\346’ in program ch1.cpp:97:37: error: stray ‘\263’ in program ch1.cpp:97:37: error: stray ‘\250’ in program ch1.cpp:97:37: error: stray ‘\351’ in program ch1.cpp:97:37: error: stray ‘\207’ in program ch1.cpp:97:37: error: stray ‘\212’ in program ch1.cpp: In function ‘int main()’: ch1.cpp:97:50: error: expected primary-expression before ‘/’ token /* 正常注释 /* 嵌套注释 */ 正常注释*/ ^ ch1.cpp:98:5: error: expected primary-expression before ‘return’ return 0; ^
intmain() { int val = 10; while (val >= 0){ std::cout << val << " "; val -= 1; } std::cout << std::endl; }
练习1.11
编写程序,提示用户输入两个整数,打印出这两个整数所指定的范围内的所有整数。
解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
intmain() { int start = 0, end = 0; std::cout << "Please input two num: "; std::cin >> start >> end; if (start <= end) { while (start <= end){ std::cout << start << " "; ++start; } std::cout << std::endl; } else{ std::cout << "start should be smaller than end !!!"; } }
练习1.12
下面的for循环完成了什么功能?sum的终值是多少?
1 2 3
int sum = 0; for (int i = -100; i <= 100; ++i) sum += i;
解:
从-100加到100,sum的终值是0。
练习1.13
使用for循环重做1.4.1节中的所有练习(练习1.9到1.11)。
解:
练习1.9
1 2 3 4 5 6 7 8 9 10 11
#include<iostream>
intmain() { int sum = 0; for (int val = 50; val <= 100; ++val){ sum += val; } std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; }
练习1.10
1 2 3 4 5 6 7 8 9
#include<iostream>
intmain() { for (int val = 10; val >=0; --val){ std::cout << val << " "; } std::cout << std::endl; }
练习1.11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
intmain() { int start = 0, end = 0; std::cout << "Please input two num: "; std::cin >> start >> end; if (start <= end) { for (; start <= end; ++start){ std::cout << start << " "; } std::cout << std::endl; } else{ std::cout << "start should be smaller than end !!!"; } }
练习1.14
对比for循环和while循环,两种形式的优缺点各是什么?
解:
1 2 3 4 5 6
The main difference between the `for`'s and the `while`'s is a matter of pragmatics: we usually use `for` when there is a known number of iterations, and use `while` constructs when the number of iterations in not known in advance. The `while` vs `do ... while` issue is also of pragmatics, the second executes the instructions once at start, and afterwards it behaves just like the simple `while`.
练习1.15
编写程序,包含第14页“再探编译”中讨论的常见错误。熟悉编译器生成的错误信息。
解:
编译器可以检查出的错误有:
语法错误
类型错误
声明错误
练习1.16
编写程序,从cin读取一组数,输出其和。
解:
1 2 3 4 5 6 7 8 9 10
#include<iostream>
intmain() { int sum = 0; for (int value = 0; std::cin >> value; ) sum += value; std::cout << sum << std::endl; return0; }
练习1.17
如果输入的所有值都是相等的,本节的程序会输出什么?如果没有重复值,输出又会是怎样的?
练习1.18
编译并运行本节的程序,给它输入全都相等的值。再次运行程序,输入没有重复的值。
解:
全部重复:
1 2
1 1 1 1 1 1 occurs 5 times
没有重复:
1 2 3 4 5 6
1 2 3 4 5 1 occurs 1 times 2 occurs 1 times 3 occurs 1 times 4 occurs 1 times 5 occurs 1 times