0% found this document useful (0 votes)
9 views199 pages

《程序设计实践》中文版

The document discusses various programming languages and their functionalities, focusing on C, C++, Java, and Unix commands. It covers topics such as data types, input/output functions, and regular expressions, along with examples of code snippets. Additionally, it references notable programming literature and authors related to these languages.

Uploaded by

2jngzxnjcs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views199 pages

《程序设计实践》中文版

The document discusses various programming languages and their functionalities, focusing on C, C++, Java, and Unix commands. It covers topics such as data types, input/output functions, and regular expressions, along with examples of code snippets. Additionally, it references notable programming literature and authors related to these languages.

Uploaded by

2jngzxnjcs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 199

9

Giles Lytton Strachey

C shell

( ) HTML
JavaScript PostScript

Visual Basic

Java
printf

9.1

( )

printf

% printf

C++ iostream Java


170
java.io

printf

%L %P %R 4

C C++
8

8 16 32
ANSI C 8 char 16 short 32 long

2 1 4

( short long )

printf

c 8 s 16
9 171
l 32 ( )
cscl

printf %

pack buf

typedef

sprintf strcpy pack


172

pack stdarg.h 4 eprintf


va_arg va_arg va_list
va_start va_arg ( va_arg
) va_end c s
char short int char short
... C int
pack

unpack
9 173
scanf unpack

ANSI C
short long
pack 32 long
32

unpack

unpack_type2 2
174

9-1 pack unpack short


long
1 32 long 64 long

9-2 pack unpack

9-3 C C++ C++ pack


unpack receive C++

9-4 printf

9-5 Java DecimalFormat

12345.67 12,345.67 .4
_ _ _ _0.40( ) DecimalFormat

9.2

pack unpack

Unix
9 175

( )

.exe

abc
(metacharacter) Unix
^ $ ^x
x x$ x ^x$ x ^$

. x.y xay x2y xy


xaby ^.$
[] [0123456789]
[0-9]
| *
+ ? \
\* * \\ \
grep
grep

grep

Regexp

Bob
176
( ) grep

grep
regex regexp grep

grep ^ $ . * *

^
matchhere
do-while ( $
.* 0 )
matchhere
matchhere

^ $
matchhere
9 177
x*
matchstar ( x)

do-while x* 0

grep
match

C 0 0
grep Unix
0 1 2( e p r i n t f)
178
grep match

grep

grep

grep

grep

match grep
( )
aaaaa a*
a match
matchstar

matchstar
9 179

grep
grep

grep
a *a* a*a* a*b
aaaaaaaaac Unix
grep egrep

match [a-zA-Z]
( )
(a b c d e f)

9-6 match strstr

9-7 matchhere

9-8 grep -v -i
-n

9-9 match +( ) ?( 0 ) a+bb?


a b

9-10 ^ $ *
match
match

9 - 11 match
[a-z]
[^0-9]

9-12 match matchstar


match gres grep
180

9-13 match grep Unicode UTF-8 UTF-8


Unicode ASCII
UTF-8

9-14

9.3
grep

Perl

Aw k
3 Aw k
$1 $NF NF
Aw k
Awk

fmt
60
9 181
fmt 3 Markov

Unix eqn eqn


piover 2 TEX
2
\pi\over 2

Aw k
Aw k
Perl Tc l

Tcl r e g s u b(regular expression substitution )


4
URL h t t p : / /( )
/ lindex ( 0)
[] Tcl $x x

< > HTML


Perl Perl
HTML

Perl

str regexp ( ) repl g


182
\s ( ) \n
&nbsp; HTML 2

Tc l Perl Aw k

Tc l Perl Aw k

9.4

printf

Awk Perl
( )

( )

(parser)
yacc bison ( )
( ) C C++
9 183
( )

2
( Aw k
)
184

pack unpack

eval

( )
9 185

generate code
generate
code

a= max(b,c/2)
186

0 divop generate

goto pc
pc
code

1 000 addop 1001 pushop

generate
Java Java
(JVM)

9.5

generate
( )

HTML HTML
9 187
JavaScript Perl C (
)
PostScript

57 000 PostScript

C C
( Modula-3 C++) C
C
C C

Visual Basic Visual Basic

(wizard)

C C++
Plan 9
188
enum

.c

Perl

Andy Koenig C++

/// ( )

C++

shell Aw k

PostScript PostScript

E
9 189
% %%
(bounding box)

Java / ** ** /
(literate programming)

9-15

9.6

C/C++
7 C

Bart Locanthi
bitblt rasterop
Locanthi
190

9-16 7-7 C++

9-17 7-8 Java Java


( )
Java

9.7

(Just In
Time) JIT

c 2 b
divop
2 0
0

m a x ( 3*3 , 4 / 2 )
9

literal x
9 191

1967 Ken Thompson IBM7094


7094

code
char int long
int
192

generate genreturn eval


flushcaches

CPU
flushcaches

( v o i d ( *) ( v o i d ) ) c o d e (Cast)

code

popreg
pushreg
addop
( A D D I N S T) (
SHIFT )

cast
9 193

addop

generate

CPU

grep
(
)
C++

9-18 m a x ( 3 *3, 4/2)

9-19

Brian Kernighan Rob Pike Unix (The Unix Programming


Environment Prentice Hall 1984) Unix
8 yacc

Don Knuth TEX: (TEX: The Program Addison-Wesley 1986)


13 000
Pascal
Chris Fraser David Hanson C
(A Retargetable C Compiler: Design and Implementation Addison-Wesley 1995) ANSI C

Java Tim Lindholm Frank Ye l l i n Java (The Java V i r t u a l


194
Machine Specification 2 Addison-Wesley 1999)
Ken Thompson ( )
(Regular Expression Search Algorithm Communications of The ACM 11 6
pp419~422 1968) Jeffrey E. F. Friedl (Mastering Regular Expression
O Reilly 1997)
Rob Pike Bart Locanthi John Reiser Blit /
(Hardware/Software Tr a d e o ffs for Bitmap Graphics on the Blit Software-Practice and
Experience 15 2 pp131~152 1985 1 )
1

William Strunk E. B. White


2

C (The C Programming Language) Java C++

( )

#define TWENTY

1.1

( )

n npoints
numberOfPoints
i j p q
s t
1 3

p
nodep Global
CONSTANTS
pch strTo strFrom
npending
numPending num_pending

C++ Java

Java

(queue) Q Queue queue


UserQueue

items users

( )
4

isoctal

getIndex 0 nTable 1
nTable inTable

1-1

1-2

1-3

1.2
1 5

(< < = = = ! = > = ) > (&& ||)


C

(& |) ( ==)
6

C C++ Java

bitoff 3 3
0 bitoff bitoff 3 3
subkey

?:

?: 4 if-else 1
1 7

++

C C++

str i
str i

i 3 3 4
I/O

yr
yr profit[yr]
scanf
&profit[yr] yr

1-4

1-5
8

1-6

1.3

strcpy
for

if

if

else(dangling else)

else

if
1 9

2000 1900 2100

C C++ Java n

n 0 n 1
for ++

C
C++ Java

C
10
for

do-while for while

getchar

putchar
do-while

nmemb i A r r a y[ 0 ] i A r r a y[n m e m b 1 ] <=


1 11

C C++

gets
6 fgets
strlen ‘\ 0’ strcpy
strcpy

C++

+1
Java
Java
C C++ strdup malloc strcpy
strdup ANSI
C
malloc
malloc
realloc strdup
else-if if ... else if ...
else

(condition)

else
else
12
else else if

if

if
( )

else

case break

case break case


1 13

else-if

case

1-7 C/C++
14
1-8 Java

1.4

C
I/O getchar isdigit
C
( )

C++ Java
C

<ctype.h>

c isupper

A Z
C isupper

ctype
getchar

ROUND_TO_INT
(C++
)
1 15
square

C++

1-9

1.5

(1) 0 1

24 80

20 21 22 23 27
24
80 26
16
3 (80 1)/26 let 26 27 ( (off-by-one)
1 )

0 NLET
( ) draw MAXROW MINCOL

C #define C

C
C++ C++
const

Java final

C const enum C
<ctype.h>

C C
1 17

C C++

Java

0
0
(v o i d *) 0 NULL C ‘\ 0’ 0

0
C++ 0( N U L L)
Java null

s i z e o f (i n t ) 2 4 sizeof(array[0])
sizeof(int)
sizeof

Java length

C C++ .length ( )
18

1-10

1.6

i++ i 1
1 19

(inverse discrete cosine transform


DCT) JPEG
20

result matchfound

ctime

C
1 21
strcmp

(
above )

1-11
22

1.7

Strunk
White (The Elements of Style Allyn & Bacon)

Brian Kernighan P. J. Plauger (The Elements of


Programming Style McGraw-Hill 1978) Steve Maguire
(Writing Solid Code Microsoft Press 1993) Steve
McConnell (Code Complete Microsoft Press 1993) Peter van der Linden
C C (Expert C Programming: Deep C Secret Prentice Hall
1994)
2

Raymond Fielding

2.1

(Java )

NULL
24

C C++ char *array[] char **array

strchr strstr C C++


Java String indexOf C++ find

HTML

(
)
1 NELEMS
2 25

htmlchars

1
/2

n 2 l o g 2n
1000 1000 10
1 20
( )

2.2

(quicksort) 1960 C. A. R. Hoare

( )

( )

quicksort
26

swap swap quicksort

( ) ( l a s t)
( i) last=0 i=1 n-1

for 1 last last+1 i-1


i n-1 v[i]>=v[0]
v[i]

0 last
2 27

• n n/2
• n/2 n/4 4
• n/4 4 n/8 8

log2n n
+2 n/2+4 n/4+8 n/8 (log 2n ) nlog2n
2 nlogn

n2

n2

2.3

C C++

C
qsort qsort
v o i d*
(
)
scmp
strcmp
28
strcmp qsort
&str[i]( char**) str[i]( char*)

str[0] str[N-1]
( ) qsort

icmp

v1 v2

qsort

ANSI C bsearch qsort bsearch


( qsort ) bsearch
bsearch NULL bsearch HTML
2 29

qsort key
Nameval
nvcmp Nameval strcmp

scmp
bsearch ey qsort
bsearch
bsearch

C++ sort O(nl o gn)

C++

2-1
(Hoare )

2.4 Java

Java
Java sort Comparable

Java
quicksort
qsort
30
Java C C++

Object

Cmp cmp
Object

Integer

Object
int double
Integer int
C quicksort Java
Cmp left right
Java
2 31

Quicksort.sort cmp swap

left right ( )

Math.abs Java
sort swap rand rgen Quicksort

Quicksort.sort String

sort

2-2 Java ( Integer)


Object Quicksort.sort

2.5 O

n( )
n l o gn
n 2
nlogn
32
CPU ( )

O
n n O
(order) O( l o gn) l o gn
n O ( f(n) ) n f(n)
O(n )
2
O(nl o gn)
O(n2) n
O(nl o gn) n

O(n2)
O(nlogn) ( O(n2) )
0 (O(nlogn)

O(1)
O(logn)
O(n)
O(nlogn) nlogn
O(n 2)
O(n 3)
O(2 n)

O( 1 )
O( l o gn) strcmp n
O(n) O(n )
3
n
n2
n 2n
O( 2n) n
(
)

2-3

2-4 n
2 33
( n )

2.6

HTML n
n O(2 )n
n

C++ Java
C struct
Nameval

Java C++
34
addname 1
realloc
NULL realloc
realloc
O(2n )2

(NVINIT=1)

realloc C void*
C++ (
) ( )
C C++ C

memmove memmove

ANSI C memcpy
memmove

memmove
memmove
2 35
memmove

memmove

NULL

O( 1 )

2-5 delname realloc

2-6 addname delname

2.7
( )
Lisp C++
Java C
C

( )
36
C List HTML
Nameval

newitem

emalloc malloc
4 emalloc

addfront

addfront

O(n)
2 37

addend O(1)

next

O(n)

apply
apply apply
apply

apply void

fn void fn
void fn
Nameval* void* fn

apply
38

listp
listp->next (n e x t)

listp->next free
freeall listp->name Nameval name

newitem freeall

Java
4
2 39

freeall delitem name


eprintf
4
eprintf

C++
(STL)
O( 1 )

(LIFO) ( )

2-7

2-8 reverse

2-9 C List void*


C++ Java Object

2-10 6

2.8
40
0

left
right

Nameval

lesser greater

Nameval
ASCII

O(n) O( l o gn)

( )
Nameval
2 41

insert
(c m p = = 0)
O( 1 ) O(n)

weprintf eprintf
warning eprintf

O( l o gn)

right

lookup insert
42

lookup insert

lookup
lookup

goto while

( )
( )
2 43

( 1)
( )

2-11 lookup nrlookup

2-12

2-13

2.9

( ) ( )

Internet
IP
44
Java
C C++ ( )

n n/ ( )
O(1)

2.7
( )
/
create lookup

O( 1 )
2 45

O( 1 )

hash

ASCII
31 37

C C++ char

Java

16 8 9
46
( .j a v a .c l a s s ) URL
h t t p : / / w w w. .h t m l
Java
( 37)

( ) (
URL)

(O ( )) (
O(xsize ysize zsize)
Gerand Holzman
Supertrace Supertrace

Supertrace
( )
Supertrace ( 0 Supertrace
) Supertrace

O( 1 )

O(n)

2-14
NHASH

2-15

2-16 lookup x y

2-17

2.10
2 47

( )

O(n)

Bob Sedgewick (Algorithms Addison-Wesley)


C++ (Algorithms in C++) 3
Don Knuth (The Art of Computer
Programming Addison-Wesley) 3 ( 2
1998)
Gerard Holzman (Design and Validation of Computer
Protocols Prentice Hall 1991) Supertrace
Jon Bentley Doug McIlroy Engineering a sort function ( S o f t w a r e—P r a c t i c e
and Experience, 23, 1, pp.1249~1265, 1993)
3

Frederick P. Brooks, Jr.,

Brooks

C C++ Aw k Perl

( )

3.1
3 49

( )

w1 w2
w1 w2

w3 w1w2
w3
w1 w2 w2 w3

Show your flowcharts


table your flowcharts
and will tables and

words words.
( )
50

3.2

n=100 000
100 000 n

100 000 1000

( )

might appear twice


3 51
might appear once
might appear twice
2

3.3 C

(N P R E F ) (N H A S H)
(M A X G E N) NPREF

NHASH=4093 10 000 ( )

S t a t e( )
Suffix( )
52

2
3 53

lookup sp->pref[]
lookup
I/O lookup

sprintf fscanf
fscanf %s fscanf

100 (
) % 9 9 s( ‘\ 0’ )
fscanf 99

( )
sprintf

build prefix NPREF


FILE prefix add

memmove 1 NPREF-1
54
0 NPREF-2

addsuffix

add
addsuffix add
build addsuffix add

3.4

NONWORD

NONWORD

NONWORD
NONWORD
3 55

generate
9
fmt
NONWORD generate

nmatch

nmatch 1/nmatch
1 1/2 3 1/3
k 1/k
prefix
suffix
lookup prefix

NONWORD
NONWORD memmove

main
56

C C

C
C

3-1

3-2

3-3 NONWORD generate


0 1 2 3 4

3.5 Java
Java Java

( )
Java C
Vector Object
Hashtable
Vector

Java State Hashtable ( )


C State
State
Hashtable put get
3 57
Prefix

Chain

main Chain

Chain NPREF NONWORD


build StreamTokenizer

add ( )
58

suf add Prefix prefix


Hashtable
C

Prefix
Prefix n
NPREF NONWORD

Prefix hashCode equals Hashtable


Hashtable
Prefix Vector
hashCode hashCode
3 59

equals

Java C Vector Hashtable

Hashtable
hashCode equals Java
C Java Java
Vector C

lookup State Suffix

3-4 Java markov ( Vector) Prefix

3.6 C++

C++ C++ C C
C markov C++
C++
Java
C++ Standard Template Library( ) STL STL
ISO C++ STL
STL
60
C++ STL
( ) C++
STL vector
vector<int> vector<string> vector

STL vector ( Java vector ) deque


d e q u e( d e c k)
NPREF O( 1 ) STL
deque

STL map map


map O( l o gn) map
O( 1 ) (
C++ hash hash_map )

STL deque deque<string> deque


typedef Prefix

map statetab C
Java equal
main ( C++ i o s t r e a m c i n)

build iostream
3 61

buf
add STL

map ([] )
statetab[prefix] statetab
prefix
vector deque push_back
deque pop_front deque

3-5 STL C++

3-6 C++ string


STL

3.7 Awk Perl

(Awk Perl)
62

Aw k Perl

Awk Perl 2

Aw k

BEGIN END
Aw k BEGIN

Awk ( ) $1 $NF
NF

nsuffix n s u f f i x [ w 1 , w2]
statetab statetab[w1,
w2, 1] statetab[w1, w2, 2]
END nsuffix
statetab
Perl (
) Perl
$ @ [] {}
3 63

statetab

statetab{$w1}{$w2}
$statetab{$w1}{$w2} $suf->[$r] r
Aw k Perl
C++ STL ( add g e n e r a t e)

3-7 Aw k Perl

3.8

42 685 (5 238 22 482 )


( Blessed is the ... ) 400

10 000 250 MHz


MIPS R10000 Irix 6.4 400 MHz Pentium II 128 M
Windows NT
64

C C++ Java Irix


C C++ Sun SPA R C DEC Alpha
C Perl

Wi n d o w s STL d e q u e deque

deque ( STL )
Irix ( )h a s h
Wi n d o w s deque
list map hash STL
STL C++
STL Java

3.9

Don P. Mitchell Bruce


Ellis 80 (deconstructionist)

Perl Aw k
3 65
C++ Java
( )

C++
STL

STL

C++
Brooks

Awk Perl

( )

3-8 Scheme Tc l Prolog


Python Generic Java ML Haskell

Matthew Austern STL


66
(Generic Programming and the STL Addison-Wesley 1998) C++
Bjarne Stroustrup C++ (C++ Programming Language 3 Addison-
We s l e y 1997) Java Ken Arnold James Gosling Java
(The Java Programming Language 2 A d d i s o n - We s l e y 1998) Perl
Larry Wall Tom Christiansen Randal Schwartz Perl (Programming Perl 2
O’Reilly 1996)

1
Erich Gamma Richard Helm
Ralph Johnson John Vlissides (Design Pattern:
Elements of Reusable Object-Oriented Software Addison-Wesley 1995)
Markov shaney 1989 6
A. K. Dewdney (The Magic Machine W.
H. Freeman 1990)
4

Robert Frost

2
3

4.1

(comma-separated value) CSV

CSV
68

( ) We b
We b

We b

CSV (
)

HTML

Tcl Web CSV

& f= printf
s l1
c1

getquotes
4 69
CSV
CSV
CSV

CSV

4.2

Fred Brooks
Brooks

CSV

csvgetline CSV

CSV scanf C
strtok strtok(p, s) p s
strtok
strtok
NULL
strtok
70
unquote

csvgetline

printf

getquotes.tcl

( HTTP )

We b
(\ r)
4 71




• ( f i e l d) csvgetline

• csvgetline

csvgetline
72

4.3

csvgetline

3
char *csvgetline(FILE *) CSV
char *csvfield(int n) n
int csvnfield(void)
csvgetline

csvgetline
NULL
csvgetline

CSV "x" "y" x"


y ""
0 csvfield(-1)
csvfield(100000) "" ( )

NULL C

( ) fgets

CSV
csvgetline
csvgetline
csvfiled(n) n csvnfield
4 73
csv

csvgetline
( ) (
) ( )

csvgetline
csvgetline
csvfield

csvgetline FILE

csvgetline NULL

ferror csvgeterror

csvgetline
74
"..."

" ""
""

char *csvgetline(FILE *f);


f
\r \n \r\n EOF
EOF NULL
NULL

char *csvfield(int n);


0
csvgetline n
n<0 NULL

"..."
"..." "" "

NULL

csvgetline
int csvnfield(void);
csvgetline
csvgetline
csvfield csvnfiled
csvgetline EOF

csvgetline
csv.h csv.c
csv.h csv.c

EOF NULL E O F( EOF


) C EOF
4 75

split static
C

line sline
line field sline
ab, cd , e f ,, g,h

csvgetline
76

line realloc
2.6 sline line csvgetline
split field

reset
csvgetline

endofline
EOF

strtok
4 77
split
strtok

split

advquoted
strcspn(p,s)
p s
advquoted

"abc"def
Microsoft
78
Excel

csvfield csvnfield

( )

4-1
4 79

4-2 (a) (b)


(c) ( 9 )

4-3 C
NULL

reset

4-4 CSV
printf
9

4.4 C++
C++ CSV C
C C++
C++

Csv
Csv
CSV

Csv

C++ string vector C


80
string 0
NULL Csv::getline

+=
endofline

split
4 81

C++ strcspn split advquoted


advquoted C++ find_first_of
s.find_first_of(fieldsep,j) s j fieldsep

for fld

find_first_of advplain
strcspn C++

Csv::getfield
82

C 30 000 25
C++ C 40% 4 markov
C++ 20%

4-5 C++ operator[] csv[i]

4-6 Java CSV

4-7 C++ CSV STL

4-8 C++ Csv


C
csvnew

4.5

CSV

C I/O
I/O FILE*
( <stdio.h> )
4 83

I/O sdtin stdout


FILE

sdtin stdout stderr


_ _i o b ANSI C

C++ Java
3

(Application Program Interface) API

C
I/O

stdout

memcpy memmove

strtok
84

C (
4-8)

C C++

C str...

C I/O FILE*
STL

C mem...
str... I/O fread
fwrite read w r i t e( ) Unix

* ( *.exe )
We b

( _ _ i o b)

4.6

( )
4 85
CSV

reset

C++ Java

Csv

C
csvgetline ( )

(
) csvgetline
csvgetline

C++
Java ( i n t)
Java markov
C
Java

( ) C
C C++ atexit

CSV
86
C++

Java

Java Java

0 C
C++

Java

( strtok C
)

Java

4.7

eprintf estrdup
eprintf fprintf(stderr, ...)
<stdarg.h>
vprintf ... stdarg
va_start va_end 9
4 87

eprintf C strerror
( ) weprintf eprintf
printf

estrdup eprintf

emalloc malloc

eprintf.h
88

setprogname progname eprintf


4 89
NULL
csvgetline
NULL
csvgetline

C C++ Java
( )
getchar char
char EOF

log IEEE
N a N( not a number)

Perl Tc l
C++ STL
pair
( )

Unix C 1(
) errno strerror

errno errno 0
90

Java

read 1
null C
C++ try I/O
IOException

C setjmp longjmp

CSV reset

4.8
4 91

estrdup

s y n t a xe r r o (r ) estrdup
failed(estrdup )

Abandon ( ) control-Z
92

Frederick P. Brooks, Jr. (The Mythical Man Month A d d i s o n - We s l e y


1975 1995 )

John Lakos C++ ( L a rge-Scale C++


Software Design Addison-Wesley 1996) C++
David Hanson C (C Interfaces and Implementation Addison-Wesley 1997)
C
Steve McConnell (Rapid Development Microsoft Press 1996)
( )
Kevin
Mullet Darrell Sano (Designing Visual Interfaces:
Communication Oriented Techniques Prentice Hall 1995) Ben Shneiderman
(Designing the User Interface: Strategies for Effective Human-
Computer Interaction 3 A d d i s o n - We s l e y 1997) Alan Cooper
(About Face: The Essentials of User Interface Design IDG 1995) Harold
Thimbleby (User Interface Design Addison-Wesley 1990)
5
bug.
b.
1889 Pall Mall 3 11 1/1
bug

(bug) bug

I/O
goto
94
6

5.1

( )
5 95

5.2

printf scanf

Sun SPA R C
( )

scanf double %lf %f


scanf printf
GNU gcc gcc printf
96

malloc realloc new

-o

-f123
0 case

1997 7
5 97

2
icmp qsort

scmp

dbx ( )

strcmp strcmp

badqs.c 13
98

5.3

3 Markov

undo

1023
1024
1024
5 99

c a n ’t get here
got here

g r e p(g r e p 9
) C C++
%x %p

A B

check C abort
check
check

check

check
100

We b

( )

I/O printf

C fflush
C++ Java fflush
I/O
setbuf setvbuf s e t b u f (f p , N U L L) fp
(stderr cerr System.err)

3 C markov x
y x
( 42685 22482 )
31 37 128
15 16 5 6
187 20

31 37 128
5 101
diff

grep

RCS

5.4

& | = =
!=

shell script UNIX DOS


102

p
5 103

Unix PC

16 32
ctype.h
isprint

( 40 '' ) (
) getchar

1994 Pentium

1/2 0.5
0.7432

5.5

C
104
C++
( ) 0

printf

msg
malloc static
2
freeall

p->next
malloc free

malloc free

5-1 malloc free


malloc free
5 105
5.6

strings

strings
ASCII ( zip )
strings
Unix strings

-a
strings ASCII

strings MINLEN=6

printf %.*s (i) (b u f)

do-whilet EOF
getc printf
106
getc
printf printf
(
)
do-while
strings strings

strings

strings Unix
Windows 95

( ) 8
getchar Wi n d o w s getchar
0x1A control-Z EOF
5 107
Unix
rb sdtin
( fdopen setmode C )

Wi n d o w s Unix Wi n d o w s

5-2 strings MINLEN


strings

5-3 vis
ASCII \Xhh hh strings
vis

5-4 \X0A vis vis

5-5 vis

5.7

(
)

grep
( )
108

0 ( ) ( )
C C++

4.0b1
4.0b2

5.4 isprint

5.8
5 109

( )

Steve Maguire ( Writing Solid Code Microsoft Press 1993) Steve


McConnell (Code Complete Microsoft Press 1993)
6

Norbert Wiener( )

Edsger Dijkstra

SUM(A1:A50)

6.1
6 111

fgets

i 0 i 1
s[-1]

i 0
‘\ 0’ s[0]

i MAX-1
getchar EOF

‘\ n’

( ) ( )
n n
0
112

n 0 avg
avg
n 4
n 0 0

1998 11
0

C C++ <assert.h> /

n 0
avg

(
) avg 0
6 113

( )

( fread fscanf) ( fopen)

( fprintf f w r i t e)
fclose EOF
0

6-1 1

(a)

(b)

(c) src dest

(defensive programming) programming


114

(d) s n t

(e)

(f)

6-2 1998 2000

(a) 2000
2000 1 1
(b) ctime

ctime
(c)

(d)

6.2
6 115

4 CSV

( )



1
116
3 5

(
) ( ) (
)

w c(
) s u m( )

(c m p) (d i f f)

freq
6 117

( )

(Profiler)

6-3 freq
118
6-4 freq 32

6.3

( )
( )

Unix cmp diff


sort grep wc sun freq

ka
(o l d _ k a) (n e w _ k a)
Unix shell Perl

-s cmp cmp
!cmp cmp
6 119

Aw k

Aw k (n e w a w k) Aw k
echo

Awk 1

( try )
1
yes 7 yes no
Aw k ( ) Aw k

( 9
)
Aw k
120

Awk

6-5 printf

6.4

m e m s e t(C/C++ m e m... )
mem

memset(s, c, n) s n c s
6 121
32 64

memset
n 0 1 2 2
2 16
16
2 memset

c 0 0 x 7 F( 8
) 0x80 0 x F F( )
( )
memset
n c

memset

s0 s1
n

0 16 i n 2i 1 2i 2i 1

memset
3500

memset
122
( )
memset
memset
0x7F c memset

memset
memmove

memset

(tile)

6-6 memset

6-7 mem...
6 123
6-8 math.h sqrt sin

6-9 C s t r... strcmp


mem... strtok strcspn

6.5

Microsoft Visual C++ Version5.0


C++ STL

1420

1594 ( )
255

( )

gets
fgets(buf, sizeof(buf), stdin) scanf( %s , buf)
124
scanf( %20s ,buf)
3.3

gets

10 num 0
gets

if

1998 7

1988 Internet
HTML

1024 gets
6 125

x y z malloc
p[x] int 16 x y z 41
x*y*z 68921 2 16
3385 malloc 3385

1996 7

64 16

7 ASCII (
)

Steve Bourne Unix ( Bourne )


254 ‘\ 0’
( Unix )
( )

6-10

6.6

( )

2 4 CSV
126

10 NULL

0
0xDEADBEEF

8
7

6.7

)
6 127
Don
Knuth TEX

Beta
Beta

( )

6.8

2
( )
( )
128
Aw k

10000 42685 6~7

Java

abc abd c
d freq
Java d
20 c Java
2

c
6 129

6.9

Don Knuth
(Software: Practice and Experience19 7 pp607~685, 1989)
The Errors of TEX TEX
TEX TRIP Perl

MakeMaker TestHarness Perl

Jon Bentley (Communications of the ACM)


(Programming Pearls) (More
Programming Pearls) Addison-Wesley 1986 1988
7

( )
( )
2

7.1

performence
7 131
Internet
Internet
( )
(spam)

Make millions in your spare time XXX-rated

grep

( )

C strstr

( ) strstr
strstr
( )
strstr
132

strchr
strcmp
strstr

strncmp strlen

strncmp
\0
( strncmp ) \0

strchr
\0 isspam \0

strstr strchr strncmp

strstr

strstr

strstr

e x
e x

strstr 30%

strstr
7 133

npat
npat strlen(mesg)*npat

j
mesg[j]
52 strlen(mesg)*npat/52
s x
52

isspam

starting[c][] c starting[c]
c nstarting[c] c
0 npat
0 20 patlen[k] strlen (pat[k])
134
b

strstr 5~10
7~15 52

strstr

7-1
isspam
(t r i e)
7 135
7.2

Unix time

real( )
user CPU system (
)CPU

( 20% )

time
C C++ clock
CPU clock
CPU

CLOCKS_PER_SEC clock

Java Date CPU

getTime
136

1971 Don Knuth

Unix -p prof

217
10 000 250MHz MIPS R10 000
strstr
(217 ) (10 000)

( )
7 137
strchr strncmp( strstr )
Knuth
50%

strstr spamtest 99.8%


strstr

isspam
spamtest memcmp
isspam isspam strstr
strlen strchr memcmp
strncmp memcmp

strlen 652 memcmp strncmp


isspam strchr
strchr

Aw k

50%
MAXFLD 200 Aw k
2 3

25%

C markov
( 42 685 22 482 ) 2
138
2 16 384 2

( )

1000
2

7-2 time clock getTime

7-3 strchr 48 350 000 strncmp


46 180 000

7.3

spam
O(n2)
O(nlogn) 2

s n strlen n
n

C C++
( )
7 139

8.1 5.9
25% strstr
strstr

(
)

7.1 isspam

6.6
(n s t a r t i n g [ c ])

5.9 10%
starting[c]
140
memcmp
3%

50%

isspam
( )

10%( ) 10%

7.4

1 sqrt
7 141

2
C C++

sqrt(dx*dx+dy*dy)

grep
142

We b
Internet
/2
1

drawchar show(lookup(c))

malloc free

malloc

C
alloca
alloca

C printf
7 143

Inferno
draw
( )

strlen(pat[i]) patlen[i]

360 ( )

isdigit

float double
IEEE

0
sin cos

C++ Java C

memset memmove
6
memset

7-4 memset
4 8
144
memset

7-5 C smalloc
malloc struct
smalloc malloc

7.5

19
(
)

short int 2-D 16


float double float
6 7
printf
( )scanf

C C++

C++
7 145

5%
m[i][j] m(i,j)

( 100 200 100 )

PPM
GIF 50K
GIF
GIF 500 PPM
10 GIF PPM
GIF

7.6
146

C C++

250 MHz MIPS 10 000


7 147

malloc free

I/O

CPU
CPU

400 MHz
200 MHz 100 MHz

7-6

7-7 C++
iostream STL

7-8 Java

7.7
148

Aw k
20

10 2
6

Bob Flandrena Ken Thompson

( )
Knuth F O RT R A N (An Empirical Study of
FORTRAN Programs) (Software: Practice and Experience 1
2 pp105~133 1971)

Jon Bentley (Programming Pearls


More Programming Pearls Addison-Wesley 1986 1988)

Rick Booth (Inner Loops Addison-Wesley 1997) PC

John Hennessy David Patterson


/ (Computer Organization and Design: The Hardware/Software Interface
Morgan Kaufman 1997)
8

Robert Venturi

8.1

(
)
150

C C++ char
8

C ANSI/ISO 1988 ISO C++


1998
Java

C C++

x char
( x
)

C C++

C // complex
near far

C C++
C
C++

C
8 151
C sqrt

ANSI C

ANSI C

func func
func 3 C

C++
STL

C C++

char 8 short int 16 long 32


int

64

PC
152
PC
far near

sdtdef.h
size_t sizeof (
strlen) ( malloc)
Java byte
8 char short 16 int 32 long 64

IEEE

C C++

getchar

count ptr

( )

errno log

&& ||
( )
?: ( )
Java

Java C C++ C C++

char char C C++


char int g e t c h a r()
int

char c 0 255 char 2


8 c 128 127
8 153
EOF (E O F stdio.h 1)
6.1
char s[i]==EOF

getchar EOF s[i] 255( 0xFF -1 unsigned


char ) char EOF 255

char EOF
0xFF EOF
char getchar int
EOF

Java unsigned 16 char

>> (
) ( 0) C
C++ Java >> >>>
short int long

i 2 4 8 int
n n double
8 8
154
struct X

s i z e o f(s t r u c tX)
sizeof(char)+sizeof(int)

char EOF sizeof

C++ C

C
(Unix Plan 9 Wi n d o w s ) C C++

8.2

C stdio
C++ iostream Java java.io

C
8 155
ANSI C strdup
strdup

stdio.h C ANSI C C++


#if #ifdef

( )

ANSI C strdup

weprintf wprintf C
stdio.h

wprintf stdio_wprintf
wprintf
wprintf
156
wprintf
#ifdef
weprintf

ANSI C
6 signal POSIX 19 Unix 32
ANSI

POSIX Microsoft
Windows API

8.3

stdlib.h

stdlib
malloc realloc
free u n s i g n e d int s i z e _ t( malloc realloc )
STDC_HEADERS _LIBC
8 157

#ifdef

malloc realloc free ANSI C

#ifdef

#endif #endif

#ifdef

( #ifdef )
158
#ifdef

DEBUG 0
#ifdef #ifdef

#ifdef

#ifdef

#ifdef
#ifdef
#ifdef

_MAC
8 159

( )
make

#ifdef

8-1

8.4

Sam Unix Windows


Sam
unix.c Unix
windows.c Wi n d o w s
Sam
C

Sam
(
X Window Sam )
160

Sam OpenGL Tc l / T k Java

I/O
/

Sam

Java
Java
Java

Java (
)

8.5

Aw k Perl
grep

PostScript

Macintosh binhex Unix


uuencode uudecode
MIME 9

PC ‘\ r’ ‘\ n’
Unix
8 161
(CR)
(LF)
PC
( CRLF c u r l i ff )

PC
HTTP CRLF
CRLF ( PC
) \r ( Unix )

8-2

8.6

( 16 ) ( )
( )

0
4 0x11223344
0x44332211
162

32

P D P - 11 (
)

64 long

( )
/

i n t( short l o n g)

x x 0x1000
0x0010

short
8 163

I/O
X Window

X Wi n d o w
Plan 9 ( )

I/O
Java C C++ Java
Serializable
C C++
#ifdef 8

( CRLF
)
printf scanf

I/O

b Unix Wi n d o w s
164
control-Z ( 032 1 A)
( 5 strings )
\r

8.7

( ) Unix
echo

echo shell
echo printf

shell
echo

echo
( DOS Wi n d o w s ) ec h o
printf(str) p r i n t f ( " % s " ,str)
str
echo

echo

Unix sum
8 165
sum sum
sum
sum

sum
sum

sum

( )

8.8

ASCII $
24
166
ASCII ASCII ctype.h

a z
isalpha

ASCII 0x7F (7 )
Latin-1 ASCII
80 FF E7 Ç
boy ASCII( Latin-1) 62 6F 79 garÇon
Latin-1 67 61 72 E7 6F 6E
ASCII 128
80 FF
8
16

Unicode
Unicode ISO 10646 16 16
00FF Latin-1 garÇon 16 0067 0061
0072 00E7 006F 006E 0401 04FF
3000 Unicode

Unicode Unicode Internet


Java Unicode Plan 9
Inferno Unicode Microsoft Windows
Unicode Wi n d o w s ASCII
Unicode
Unicode Unicode
Unicode
UTF-8 16 1 2 3
ASCII 00 7F UTF-8
UTF-8 ASCII 80 7FF
800 garÇon UTF-8 67
61 72 C3 A7 6F 6E Unicode E7 Ç UTF-8 C3 A7
UTF-8 ASCII
Unicode UTF-8 3
8 167
Markov
ASCII

C C++ 16
Unicode L ". . ."

UTF-8 C

mm/dd/yy

8.9
168
Brian Kernighan Dennis Ritchie C
(The C Programming Language Prentice Hall 1988)
Sam Harbison Guy Steele C (C: A Reference Manual
Prentice Hall 1994) 4
C C++ ISO( ) Java James
Gosling Bill Joy Guy Steele Java (Java Language Specification Addison
Wesley 1996)
Rich Stevens Unix (Advanced Programming in the Unix Evironment
Addison We s l e y 1992) Unix Unix

POSIX( Portable Operating System Interface) Unix

I/O IEEE
1726 Jonathan Swift Danny Cohen
(On holy wars and a plea for peace IEEE Computer 1981 10 )

Plan 9
#ifdef Unicode Sam
sam (The Text Editor sam) (Software Practice
and Experience) 17 11 pp813~845 Unicode
Rob Pike Ken Thompson Hello World or Kαληµ έρακοσµε or
(Proceedings of the Winter 1993 USENIX Conference 1993 USENIX
1993 pp43~50 ) 16 Unicode UTF-8
Plan 9 Sam

Inferno Java Plan 9


(Limbo)
Unicode
Inferno (The Inferno Operating System) Sean
Dorward Rob Pike David Leo Presotto Dennis M. Ritchie Howard W. Tr i c k e y Philip
Wi n t e r b o t t o m (Bell Labs Technical Journal) 2 1
1997
René Descartes

else-if
198
199

ASCII
Samuel Taylor Coleridge
196

You might also like