|
|_c__\0
|_x__\0
() 10 , 'a','b','c', -
'\0' 'x'. : 15 . , ""
, .
: !
8.18. ""
CBREAK. curses-. curses-
: ( ), ,
/ , , ,
, , . , -
, (),
. -
.
addstr( "\b \b" );
addch( '\b' ); delch();
( x==0).
!
8.19. curses- .
. :
, '\n'.
8.20. , (x1,y1) (x2,y2). :
( ). :
putpixel(x,y,color) (x,y) color.
void line(int x1, int y1, int x2, int y2,
int color){
int dx, dy, i1, i2, i, kx, ky;
register int d; /* "" */
register int x, y;
short /* boolean */ l;
. , 1992-95 - 387 - UNIX
dy = y2 - y1; dx = x2 - x1;
if( !dx && !dy ){
putpixel(x1,y1, color); return;
}
kx = 1; /* x */
ky = 1; /* y */
/* */
if( dx < 0 ){ dx = -dx; kx = -1; } /* Y */
else if( dx == 0 ) kx = 0; /* X */
if( dy < 0 ){ dy = -dy; ky = -1; }
if( dx < dy ){ l = 0; d = dx; dx = dy; dy = d; }
else l = 1;
i1 = dy + dy; d = i1 - dx; i2 = d - dx;
x = x1; y = y1;
for( i=0; i < dx; i++ ){
putpixel( x, y, color );
if( l ) x += kx; /* . */
else y += ky;
if( d < 0 ) /* */
d += i1;
else{ /* */
d += i2;
if( l ) y += ky; /* */
else x += kx;
}
}
putpixel(x, y, color); /* */
}
8.21. , sin(x) 0
2*. : ,
; double int, .. -
|- - .
8.22. , count ,
x- :
0 | 1
7 6 5 4 3 2 1 0 | 7 6 5 4 3 2 1 0 :
0 1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 15 : x
==========================
x=2, count=11
( - ,
).
( , "" pattern):
void horizLine(char *addr,int x,int count,char pattern){
static char masks[8] = {
0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
/* 0- */
____________________
|- (pixel, pel) - picture element, - -
.
. , 1992-95 - 388 - UNIX
register i;
char mask;
short lbits, rbits; /* */
short onebyte; /* ? */
addr += x/8; /* 8 */
mask = masks[ lbits = x & 7 ]; /* x % 8 */
if( count >= (rbits = 8 - lbits)){
count -= rbits; onebyte = 0;
}else{
mask &= ~masks[ lbits = (x+count) & 7 ];
onebyte = 1;
}
/* */
*addr = (*addr & ~mask) | (pattern & mask);
addr++;
/* pattern==0xFF
* *addr++ |= mask;
* (a &~m)|(0xFF & m) = (a &~m) | m =
* (a|m) & (~m|m) = (a|m) & 0xFF = a | m
* *addr++ = (*addr...) ?
* , ++
* !
*/
if(onebyte) return;
/* */
for(i = count/8; i > 0; --i)
*addr++ = pattern; /* mask==0xFF */
/* */
if((lbits = count & 7) == 0) return;
/* */
mask = ~masks[lbits];
*addr = (*addr & ~mask) | (pattern & mask);
}
, .
8.23. curses- " ",
(, 8x8 ) 5 . -
alarm(), pause().
8.24. , ,
. :
. , 1992-95 - 389 - UNIX
menu2_12
-----------------------------------------------
_
+_____
-_____
_1
1_1
1_2
_2
2_1
2_2 #
2_3
_3
>menu2_2 #
_4
>>menu3_7 # menu3_7
...
...
-----------------------------------------------
: Esc (
"" , "
":
.rootmenu.menu1_2.menu2_4.menu3_1
menuI_J - ), 'q'
ESC, F1, F2.
> , -
. >> , ..
, ( ,
- ).
""
( - , y=1 x=-2), -
. :
|-------------------
|--_---- |
| _1 | |
| _2 | |
| *_3 | |
| _4 |--
---------------------
, ""
"".
system().
,
( ):
Compile
Edit
Run program
8.25. curses- , - -
:
. , 1992-95 - 390 - UNIX
1 4 7
2 *5 8
3 6
- - .
. ,
, "-
" (..
"" ,
). ,
( ).
8.26. curses, ,
"". : ( , -
), ""
. 8 . ""
:
- "" 2 3 - .
- "" 2 3 - .
- , 3 , "".
: , ,
"", .
8.27. curses- (
rwxrwxrwx). , -
, :
1 rwxrw-r--
2 rw-r-xr-x
3 rwxrwxr--
main(). :
stat(), -
chmod().
. , 1992-95 - 391 - UNIX
* 9. . *
9.1. C++
, , .
--------------------------------------------------
1. () [] -> :: . Left to right
2. ! ~ + - ++ -- & *
(typecast) sizeof new delete Right to left
3. .* ->* Left to right
4. * / % Left to right
5. + - Left to right
6. << >> Left to right
7. < <= > >= Left to right
8. == != Left to right
9. & Left to right
10. ^ Left to right
11. | Left to right
12. && Left to right
13. || Left to right
14. ?: ( ) Right to left
15. = *= /= %= += -= &=
^= |= <<= >>= Right to left
16. , Left to right
"*" "&" 2 - ; 2 "+" "-" - ;
"&" 9 - ""; "(typecast)" - ; "new" "delete" -
C++.
Left to right ( )
:
A1 @ A2 @ A3
((A1 @ A2) @ A3)
Rigth to left ( )
A1 @ A2 @ A3
(A1 @ (A2 @ A3))
9.2. .
9.2.1. .
1. int double, :
signed char --> int (7)
unsigned char --> int
short --> int (15)
unsigned short --> unsigned int
enum --> int
float --> double
2. double, dou-
ble. : double. :
. , 1992-95 - 392 - UNIX
if(double) -->double double
else if(unsigned long) -->unsigned long unsigned long
else if(long) -->long long
else if(unsigned int) -->unsigned int unsigned int
else int int
- , char,short
int float double. , ( ) -
int double char,short float .
unsigned .
9.2.2. .
op = expr;
expr - op.
"" "" , :
int --> char .
long --> int .
float --> int
double --> int , .
double --> float .
:
signed --> unsigned (
unsigned --> signed ).
unsigned int --> long .
int --> long .
float --> int
int --> float : .
, :
char --> long
char --> int --> long
char --> unsigned long
char --> int --> unsigned long
9.3. (HEX).
%d %o %X
--------------------------------
0 0 0x0 0000
1 1 0x1 0001
2 2 0x2 0010
3 3 0x3 0011
4 4 0x4 0100
5 5 0x5 0101
6 6 0x6 0110
7 7 0x7 0111
. , 1992-95 - 393 - UNIX
--------------------------------
8 010 0x8 1000
9 011 0x9 1001
10 012 0xA 1010
11 013 0xB 1011
12 014 0xC 1100
13 015 0xD 1101
14 016 0xE 1110
15 017 0xF 1111
16 020 0x10 10000
9.4. .
n 2**n | n 2**n
--------------|---------------
0 1 | 8 256
1 2 | 9 512
2 4 | 10 1024
3 8 | 11 2048
4 16 | 12 4096
5 32 | 13 8192
6 64 | 14 16384
7 128 | 15 32768
| 16 65536
9.5. : .
. 16 .
0. i b[i]. 0,
1.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| 0| 0| 0| 0| 1| 0| 1| 1| 0| 1| 1| 0| 1| 1| 0| 0|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
unsigned , ,
d = 2**15 * b[15] +
2**14 * b[14] +
...
2**1 * b[1] +
b[0];
(2**n - 2 n). d .
:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 1
. b[15] : 0 -
, 1 - .
:
-a = ~a + 1
:
. , 1992-95 - 394 - UNIX
2 = 0000000000000010
~2 = 1111111111111101
~2+1 = 1111111111111110 = -2
-1 = 1111111111111111
-2 = 1111111111111110
-3 = 1111111111111101
-4 = 1111111111111100
-5 = 1111111111111011
a + (-a) = 0
|
2 = 0|000000000000010
-2 = 1|111111111111110
---------|---------------
: 10|000000000000000
, 1 16. 0..15
b[16] . ,
0000000000000000 = 0
.
a - b = a + (-b) = a + (~b + 1)
3
( ).
4 (nibble):
x = 0010011111011001
: 0010 0111 1101 1001
16-: 0x 2 7 D 9 = 0x27D9
: 0 010 011 111 011 001
8-: 0 0 2 3 7 3 1 = 023731
10. .
, ,
UNIX, .
.
, . -
.
; , ,
" " (man) UNIX
.
- "C". "C"
. (
"Modula-2", "Ada", "CLU"), ("Ada", "CLU"), -
("Smalltalk", "CLU") .
"C" "C++" |-,
-
____________________
|- C++ C AT&T; " -"
. , 1992-95 - 395 - UNIX
. "C" - -
("Objective-C").
, (, "C", "Pascal", "Pro-
log").
____________________
|=|=|= UNIX,
; /; Usenet Rel-
com, ; -
.
..
,
.
?
, .
.
0. . .......................................... 1
1. . , . ........................... 3
2. , , . ................................................ 81
3. . .
........................................................................... 122
4. . .......................................................... 137
5. . .......................................................... 172
6. UNIX. .................................. 186
6.1. . ........................................................ 189
6.2. UNIX. ............................................................ 198
6.3. . ................................................ 207
6.4. . ................................................................. 212
6.5. . ......................................................... 219
6.6. FIFO-. ...................................................... 230
6.7. . ..................................................... 233
6.8. , , . ........................... 235
6.9. . ............................................. 240
6.10. . ........................................................ 244
6.11. - ......................................... 259
6.12. . ........................................... 271
7. . ....................................................... 283
8. . ............................... 348
9. . ................................................................ 391
9.1. C++ .................................... 391
9.2. . ............................................ 391
9.3. (HEX). ................................... 392
9.4. . ................................................. 393
9.5. : . ...................... 393
10. . .................................................................. 394
1. . ......................................................
2. . ..............................................
3. . ...............................................
4. nroff. .........................................
5. . .............................
6. . ............................................
7. -. .......................................................
8. . ...............................................
9. / . .....................................
10. free, -
. .....................................................................
11. . ..................
12. / . ....................................
13. MS DOS chdir. ..............
14. . ...............................................
15. . ..........................
16. . ..........................................
17. curses. uxcom. ............
18. . uxcom. ..............
19. . uxcom. ............................
20. -. uxcom. .........................
21. . uxcom. .............................
22. . uxcom. ...............
23. UNIX commander - .
uxcom. ...................................................................
24. "". .............................
25. FIFO-. ................................
26. . ..................
27.
. ...............................................................
28. . ......................
29. BSD-2.9. ........................
30. MS DOS UNIX. ..............................
31. , . .....................
32. -. ...............................
1.11. . ............................................... 6
1.34. . .......................................................... 10
1.36. . ........................................ 12
1.39. . ....................................... 14
1.49. . ....................................................... 20
1.50. . ..................................................... 21
1.67. . .................................................. 28
1.88. . ................................................. 38
1.117. . ......................................................... 58
1.137. qsort - . ............................... 67
1.146. . ........................................... 76
2.58. bcopy. .......................................................... 108
2.59. strdup. ......................................................... 111
2.61. printf. ....................................... 112
3.9. _ctype[] .................................................................. 126
3.12. : tr. ........................................... 129
3.16. ( ) . ................... 132
3.18. : #ifdef .............................................. 132
4.39. . ......................................... 161
4.45. STDIO, 4.2 BSD. .................... 165
5.12. . ............................................ 180
5.16. . ................................. 183
5.17. "". .................................................. 184
6.1.1. . ................................................ 189
6.1.3. (ls). ................... 191
6.1.5. . ............................ 192
6.2.9. . ...................................... 201
6.4.3. sleep. ......................................................... 217
6.10.1. : getwd. ......................... 252
6.10.2. . ...................................... 257
6.11.1. . ....................... 259
6.11.2. script. ..................................................... 261
7.12. uniq. ......................................................... 285
7.14. , untab. .......................... 285
7.15. tabify. ......................................................... 285
7.25. . ...................................... 288
7.31. . .......................................... 292
7.33. . ................................... 296
7.34. . .................................... 298
7.36. . ............................ 300
7.41. match. .......................................................... 309
7.43. . .................... 313
7.44. . ............................ 316
7.52. . ................................................. 321
7.67. -1. .......................................................... 330
7.68. -2. .......................................................... 336
8.1. . ....................................................... 350
8.13. termcap. ....................................... 359
8.17. ESC- . ............................ 371
11. .
1) ., ., .. . . -
.: , 1985.
2) ., ., .. . . - .: ,
1988.
3) .. . . - .: , 1988.
4) ., .. . - .:
, 1986.
5) ., .. UNIX . - .:
, 1989.
6) ., .. . . - .: -
, 1988.
7) ., ., ., .. -
. - .: , 1985.
8) .. UNIX. - .: ,
1985.
9) .. UNIX. - .: ,
1986.
10) ., .. UNIX. - .:
, 1986.
11) .. UNIX. - .: , 1986.
12) .. UNIX. - .: , 1987.
13) M.Bach. The design of the UNIX operating system. - Prentice Hall, Englewood
Cliffs, N.J., 1986.
14) S.Dewhurst, K.Stark. Programming in C++. - Prentice Hall, 1989.
15) M.Ellis, B.Stroustrup. The annotated C++ Reference Manual. - Addison-Wesley,
1990.
/* 1 */
/* :
* a0 .. an S
*
* S = a0 * c0 + a1 * c1 + ... + an * cn
* c0 .. cn .
* (ai >= 0, ci >= 0).
*/
#include <stdio.h>
/* ( ci) */
int cost[] = {
1, 2, 3, 5, 10, 15, 20, 50, 100, 300, 500 /* */
};
#define N (sizeof cost / sizeof(int))
int count[ N ]; /* ( ai) */
long nvar; /* */
main( ac, av ) char *av[];
{
int coin;
if( ac == 1 ){
fprintf( stderr, ", : %s \n",
av[0] );
exit(1);
}
coin = atoi( av[1] );
printf( " %d .\n", coin );
printf( " .\n" );
printf( "-------------------------------------------------------------------\n" );
printf( "| 5. | 3. | 1. | 50.| 20.| 15.| 10.| 5.| 3.| 2.| 1.|\n" );
printf( "-------------------------------------------------------------------\n" );
change( N-1, coin );
printf( "-------------------------------------------------------------------\n" );
printf( " %ld \n", nvar );
}
/* */
change( maxcoin, sum )
int sum; /* , */
int maxcoin; /* cost[]
* , .
*/
{
register i;
if( sum == 0 ){ /* */
/* */
putchar( '|' );
for( i = N-1 ; i >= 0 ; i-- )
if( count[i] )
printf(" %3d |", count[ i ] );
else
printf(" |" );
putchar( '\n' );
nvar++;
return;
}
if( sum >= cost [ maxcoin ] ){
/* cost[maxcoin] ,
* :
*/
count[ maxcoin ] ++; /* */
/* :
* - ?
* - cost[maxcoin].
*/
change( maxcoin, sum - cost[maxcoin] );
count[ maxcoin ] --; /* ... ... */
}
/* */
if( maxcoin )
change( maxcoin-1, sum );
}
/* 2 */
/* .
* .
* .
*/
#include <stdio.h>
#include <ctype.h>
long bcnt[8];
char masks[8] = { /* */
1, 2, 4, 8, 16, 32, 64, 128 };
long cnt[256]; /* 256 */
/* */
char *pr( c ){
static char buf[ 20 ];
switch( c ){
case '\n': return " \\n " ;
case '\r': return " \\r " ;
case '\t': return " \\t " ;
case '\b': return " \\b " ;
case '\f': return " \\f " ;
case '\033': return " ESC" ;
case '\0': return " \\0 " ;
case 0177: return " ^? " ;
}
if( c < ' ' ){
sprintf( buf, " ^%c ", c + 'A' - 1 );
}else if( isspace(c)){
sprintf( buf, " '%c'", c );
}else if( ! isprint( c ))
sprintf( buf, "\\%3o", c );
else sprintf( buf, " %c ", c );
return buf;
}
main( argc, argv ) char **argv; {
FILE *fp;
if( argc == 1 ) process( stdin );
else{ argv++; argc--;
while( *argv ){
printf( "----- FILE %s -----\n", *argv );
if((fp = fopen( *argv, "r" )) == NULL ){
printf( "Can not open\n" );
}else{ process( fp ); fclose( fp ); }
argv++; argc--;
}
}
exit(0);
}
/* fp */
process( fp ) FILE *fp;
{ register i; int c; int n;
/* */
for( i=0; i < 256; i++ ) cnt[i] = 0L;
for( i=0; i < 8 ; i++ ) bcnt[i] = 0;
while( ( c=getc(fp)) != EOF ){
c &= 0377;
/* */
cnt[ c ] ++;
/* */
for( i=0; i < 8; i++ )
if( c & masks[i] )
bcnt[ i ] ++;
}
/* COL */
#define COL 4
printf( "\tASCII map\n" );
for( n=i=0; i < 256; i++ ){
/* if( cnt[i] == 0l ) continue; */
printf( "%s %5ld |", pr(i), cnt[i] );
if( ++n == COL ){ n = 0; putchar('\n'); }
/* if((i % COL) == (COL-1)) putchar('\n'); */
}
printf( "\n\tBITS map\n" );
for( i=7; i >=0 ; i-- ) printf( "%6d ", i );
putchar( '\n' );
for( i=7; i >=0 ; i-- )
printf( "%6ld ", bcnt[i] );
putchar( '\n' ); putchar( '\n' );
}
/* 3 */
/* . . */
/* */
/* : a.out < _ */
#include <stdio.h>
extern char *gets();
#define WIDTH 60 /* */
main(){
char rd[81]; register char *s;
char *head, /* */
*tail; /* */
register int len, i;
int shift; /* */
/* rd ,
* .
* CTRL+D
*/
while( gets( rd ) != NULL ){
if( !*rd ){
/* */
putchar( '\n' ); continue;
}
/* */
for( s = rd; *s == ' ' ; s++ );
if( ! *s ){
/* */
putchar( '\n' ); continue;
}
head = s;
/* */
while( *s ) s++;
/* */
s--;
while( *s == ' ' && s != rd ) s--;
tail = s;
/* */ len = (tail-head) + 1;
/* - */
shift = (WIDTH - len)/2;
if(shift < 0 ){
fprintf(stderr, " %d\n", WIDTH );
shift = 0;
}
/* */
for( i=0; i < shift; i++ ) putchar( ' ' );
while( head <= tail ) putchar( *head++ );
putchar( '\n' );
}
}
/* 4 */
/* nroff */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h> /* strchr() */
#include <locale.h>
FILE *fout = stdout; /* */
/* */
#define SPACE 0 /* */
#define TEXT 1 /* */
#define PUNCT 2 /* */
#define UC(c) ((unsigned char)(c))
/* */
void putstr (FILE *fp, unsigned char *s) {
/* Punct - ,
* .
* PunctS - , .
* PunctN - ,
* .
*/
static char Punct [] = ",:;!?.)" ;
static char PunctS[] = ",:;" ;
static char PunctN[] = " \t\"'" ;
#define is(c, set) (strchr(set, UC(c)) != NULL)
int c, state = TEXT, cprev = 'X';
while ((c = *s) != '\0') {
/* */
if(isspace(c)) state = SPACE;
/* . .
*/ else if(is(c, Punct)){
switch(state){
case SPACE: if(is(cprev, Punct ) && cprev==c && c != ')')
putc(' ', fp);
/* - */ break;
case PUNCT: if(is(cprev, PunctS)) putc(' ', fp); break;
}
putc(cprev = c, fp); /* */
state = PUNCT;
} else {
/* */
switch(state){
case SPACE: putc(' ', fp); break;
case PUNCT: if(!is(c, PunctN)) pu