On the programming method http://tech.163.com/school · 2005-04-04 22:28:23 · Source: vckbase If you are an initiator --------------- - Please don't read; but I am ambition to become a medium-sized programmer -------- Please read; if you are an intermediate programmer ------------ Please read; if your advanced program Member -------------- Please criticize. This article is the explanation of the first lesson of the "Software Engineer Class", and the contents of the "computer software design development" lecture. The purpose of writing this article is to guide students to view programming methods from higher levels to make theoretical preparation for future advanced programmers. I. Computer hardware environment restrictions on software design methods have now been 60 years, and computer programming methods are also constantly evolving along with computer hardware technology. The hardware environment has both a serious restrictive role in software design, and has actively promoted. In my college alma mater (here, 6 words deleted), some teachers of mathematics department were fortunate to become a computer DIY family in my country. Oh, don't think it is an assembled PC, and they are assembled. One person with a high-speed rail cabinet size host, coupled with paper tapers (later improved to card reader), after assembly, in addition to their own research, there are still more than ten. At that time (seventies), a price of 10 million yuan. If it is converted to today, it is equivalent to a small computer worth approximately more than 1 million yuan, very high-grade computer. Let's guess such a high-end computer, how much is its memory? (Every mouth is closed, I want to announce the answer) - 4K. A 50-cential square memory board, insert into the host, well ------ 1K; plug in a memory board, okay ------ 2K; plug in a memory board, okay - ---- 3K; insert a memory board, okay ------ 4K; again ... no, can't stand up, too expensive! This is the environment at the time. What to write in this environment? Of course there is only the machine code. Written first, then read the manual to handle it to machine code, then tap or wear paper strips, enter the run. I can imagine that under the conditions at the time, what is the prime program? What is an excellent program? - Tips! The initial stage of program design is the era of skill. How can I save a byte, how to improve the efficiency of the program running, which is a problem that serious consideration. The so-called readability of the program, the maintenanceability of the program is not within consideration. Today, 35 years old or more friends who have learned computer may have used a personal computer --apple-ii (China also produced such a similar product "Chinese Learning Machine"). The main frequency is 1M, memory 48K (after the extension, up to 64K). I just use this computer to grow up :). At that year, similar personal computer products, there are PC1500, Layser310, etc. The Basic language has been cured on this computer, of course just for learning. To develop a real business program, you must use assembly, otherwise, the program is slower than the snail.
Therefore, the application of skills in programming is critical. Exterior 1: Bill Gates is the faithful support and promoters of Basic. At that year, he wrote a Basic interpreter with only 4K-size in the case of the assembly language, and once passed. There is indeed another person. (Not like the program that is now Microsoft, it is tens of trillion.) This may be the reason of Bill to Basic's love, whenever Microsoft launches (Linyi) a new technology, he will immediately provide support in Basic. Exterior 2: There is a game software "Police Cut thief" on Apple-II, and staying up late at the night. Later, this game was transplanted to the PC, cough ~~~ There is no way to play, because the thief has been caught by the police. The speed of the hardware is increased, and I can't re-refer to the previous time. Second, the structured programming With the price of the computer, the hardware environment is constantly improving, and the running speed is continuously improved. The more the program is more and more, the function is getting stronger, and the programming method of the skills can not adapt to the demand. Remember which book is said, the development cost of a software is: 30% of program design and 70% maintenance. This is a theoretical value given in the book, but in fact, from my more than ten years of work experience, the experience I get is: the program design accounts for 10%, while maintaining 90%. Maybe I am still too conservative, and the cost of maintenance should also increase. Below this procedure, provide two design plans, let's see which one is better? Title: 100 elements in an array, from small to large, display output. (BASIC) Method 1: Sort by bubbling, and output. For i = 1 to 100 for j = i 1 to 100 if A [i]> a [j] THEN T = a [J]: a [j] = a [i]: a [i] = t next ? A [i] Next I method 2: Sort by bubbling method, then output. For i = 1 to 100 for j = i 1 to 100 if a [i]> a [j] THEN T = a [J]: a [j] = a [i]: a [i] = t next Next Next Next Next Next NEXT FOR i = 1 to 100? A [i] next obvious, "Method 1" is higher than "Method 2", and it is run faster. However, from the current program design perspective, "Method 2" is higher. The reason is very simple: (1) Function module segmentation clear - easy to read; (2) is also the most important - easy maintenance. When the program is in the design phase, we must consider the future maintenance problem. For example, it is now realized the output on the screen. Perhaps in the future, you have to modify the program, output to the printer, output to the plotter; maybe in the future, you learn a new advanced sort method, by " The foaming method is improved to "rapid sort", "stack sort". So, on the basis of "Method 2", is it more simple, it is easier? ! This method of designing the function module is called "structured programming".
Third, thinking about the use of skills in the programming I can definitely, everyone must have done such a topic when starting the design of the program: seeking 100 prime numbers within 100. The teacher wrote the first program on the blackboard, and the eyebrow dance wrote the first program: (C procedure) method 1: for (i = 1; i <100; i ) {for (j = 2; j = i) Printf ("% d,", i);} then, the teacher starts to criticize this program "What is this? Too slow! Because we all know the big number It is impossible to be a number of ps. Therefore, it is necessary to exclude! "So, the second procedure is written unnecessively: Method 2: Printf (" 2, "); for (i = 3; i <100; i = 2) {for (j = 2; j = i) Printf ("% d,", i);} Teacher said: " Look! We only changed a little bit, and the speed of the program was more than doubled. " Then use the induction teaching method to continue to ask questions "The efficiency of the program" can be improved? Can you! ", You can write the third program: Method 3: Printf (" 2, "); for (i = 3; i <100; i = 2) '' does not consider the big minimum {for (j = 3; j = i) Printf ("% d,", i);} "Let's see that we only change a little bit, and the running speed is more than double. Yes. No! We can also improve again. " So it was proudly written in the fourth program: Method 4: Printf ("2,"); for (i = 3; i <100; i = 2) {INT K = SQRT (I); for (j = 3; j <= k; j = 2) IF (i% j == 0) Break; if (j> = k) Printf ("% d", i);} then, start prove why we judge the number of prime It is enough to verify that the square root is enough: assuming that the P is the number, then the order: p = a * b. Anyway: Since we have judged an integer within the square root of P, it cannot be removed by P, so A> SQRT (P). Based on the same reason B> SQRT (P). So P = a * b> SQRT (P) * SQRT (P) = P draws contradiction, proposition is correct. Indeed, "Method 4" is indeed a few times more than the "method 1" running speed, or even dozens of times. But let's take a closer look at the test.