4.9 程序35:输出一百万
我不知道可以在C++常量中使用逗号。那么,下面的程序是如何通过编译的?结果如何呢?
1 /************************************************
2 * print the value on one million. ?? *
3 ************************************************/
4 #include <iostream>
5
6 int main()
7 {
8 // Variable to hold a million
9 long int one_million;
10
11 // Set the variable
12 one_million = 1,000,000;
13
14 std::cout <<
15 "One million " << one_million <<
16 std::endl;
17 return (0);
18 }
(请参见“提示55”和“答案44”)
花絮
问:需要多少程序员来修理日光灯?
答:一个都不需要。那是一个硬件问题。
问:需要多少Microsoft程序员来修理日光灯?
答:一个都不需要。因为Microsoft声称黑暗是Cutting-Edge技术的最新革新。
4.10 程序36:超出堆栈空间
下面这个程序为何用完了堆栈的空间?
1 /************************************************
2 * test the data_holder class. ?*
3 ************************************************/
4 #include <iostream>
5 /************************************************
6 * data_holder -- A class to hold a single ?*
7 * integer ?*
8 * ?? *
9 * Member functions: ?????????*
10 * get -- Get value ?????????? *
11 * ?????????? *
12 * Note: By default the value of the data is 5. ?????????*
13 * ?????????*
14 * Warning: More member functions need to be ??????????
*
15 * added to this to make it useful. ?????????? *
16 ************************************************/
17 class data_holder {
18 private:
19 int data; // Data to store
20 public:
21 // Constructor -- Set value to default (5)
22 data_holder(void):data(5) {};
23
24 // Destructor defaults
25 //
26 // Copy constructor
27 data_holder(const data_holder &old) {
28 *this = old;
29 }
30
31 // Assignment operator
32 data_holder operator = (
33 data_holder old_data_holder) {
34 data = old_data_holder.data;
35 return (*this);
36 }
37
38 // Get the data item
39 int get(void)
40 {
41 return (data);
42 }
43 };
44
45 int main() {
46 // A data holder
47 data_holder var1;
48
49 // Copy of a data holder
50 data_holder var2(var1);
51 return (0);
52 }
(请参见“提示53”和“答案12”)
花絮
在UNIX文档中有下面这段文字:
The device names /dev/rmto, /dev/rmt4, /dev/rmt8, /dev/rmt12
are the rewinding low density, rewinding high density, non-rewinding
low density and non-rewinding high density tape drives respectively.
(设备名称/dev/rmt0, /dev/rmt4, /dev/rmt8和 /dev/rmt12分别表示低密度重绕、高密度重绕、非低密度重绕以及非高密度重绕磁带机。)
UNIX文档中关于FED命令的描述是:
BUGS
The terminal this program runs on has been stolen.
BUGS
(运行该程序的终端已被窃取)。
UNIX文档中关于TUNEFS命令(用于调整文件系统)的描述是:
You can tune a file system but you can’t tune a fish.
(您可以调整文件系统,但是无法调教笨蛋)。
4.11 程序37:程序的故障点
下面的程序设计用于为数组赋零值,但是有时却出现问题。
1 /************************************************
2 * Pointer demonstration. ? *
3 ************************************************/
4 #include <iostream>
5
6 static int data[16]; // Data to be stored
7 static int n_data = 0; // Number of items stored
8
9 int main()
10 {
11 int *data_ptr; // Pointer to current item
12
13 // Zero the data array
14 for (data_ptr = data+16-1;
15 data_ptr >= data;
16 --data_ptr)
17 {
18 *data_ptr = 0;
19 }
20
21 // Enter data into the array
22 for (n_data = 0; n_data < 16; ++n_data) {
23 std::cout <<
24 "Enter an item or 0 to end: ";
25 std::cin >> data[n_data];
26
27 if (data[n_data] == 0)
28 break;
29 }
30
31 // Index for summing
32 int index;
33
34 // Total of the items in the array
35 int total = 0;
36
37 // Add up the items in the array
38 for (index = 0; index < n_data; ++index)
39 total += data[index];
40
41 // Print the total
42 std::cout << "The total is: " <<
43 total << std::endl;
44
45 return (0);
46 }
(请参见“提示87”和“答案21”)
花絮
我所在的公司中,曾有一个通信线路在每天下午5点正开始不能工作,直到第2天早上7点左右才自动启动。在对这个硬件做大量的检查后,仍没有发现问题所在。最后,请来一个工程师,他花费几个小时来观察通信线路,但那天晚上问题没有出现。
第2天晚上,这个通信线路仍然象往常一样安静地工作着。又一天晚上,工程师呆得很晚,但问题仍没有出现。几天后,这个工程师确定,只要自己不看着通信线路,它就会在下午5点崩溃。
最后有一天,工程师决定在离开之前对用于通信的调制解调器做最后一次检查。它正在正常工作。工程师关掉照明灯,准备回去,此时碰巧撇了一眼调制解调器,居然发现它没有工作了。他打开照明灯,那个调制解调器又恢复了工作。在来回开关照明灯时,工程师发现调制解调器的电源插头插在一个受照明灯开关控制的墙壁插座上。问题找到了。当工作人员每天下班时,他们都会关掉照明灯,从而也就断了调制解调器的电,使其无法工作。当工作人员第2天来上班时,他们都会首先开灯,这样就启动了调制解调器。工程师之所以没有发现问题,是因为他整夜都呆在那个办公室,没有关掉照明灯,进而就无法观察到那个调制解调器存在的问题。最后,将调制解调器的电源插头插在一个常规的墙壁插座上,一切通信问题都没有了。
4.12 程序38:奇偶测试
下面的代码所执行的任务很明显,但它真正打印出的内容是又什么呢?
File: main.cpp
1 /************************************************
2 * test the check_for_even function. ?? *
3 ************************************************/
4 #include <iostream>
5
6 int value = 21; // Value of the system size
7
8 // Checks global value for even or not.
9 extern void check_for_even(void);
10
11 int main(void)
12 {
13 check_for_even();
14 std::cout << "Value is " << value
<< '\n';
15 return (0);
16 }
File: check.cpp
1 #include <iostream>
2
3 // Value of the control system size
4 int value = 30;
5
6 /************************************************
7 * check_for_even -- Check to see if global ?? ? *
8 * value is even. ? *
9 ************************************************/
10 void check_for_even(void)
11 {
12 if ((value % 2) == 0)
13 std::cout << "Ok\n";
14 else
15 std::cout << "Value problem\n";
16 }
(请参见“提示248”和“答案57”)
|