3.4 程序20:比设想的情况更简单
下面这个程序的目的是生成一个列出1到10之间数的平方的列表。该程序确实生成了一个数的平方列表,但却不是程序员所期望的内容。
1 /***********************************************
2 * Print out the square of the numbers ?*
3 * from 1 to 10 ?? *
4 ***********************************************/
5 #include <iostream>
6
7 int main()
8 {
9 int index; /* Index into the table */
10
11 for (index = 1; index <= 10; ++index);
12 std::cout << index << " squared "
<<
13 (index * index) << '\n';
14
15 return (0);
16 }
(请参见“提示193”和“答案34”)
花絮
真正的程序员从不利用PL/I编写程序。PL/I主要是为那些不能决定是否使用COBOL或FORTRAN语言编写程序的程序员准备的。
真正的程序员在玩Adrenture或Rogue时考虑的问题更多。
真正的程序员从不使用FORTRAN编写程序。FORTRAN主要针对那些知识贫乏、能力有限的人员。只有那些穿着白色短袜的无能工程师们才使用FORTRAN。
真正的程序不使用共享的文本。否则,这些程序在完成对函数的调用之后就无法利用预留的空间(执行一些不为人知的操作)?
真正的软件工程师从不调试程序;他们只验证正确性。这个过程没有必要涉及到计算机上的任何程序的执行,除非是一个Correctness
Verification Aid程序包。
真正的软件工程师不喜欢使用那些不在本地、功能不明确的花哨硬件,它们随时都有可能停止工作。他们也不太信任硬件工程师,希望系统在各个层次上都是虚拟的。他们只愿意使用个人计算机(这样就没有人会绊倒某些部件,导致您的DFA意外终止)。除非,他们需要8兆字节来运行Correctness
Verification Aid程序包。
3.5 程序21:错误的注释
下面的程序要输出什么内容?原因是什么?
1 /************************************************
2 * demonstrate how to do a divide. ?? *
3 ************************************************/
4 #include <iostream>
5
6 /************************************************
7 * div -- Do a divide ????????????? ?*
8 * ?? *
9 * Returns: Result of the divide. ? *
10 * ? *
11 * divisor is reset to 1. ? *
12 ************************************************/
13 static int div(
14 int *divisor // Pointer to the divisor
15 )
16 {
17 int result = 5; // Dividend
18
19 result=result/*divisor; /* Do divide */;
20 *divisor=1;
21 return (result);
22 }
23
24 int main()
25 {
26 int num = 5; // Divisor
27
28 std::cout << "Division " <<
29 div(&num) << std::endl;
30 return (0);
31 }
(请参见“提示168”和“答案91”)
花絮
最隐密的错误奖应该授予如下消息:
Error:Success
我仍要尝试找出这样的错误。
3.6 程序22:参数的取值范围过大
该程序中的代码的意图很简单:通过限制它的MAX属性值确保其大小不超过一个上限。但是该目的并没有达到,我们仍需要为此而修改代码。
1 /************************************************
2 * Test the logic to limit the size of a ?????????? *
3 * variable. ?????????*
4 ************************************************/
5 #include <iostream>
6
7 int main()
8 {
9 int size = 20; // Size to be limited
10 const int MAX = 25; // The limit
11
12 if (size > MAX)
13 std::cout << "Size is too large\n";
14 size = MAX;
15
16 std::cout << "Size is " << size
<< '\n';
17 return(0);
18 }
(请参见“提示304”和“答案4”)
花絮
UNIX命令true不做任何事情。实际上,下面程序的第一个版本是一个0行批处理文件(UNIX称之为shell scripts)。在后来的几年中,有许多不必要的源控制和其他“垃圾”被添加到该程序中,直到0行程序最终变成下面的样子:
#! /bin/sh
#
# @(#)true.sh 1.5 88/02/07 SMI; from UCB
#
exit 0
1.5是其版本号,它意味着相关开发人员已经经历4次升级才获得当前这个版本。那么他们为什么要重复改造一个无效程序4次呢?我无法理解。
3.7 程序23:计算字符个数
一个程序员想检查自己使用的strlen字符串长度函数。这个函数非常简单,但也许是过于简单,以至于其功能受到限制。那么下面的字符串的长度是多少呢?
Sam
This is a test
Hello World
1 /************************************************
2 * Compute the length of a string entered by ?*
3 * the user. ?? *
4 ************************************************/
5 #include <iostream>
6
7 /************************************************
8 * length -- Find the length of a string ?*
9 * (strlen does a better job.) ?? *
10 * ? *
11 * Returns: ?? ? *
12 * length of the string. ?? *
13 ***********************************************/
14 static int length(
15 const char string[] // String to check
16 )
17 {
18 int index; // index into the string
19
20 /*
21 * Loop until we reach the
22 * end of string character
23 */
24 for (index=0; string[index] != '\0';++index)
25 /* do nothing */
26
27 return (index);
28 }
29
30 int main()
31 {
32 char line[100]; // Input line from user
33
34 while (1) {
35 std::cout << "Enter a string: ";
36 std::cin.getline(line, sizeof(line));
37
38 std::cout << "Length is " <<
39 length(line) << '\n';
40 }
41 return (0);
42 }
(请参见“提示114”和“答案97”)
|