Practical_C_Programming_chapter_7

来源:互联网 发布:东风(十堰)整合优化 编辑:程序博客网 时间:2024/03/29 12:46
Practical C Programming                                      Chapter 7

Programming is more than just writing code. Software has a life cycle. It is born, grows up, becomes mature, and finally dies, only to be replaced by a newer, younger product.

The major steps in making a program are:
    a. Requirements: the requirement document describes, in very general terms, what is wanted
    b. Program Specification: The specification is a description of what the program does.
    c. Code Design:    the design should include major algorithms, module definitions, file formats, and data structures.
    d. Coding: first writing a prototype and then filling it in to create the full program
    e. Testing: design a test plan and use it to test. when possible, there should be someone else test the program
    f. Debugging: correct program and test again
    g. Release: The program is packaged, documented, and sent out into the world to be used
    h. Maintenance: Programs are never perfect. Bugs will be found and will need correction
    i. Revision and updating: a new specifications need to be created and the process starts again

1. Setting UP
    group files in directories.
    cd ~    mkdir calc    cd ~/calc

2. Specification
    the program refines the specification into something that exactly defines the program that he is going to produce.
    so the first step is to write a preliminary user's specification
    The preliminary specification serves two purposes. First, you should give it to your boss or customer to make sure that you agree on what each of you said. second, you can circulate it among your colleagues and see if they have any suggestions or corrections.

3. Code Design
    In large programming projects involving many people, the code would be broken up into modules, to be assigned to the programmers. At this stage, file formats are planned, data structures are designed, and major algorithms are decided upon.
    the major algorithm is:
    
    Loop
        Read an operator and number
        Do the calculation
        Display the result
    End-Loop

4. Prototype
    Rather than try to write the entire program at once and then debug it, we will use a method called fast prototyping. We implement the smallest portion of the specification that will still do something.
    After we get this small part working, we can build the rest of the functions onto this stable foundation. Also, the prototype gives the boss something to look at and play with, giving him a good idea of the project's direction.
    Good communication is the key to good programming, and the more you can show someone the better.
#include <stdio.h>char line[256]; /* line of data from the input */int result;/* the result of the calculations */char operator; /* operator the user specified */int value; /* value specified after the operator */int main() {result = 0; /* initialize the result *//* Loop forever (or till we hit the break statement) */while (1) {printf("Result: %d\n", result);printf("Enter operator and number: ");fgets(line, sizeof(line), stdin);sscanf(line, "%c%d", &operator, &value);if (operator = '+') {result += value;} else {printf("Unknow operator %c\n", operator);}}}

5. Makefile
    Fortunately, both Unix and ms-dos/windows have a utility called make that will handle the details of compilation.
    Because the makefile contains the rule for compilation, it is customized for the compiler.

#-------------------------------------------------------##        Makefile for Unix systems                      ##      using a GUN C complier                           ##-------------------------------------------------------#CC = gccCFLAGS = -Wall -Wextra -g -D__USE_FIXED_PROTOTYPES__ -ansi## Complier flags:# -Wall -- Enables all the warnings about constructions# -Wextra -- Enables some extra warning flags that are not# enabled by  -Wall# -g-- Enable debugging# -D__USE_FIXED_PROTOTYPES__#-- Force the complier to use the correct headers# -ansi-- Don't use GNU extensions. Stick to ANSI C.calc: calc.c$(CC) $(CFLAGS) -o calc calc.cclean:rm -f calc

6. Testing
    Now is the time to start writing a test plan, this document is simply a lsit of the steps we performed to make sure the program works. It's written for two reasons:
        a. If a bug is found, we want to be able to reproduce it;
        b. If we change the program, we will want to retest it to make sure new code will not break any of the sections of the program that were previously working.

7. Debugging
    First we inspect the program to see if we can detect the error. however, for a large project .....
    Most systems have C debugging programs;
    and we can restore to a diagnostic print statement. The technique is simple: put a printf at the points at which you know the data is good (just to make sure the data is really good). Then put a printf at points at which the data is bad. Run the program and keep running in printf statement until you isolate the area in program that contains the mistake.
#include <stdio.h>char line[256]; /* line of data from the input */int result;/* the result of the calculations */char operator; /* operator the user specified */int value; /* value specified after the operator */int main() {result = 0; /* initialize the result *//* Loop forever (or till we hit the break statement) */while (1) {printf("Result: %d\n", result);printf("Enter operator and number: ");fgets(line, sizeof(line), stdin);sscanf(line, "%c%d", &operator, &value);printf("## after sscanf %c\n", operator);if (operator == '+') {printf("## after if %c\n", operator);result += value;} else {printf("Unknow operator %c\n", operator);}}}
    Note: the ## at the beginning of each printf is used to indicate a temporary debugging printf. When the debugging is complete, the ## makes the associated statements easy to identify and remove.


The final program is;

#include <stdio.h>char line[256]; /* line of data from the input */int result;/* the result of the calculations */char operator; /* operator the user specified */int value; /* value specified after the operator */int main() {result = 0; /* initialize the result *//* Loop forever (or until break reached) */while (1) {printf("Result: %d\n", result);printf("Enter operator and number: ");fgets(line, sizeof(line), stdin);sscanf(line, "%c%d", &operator, &value);if ((operator == 'q') || (operator == 'Q')){break;}if (operator == '+') {result += value;} else  if (operator == '-') {result -= value;} else  if (operator == '*') {result *= value;} else  if (operator == '/') {if (value == 0) {printf("Error:Divide by zero\n");printf("operation ignored\n");} else {result /= value;}} else {printf("Unknow operator %c\n", operator);}}return 0;}

8. Maintenance


9. Revisions

10. Electronic Archaeology
    cross references: xref, cxref, and cross. System V unix has the utility cscope
    Program indenters: cb and indent
    pretty printers: vgrind or cprint
    call graphs: cflow, calls

11. Marking up the Program

12. Using the Debugger
    The debugger is a great tool for understanding how something works

13. Text Editor as a Browser
    find:    ctrl + f
    replace: ctrl + h

14. Add Comments:
    Don't be afraid of putting any information you have, no matter how little, into comments.
    As you go through someone else's code adding comments and improving style, the structure will become clearer to you. By inserting notes (comments), you make the code better and easier to understand for future programmers.


0 0
原创粉丝点击