Practical_C_Programming_chapter_7

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 = gcc
CFLAGS = -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.c

clean:
	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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This book is devoted to practical C programming. C is currently the premier language for software developers. That's because it's widely distributed and standard. Newer languages are available, such as C++, but these are still evolving. C is still the language of choice for robust, portable programming. This book emphasizes the skills you will need to do real-world programming. It teaches you not only the mechanics of the C language, but the entire life cycle of a C program as well (including the program's conception, design, code, methods, debugging, release, documentation, maintenance, and revision). Good style is emphasized. To create a good program you must do more than just type in code. It is an art in which writing and programming skills blend themselves together to form a masterpiece. True art can be created. A well-written program not only functions correctly, but is simple and easy to understand. Comments allow the programmer to include descriptive text inside the program. When clearly written, a commented program is highly prized. A program should be as simple as possible. A programmer should avoid clever tricks. This book stresses simple, practical rules. For example, there are 15 operator precedence rules in C. These can be simplified into two rules: 1. Multiply and divide come before add and subtract. 2. Put parentheses around everything else. Consider two programs. One was written by a clever programmer using all the tricks. The program contains no comments, but it works. The other program is well commented and nicely structured, but it doesn't work. Which program is more useful? In the long run, the broken one. It can be fixed. Although the clever program works now, sooner or later all programs have to be modified. The worst thing that you will ever have to do is to modify a cleverly written program. This handbook is written for people with no previous programming experience or programmers who already know C and want to improve their style and reliability. You should have access to a computer and TEAM FLY PRESENTS 9 know how to use the basic functions such as a text editor and the filesystem. Specific instructions are given for producing and running programs using the UNIX operating system with a generic cc compiler or the Free Software Foundation's gcc compiler. For MS-DOS/Windows users, instructions are included for Borland C++, Turbo C++, and Microsoft Visual C++. (These compilers compile both C and C++ code.) The book also gives examples of using the programming utility make for automated program production. How This Book is Organized You must crawl before you walk. In Part I we teach you how to crawl. These chapters enable you to write very simple programs. We start with the mechanics of programming and programming style. Next, you learn how to use variables and very simple decision and control statements. In Chapter 7, we take you on a complete tour of the software life cycle to show you how real programs are created. Part II describes all of the other simple statements and operators that are used in programming. You'll also learn how to organize these statements into simple functions. In Part III we take our basic declarations and statements and learn how they can be used in the construction of advanced types such as structures, unions, and classes. We'll also introduce the concept of pointers. Finally, a number of miscellaneous features are described Part IV. Chapter

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值