0%

### 几点原则

• 使用数组重新编写重复代码。冗长的相似代码常常可以使用最简单的数据结构——数组来更好地表述。
• 封装复杂结构。当需要非常复杂的数据结构时，使用抽象术语进行定义，并将操作表示为类。
• 尽可能使用高级工具。超文本、名字——值对、电子表格、数据库、编程语言等都是特定问题领域中的强大的工具。
• 从数据得出程序的结构。在动手编写代码之前，优秀的程序员会彻底理解输入，输出和中间数据结构，并围绕这些结构创建程序。

### 习题

1. A programming text gives the following twenty-five if statements as a reasonable approach for calculating the 1978 United States Federal Income Tax. The rate sequence 0.14, 0.15, 0.16, 0.17, … exhibits jumps larger than 0.01 later in the sequence. Any comments?

2. A$k^{\text{th}}$-order linear recurrence with constant coefficients defines a series as

$a_n = c_1a_{n-1} + c_2a_{n-2} + \dots + c_ka_{n-k} + c_{k+1},w$

where$c_1, \dots ,c_{k+1}$ are real numbers. Write a program that with input$k, a_1, \dots , a_k, c_1, \dots , c_{k+1}$, and N produces the output$a_1$ through$a_N$. How difficult is that program compared to a program that solves one particular fifth-order recurrence, but does so without using arrays?

3. Write a “banner” procedure that is given a capital letter as input and produces as output an arrar of characters that graphically depicts that letter.

4. Write procedures for the following date problems: given two dates, compute the number of days between them; given a data, return its day of the week; given a month and year, produce a calendar for the month as an array of character. The first version of your programs may assume that the year is in the 1990’s; the second version should be as general as possible.

5. This problem deals with a small part of the problem of hyphenating English words. The following list of rules describes some legal hyphenations of words that end in the letter “C”:

The rules must be applied in the above order; thus the hyphenations “ethnic”(while is caught by the rule “h-nic”) and “clinic”(which fails that test and falls through to “n-ic”). How would you represent such rules in a subroutine that is given a word and must return suffix hyphenations?

6. Write a form letter generator that is general enough to interpret the schema we saw earlier; make your program as simple as possible. Design small schemas and input files to test the correctness of your program.

7. Typical dictionaries allow one to look up the definition of a word, and Problem 2.1 describes a dictionary that allows one to look up the anagrams of a word. Design dictionaries for looking up the proper spelling of a word and for looking up the rhymes of a word. Discuss dictionaries for looking up an integer sequence (such as 1, 1, 2, 3, 5, 8, …), a chemial structure, or the metrical structure of a song.