2.7 程序10:蹩脚的除法运算
这是一个简单的程序,设计用于指出浮点数的有效位数。意图很简单:选择一个循环分数,例如1/3 (0.333333)并打印它,看看能获得多少有效数字。然而,结果让程序员感到迷惑。他知道,计算机不可能如此麻木地进行重复计算。那么,出现了什么错误呢?
1 /************************************************
2 * divide -- Program to figure out how many ? *
3 * digits are printed in floating point ?*
4 * by print 1/3 or 0.333333. ???????? ??????????????????
*
5 ************************************************/
6 #include <iostream>
7
8 int main()
9 {
10 float result; // Result of the divide
11
12 result = 1/3; // Assign result something
13
14 std::cout << "Result is " << result
<< '\n';
15 return (0);
16 }
(请参见“提示292”和“答案27”)
花絮
一个用于气象预报的计算机要求气象工作人员按英寸输入降雨量。但现在气象人员习惯于用英寸的百分之一来表示降雨量,因此,当您问他们今天的降雨量时,他们会说:“50”,这意味着那天的降雨量是50%英寸或者是半英寸。
可是,为了将这个数据输入到计算机中,您必须输入0.50。一个小伙子忘记了这一点,将50作为那天的降雨量输入到计算机中。现在,50英寸是一个很大、很可怕的降雨量。然后,计算机发现了这个错误,立即发出一个相应的警告信息:
赶快修建船只!将人们和动物都集中起来……
2.8 程序11:画蛇添足
下面的程序是完成Hello World程序任务的另一种方法,且经常有人使用这种方法。结果如何?
File: sub.cpp
1 // The string to print
2 char str[] = "Hello World!\n";
File: main.cpp
1 /************************************************
2 * print string -- Print a simple string. ?? *
3 ************************************************/
4 #include <iostream>
5
6 extern char *str; // The string to print
7
8 int main()
9 {
10 std::cout << str << std::endl;
11 return (0);
12 }
(请参见“提示269”和“答案7”)
花絮
一个程序员曾想指出如何才能永远杜绝违规停车罚单。他有3种选择,那就是私人汽车牌照要么是0O0O0O,要么是O0O0O0或I1I1I1。他指出,如果一个警察发现了这辆汽车,因为字符O和数字0(或是I和1)看上去差不多,警察不可能正确地记录下车牌号。
不幸的是,他的计划没有得以实施。负责发放牌照的DMV工作人员认为这容易引起混淆,结果他的车牌照成了OOOOOO。
2.9 程序12:加快和等待
下面这个程序取自由一个经验丰富的系统程序员编写的代码。我也曾在该程序员所在的公司工作过一段时间。
这个程序设计用于通过连续的线路发送数据。尽管连续的线路能够每秒发送960个字符,但是我们每秒只能接收到300个字符。其原因是什么呢?
1 /***********************************************
2 * send_file -- Send a file to a remote link ?*
3 * (Stripped down for this example.) ? *
4 ***********************************************/
5 #include <iostream>
6 #include <fstream>
7 #include <stdlib.h>
8
9 // Size of a block
10 const int BLOCK_SIZE = 256;
11
12 /**********************************************
13 * send_block -- Send a block to the output port *
14 **********************************************/
15 void send_block(
16 std::istream &in_file, // The file to read
17 std::ostream &serial_out // The file to write
18 )
19 {
20 int i; // Character counter
21
22 for (i = 0; i < BLOCK_SIZE; ++i) {
23 int ch; // Character to copy
24
25 ch = in_file.get();
26 serial_out.put(ch);
27 serial_out.flush();
28 }
29 }
30
31 int main()
32 {
33 // The input file
34 std::ifstream in_file("file.in");
35
36 // The output device (faked)
37 std::ofstream out_file("/dev/null");
38
39 if (in_file.bad())
40 {
41 std::cerr <<
42 "Error: Unable to open input file\n";
43 exit (8);
44 }
45
46 if (out_file.bad())
47 {
48 std::cerr <<
49 "Error: Unable to open output file\n";
50 exit (8);
51 }
52
53 while (! in_file.eof())
54 {
55 // The original program output
56 // a block header here
57 send_block(in_file, out_file);
58 // The original program output a block
59 // trailer here. It also checked for
60 // a response and resent the block
61 // on error
62 }
63 return (0);
64 }
(请参见“提示183”和“答案65”)
花絮
某个系统管理员养成了一个坏习惯,那就是在真正安装一个升级系统之前至少两个星期就宣称升级系统已经安装。通常,他会有许多抱怨,诸如“我的软件就要崩溃了,这都是因为您们的升级系统”。在宣称已经安装升级系统的日子里,这个系统管理员知道,这些故障不可能由升级而引起,因为他并没有真正安装升级系统。当他确实进行升级(当然是秘密进行安装)之后,随之而来的抱怨才是合理的。
一些业余无线电操作员也使用前面的伎俩。他们将安装一个新的无线电天线塔,并断开天线塔与用户之间的连接几个星期。这就使用户误认为:连续两个星期无法收看电视的原因是由新的天线引起的。
|