Вы находитесь на странице: 1из 214

. .

2011

004.4
32.965
27



:
..-.., .. ;
..., .. .
, . .
27
. : / . . . :
, 2011. 214 .
, . . . .
, .
004.4
32.965

.., 2011
, 2011
2


............................................................................................... 6
1. ............................................................. 7
1.1. .................................. 7
1.2. ..................................................... 12
1.3. ............................................................................ 16
1.4. main() ..................................... 18
2. ......................................................................... 19
2.1. - ................................................... 19
2.2. - ...................................................... 23
3. .................................................... 29
3.1. ............................................................. 29
3.2. .................................................... 30
3.3. ................................................................. 31
3.4. break continue................................................ 34
3.5. ............................................................................... 35
3.6. ................................................................................... 41
4. ................................................. 43
4.1. ............................................................................... 43
4.2. ................................................................................... 48
5. ................................................................... 50
5.1. ........................................ 50
5.2. ........................................... 52
5.3. ................................................................................... 55
6. ................................................................. 58
6.1. ..... 58
6.2. ................................................................................... 59
7. .................................................................... 61
7.1. .............................................................. 61
7.2. ....................................... 62
7.3. ........................................................... 64
7.4. const ............................................................. 67
7.5. .......................................... 67
7.6. ............................................................ 71
7.7. ...................... 72
3

8. ......................................................................... 76
8.1. ............ 76
8.2. ....................................... 77
8.3. ............................................................... 78
8.4. .................................................. 83
8.5. ........................................ 86
8.6. ............................................ 88
8.7. ............................................ 93
8.8. ................................................................................... 98
9. .................................................................................... 100
9.1. .................................... 100
9.2. ...................................................................... 103
10. ....................................................... 106
10.1. #include.......................................................... 106
10.2. #define............................................................ 106
10.3. #undef............................................................. 108
11. ..................................................................................... 109
11.1. .................................... 109
11.2. ....................................................... 110
11.3. ................................................................. 112
11.4. .................................................... 116
11.5. ........................................................................... 120
11.6. .......................... 121
11.7. .................................... 125
12. ............................................................ 128
12.1. ........................................................... 128
12.2. ................ 131
12.3. ............................................................................... 135
13. ...................................................................... 137
13.1. ............................................................... 137
13.2. ............................................................. 141
13.3. ............................................................. 145
13.4. ........................................... 153
13.5. ............................................ 156
14. .................................................................... 161
14.1. ....................................................... 161
14.2. ............... 164
4

14.3. ...................................... 175


14.4. , ................................................................. 177
14.5. .............................................. 181
14.6. .......................................................... 182
14.7. ........................................ 182
14.8. ............................................................ 183
14.9. ......... 185
14.10. ........................................ 199
1. ............................................... 201
1. ................................................................... 201
2. ................................................................. 201
3. ................................................................... 202
2. ....................................... 204
1. ........................................................ 204
1.1.
.................................................................. 205
1.2. ...................... 206
2. .................................................. 206
3. ..................................................... 207
4. ........................................................ 209
5. ............................................................. 210
6. ......................................................... 211
....................................................................................... 213



. .

.
, ,
, , ,
.
UNIX .
, .
. , ,
, .
,
.
. . , -, #,
() .

1.
1.1.
.
. . .
.
. , . , :
_ _;
,
, .
,
,
.
, , , . , .
:
char ;
int ;
float ;
double .
, . short long
int. short
, , .
, 7

long. ,
i j, i short int, j long int:
short int i;
long int j;
short int long int int .
short i;
long j;
signed ( ) unsigned ( ) char, int, short int, long int.
unsigned . unsigned, , ,
(signed).

.

unsigned char
char
unsigned short
short
unsigned int (16 )
unsigned int (32 )
int (16 )
int (32 )
unsigned long
long
float
double
long double

( )
1
1
2
2
2
4
2
4
4
4
4
8
10

0 255
-128 127
0 65535
-32768 32767
0 65535
0 4294967295
-32768 32767
-2147483648 2147483647
0 4294967295
-2147483648 2147483647
1.2e-38 3.4e+38
2.2e-308 1.7e+308
1.7e-4932 1.7e+4932

, int 16-,
32-. . 8

(0 , 1 ). , , 16- int
: -215 215-1.
char (
1 ), .
long double .
, sizeof(),
. , sizeof(int), sizeof(double) .. , , double x
x, sizeof(x).
sizeof() ,
,
.
<limits.h> <float.h>
,
. , INT_MIN
(INT_MAX) ()
, int; DBL_MIN, DBL_MAX
double ..
<limits.h>.

CHAR_BIT

CHAR_MIN

-128

CHAR_MAX

127

SCHAR_MIN

-128

SCHAR_MAX

127

UCHAR_MAX

255

char

char

char

signed char

signed char

unsigned char
9

SHRT_MIN

-32768

SHRT_MAX

32767

USHRT_MAX

65535

INT_MIN

LONG_MIN

-32768
(-2147483648)
32767
(2147483647)
65535
(4294967295)
-2147483648

LONG_MAX

2147483647

ULONG_MAX

4294967295

INT_MAX
UINT_MAX


short

short

unsigned short

int

int

unsigned int

long

long

unsigned long

<float.h>:

FLT_MIN 1.175494351e-38


float
FLT_MAX 3.402823466e+38

float
DBL_MIN 2.2250738585072014e-308

double
DBL_MAX 1.7976931348623158e+308
double
:
.
10

.
, , ( 0 M, M
),
, M+1.
, a unsigned short. a
USHRT_MAX (a = USHRT_MAX). a 65535. a
(a++), a 0.
a (a--), a
65535.
. , ,
a short a = SHRT_MAX (
a = 32767). a (a++), a
SHRT_MIN, a
. ,
a SHRT_MIN a (a--), a SHRT_MAX.
. , .
- long
double, long double.
, - double, double.
, - float, float.
, - unsigned long,
unsigned long.
, - long, long.
, - unsigned int,
unsigned int.

11

, unsigned
short / unsigned char, unsigned int.
int.
1.2.
. (+),
(-), (*), (/), (%).
. a % b a b. ,

int a = 5, b = 3, q, r;
q = a / b;
r = a % b;
q 1, r 2.
, % .
. : (>=), (>),
(<=), (<), (==), (!=), (&&), (||), (!).
, .
,
.
A :
A && = ,
A && = A,
A || = A,
A || = .
, && ||,
, , , . ,
&& ||.
12

, x y :
int x = 1, y = 2;

(x > y && y > 0)
x > y,
(). A && B, A B , , A, B
, (x > y && y > 0) . y > 0.
, x = 1, y = 2.
(y > x && x > 0 || x == y && x > 0)
, y > x && x > 0,

x == y && x > 0 (, A || B
,
A B).
, && , . ||, ,
, .
. :
_ =
:
double x, y, z; /*
*/
x = 0.2;
/* x 0.2
*/
y = 0.5;
/* y 0.5
*/
z = x + y;
/* z x y */

13

,
, , x = y = 0. y = 0, y ,
x.
:
, ,
, . , (x = 1 + 2) 3.
:
y = (x = 1 + 2) + 4,
y 7, x
3. ,
,
((x = 1 + 2) < 4),
.
,
, :

+=
-=
*=
/=
%=

a += 10;
a -= 2;
a *= 5;
a /= 10;
a %= 3;

a = a + 10;
a = a 2;
a = a * 5;
a = a / 10;
a = a % 3;

.
. 1 , 1 .
: (++),
1, (--), , , 14

1. , x double. :
x++;

x = x + 1;

x += 1;

: . (++x),
(x++). ,
x++; ++x;
. ( ) . , . , y
2:
x = 1;
y = ++x;

x = 1;
y = x++;
y 1. x
2.
. (;)
.
.
, .

. , , /* */, . :
/* */

15

1.3.
, ,
. , .
#define, :
#define
.
() , .

. ,
#define PI 3.14

PI 3.14:
/* */
#include <stdio.h>
#define PI 3.14
int main( )
{
double x = 2*PI;
printf("%f\n", PI*PI);
return 0;
}

/* */

int main( )
{
double x = 2*3.14;
printf("%f\n", 3.14*3.14);
return 0;
}

. , , . PI ,
3.14 .

#define N 100
N , 100
.
16

float, , ,

#define X 2.0
,
:
#define LETTER a
,
:
#define SENTENCE This is a string constant

\0,
ASCII .
.
, sizeof()
, :
#include <stdio.h>
#define EXP 2.711828
#define GREETING "Hi"
int main( )
{
printf("%d\n", sizeof(EXP));
printf("%d\n", sizeof(GREETING));
return 0;
}
GREETING
3, \0.
printf("%d\n", sizeof(Hi));

17

1.4. main()
. ,
, ,
main(),
.
, main() int. C99 (, 1999 ): ,
, main. . int
int, , -, .
main() int, ( , ) .
,
. , . , .

18

2.
2.1. -
, - .
,
.
. putchar() -.
:
int putchar( int c)

c, EOF.

:
/* */ /* */ /* */

#include <stdio.h>
#define LETTER 'A'
int main( )
{
putchar(LETTER);
return 0;
}

#include <stdio.h>
int main( )
{
putchar('A');
putchar('\n');
return 0;
}

#include <stdio.h>
int main( )
{
char letter = 'A';
putchar(letter);
return 0;
}

, :

\b
\t
\n

8

9

10

19

\v
\r

11
12

\f
\
\
\?
\\

13
34
39
63
92





()
()
(?)
(\)

puts() :
int puts(const char *s)

:
/* */
#include <stdio.h>
#define MESSAGE "a message"

int main( )
{
puts(MESSAGE);
return 0;
}

/* */
#include <stdio.h>
int main( )
{
puts("a message");
return 0;
}

/* */
#include <stdio.h>
int main( )
{
char message[] = "a message";

puts(message);
return 0;
}

, , .
puts() ,
. , s ,
EOF.
. gets() s ,
. s \0:
char *gets(char *s)
. :
20

#include <stdio.h>
int main( )
{
char s[10];
gets(s); /* */
puts(s); /* */
return 0;
}
gets() 9 ,
, s,
\0. .
gets(char *s) s. , ,
, ,
ENTER.
#include <stdio.h>
#include <string.h>
int main( )
{
char s[10];
while (strlen(gets(s)) > 0)
printf("%s %d\n", s, strlen(s));
return 0;
}
getchar() :
int getchar(void).
getchar()
, , EOF.
#include <stdio.h>
int main( )
21

{
char c;
c = getchar();
putchar(c);
putchar('\n');
return 0;
}
, ASCII :
#include <stdio.h>
int main( )
{
char c;
c = getchar();
printf("ASCII %c %d\n", c, c);
return 0;
}
gets() , , ( -),
. s
:
#include <stdio.h>
int main( )
{
char c, s[10];
int i = 0;
while ((c = getchar()) != '\n' && i < 9)
s[i++] = c;
s[i] = '\0';
puts(s);
return 0;
}
22

gets() fgets(), ,
. ,
gets(). fgets() ,
\n, gets().
fgets().
13 .
#include <stdio.h>
int main()
{
char s[10];
fgets(s, 10, stdin);
printf("%s", s);
return 0;
}
getchar(), , :
int main( )
{
/* */
getchar();
return 0;
}
2.2. -
. putchar()
puts() , .
.
23

printf()
.

( ) :
int printf(char *format, argument_1, argument_2,, argument_n)
printf() , .
.
format
, , ,
.
printf():

%d (%i)
%u
%o
%x
%f
%e
%g

%c
%s






float double


( %f,
%e)

\0


,
(%)
. , printf(%10d, 15)
15 10.
, (%) (.),
, 24

. , printf(%.2f, 0.14788)
0.15.
,
, . , printf(%.3s, abcde) abc.
,
printf(%10.2f, 0.14788).
printf().

printf(%d, 50);
printf(a = %d, 10);
printf(%o, 9);
double a = 0.01, b = 1.1;
printf(%.2f + %.2f = %.2f \n, a, b, a+b);
char *s = abc;
printf("s = %s, s);
char s[] = abc;
printf(s = %.2s, s);
printf(s = %5.1s, abc);


50
a = 10
11
0.01 + 1.1 = 1.11
s = abc
s = ab
s=

sprintf() , printf(),
,
string ( ),
:
int sprintf(char *string, char *format, argument_1, argument_2,, argument_n)

:
int main( )
{
char s[10];
sprintf(s, "a = %d", 50);
puts(s);
return 0;
}
s a = 50.
25

. scanf() ,
.
int scanf(char *format, &argument_1, &argument_2,, &argument_n)

scanf() . :
int main( )
{
double x;
while (scanf("%lf", &x) == 1)
printf("%10.2f\n", x);
return 0;
}
, scanf().
.
format.
,
\t, \n ( ),

, . , format ,
%, .
,
(&argument_i).
,
.
format , , - ,

.
26

, scanf() ( ),
. , scanf()
.
, scanf() :
int a;
scanf("a = %d", &a);
a,

a = 10
scanf() gets()
fgets() s . scanf() s ,
( , \t, \n).
gets() fgets() s ,
\n. ,
scanf() s ,
.
, char s[100] scanf(%s, s) , ! \n
, s ,.
scanf() gets(),
s , !. fgets(), s : , ! \n.
sscanf() string:
int scanf(char *string, char *format, &argument_1,, &argument_n)
string
format argument_1, argument_2,
, argument_n.

printf scanf.
27

long double
double
float
unsigned long
long
unsigned int
int
short
char

printf
%Lf
%f
%f
%lu
%ld
%u
%d
%hd
%c

28

scanf
%Lf
%lf
%f
%lu
%ld
%u
%d
%hd
%c

3.
3.1.


. :
if ()
_1;
else
_2;
else- . ( ), _1, ( )
else- _2. ,
.
if , , , .
if ( != 0)
:
if ()
else if. if-else , . , else if:
else ,
.

29

if (a > 10)
if (b < 10)
a = 0;
else
b = 0;

if (a > 10)
{
if (b < 10)
a = 0;
}
else
b = 0;

,
. ? : . :
_1 ? _2 : _3
: _1
, _2 _2
.
_3, . ,
max a b, :
max = (a > b) ? a : b; /* max = max{a, b} */
max
(a > b) ? a : b.
3.2.
switch . :
switch ()
{
case -_1: _1; break;

case -_n: _n; break;


default: _(n+1);
}
30

, -.
- .
- .
, default. default
, .
, ,
:
#include <stdio.h>
int main( )
{
int a;
printf("a = ");
scanf("%d", &a);
switch (a)
{
case 1: case 3: case 5: case 7: case 9:
printf(" ");
break;
case 0: case 2: case 4: case 6: case 8:
printf(" ");
break;
default:
printf(" ");
}
return 0;
}
3.3.
: for, while do-while.
for :
for(_1; _2; _3)
31

, , . _1 ,
( ). _2 , . _3 . for :
1. _1.
2. _2.
3. _2 (), ,
_3 _2.
_2 (), for .
_2 .
,
for (i = 1; i <= 10; i++)
printf(%d\n, i);
1, 2, , 10,
for (i = 10; i >= 1; i--)
printf(%d\n, i);
10, 9, , 1.
for :
for (i = 1, j = 10; i < j; i++, j--)
printf(%d %d\n, i, j);
. ,
:
char c;
for(c = 'a'; c <= 'z'; c++)
printf("%c ", c);
32

. ,
for (; ;) { }
.
while
while ()
(),
() . , . ,
. , 1, 2, , 10
:
i = 1;
while (i <= 10)
{
printf(%d\n, i);
i++;
}
for
for(_1; _2; _3)
while :
_1;
while (_2)
{

_3;
}
do-while.
while ,
do-while
. do-while
do while ();

33

,
.
, .
3.4. break continue
break for,
while, do-while switch.
. ,
[0, 1] 0.1:
#include <stdio.h>
int main( )
{
double x;
for (x = 0; ; x += 0.1) /* */
{
if (x > 1)
break;
else
printf("%.1f\n", x);
}
return 0;
}
continue
.
while do-while
, for _3. continue .
continue :
#include <stdio.h>
int main( )
34

{
int i;
for (i = 0; i <= 9; i++) /* i 0 9 */
{
if (i%2 == 0) /* , */
continue;
printf("%d\n", i);
}
return 0;
}
3.5.
1. y = ex x2 [A, B] H.
2 .
H .
modf(),
:
double modf(double x, double *ip)
x
, , x. *ip,
.
#include <stdio.h>
#include <math.h>
#define A -2
/*
*/
#define B 2
/*
*/
#define H 0.1
/* ( ) */
/* F() y = ex x2 x */
double F(double x)
35

{
return exp(x) - x*x;
}
int main( )
{
double x, y, /* y = F(x)
*/
min, /* H */
q;
/*
*/
int n; /* , */
n = 0;
x = A;
min = F(A); /* */
while (x <= B)
{
y = F(x); /* y = ex - x2 */
printf("%10.2f %10.2f\n", x, y); /* x y */
if (y < min)
min = y;
modf(y, &q); /* q y */
if ((long)q % 2 == 0) /* */
n++;
x += H;
/* x H */
}
printf("min = %.2f\n", min);
printf("n = %d\n", n);
return 0;
}
2. n
, .
, / % :
a b, b > 0,
a = bq + r, q r 0 r < b.

36

#include <stdio.h>
int main( )
{
int n,
/* n
*/
sum,
/* n
*/
k;
/* n */
sum = k = 0; /* */
printf("n= ");
scanf("%d", &n);
/* n
*/
do
{
k++;
/* n */
/* n */
sum += n%10;
n /= 10;
/* n */
} while (n != 0);
printf("sum = %d k = %d\n", sum, k);
return 0;
}
3. , digit
n.
#include<stdio.h>
int Search(int n, int digit)
{
int d; /* n */
do
{
d = n % 10;
n /= 10;
}
while (n != 0 && d != digit);
return d == digit;
}

37

int main( )
{
int n, digit;
scanf("%d%d", &n, &digit);
printf("%s\n", Search(n, digit) ? "yes" : "no");
return 0;
}
4. ,
n > 0.
:
a , a > 1,

a = p1k p2k ... pnk , k1, k2, ., kn ,
p1, p2, ., pn p1 < p2 < < pn.
1

#include <stdio.h>
int main( )
{
unsigned int n,
/* n */
i;
/* n */
printf("n = ");
scanf("%u", &n); /* n */
i = 2;
/* , 2 */
while (n != 1)
{
while (n % i != 0)
i++;
printf("%u\n", i); /* i */
n /= i; /* n i, */
}
return 0;
}
, , , -

38

,
. :
#include <stdio.h>
int main( )
{
unsigned int n, i;
printf("n = ");
scanf("%u", &n);
i = 2;
while (n != 1)
{
if (n % i == 0)
{
printf("%u\n", i);
n /= i;
}
else
if (i*i > n) /* , */
i = n;
else i++;
}
return 0;
}
5. , n .
, ,
. 1 .
: n a b (n = ab), a 2 b 2,
a [ n ], b [ n ], [] .
n 2 [ n ].
#include <math.h>
/* */
39

int Prime (unsigned int n)


{
if (n == 0 || n == 1) return 0;
else
{
int bound = sqrt((double)n);
/* bound = [
int i = 1;
while (++i <= bound && n % i != 0)
;
return (i > bound);
}
}

n]

*/

6 ( ).
n m. (), :
(n, 0) = (0, n) = n;
(n, m) = (n - m, m), n m;
(n, m) = (n, m - n), m > n.
unsigned int Nod(unsigned int n, unsigned int m)
{
while (n != 0 && m != 0)
if (n >= m)
n -= m;
/* n m */
else
m -= n;
/* m n */
if (m == 0) return n;
else return m;
}
,
:
(n, m) = (n, n % m), m 0 (n, 0) = n.
unsigned int Nod(unsigned int n, unsigned int m)

40

{
while (n != 0 && m != 0)
if (n >= m)
n %= m; /* n n m */
else
m %= n; /* m m n */
if (m == 0) return n;
else return m;
}
3.6.
1. (s1) (s2) 1 100.
2. , , .
3. n, n! ( 0! = 1, n! = n(n - 1)!).
4. n > 0. ,
3.
5. n > 0. k, n: k2 > n.
.
6. n > 0. , n
n = k3, k .
7. a b ().
8. , .
9. , .
10. , n
.
11. , n > 0
.

41

12. n. ,
: n 0 5,
.
13. x n > 0.
, 1 + x + x2 + x3 + + xn.
14. x n>0. x x3/(3!) + x5/(5!) + (1)nx2n+1/((2n+1)!).
15. : x0 = 0, x1 = 1, xn = xn-1 + xn-2 n 2. n. xn.
16. n k.
,
n k.
17. , n .
18. ,
n > 0.
19. n. 1 n, (
(n + 1)2 = n2 + 2n + 1).

42

4.
. : ,
..
, .
0, .
4.1.
1.
.
#include <stdio.h>
int main( )
{
double a,
/* */
min; /*
*/
int n = 0;
/*
*/
printf("a = ");
scanf("%lf", &a);
min = a;
/*
*/
while (a != 0)
{
if (a == min)
n++;
else
if (a < min)
{
/* */
/* , min n */

min = a;
n = 1;
43

}
printf("a = ");
scanf("%lf", &a);
}
printf("n = %d\n", n);
return 0;
}
2. , .
max ( ) 0, .
, ,
min max
0.
, .
#include <stdio.h>
int main( )
{
int a,
/* */
/* */
n,
max; /* . . */
max = 0; /* */
printf("a = ");
scanf("%d", &a);
while (a != 0)
{
n=0;
/* */

while (a > 0)
{
44

n++;
printf("a = ");
scanf("%d", &a);
}
if (n > max)
max = n;
/* */
while (a < 0)
{
printf("a = ");
scanf("%d", &a);
}
}
printf("max = %d\n", max);
return 0;
}
,
,
, ,
.
3. .
.
/* */ /* */

#include <stdio.h>
int main( )
{
int a, n;
n = 0;
printf("a = ");
scanf("%d", &a);
while (a != 0)
{
while (a > 0)
{

#include <stdio.h>
int main( )
{
int a, n_plus, n_minus;
n_minus = n_plus = 0;
printf("a = ");
scanf("%d", &a);
while (a != 0)
{
if (a > 0)
{
45

n++;
printf("a = ");
scanf("%d", &a);

n_plus++;
if (n_minus > 0)
{
printf("-: %d\n", n_minus);
n_minus = 0;
}

}
if (n > 0)
{
printf("+: %d\n", n);
n = 0;
}
while (a < 0)
{
n++;
printf("a = ");
scanf("%d", &a);
}
if (n > 0)
{
printf("-: %d\n", n);
n = 0;
}

}
else
{
n_minus++;
if (n_plus > 0)
{
printf("+: %d\n", n_plus);
n_plus = 0;
}
}
printf("a = ");
scanf("%d", &a);
}
/* */

if (n_minus > 0)
printf("-: %d\n", n_minus);
if (n_plus > 0)
printf("+: %d\n", n_plus);
return 0;

}
return 0;
}
}

4.
. ,
1, 2, 3, 2, 2 ,
1, 2, 3 3, 2.
#include <stdio.h>
int main( )
{
int a, /* */
b, /* */
46

n; /*
*/
printf("a = ");
scanf("%d", &a);
if (a != 0)
{
n = 1;
printf("b = ");
scanf("%d", &b);
while (b != 0)
{
/* */
while (b != 0 && b > a)
{
n++;
a = b;
printf("b = ");
scanf("%d", &b);
}
if (n > 1)
{
printf ("n = %d\n", n);
n = 1;
}
/* */
while (b != 0 && b < a)
{
n++;
a = b;
printf("b = ");
scanf("%d", &b);
}
if (n > 1)
{
printf ("n = %d\n", n);
n = 1;
}
/* */
47

while (b != 0 && b == a)
{
printf("b = ");
scanf("%d", &b);
}
}
}
return 0;
}
4.2.
1. .
2. k . , k.
3. , .
4. ,
, .
5. , .
6. , .
7. . .
8.

.
9. ,
, .

48


10.
.
11. k .
,
k.
12. ,
, .
13. , . , .
14. .
15. .
16. , .
17. ,
. ,
.
18.
.

49

5.
5.1.
, , . , . ,
, ,
. :
#include <stdio.h>
#include <stdlib.h>
#define N 10
int main( )
{
int a[N], /* a N */
i;
/* */
for (i = 0; i < N; i++)
{
a[i] = rand()%100;
printf("%d\n", a[i]);
}
return 0;
}
a 10 (
10
int), 0
99. , 0. a : a[0], a[1], , a[N-1].
, , .
.
50

, . ,
.
,
int a[5] = {10, 20, 30, 40, 50};
:
a[0]=10, a[1]=20, a[2]=30, a[3]=40, a[4]=50.
,
. ,
5 , , :
int a[] = {10, 20, 30, 40, 50};
,
, . ,
:
int a[10] = {0};
.
, .
.
, ,
sizeof(), :
#include <stdio.h>
int main( )
{
int a[10];
printf("%d\n", sizeof(a));
return 0;
}

51

5.2.
1. ,
.
#include <stdio.h>
#include <stdlib.h>
#define N 10
/* const , */

/* a n
int Progression(const int *a, const int n)
{
int d, i;
d = a[1] - a[0];
i = 1;
while ((++i < n) && (a[i] - a[i - 1] == d))
;
return (i >= n);
}

*/

int main( )
{
int i, a[N];
for (i = 0; i < N; i++)
a[i] = rand()%10;
printf("%s\n", Progression(a, N) ? "progression" :"no progression");

return 0;
}
2. , a
.
flag
. 1 (), ,
.
1, 2, , n-1 a[0],
52

2, 3, , n-1 a[1] .. ,
Different () . 1 ().
int Different(const double *a, const int n)
{
int i, j, flag;
i = 0;
flag = 1;
while (i < n && flag)
{
j = i;
while (++j < n && a[i] != a[j])
;
if (j < n)
/* j<n, a[i]==a[j] */
flag = 0;
else i++;
}
return flag;
}
3. .
int CountDifferent(const double *a, const int n)
{
int k, /* */
i, j;
k = 0;
for (i = 0; i < n; i++)
{
j = i;
while (++j < n && a[i] != a[j])
;
/* a[i] a[i+1], a[i+2],,a[n-1],*/

/* k */
53

if (j >= n)
k++;
}
return k;
}
4.
a x. , ( ) , x, , x.
.
,
a[i]>=x. , a[j]<x. a[i] a[j] .
void Exchange(double *a, const int n, const double x)
{
int i = 0, j = n - 1, buf;
while (i < j)
{
while (i < j && a[i] < x)
i++;
while (i < j && a[j] >= x)
j--;
if (i < j)
{
/* a[i] a[j] */
buf = a[i];
a[i] = a[j];
a[j] = buf;
i++;
j--;
}
}
}

54

5.3.
1. n. , .
2. a n.
a[i], a[1]<a[i]<a[n].
3.
.
4.
.
5. . ,
.
6. , a ,
, ..
7. ,
, .
8. r n. , .
9. .
10.
.
11. n.
, .
.
12. .
13. .
14. a b,
. , .
15. n .
16. .
, .
55

17. , , ( ).
18. a, . a, .

19. n > 2, 3.
20. n, n . , ..
21. , .
22. a b n.
c , a b .
23. a n. b
: bk
a 1 k.
24. , , .
, .
25. n, , , . , .
26. n, , ,
. ,
, , .
27. n.
.
28. .
29. n > 2.
(2, 4, ). .
56

30. , .
31. n. ,
.
32. ,
.
33. .
34.
a b. , a, b.
35. a n. b a .
36. .
37. 2 n.
, . 2, 3, ..., n , 2,
2. 3
, 3, 3. 3 ,
5, .
.

57

6.
6.1.
n- . ANSI , n
1 12.
.
:
int a[10][20];
a,
10 20 .
i j: a[i][j],
i , j .
, i j :
a[0][0] ,
a[9][19] , .
:
int a[3][2] = {{1, 2}, {3, 4}, {5, 6}};
.
,
: , .. , a :
int a[3][2] = {1, 2, 3, 4, 5, 6};
- ,
.
, a :
int a[3][2] = {{1}, {3}, {5}};

:
1
3
5

0
0
0
58

, :
int a[10][20] = {0};
6.2.
1. m n.
.
2. m n.

.
3.
.
4. ,
, .
5. , .
6. n.
.
7. n. .
8. n.
, , .
9. n. ,
.
10. , .
11.
.
12. . ,
.
13. m n, m n .
.
59

14. ,
.
15. ,
, .
, .

16. .
17. , .
18. , .
19. n.
, .
20. .
, , .
21. , .
22. .
23. , , .
24. n. , .
25. , m n
.
26. , ,
,
.
27. .
, .
28. ,
.
60

7.
7.1.
, . ,
, .
,
.
:
*_,
, , _ ,
, .
, , (int),
. &
:
int x, *px;
= &; /* px x */
px
.
& ,
register. ,
.
(NULL) , ,
:
double *pd = NULL;
, .
* ( )
61

, ,
. ,
printf(%d, *px);
x.
-
void. - ,
* .

(<, <=, >, >=, ==, !=). .
7.2.
:
.
;
. ,
Swap(a, b) a b main()
.
void Swap(int a, int b)
{
int buf;
buf = a;
a = b;
b = buf;
}
int main( )
{
int a = 1, b = 2;
Swap(a, b);
return 0;
}

62

,
main(), :
void Swap(int *pa, int *pb)
{
int buf;
buf = *pa;
*pa = *pb;
*pb = buf;
}
int main( )
{
int a = 1, b = 2;
Swap(&a, &b);
return 0;
}

Swap() ,
a b, (*).
,
, n:
#include <stdio.h>
void Digits(int n, int *psum, int *pk)
{
*psum = 0;
*pk = 0;
do
{
(*pk)++;
(*psum) += n%10;
n /= 10;
63

} while (n != 0);
}
int main( )
{
int n,
/* n
*/
sum,
/* n
*/
k;
/* n */
printf("n= ");
scanf("%d", &n);
/* n */
Digits(n, &sum, &k);
printf("sum= %d\n", sum);
printf("k= %d\n", k);
return 0;
}
7.3.
, , , .
:

:
int a[10], *pa;
pa = a;
- pa
a. ,
pa = a
: pa = &a[0].

64

. , ,
, . , i- double
a[10], i
double ( 8) 8*i .
8*i
i- .
i- , .
. , , a[20], ,
160
, .
, .
. () [], a[0], a[5].
. ,
1. -,
, . , float, 4. a[i] : *(a+i). a+i i- a, *(a+i) i- . , &a[i] a+i . a[i] *(a+i),
.
, .
65

#include <stdio.h>
#include <stdlib.h>
#define N 10
int main( )
{
int a[N], /* a N */
i;
/*
*/
for (i = 0; i < N; i++)
{
a[i] = rand()%10;
printf("%d ", a[i]);
}
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define N 10
int main( )
{
int a[N], /* a N */
i;
/*
*/
for (i = 0; i < N; i++)
{
*(a+i) = rand()%10;
printf("%d ", *(a+i));
}
return 0;
}

, , , , a++, pa++.

:
1. ,
_ (int a[100])
2. ,
_ (int a[])
3. ,
_ (int *a)

, int a[100] int a[], int *a.

66

7.4. const
,
const, :
const int i = 10;

. ,
,
, , , , . ,
a (,
) n, n ,
:
_ (const int *a, const int n)
,
const,
.
7.5.
. ,
, . ,
, , ,
. ,
,
.
,
calloc() . 67

, a 100
double, :
double *a; /* */
/* 100 double: */
a = (double *)calloc(100, sizeof(double));
calloc()
, NULL. .
malloc():
double *a;
/* 100 double:*/
a = (double *)malloc(100 * sizeof(double));
calloc(), .
a
, free():
free(a);
.
#include<stdio.h>
#include<stdlib.h>
/* */
void Print(const int *a, const int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}

68

int main( )
{
int *a,
/* */
size,
/* a */
i;
/* */
scanf("%d", &size); /* */
a = (int *)calloc(size, sizeof(int)); /* */
if (a == NULL)
printf(" ");
else
{
/* */
for (i = 0; i < size; i++)
a[i] = rand()%10;
Print(a, size);
free(a);
/* , */
}
return 0;
}

, realloc():
void *realloc(void *p, size_t size)
realloc() size , p. , , . , , realloc()
. , realloc() NULL, *p .
#include<stdio.h>
#include<stdlib.h>
void Print(const int *a, const int n)
{
69

int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main( )
{
int *a,
/* */
size,
/* a */
new_size , /* a */
i,
/* */
*tmp;
scanf("%d", &size); /* */
a = (int *)calloc(size, sizeof(int)); /* */
if (a == NULL)
printf(" ");
else
{
for (i = 0; i < size; i++)
a[i] = rand()%10;
Print(a, size);
new_size = 2 * size;
/* a */
tmp = (int *)realloc(a, new_size * sizeof(int));
if (tmp != NULL)
{
for (i = size; i < new_size; i++)
a[i] = rand()%10;
Print(a, new_size);
}
free(a);
}
return 0;
}

70

7.6.
.

. , , ,
.
,
color, (
):
char *color[3] = {red, green, blue};
color
:


. color .
.
10 ,
, , :

#include <stdio.h>
#include <stdlib.h>
int main( )
{
int *a[10] = {NULL}; /* */
71

int i, j;
for (i = 0; i < 10; i++)
{
a[i] = (int *)calloc((i+1), sizeof(int));
for (j = 0; j <= i; j++)
a[i][j] = j*10 + 10;
}
for (i = 0; i < 10; i++)
{
for (j = 0; j <= i; j++)
printf("%d ", a[i][j]);
printf("\n");
}
for (i = 0; i < 10; i++)
{
free(a[i]);
a[i] = NULL;
}
return 0;
}
7.7.
. , . ,
.
,
a[m][n] double:

72

Dispose() ,
, , .
Allocate() ,
. -
,
Dispose() , Allocate()
NULL. Allocate() .
#include<stdio.h>
#include<stdlib.h>
/* , a */
void Dispose(double **a, int m)
{
int i;
for (i = 0; i < m; i++)
if (a[i] != NULL)
free(a[i]);
free(a);
}
/* a */
double **Allocate(int m, int n)
{
double **a;
int i;
int flag; /* */
a = (double **)calloc(m, sizeof(double *));
if (a != NULL)
{
i = 0;
73

flag = 1;
while (i < m && flag)
{
a[i] = (double *)calloc(n, sizeof(double));
if (a[i] == NULL)
flag = 0;
else i++;
}
if (!flag)
{
Dispose(a, m);
a = NULL;
}
}
return a;
}
/* */
void InitArray(double **a, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i][j] = rand()%10;
}
/* */
void PrintArray(double **a, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
printf("%.1f ", a[i][j]);
printf("\n");
}
}
74

int main( )
{
double **a;
int m, n;
scanf("%d%d", &m, &n);
a = Allocate(m, n);
if (a != NULL)
{
InitArray(a, m, n);
PrintArray(a, m, n);
Dispose(a, m);
a = NULL;
}
return 0;
}

75

8.
8.1.
,
-. . ,
, :
,
;
;
;
(, ,
..);
.

, 256 .
8 (1 )
. 0 255,
8- 00000000
11111111.
ASCII (American Standard Code for
Information Interchange ). 128 ( 0 127). () ( 0 31). ,
, , ,
,
0 9. 128
ASCII , .
76

char. char
8- , .
8.2.
(<ctype.h>) , .
, unsigned char EOF. (), c , 0 () .

int isdigit(int c)
int islower(int c)
int isupper(int c)
int isalpha(int c)
int isalnum(int c)
int tolower(int c)
int toupper(int c)
int isspace(int c)

int iscntrl(int c)
int isprint(int c)
int isgraph(int c)
int ispunct(int c)


, c
, c
(a z)
, c
(A Z)
, c
(isalpha(c) = islower(c) || isupper(c))

(isalnum(c) = isalpha(c) || isdiglt ())
c
c
, c ( ),
(\f), (\n),
(\r), (t),
(\v)
, c
, c ,

, c ,

, c ,
,

77

,
printf("%s%s%s\n", "5 ", isdigit('5') ? "is " : "not is ", "a digit");
: 5 is a digit.
8.3.
, .
.
. -, ,
\0. - (\0) , . ASCII
0.
, .
, char s[10].
. s , .
, s,
fgets(), puts().
#include <stdio.h>
int main( )
{
char s[10];
/* */
printf(" 9 : ");
fgets(s, 10, stdin);
puts(s);
/* */
return 0;
}
, :
char s[] = moon;
78

s, 5 : m, o, o, n, \0. , s
.

:
char s1[5] = {m, o, o, n, \0};
char s2[7] = {m, o, o, n, \0}.
s1 s2
:
s1:

\0

s2:

\0

\0

\0

, :
char s1[5] = moon;
char s1[7] = moon;
, ,
, ,
.
, ,
char s[10];

s = Hello;

.
.

. char *ps, ps, .

, , ps
.
79

- char *ps ps = sun,


: (
read only) s, u, n \0; ps , , , s.
ps : ps[0], ps[1], ps[2].
:
#include <stdio.h>
int main( )
{
char s[17] = "this is a string";
/* ps s */
char *ps = s;
int i;
for (i = 0; i < 16; i++)
/* ps : */
printf("%c", ps[i]);
printf("\n");
ps = ps + 8;
/* ps a */
printf("%s\n", ps); /* */
return 0;
}
:
this is a string
a string
, ,
, ,
char *ps = sun;
- ps, sun.

80

, ,
, ,
, ps.
,
(, char s[10])
, (char *s) , .

.
1. s .
int Atoi(const char *s)
{
int i, n;
i = n = 0;
while (s[i] >= '0' && s[i] <= '9')
{
n = n*10 + (s[i] - '0');
i++;
}
return n;
}
s[i] 0
, s[i],
0, 1, , 9 . 0 0,
1 1 9,
9.
2.
. , .

81

int ToLower(const int c)


{
if (c >= 'A' && c <= 'Z')
return c + ('a' - 'A');
else return c;
}
3. s , c.
DeleteSymbol() , s, , c.
void DeleteSymbol(char *s, const int c)
{
int i, j;
i = j = 0;
while (s[i] != '\0')
{
if (s[i] != c)
s[j++] = s[i];
i++;
}
s[j] = '\0';
}
4.
\0.
int Strlen(const char *s)
{
int i = 0;
while (s[i] != \0)
i++;
return i;
}

82

5. t s.
void Strcpy(char *s, const char *t)
{
int i = 0;
while ((s[i] = t[i]) != '\0')
i++;
}
6. t s.
void Strcat(char *s, const char *t)
{
int i, j;
i = j = 0;
while (s[i] != '\0')
i++;
while ((s[i++] = t[j++]) != '\0')
;
}
8.4.

( <string.h>):

int strlen(const char *s)


char *strcpy(char *s, const char *t)
char *strncpy(char *s, const char *t,
size_t n)

char *strcat(char *s, const char *t)


83


s
t s,
\0; s
n
t s; s.
\0, t ,
n
t s; s

n t s, s
s t;
<0, s<t, 0, s == t, >0,
s>t
int strncmp(const char *s, const char *t, strcmp(),
size_t n)

n s
t
char *strchr(const char *s, int c)

s, NULL,
char *strrchr(const char *s, int c)

s, NULL,
char *strpbrk(const char *s,

const char *t)
s ,
,
t, NULL,

char *strstr(const char *s, const char *t) t s, NULL,
s
char *strtok(char *s, const char *t)
s , t. , NULL,

char *strncat(char *s, const char *t,
size_t n)
int strcmp(const char *s,
const char *t)

strchr().
char *s, c;
s = "check";
c = 'e';
84

printf("%s%c%s%s%s\n", "symbol ", c, strchr(s, c) ? " was " : " was not ",
"found in ", s);
:
symbol e was found in check.
strstr():
char *s, *t;
s = "this is a test!!!";
t = "a test";
printf("%s\n", strstr(s, t));
:
a test!!!.
strtok() , (,
). strtok()
s (\0) s .
#include <stdio.h>
#include <string.h>
#define DELIMITERS " .,:;\n\t"
/* - */
int main( )
{
char sentence[500]; /* , */
char *word;
/* */
fgets(sentence, 500, stdin);
word = strtok(sentence, DELIMITERS);
while (word != NULL)
{
puts(word);
word = strtok(NULL, DELIMITERS);
}
return 0;
}
85

strtok()
. NULL , strtok()
sentence, .
<string.h>
. , , Strlen() , s.
int Strlen(char *s)
{
int i = 0;
while (*s != '\0')
{
s++;
i++;
}
return i;
}

int Strlen(char *s)


{
int i = 0;
while (*s++ != \0)
i++;
return i;
}

int Strlen(char *s)


{
int i = 0;
while (*s++)
i++;
return i;
}

Strcpy() t
s.
void Strcpy(char *s, char *t) void Strcpy(char *s, char *t) void Strcpy(char *s, char *t)

while ((*s = *t) != '\0')


while ((*s++ = *t++)
{
!= '\0')
s++;
;
t++;
}
}

while (*s++ = *t++)


;
}

8.5.
(
<stdlib.h>).
86


double atof(const char *s)


s
int atoi(const char *s)
s
long atol(const char *s)
s

double strtod(const char *s, char
**endp)
s double,
-;
endp NULL, *endp
, -

long strtol(const char *s, char
**endp, int base)
s long,
-;
*endp
,
-
; base ,
unsigned long strtoul(const char *s, , strtol(),
char **endp, int base)
unsigned long
, ,

x = atof(s);

x = strtod(s, NULL);

/* atof() */
#include<stdio.h>
#include<stdlib.h>
int main( )
{
char *s = "1.5";

/* strtod() */
#include<stdio.h>
#include<stdlib.h>
int main( )
{
char *s = "1.5";
87

double x;
x = atof(s);
printf(x = %f\n, x);
return 0;
}

double x;
x = strtod(s, NULL);
printf(x = %f\n, x);
return 0;
}


, sprintf(). :
int a = 123;
char s[10];
sprintf(s, "%d", a);
puts(s);
8.6.
1. n, 1 n 26.
s n .
#include <stdio.h>
#define LENGTH 27
int main( )
{
char s[LENGTH];
int i;
unsigned int n;
printf(n = );
scanf("%u", &n);
for (i = 0; i < n; i++)
s[i] = 'A' + i;
s[n] = '\0';
puts(s);
return 0;
}

88

2. ,
. .
#include <ctype.h>
int Sum(const char *s)
{
int i, sum;
i = sum = 0;
while (isdigit(s[i]))
{
sum += (s[i] - '0');
i++;
}
return sum;
}
3. n
2 p 10.
, s n
p- .
void Itoa(int a, int p, char *s)
{
int i, j, buf;
i = 0;
/* */
do
{
s[i] = '0' + a % p;
a /= p;
i++
} while (a != 0);
s[i] = '\0';
/* */
j = i 1;
i = 0;
while (i < j)
89

{
buf = s[i];
s[i] = s[j];
s[j] = buf;
i++;
j--;
}
}
4. s. .
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 500
/* s */
#define VOWEL "AEIOUYaeiouy" /* */
void Count(char *s, int *ng, int *ns)
{
int i;
i = *ng = *ns = 0;
while (s[i])
{
if (isalpha(s[i]))
if (strchr(VOWEL, s[i]))
(*ng)++;
else (*ns)++;
i++;
}
}
int main( )
{
char s[N];
int ng,
ns;

/* */
/* */
90

fgets(s, N, stdin);
Count(s, &ng, &ns);
printf("ng = %d ns = %d\n", ng, ns);
getch();
return 0;
}
. Count() strchr()
<string.h>, ,
s[i] VOWEL, s[i]
. s.
.
s[i] VOWEL,
s[i] .
int flag[256] , ASCII. flag, , ,
0:
int flag[256] = {0};
for(i = 0; i < strlen(VOWEL); i++)
flag[VOWEL[i]] = 1;
, c
,
if (flag[c])
, Count() :
void Count(char *s, int *ng, int *ns)
{
int i, size;
int flag[256] = {0};
size = strlen(VOWEL);
for(i = 0; i < size; i++)
91

flag[VOWEL[i]] = 1;
i = *ng = *ns = 0;
while (s[i])
{
if (isalpha(s[i]))
if (flag[s[i]])
(*ng)++;
else (*ns)++;
i++;
}
}
5. s. .
digit, s.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 300
/* s */
int Sum(char *s)
{
int i, j, sum;
char *digit = NULL;
sum = i = 0;
while (s[i])
{
/* , : */
while (s[i] && !isdigit(s[i]))
i++;
j = i; /* */
/* ,
:
*/
92

while (s[i] && isdigit(s[i]))


i++;
/* digit: */
digit = (char *)malloc((i - j + 1) * sizeof(char));
strncpy(digit, &s[j], i - j); /* digit */
digit[i - j] = '\0';
sum += atoi(digit); /* */
free(digit);
/* */
digit = NULL;
}
return sum;
}
int main( )
{
char s[N];
fgets(s, N, stdin);
printf("sum = %d\n", Sum(s));
return 0;
}
8.7.
-. sentence -,
, word
sentence.
, sentence ,
char sentence[200];
, sentence :
char *sentence;
word.
.
93

1. :
sentence ,
-,
\0, word .
, (, ) - \0,
word sentence.
, sentence
:
H i

T h

e s

. \0

- \0
sentence :

, word H:

word
Hi. word T:

word This ..

sentence.
#include <stdio.h>
#include <string.h>
94

#define DELIMITERS " .,:;\n\t"


#define N 300

/* - */

int main( )
{
char sentence[N];
/* */
char *word; /* */
int i, j, n;
fgets(sentence, N, stdin);
/* */
n = strlen(sentence);
/* sentence */
/* - '\0' */
for (i = 0; i < n; i++)
{
if (strchr(DELIMITERS, sentence[i]))
sentence[i] = '\0';
}
i = 0;
while (i < n)
{
/* */
while (i < n && sentence[i] == '\0')
i++;
word = &sentence[i]; /*
*/
puts(word);
/* */
/*
*/
while (i < n && sentence[i] != '\0')
i++;
}
return 0;
}
.
sentence strchr(), ,
-.
, sentence -
, sentence :
95

strlen(sentence) * strlen(DELIMITERS).
,
sentence. .
int flag[256], () ASCII.
: i , flag[i] = 1, flag[i] = 0.
ASCII (i = 0, 1, , 255):
int flag[256] = {0};
for (i = 0; i < strlen(DELIMITERS); i++)
flag[DELIMITERS[i]] = 1;
flag :

, i- sentence , :
if (flag[sentence[i]])
- -
:
for(i = 0; i < n; i++)
if (flag[sentence[i]])
sentence[i] = \0;
2. . - sentence, sentence
( , , , ), , . word

96

, sentence
word.
,
, 1.
#include <stdio.h>
#include <string.h>
#define DELIMITERS " .,:;\n\t"
#define N 300

/* - */

int main( )
{
char sentence[N];
/* */
char *word;
/* */
int flag[256] = {0}, len, i, j;
len = strlen(DELIMITERS);
/* i -,
flag[i] = 1:
*/
for (i = 0; i < len; i++)
flag[DELIMITERS[i]] = 1;
fgets(sentence, N, stdin);
/* */
i = 0;
while (sentence[i])
{
/* */
while (sentence[i] && flag[sentence[i]])
i++;
j = i;
/* */
/* */
while (sentence[i] && !flag[sentence[i]])
i++;
/* : */
word = (char *)calloc(i j + 1, sizeof(char));
/* word */
strncpy(word, &sentence[j], i - j);
word[i - j] = '\0';
97

puts(word);
free(word);
/* */
word = NULL;
}
return 0;
}
8.8.
1. ,
.
2. n > 0 c1 c2.
n, c1 c2.
3. . ,
, .
4. s n. t 2n s, .
5. s. , , s .
6. s.
s.
7. .
, .
8. , . ,
.
9. s s0. s , s0.
10. s s0. s0
s.
11.
.
12. -. .
13. void WordN(char *s, char *t, int k),
k- s t.
98

14. -. ,
, .
15. -.
.
16. .
.

17. n, 1 n 26. n ( )
, z.
18. , s.
19. . .
20. , . ,
.
21. .
22. s s0. s ,
s0. , s .
23. -. , .
24. -. .
25. -. ,
, ,
.
26. void FillStr(char *s, char *t, int n),
t n s.
27. s ,
( ). .
,
.
99

9.
9.1.
,
, , .. ,
, , : (double x) (double y) ,
(char name[20]), (int year) (char group[30])
..

. ,
, .

struct, , , . , , , :
struct DOT
{
double x;
double y;
};

/* */
/* */

,
, :
struct STUDENT
{
char name[20];
int year;
char group[30];
};

/* */
/*
*/
/*
*/

100

, , .
,
. .
,
. , :
struct DOT A, B;
/* A B DOT
*/
struct STUDENT stud; /* stud STUDENT */
- A B, , - stud. (.): A.x, A.y, stud.name,
stud.year, stud.group.
A,
.
#include <stdio.h>
#include <math.h>
struct DOT
{
double x;
double y;
};
int main( )
{
struct DOT A;
scanf("%lf", &A.x);
scanf("%lf", &A.y);
printf("d = %.2f\n", sqrt(A.x*A.x + A.y*A.y));
return 0;
}

101

,
. ( ),
, . - ,
.
,
, :
struct DOT A = {1.2, -2.5};
struct STUDENT stud = {"", 1990, "-11"};
, ,
, .
,
.
,
, ,
:
#include <stdio.h>
struct DOT
{
double x;
double y;
};
struct DOT Init(double x, double y)
{
struct DOT A;
A.x = x;
A.y = y;
return A;
}
int main( )
{
struct DOT B = Init(2.4, 1.5);
return 0;
}
102

,
, :
struct DOT A = {1.2, -2.5}, B;
B = A;

,
.
. ,
10 :
struct DOT a[10];
:
a[0].x, a[0].y,, a[9].x, a[9].y
9.2.
,
.
. ,
, . , . , .

union, :
union NUMBER
{
long l;
double d;
};
103

l 4 , d 8 .
NUMBER 8 ,
,
.
, . ,
long,
union NUMBER value = {10};
/* value.l == 10 */

union NUMBER value = {10.0};
:
#include <stdio.h>
union NUMBER
{
long l;
double d;
};
int main( )
{
union NUMBER value = {10};
printf("l = %d\n", value.l);
value.d = 5.0;
printf("d = %f\n", value.d);
return 0;
}
. . , STR:
union NUMBER
{
long l;

struct STR
{
char s[10];
104

double d;
};
struct STR
{
char s[10];
union NUMBER value;
};

union NUMBER
{
long l;
double d;
} value;
};


STR :
int main( )
{
struct STR st = {"abc", 10}; /* st.s == abc, st.value.l == 10 */
printf("s = %s\n", st.s);
printf("l = %d\n", st.value.l);
st.value.d = 1.0;
printf("d = %f\n", st.value.d);
return 0;
}

105

10.
,
,
. .
#. .
10.1. #include
#include
. :
#include " "
#include < >
,
, .
, , , PATH.
#include
: stdio.h
( -), stdlib.h (
), string.h ( , ) ..
10.2. #define
,
. ,
,
. , , , .
106

#define :
#define
#define ( )
. .

. ,
#define E 2.711828 ( ),
E 2.711828.
, .
,
. ,
#define FOREVER for(; ; )
FOREVER.
#define
, , .
, .

, .

:
#include <stdio.h>
#define SQR(x) ((x) * (x))
int main( )
{
printf("%d\n", SQR(5));
printf("%f\n", SQR(SQR(0.12)));
return 0;
}
((x) * (x)).
,
107

#define SQR(x) x * x
,
. ,
r = SQR(a + b);
:
r = a + b * a + b;

#define SQR(x) ((x) * (x))

:
#include <stdio.h>
#define MAX(x, y) ((x) > (y) ? (x) : (y))
int main( )
{
int x = 1, y = 2;
int max = MAX(x, y);
return 0;
}
10.3. #undef
#undef #define. :
#undef
#undef #dedef
.
#undef , .
.
#define.
108

11.
11.1.
,
.

main(), .
main() , ,
, .
.
, ,
.
,
,
. ( ) ( ), . , .
:
__ _ (_)
{

}
, . ,

void. ,
void.
.
109

.
return
. return :
return ;
, ,
.

, .
, .
, .
return;
, ,
return ;
.
return .

.
11.2.
, , , . ,
. ,
, , , . ,
,
.
110

, , a b,
[a,b], Middle().
:
double Middle(double, double);
, Middle() double double.
#include<stdio.h>
double Middle(double, double);

/* */

int main( )
{
double a, b;
scanf("%lf%lf", &a, &b);
printf("middle = %f\n", Middle(a,b));
return 0;
}
/* Middle() */
double Middle(double a, double b)
{
return (a + b) / 2;
}

. , Middle()
1 2,
double (1.0 2.0),
(a + b) / 2. : 1.5.
double, 1.0.

111

11.3.
,
.
. (auto), (register),
(extern) (static).
, , ,
.
, , , , .
auto register .
.
register, ,
. , , -,
.. ,
.
, .
. :
register int i;
register char c;
register
. :
long Sum(register int n)
{
register long sum;
register int i;
for (i = 0; i < n; i++)
112

sum += i;
return sum;
}
,
. .
,
, . , :
#include <stdio.h>
int i = 1; /* */
void Print()
{
printf("%d\n", i);
}
int main( )
{
Print();
i++;
Print();
return 0;
}
:
1
2
,
#include <stdio.h>
void Print()
{
printf("%d\n", i); /* */
}
int i = 1; /* */
113

int main( )
{
Print();
i++;
Print();
return 0;
}
, Print()
i.
, extern:
#include <stdio.h>
void Print()
{
extern int i;
printf("%d\n", i);
}
int i = 1; /* */
int main( )
{
Print();
i++;
Print();
return 0;
}
extern int i , i int, .

. , , , , .
, ,

.
114

(static)
.
, .
, .
, . ,
10, 20, , 100:
#include <stdio.h>
void Print()
{
static int i = 10;
printf("%d\n", i);
i += 10;
}
int main( )
{
int j;
for (j = 1; j <= 10; j++)
Print();
return 0;
}
, . .
:
#include <stdio.h>
static int i = 10;
void Print()
{
printf("%d\n", i);
i +=10;
115

}
int main( )
{
int j = 1;
for (j = 1; j <= 10; j++)
Print();
return 0;
}
11.4.
, , .
,
.
.
, ,
..
, ( ) , F() , F
. :
#include<stdio.h>
int Compare(int a, int b)
{
return a >= b;
}
int main( )
{
printf("%p\n", Compare);
printf("%p\n", main);
getch();
return 0;
}
116


, .

.
#include<stdio.h>
int ShowMsg(char *msg)
{
return puts(msg);
}
int main( )
{
int (*pFunc)(char *);
/* */
pFunc = ShowMsg;
(*pFunc)("A message \n"); /* */
return 0;
}
. Count(),

s. int Big(int c) int Small(int c) , c
. Count() Big(),
s. Small(), s.
Count() :
int (*flag)(int)
, Count() ,
, int. , *flag , .
,
(*).

117

int *flag(int)
,
.
, Count() , if:
if ((*flag)(s[i]))
,
.
#include<stdio.h>
#define N 200
int Big(int);
/* Big() */
int Small(int);
/* Small() */
int Count(char *s, int (*)(int)); /* Count() */
int Big(int c)
{
return (c >= 'A' && c <= 'Z');
}
int Small(int c)
{
return (c >= 'a' && c <= 'z');
}
int Count(char *s, int (*flag)(int))
{
int i, n;
i = n = 0;
while (s[i] != '\0')
{
if ((*flag)(s[i]))
n++;
i++;
}
118

return n;
}
int main( )
{
char s[N];
fgets(s, N, stdin);
printf("%d\n", Count(s, Big));
printf("%d\n", Count(s, Small));
return 0;
}

. : Sum, Subtraction, Multiplication,
(, , ).
f 3, :
int (*f[3])(int, int) = {Sum, Subtraction, Multiplication};
(a b) (0 ,
1 , 2 ), (a+b, a-b a*b).
#include<stdio.h>
int Sum(int, int);
int Subtraction(int, int);
int Multiplication(int, int);
int Sum(int a, int b)
{
return a + b;
}
int Subtraction(int a, int b)
119

{
return a - b;
}
int Multiplication(int a, int b)
{
return a * b;
}
int main( )
{
int a, b, i;
int (*f[3])(int, int) = {Sum, Subtraction, Multiplication};
scanf("%d%d", &a, &b);
scanf("%d", &i);
if (i >= 0 && i <= 2)
printf("%d\n", (*f[i])(a, b));
return 0;
}
11.5.
,

.
.
, .
, , .

().
, . ,
.
120

, , , ,
.
.
.
11.6.
1. 10, 9, , 1 Print(). ,
. , , . Print() , ,
.
#include <stdio.h>
void Print(int);
void Print(int i)
{
if (i >= 1)
{
printf("%d\n", i);
Print(i - 1);
}
}
int main( )
{
Print(10);
return 0;
}

121

2.
,
1 n ( ),
n . Sum(n) =
= 1+2++n. :
Sum(n) = Sum(n - 1) + n n>0;
Sum(0) = 0.
:
#include <stdio.h>
int Sum(int);
int Sum(int i)
{
if (i >= 1)
return i + Sum(i - 1);
else return 0;
}
int main( )
{
int n;
printf("n = ");
scanf("%d", &n);
printf("sum = %d\n", Sum(n));
return 0;
}
3.
int Count(char *s, int n) ,
s.
F
ASCII: F(c) = 1, c , F(c) = 0 .
s n. (count) :
count = F(s[0]) + F(s[1]) + + F(s[n-1]).
122

int Count(char *s, int n) :


Count(s, 0) = 0,
Count(s, n) = Count(s, n - 1) + F(s[n-1]) n > 0.
#include<stdio.h>
#include<string.h>
#define N 300
int F(char);
int Count (char *, int);
int F(char c)
{
if (c >= 'a' && c <= 'z')
return 1;
else return 0;
}
int Count (char *s, int n)
{
if (n == 0)
return 0;
else return Count(s, n - 1) + F(s[n-1]);
}
int main( )
{
char s[N];
fgets(s, N, stdin);
printf("count = %d\n", Count(s, strlen(s)));
return 0;
}
4 ( ).
. n! n- .
[13] [15].
123

,
{1, 2, , n} n n- a.
. a
1, 2, , n, a
{1, 2, , n}. a , . ,
1.
a, n-1 .
2, 3, , n.
2, 3, , n ..

.
#include <stdio.h>
#define N 3
/* Print */
void Print(const int *a, const int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
/* Swap */
void Swap(int *a, int *b)
{
int buf;
buf = *a;
*a = *b;
*b = buf;
}
/* Perestanovka
* a ,
124

* k ,
* n a
*/
void Perestanovka(int *a, const int k, const int n)
{
if (k < 0)
{
/* */
Print(a, n);
}
else
{
int i;
for (i = 0; i <= k; i++)
{
Swap(&a[i], &a[k]);
Perestanovka(a, k-1, n);
Swap(&a[i], &a[k]);
}
}
}
int main( )
{
int a[N];
int i;
/* a */
for (i = 0; i < N; i++)
a[i] = i+1;
Perestanovka(a, N-1, N); /* */
return 0;
}
11.7.
1. double Fact(double n) , n! = 12n.
125

2. double Fact2(double n) ,
n!! = n(n2)(n4), 2, n , 1, n .
3. double PowerN(double x, int n)
, n- x
: x0 = 1, xn = xxn1 n > 0, xn = 1 / xn n < 0.
4. long Fib(long n) ,
n- .
5. int DigitSum(int n)
, n, .
6. , n.
7. , .
8. ,
, .
9. , .
10. , .
11. int DigitCount(char *s, int n)
, s,
.
12. int Symmetric(char *s, int n)
, , s (
). .
13. , , a .
14. ,
x a.
15. ,
, x a.
16.
.
126

17. ,
, a .
18. a > 1 b > 1. (a, b). :
(a, 0)=(0, a)=a;
(a, b)=(a-b, b), a b;
(a, b) = (a, b-a), b > a.
19. C(n,m) , n m, :
C(n, 0) = C(n, n) = 1, C(n, m) = C(n1, m) + C(n1, m1).
20. n . k. ,
1 n.
21. . n1,
n2, , nk ( n1, n2, , nk ),
. ,
N ,
, , . .
F(N) = F(N n1) + F(N n2) + + F(N nk),
F(N) = 0 N < 0,
F(0) = 1.
22. a n , (, , a1, a2, , an).
23. a n ,
.

127

12.
12.1.
, , .
, . , 8-
:

0
1
2
3

255

8-
00000000
00000001
00000010
00000011

11111111

, (), . , , ,
.
, 8-
-1, . -, -1, 1:
00000001.
-, :
11111110.
-, :
11111111.
8- -1.
-1
1, :
100000000.

128

8-,
1 , 8- 0.

: 0, 1. 8-
.

0
1
2

126
127


00000000
00000001
00000010

01111110
01111111

-128
-127
-126

-2
-1


10000000
10000001
10000010

11111110
11111111

, 0 1. .

&
|
^
<<
>>
~






( )

<< >>
, . , , , .
, 8- (unsigned char) 27
:
00011011 << 1 = 00110110
(27 << 1 = 54),
00011011 << 2 = 01101100
(27 << 2 = 108),
129

00011011 << 3 = 11011000


(27 << 3 = 216),
00011011 << 4 = 10110000
(27 << 4 = 176),
00011011 << 5 = 01100000
(27 << 5 = 96),
00011011 >> 1 = 00001101
(27 >> 1 = 13),
00011011 >> 2 = 00000110
(27 >> 2 = 6).
(>>) (signed),
. , 8- (char) -27
:
11100101 << 1 = 11001010
(-27 << 1 = -54),
11100101 << 2 = 10010100
(-27 << 2 = -108),
11100101 << 3 = 00101000
(-27 << 3 = 40),
11100101 >> 1 = 11110010
(-27 >> 1 = -14),
11100101 >> 2 = 11111001
(-27 >> 2 = -7).
, ()
() 2,
.
~ ,
. &, | ^ ; . .
:

(x)
0
0
1
1


(y)
0
1
0
1

~x

x&y

x|y

x^y

1
1
0
0

0
0
0
1

0
1
1
1

0
1
1
0

:
~:
~01010101
10101010

&:
01010101 &
00000001
00000001
130

|:
01010101 |
00101011
01111111

^:
01010101 ^
01011010
00001111

&, | ^, :
x | 1 = 1, x | 0 = x,
x & 1 = x, x & 0 = 0,
x ^ 1 = ~x, x ^ 0 = x.
12.2.
1. ,
a.
DisplayBits a,
, n.
#include <stdio.h>
void DisplayBits(int a, char n)
{
char i, bit;
for (i = n - 1; i >= 0; i--)
{
/* i- */
bit = (a >> i) & 1;
printf("%d ", bit);
}
}
int main( )
{
int a;
scanf("%d", &a);
DisplayBits(a, sizeof(int)*8);
return 0;
}

131

2. a. i-
a 1,
.
b , 1 i- 0
. : b = 1 << i.
: a | b.
a |= 1 << i;
3. a. i-
a 0,
.
b, 0 i- 1 : b = ~(1 << i). : a & b.
a &= ~(1 << i);
4. a i- , .
a ^= (1 << i);
5. x y .
i- x j- y, y.
x, i-
xi, j- x:
((x >> i) & 1) << j.
j- y,
:
y & ~(1 << j).

:
y = ((x >> i) & 1) << j | y & ~(1 << j).
6. a.
132

BitCount
, ,
, n .
short BitCount(unsigned int a, short n)
{
short count;
int i;
count = 0;
for (i = 0; i < n; i++)
{
if (a & 1)
count++;
a >>= 1;
}
return count;
}
BitCount ,
,
a ,
. n , , () .
short BitCount2(unsigned int a)
{
short count;
count = 0;
while (a != 0)
{
if (a & 1)
count++;
a >>= 1;
}
return count;
}
133

7. n > 0,
2: n = 2k. k
.
int Degree(unsigned int n)
{
int k;
k = -1;
while (n)
{
n >>= 1;
k++;
}
return k;
}
8 ( ).
an, a n ,
n 0. a:
a, a 2 , a 4 , a 8 , ..., a 2 ,
t = [log2 n]. , . n :
n = nt 2 t + nt 1 2 t 1 + ... + n1 2 + n0 ,
n0, n1, , nt {0, 1}. an
:
t

a n = a n a n 2 a n 4 ... a n
0

t 1

2t 1

a n 2 .
t

an
2 log2 n.
long Degree(int a, unsigned int n)
{
long deg, /* a: a, a2, a4, a8, */
rez; /* */
rez = 1;
deg = a;
134

while (n != 0)
{
if (n & 1)
rez *= deg;
deg *= deg;
n >>= 1;
}
return rez;
}
12.3.
1. a.
2. a n 32,
. a
unsigned long a[n-1] a[1]a[0].
3. , .
4. a.
, .
5. a. ,
.
6. a k , .
7. a
k .
8. a
k .
9. ()
a.
10. () a.

135

11. , a
, 2n 1
n.
12. .
13. a > 0. ,
: a ( ),
.
14. a > 0. , ,
( ).
15.
unsigned long Perest(unsigned long x, unsigned char *pi, char n),
x pi Sn (n = 32)
.

136

13.
13.1.
,
, , . , , , ..
, :

,
.
,
, ,
.
, .
.

, fopen
<stdio.h>. ( ) FILE, . : ; , ; ;
; ;
.

FILE. :
FILE *f;

137

fopen() :
FILE *fopen(char *name, char *mode)
fopen() :
f = fopen(name, mode);
(name) , . , . :

"r"
"w"
"a"
"r+"
"w+"
"a+"

.
; ,
() .
,
.

. ,
.
,


. , , ..
, fopen
NULL. ,
FILE *f;
if ((f = fopen(c:\\a.data, "r")) == NULL)
printf(File could not be opened\n);
c:\\a.data .
(
NULL), File could not be
opened.
138

(
<stdio.h>) .
FILE *fopen ( const char *filename, const char* mode )
fopen()
NULL, .
int fflush(FILE *f)

. .
EOF ,
0. fflush(NULL) .
int fclose(FILE *f)
f.
. , , , . EOF
0 .
int feof(FILE *f)
, . , 0.
int remove(const char *filename)
. , 0.
int rename(const char *oldname, const char* newname)
rename() oldname
newname; , , 0.
int fputc(int c, FILE *f)
c f.
, EOF.
139

int fgetc(FILE *f)


f, . ; EOF.
int fputs(const char *s, FILE *f)
s f. ; EOF.
char *fgets(char *s, int n, FILE *f)
s.
n-1 , ,
\n. s - \0. s; , NULL.
int fprintf(FILE *f, const char *format, argument_1, , argument_n)

f . ; EOF.
int fscanf(FILE *f, const char *format, &argument_1, , &argument_n)

f .
; EOF.
scanf() (. . 2.2.
-).
int fseek(FILE *f, long count, int origin)
f . (
). count :
, origin
SEEK_SET;
, origin
SEEK_CUR;
, origin
SEEK_END.

140

int rewind(FILE *f)


.
fseek(f, 0, SEEK_SET);
long ftell(FILE *f)
f;
-1.
int ferror(FILE *f)
,
f , 0.
13.2.
,
\n \0.
(
ASCII), , , . , 123
- 1, 2 3, , 123.
, , mode t: rt,
wt, at, r+t, w+t, a+t.

.
1 ( fputc()). fileName, ,
EOF.
. MS-DOS
Ctrl+z,
Enter. Windows
Ctrl+z, UNIX Ctrl+d.
141

int CreateTextFile(char *fileName)


{
FILE *f;
char c;
if ((f = fopen(fileName, "wt")) == NULL)
return 1;
while ((c = getchar()) != EOF)
fputc(c, f);
fclose(f);
return 0;
}
2 ( fgetc()).
.
.
int ReadTextFile(char *fileName)
{
FILE *f;
char c;
if ((f = fopen(fileName, "rt")) == NULL)
return 1;
while ((c = fgetc(f)) != EOF)
putchar(c);
fclose(f);
return 0;
}
3 ( fputs()). .
10 .
int CreateTextFile(char *fileName)
{
FILE *f;
char s[300];
int i;
142

if ((f = fopen(fileName, "wt")) == NULL)


return 1;
for (i = 0; i < 10; i++)
{
fgets(s, 300, stdin); /* s */
fputs(s, f);
/* s */
}
fclose(f);
return 0;
}
4 ( fgets()). .
int ReadTextFile(char *fileName)
{
FILE *f;
char line[500];
if ((f = fopen(fileName, "rt")) == NULL)
return 1;
while (fgets(line, 500, f) != NULL)
{
printf(line);
}
fclose(f);
return 0;
}
, , .
, , fprintf() fscanf().
5 ( fprintf()). : , , .
,

000.
143

int CreateFile(char *fileName)


{
FILE *f;
char name[50];
/*
*/
int year;
/* */
float earnings;
/* */
if ((f = fopen(fileName, "wt")) == NULL)
return 1;
printf("name: ");
scanf("%s", name);
while (strcmp(name, "000"))
{
printf("year: ");
scanf("%d", &year);
printf("earnings: ");
scanf("%f", &earnings);
fprintf(f, "%s %d %.2f\n", name, year, earnings);
printf("name: ");
scanf("%s", name);
}
fclose(f);
return 0;
}
6 ( fscanf()). , . .
int ReadFile(char *fileName)
{
FILE *f;
char name[50];
/*
*/
int year;
/* */
float earnings;
/* */
if ((f = fopen(fileName, "rt")) == NULL)
return 1;
while (fscanf(f, "%s%d%f", name, &year, &earnings) != EOF)
144

{
printf("%s %d %.2f\n", name, year, earnings);
}
fclose(f);
return 0;
}
7 ( fscanf()).
,
( , \t, \n), .
int ReadWords(char *fileName)
{
FILE *f;
char s[500];
if ((f = fopen(fileName, "rt")) == NULL)
return 1;
while (fscanf(f, "%s", s) != EOF)
{
puts(s);
}
fclose(f);
return 0;
}
13.3.
, .
, .
( ASCII ). . ,
123 1111011 145

. 3 .
fwrite() fread().
fwrite(), fread().
:
size_t fwrite(void *ptr, size_t size, size_t n, FILE *f)
size_t fread(void *ptr, size_t size, size_t n, FILE *f)
fwrite() f , ptr, n , size.

.
fread() f ,
ptr, n , size.
.
, , f x double, :
fwrite(&x, sizeof(x), 1, f);
a, n , :
fwrite(a, sizeof(int), n, f);
, , b: rb, wb,
ab, r+b, w+b, a+b. .
1. .
int CreateFile(char *fileName)
{
FILE *f;
int a;
if ((f = fopen(fileName, "wb")) == NULL)
return 1;
printf("a = "); scanf("%d", &a);
while (a != 0)
146

{
fwrite(&a, sizeof(a), 1, f);
printf("a = "); scanf("%d", &a);
}
fclose(f);
return 0;
}
2. , .
int ReadFile(char *fileName)
{
FILE *f;
int a;
if ((f = fopen(fileName, "rb")) == NULL)
return 1;
while (fread(&a, sizeof(a), 1, f))
printf("a = %d\n", a);
fclose(f);
return 0;
}
3. Size()
, fileName.
, -1.
long Size(char *fileName)
{
FILE *f;
long n;
if ((f = fopen(fileName, "rb")) == NULL)
return -1;
fseek(f, 0, SEEK_END); /* */
n = ftell(f);
/* */
fclose(f);
return n;
}
147

4.
.
int ReadConverseFile(char *fileName)
{
FILE *f;
int a;
long i, n;
if ((f = fopen(fileName, "rb")) == NULL)
return 1;
n = Size(fileName)/sizeof(int); /* */
for (i = n - 1; i >= 0; i--)
{
fseek(f, i*sizeof(int), SEEK_SET);
fread(&a, sizeof(a), 1, f);
printf("a = %d\n", a);
}
fclose(f);
return 0;
}
5. .
int SquareRechange(char *fileName)
{
FILE *f;
long i, n;
int a;
if ((f = fopen(fileName, "r+b")) == NULL)
return 1;
n = Size(fileName)/sizeof(int);
for (i = 0; i < n; i++)
{
fseek(f, i*sizeof(a), SEEK_SET);
fread(&a, sizeof(a), 1, f);
a *= a;
148

fseek(f, i*sizeof(a), SEEK_SET);


fwrite(&a, sizeof(a), 1, f);
}
fclose(f);
return 0;
}
fwrite() , fread().
6.
: , , .

WORKER:
struct WORKER
{
char name[20];
int year;
float earnings;
};

/*
*/
/* */
/* */

(
), :
fwrite(&worker, sizeof(worker), 1, f);
worker WORKER. ,
,
fread(&worker, sizeof(worker), 1, f);
CreateFile() , ReadFile()
.
#include<stdio.h>
struct WORKER
{
char name[20];
int year;
149

float earnings;
};
/* */
int CreateFile(char *fileName)
{
FILE *f;
struct WORKER worker;
if ((f = fopen(fileName, "wb")) == NULL)
return 1;
printf("name: ");
scanf("%s", worker.name);
while (strcmp(worker.name, "000"))
{
printf("year: ");
scanf("%d", &worker.year);
printf("earnings: ");
scanf("%f", &worker.earnings);
fwrite(&worker, sizeof(worker), 1, f); /* */
printf("name: ");
scanf("%s", worker.name);
}
fclose(f);
return 0;
}
/* */
int ReadFile(char *fileName)
{
FILE *f;
struct WORKER worker;
if ((f = fopen(fileName, "rb")) == NULL)
return 1;
while (fread(&worker, sizeof(worker), 1, f))
{
printf("%s ", worker.name);
printf("%d ", worker.year);
150

printf("%.2f\n", worker.earnings);
}
fclose(f);
return 0;
}
int main( )
{
char *fileName = "c:\\a.data";
CreateFile(fileName);
ReadFile(fileName);
return 0;
}
7.
.
.
/* f */
int AddWorker(FILE *f, const struct WORKER *worker)
{
fseek(f, 0, SEEK_END); /* f */
fwrite(worker, sizeof(*worker), 1, f);
if (ferror(f))
return 1;
else return 0;
}
/* */
int CreateFile(char *fileName)
{
FILE *f;
struct WORKER worker;
if ((f = fopen(fileName, "wb")) == NULL)
return 1;
printf("name: ");
scanf("%s", worker.name);
151

while (strcmp(worker.name, "000"))


{
printf("year: ");
scanf("%d", &worker.year);
printf("earnings: ");
scanf("%f", &worker.earnings);
AddWorker(f, &worker); /* */
printf("name: ");
scanf("%s", worker.name);
}
fclose(f);
return 0;
}
8. .
. CopyFile(), fileNameIn fileNameOut.
#define LENGTH 1024 /* */
int CopyFile(const char *fileNameIn, const char *fileNameOut)
{
FILE *in, *out;
char buffer[LENGTH];
long n;
in = fopen(fileNameIn, "rb");
out = fopen(fileNameOut, "wb");
if (in == NULL || out == NULL)
{
fcloseall();
/* */
return 1;
}
while (n = fread(buffer, sizeof(char), LENGTH, in))
fwrite(buffer, sizeof(char), n, out);
fcloseall();
return 0;
}
152

13.4.
1. . .
2. ,
.
3. s . ,
s .
4. n, 1 n 26. n :
a, ab,
abc ..; n
.
5. .
6. .
7. , .
8.
.
9. ,
<>< >.
, , .
.
10. . ,
.
11. , , . 100 .
,
.
12. , 100 . ,
.

153

13. ,
, .
.
14. ,
m n. k-
.
15. ,
m n. .
16. ,
n n.
.
17. ,
n n. , . .
18. , .
19.
.
20. ,
.
21. .
22. ,
m n,
. .
23. ,
. ,
, . )
,
. ) ,
.
24.
, <>< >. .
154


25. .
.
26. n, 1 n 26.
n n;
k, k = 1, , n, k
( ) ,
* (). , n = 4
A***, AB**, ABC*, ABCD.
27. s . s
.
28. .
.
29.
, .
30. .
31. s.
32. .
.
33. .
34. . ,
5-
( ). .
35. ,
m n. k- .
36. ,
n n.
.
37. .
38. .
39. ,
.
40. .

155

41. ,
, .
42. , , . 100 .
,
.
43. ,
, (
). .
44. : , , , . ) . ) .
13.5.
1. .
2. . , , .
3. .
.
4. . ,
, , .
5. .
6. . , .
7. .
, .
8. , .
, .
156

9. ,
.
10. . ,
( ).
11. . ,
( ).
12. . .
13. , xOy,
. )
,
. )
,
.
14. , .
:
char mark[30];
/* */
unsigned int year; /* */
double mas, speed; /* */
) , . ) . ) . ) .
15. ( ).

void Sdvig(char *fileName, char k),


fileName
k.
. ( char), . k
char. char c,
: c += k.
256.
. 157

. , ,
, .
. ,
,
c -= k .
16. f.
void Vizhener(char *fileName, char *key),
. :
key = k1k2ks s ( ). f k1 ( ),
f k2 .. (s+1)-
k1, , , f s, key. .
17. f.
void Zamena(char *fileName, char k[256]),
. . A char, S(A)
A. k S(A). x1x2xs
f, y1y2ys =
= k(x1)k(x2)k(xs). : x1x2xs = k-1(y1)k-1(y2)k-1(ys), k-1 S(A), k.

18. . , ,
.
19. .
.

158

20. ,
.
21. . , .
22.
.
23. . , , .
24. N (FILE *f[N]), . ,
.
25. , . :
char name[30];
/* */
unsigned int year;
/*
*/
struct SUBJECT mark[10];
/* */
SUBJECT , :
struct SUBJECT
{
char subjectName[30]; /* */
int z;
/*
*/
};
, :
char subjectName[30]; /* */
char name[30];
/* */
) , , , . ) .
26.
: , .
, .
27. : , , , . ) 159

. ) .
28. ,
. , . ) . ) .

160

14.
14.1.
, , .
:

:
struct ELEMENT
{
int data;
/*
*/
struct ELEMENT *next; /* */
};

first , :
struct ELEMENT *first;
.

(NULL), .
,

.

, -

161

, ,
, . head:
struct ELEMENT *head;


, (
).
malloc(), :
void *malloc(int size)
malloc() size. , NULL.
, :
struct ELEMENT *q; /* */
q = (struct ELEMENT *)malloc(sizeof(struct ELEMENT));
( )
sizeof(struct ELEMENT)
. ,
malloc(), , ,
free():
void free(void *p)
free() ,
p. p NULL, .

162

,
free() . , :
int *a;
a = (int *)malloc(sizeof(int));
free(a);
free(a);
/* */
free() . . ,
:
int *a;
a = (int *)malloc(sizeof(int));
free(a);
a = NULL;
free(a);
:
1) ;
2) , ;
3) ;
4)
;
5) ;
6) (, );
7) ;
8) .

163


q ( q
malloc()) , ,
:
(*q).data
(*q).next

q:
q->data
q->next

. , ,
q :
(*(*q).next).data
q->next->data
(*(*q).next).next
q->next->next
struct
ELEMENT, typedef:
typedef struct ELEMENT
{
int data;
struct ELEMENT *next;
} ELEMENT;
ELEMENT, ELEMENT q,
struct ELEMENT q.
14.2.
.
(head), , , ..

164

1. , .
, x.
- head, , - last, :



AddElement(ELEMENT **last, int x)
x,
, - last (
, ,
last,
-).
#include<stdio.h>
#include<malloc.h>
/* */
typedef struct ELEMENT
{
int data;
struct ELEMENT *next;
} ELEMENT;
/*
*/
/* *head , */

165

/* *last */
void CreateHead(ELEMENT **head, ELEMENT **last)
{
*head = (ELEMENT *)malloc(sizeof(ELEMENT));
(*head)->next = NULL;
*last = *head;
}
/* x ,
, last
*/
void AddElement(ELEMENT **last, int x)
{
ELEMENT *q; /* */
/* : */
q = (ELEMENT *)malloc(sizeof(ELEMENT));
/* : */

q->data = x;
q->next = NULL;
/* q ,

*last
:
*/
(*last)->next = q;
*last = q;
/*

*/
}
/* */
void Print(ELEMENT *head)
{
ELEMENT *q;
q = head->next;
while (q != NULL)
{
166

printf("%d ", q->data);


q = q->next;
}
printf("\n");
}
/* x */
/* x,

,
q
*/
void DeleteElement(ELEMENT *head, ELEMENT **last, int x)
{
ELEMENT *q, *t;
q = head;
while (q->next != NULL)
{
if (q->next->data == x)
{
t = q->next;
q->next = q->next->next;
free(t);
}
else q = q->next;
}
*last = q;
}
/* , */
void Distruct(ELEMENT *head, ELEMENT **last)
{
ELEMENT *q, *t;
q = head->next;
while (q != NULL)
{
t = q;
167

q = q->next;
free(t);
}
head->next = NULL;
*last = head;
}
int main( )
{
ELEMENT *head, *last; /* */
int a;
CreateHead(&head, &last); /*
*/
printf("a = ");
scanf("%d", &a);
/* */
while (a !=0 )
{
AddElement(&last, a); /* */
printf("a = ");
scanf("%d", &a);
}
Print(head); /* */
printf("deleted x = ");
scanf("%d", &a);
/* , a */
DeleteElement(head, &last, a);
Print(head);
/* */
Distruct(head, &last);
/*
*/
return 0;
}
2.
, AddElement().
.
168

,
last .
void CreateList(ELEMENT **head)
{
int a;
ELEMENT *q;
/* */
*head = (ELEMENT *)malloc(sizeof(ELEMENT));
(*head)->next = NULL;
q = *head;
printf("a = ");
scanf("%d", &a);
/* */
while (a !=0 )
{
q->next = (ELEMENT *)malloc(sizeof(ELEMENT));
q = q->next;
q->data = a;
q->next = NULL;
printf("a = ");
scanf("%d", &a);
}
}
3. ,
, .

.
last , , ,
,
. Insert().
Print() .
169

#include<stdio.h>
#include<malloc.h>
typedef struct ELEMENT
{
int data;
struct ELEMENT *next;
} ELEMENT;
void CreateHead(ELEMENT **head)
{
*head = (ELEMENT *)malloc(sizeof(ELEMENT));
(*head)->next = NULL;
}
/* x */
void Insert(ELEMENT *head, int x)
{
ELEMENT *q, *t;
q = head;
while (q->next != NULL && q->next->data < x)
q = q->next;
if (q->next == NULL || x < q->next->data)
{
t = (ELEMENT *)malloc(sizeof(ELEMENT));
t->data = x;
t->next = q->next;
q->next = t;
}
}
/* ,

*/
void Print(ELEMENT *q)
{
if (q != NULL)
170

{
printf("%d ", q->data);
Print(q->next);
}
}
/* */
int Sum(ELEMENT *head)
{
ELEMENT *q;
int s = 0;
q = head->next;
while (q != NULL)
{
s += q->data;
q = q->next;
}
return s;
}
/* */
void Distruct(ELEMENT *head)
{
ELEMENT *q, *t;
q = head->next;
while (q != NULL)
{
t = q;
q = q->next;
free(t);
}
head->next = NULL;
}
int main ( )
{
ELEMENT *head;
171

int a;
CreateHead(&head);
printf("a = ");
scanf("%d", &a);
while (a !=0 )
{
Insert(head, a);
printf("a = ");
scanf("%d", &a);
}
Print(head->next);
printf("\n");
printf("sum = %d\n", Sum(head));
Distruct(head);
return 0;
}
4. x. Find x. x
, NULL.
ELEMENT *Find(ELEMENT *head, int x)
{
ELEMENT *q;
q = head->next;
while (q != NULL && q->data != x)
q = q->next;
return q;
}
5. , . ,
x , .
void Insert(ELEMENT *head, int x)
172

{
ELEMENT *q, *t;
q = head;
while (q->next != NULL && q->next->data < x)
q = q->next;
t = (ELEMENT *)malloc(sizeof(ELEMENT));
t->data = x;
t->next = q->next;
q->next = t;
}
6.
.
long MaxPovt(ELEMENT *head)
{
ELEMENT *q, *t;
long max, n;
q = head->next;
max = 0;
while (q != NULL)
{
n = 1;
t = q->next;
while (t != NULL)
{
if (q->data == t->data)
n++;
t = t->next;
}
if (n > max)
max = n;
q = q->next;
}
return max;
}

173

7. , , .
int IsEmpty(ELEMENT *head)
{
return (head->next == NULL);
}
8. , , ( ) , ..
:

, q, q ,
.
(
) NULL. i- .
a) t
, q , t:

) q:

) t q:

.
174

void Reverse(ELEMENT *head)


{
ELEMENT *q, *r, *t;
q = head->next;
t = NULL;
while (q != NULL)
{
r = q->next;
q->next = t;
t = q;
q = r;
}
head->next = t;
}
14.3.
1. .
2. , L : L1 L2
L.
3. , .
4. , ,
, ,
.
5. , L .
6. , , .
7. . .
8. , , , .
175

9. , L1 L2
L, ,
L1, L2.
10. ,
.
11. , , .
12. L1 .
L2 L1
.
13. a1a2...a.
b1b2...b : bk 1 k.

14. , ,
q x,
x () ,
q.
15. , ,
q , , ( ), q.
16. . ,
.
17. ,
.
18. .
.
19. , ,
.
20. , .
176

21.
.
22. ,
. , .
23. a1a2...a,
.
b1b2...b :
bk
1 k.
24. . ,

, <>
< >. .
25. ,
.
,
.
,
.
26. n , . , q k > 0. , q, k-
, ,
, ,
. .
14.4. ,
, ,

, .
177

. ,
. :
1) ;
2) (
);
3) .
. ,
, , , .
,
:
,
, .
1. , , .
#include<stdio.h>
#include<malloc.h>
typedef struct ELEMENT
{
int data;
struct ELEMENT *next;
} ELEMENT;
/* */
void Push(ELEMENT **first, int x)
{
ELEMENT *q;
q = (ELEMENT *)malloc(sizeof(ELEMENT));
q->data = x;
q->next = *first;
*first = q;
}
178

/*
.
first x, .
*/
int Extract(ELEMENT **first, int *x)
{
ELEMENT *q;
if (*first == NULL)
return 0;
*x = (*first)->data;
q = *first;
*first = (*first)->next;
free(q);
return 1;
}
int main ( )
{
ELEMENT *first = NULL; /* */
int i, x;
for (i = 0; i < 10; i++)
Push(&first, i);
while (Extract(&first, &x))
printf("x = %d\n", x);
return 0;
}

. :
1) ;
2) ;
3) .
2.
, .
#include<stdio.h>
#include<malloc.h>
179

typedef struct ELEMENT


{
int data;
struct ELEMENT *next;
} ELEMENT;
/* */
void AddElement(ELEMENT **first, ELEMENT **last, int x)
{
ELEMENT *q;
q = (ELEMENT *)malloc(sizeof(ELEMENT));
q->data = x;
q->next = NULL;
if (*last == NULL)
*first = q;
else (*last)->next = q;
*last = q;
}
/*

*/
int Extract(ELEMENT **first, ELEMENT **last, int *x)
{
ELEMENT *q;
if (*first == NULL)
return 0;
*x = (*first)->data;
q = *first;
*first = (*first)->next;
if (*first == NULL)
*last = NULL;
free(q);
return 1;
}

180

int main ( )
{
int i, x;
ELEMENT *first = NULL, *last = NULL;
for (i = 0; i < 10; i++)
AddElement(&first, &last, i);
while (Extract(&first, &last, &x))
printf("x = %d\n", x);
return 0;
}
14.5.
1. , ,
, ,
.
.
2. n
.
3. , .
.
4. . , .
5. , . ,
.
6. .
.
.
7.
C. , , C, .

181

14.6.

, :

, .
14.7.
1. , () prev. , (
next), ( prev).
2. ,
.
3. , ,
,
, .
4. x q
.
x.
5. ,
, .
182

6. . .
7. x.
x.
8.
C.
, ( ) , C, , C.
9. ,
.
10.
k .
14.8.
, , ,
, , . .

.
. , 183

. . , ,
. .
D, G, H I
.
, , . .
,
, , .
v, u,
u. u i- , - v (i+1)-
. 0,
,
.. -
.
v (),
v.
i- , i.
,

.
,
v ,
v v,
v v. ,
, ,
, .

:
struct ELEMENT
{
int data;

/*
184

*/

struct ELEMENT *left; /* */


struct ELEMENT *right; /* */
};
14.9.
1. n- .

a. .
Node n , nl = n/2
nr = n nl 1 .
#include<stdio.h>
#include<malloc.h>
typedef struct ELEMENT
{
int data;
struct ELEMENT *left, *right;
} ELEMENT;
ELEMENT *Node(long n, int *a, long *pi)
{
if (n <= 0)
return NULL;
else
{
long nl, nr;
ELEMENT *q;
nl = n/2;
/* */
nr = n - nl - 1; /* */
q = (ELEMENT *)malloc(sizeof(ELEMENT));
q->data = a[*pi];
(*pi)++;
185

q->left = Node(nl, a, pi);


q->right = Node(nr, a, pi);
return q;
}
}
/*
*/
/*
*q */
void Distruct(ELEMENT **q)
{
if (*q != NULL)
{
Distruct(&((*q)->left));
Distruct(&((*q)->right));
free(*q);
}
}
/* */
void Print(ELEMENT *q, long n)
{
if (q != NULL)
{
long i;
Print(q->right, n+5);
for (i = 0; i < n; i++)
printf(" ");
printf("%d\n", q->data);
Print(q->left, n+5);
}
}
int main( )
{
ELEMENT *root = NULL;
long i;
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
186

i = 0;
root = Node(10, a, &i);
Print(root, 0);
Distruct(&root);
return 0;
}
Print()
. , .
. ,

:
6
11
8
12
9
10
5
2. A , q , (,
, ..). ,
A. : A(q) = 1,
A, A(q) = 0 . , A, :
187

long Count(ELEMENT *q)


{
if (q == NULL)
return 0;
else return A(q) + Count(q->left) + Count(q->right);
}
,
, A
. :
long NodeCount(ELEMENT *q)
{
if (q == NULL)
return 0;
else return 1 + NodeCount(q->left) + NodeCount(q->right);
}

:
long LeafCount(ELEMENT *q)
{
if (q == NULL)
return 0;
else
{
if ((q->left == NULL) && (q->right == NULL))
return 1;
else return LeafCount(q->left) + LeafCount(q->right);
}
}
3. , :

188

int Balance(ELEMENT *q)


{
if (q == NULL)
return 1;
else
{
if (abs(NodeCount(q->left) - NodeCount(q->right)) > 1)
return 0;
else return (Balance(q->left) && Balance(q->left));
}
}
4. x , .
void AddElement(ELEMENT **q, int x)
{
if (*q == NULL)
{
*q = (ELEMENT *)malloc(sizeof(ELEMENT));
(*q)->data = x;
(*q)->left = (*q)->right = NULL;
}
else
{
/*
q
,
,
*/
if (NodeCount((*q)->left) <= NodeCount((*q)->right))
AddElement(&((*q)->left), x);
else AddElement(&((*q)->right), x);
}
}

189

5. .
Insert x
. q x. ,
count q. q , x, . q x, . q , x .
Display .
#include<stdio.h>
#include<malloc.h>
typedef struct ELEMENT
{
int key;
long count;
struct ELEMENT *left, *right;
} ELEMENT;
void Insert(ELEMENT **q, int x)
{
if (*q == NULL)
{
*q = (ELEMENT *)malloc(sizeof(ELEMENT));
(*q)->key = x;
(*q)->count = 1;
(*q)->left = (*q)->right = NULL;
}
else
{
if ((*q)->key == x)
((*q)->count)++;
else
if ((*q)->key > x)
Insert(&((*q)->left), x);
190

else Insert(&((*q)->right), x);


}
}
void Display(ELEMENT *q)
{
if (q != NULL)
{
Display(q->left);
printf("%d : %d\n", q->key, q->count);
Display(q->right);
}
}
int main( )
{
ELEMENT *root = NULL;
int a;
printf("a = ");
scanf("%d", &a);
while (a != 0)
{
Insert(&root, a);
printf("a = ");
scanf("%d", &a);
}
Display(root);
return 0;
}
6.
, , x
:
/*
*/
int Search(ELEMENT *q, int x)
{

/*
*/
int Search2(ELEMENT *root, int x)
{
191

if (q == NULL)
return 0;
else
if (q->key == x)
return 1;
else
if (q->key > x)
return Search(q->left, x);
else
return Search(q->right, x);

ELEMENT *q = root;
for (;;)
{
if (q == NULL)
return 0;
else
if (q->key == x)
return 1;
else
if (q->key > x)
q = q->left;
else q = q->right;
}

}
7.
x .
:
, x, ;
x ;
x .
, ,
.
,
, . .
. q
. Max q . Max
q. q
Max. Max.

192

ELEMENT *Max(ELEMENT *q)


{
if (q == NULL)
return NULL;
else
if (q->right == NULL)
return q;
else return Max(q->right);
}
void Delete(ELEMENT **q, int x)
{
ELEMENT *t;
if (*q != NULL)
{
if ((*q)->key > x)
Delete(&((*q)->left), x);
else
if ((*q)->key < x)
Delete(&((*q)->right), x);
else
{
if ((*q)->left == NULL)
{
t = *q;
*q = (*q)->right;
free(t);
}
else
if ((*q)->right == NULL)
{
t = *q;
*q = (*q)->left;
free(t);
}
else
{
193

t = Max((*q)->left);
(*q)->key = t->key;
(*q)->count = t->count;
Delete(&((*q)->left), t->key);
}
}
}
}
8.
.
long Height(ELEMENT *q)
{
if (q == NULL)
return -1;
else
{
long i, j;
i = Height(q->left);
j = Height(q->right);
if (i > j)
return i+1;
return j+1;
}
}
9. k-
.
/* level q */
long Count(ELEMENT *q, long level, long k)
{
if (q == NULL || level > k)
return 0;
else
if (level == k)
194

return 1;
else return Count(q->left, level+1, k) + Count(q->right, level+1, k);
}
10. ,
.

, . ObhodTree() . q ,
1) q ;
2) q , ,
, q ,
ObhodTree()
q, ;
3) q .
#include<stdio.h>
#include<malloc.h>
/* */
typedef struct TREE_ELEMENT
{
int data;
struct TREE_ELEMENT *left, *right;
} TREE_ELEMENT;
/* */
typedef struct LIST_ELEMENT
{
TREE_ELEMENT *node;
struct LIST_ELEMENT *next, *prev;
} LIST_ELEMENT;
/* */
void CreateHead(LIST_ELEMENT **head, LIST_ELEMENT **last)
195

{
*head = (LIST_ELEMENT *)malloc(sizeof(LIST_ELEMENT));
(*head)->prev = (*head)->next = NULL;
*last = *head;
}
/* t */
void AddToList(LIST_ELEMENT **last, TREE_ELEMENT *t)
{
LIST_ELEMENT *q;
q = (LIST_ELEMENT *)malloc(sizeof(LIST_ELEMENT));
q->node = t;
q->next = NULL;
q->prev = *last;
(*last)->next = q;
*last = q;
}
/* ( ) */
void PrintList(LIST_ELEMENT *head)
{
LIST_ELEMENT *q;
q = head->next;
while (q != NULL)
{
printf("%d ", q->node->data);
q = q->next;
}
printf("\n");
}
/* */
void DeleteFromList(LIST_ELEMENT **last)
{
LIST_ELEMENT *q;
q = *last;
(*last)->prev->next = NULL;
196

(*last) = q->prev;
free(q);
}
/* */
void ObhodTree(TREE_ELEMENT *q, LIST_ELEMENT *head,
LIST_ELEMENT **last)
{
if (q != NULL)
{
AddToList(last, q);
if (q->left == NULL && q->right == NULL)
PrintList(head);
else
{
ObhodTree(q->left, head, last);
ObhodTree(q->right, head, last);
}
DeleteFromList(last);
}
}
int main( )
{
LIST_ELEMENT *head = NULL, *last = NULL;
TREE_ELEMENT *root = NULL;
/* */
CreateHead(&head, &last);
ObhodTree(root, head, &last);
return 0;
}
.
, , .
, ,
, .
197

/* */
typedef struct ELEMENT
{
int data;
struct ELEMENT *left, *right;
} ELEMENT;
/* */
void PrintRoute(ELEMENT **a, long n)
{
long i;
for (i = 0; i < n; i++)
printf("%d ", a[i]->data);
printf("\n");
}
/* */
void ObhodTree(ELEMENT *q, ELEMENT **a, long *pn)
{
if (q != NULL)
{
a[*pn] = q; /* a */
(*pn)++; /* */
/* ,

*/
if (q->left == NULL && q->right == NULL)
PrintRoute(a, *pn);
else
{
ObhodTree(q->left, a, pn);
ObhodTree(q->right, a, pn);
}
(*pn)--;
/* q a */
}
}

198

int main( )
{
ELEMENT *root = NULL; /* */
ELEMENT **a;
/* */
long n,
/*
*/
h;
/*
*/
/* */
h = Height(root); /* */
/* ,

*/
a = (ELEMENT **)malloc((h + 1)*sizeof(ELEMENT *));
n = 0;
ObhodTree(root, a, &n);
free(a);
return 0;
}
14.10.
1. .
2. , .
3. , .
4. , x .
5.
.
6. , ,
.
7. ,
.
8. , ,
.
9. , ,
.
199

10. T.
T.
11. , , .
12. ,
.
13. .
14. .
15. .
16.
.
17.
, .
18. . , ,
.
19. ,
,
, . .
. . ,
.
:
1) q;
2) q ;
3) q - ,
;
4) q - , ;
5) q.

200

1

a (, ) n. , a
x.
: , . (LinearSearch(), BarrierSearch() BinarySearch()) , x
a.
1.
,
,
x, ( x ).
int LinearSearch(const double *a, const int n, const double x)
{
int i;
i = -1;
while (++i < n && a[i] != x)
;
return i < n;
}
2.
, while (i < n). , x a, while .

201

x. .
int BarrierSearch(const double *a, const int n, const double x)
{
int i;
a[n-1] = x; /* */
i = -1;
while (a[++i] != x)
;
return i < n-1;
}
3.
, ,
.
a . .
l r
a, x:

l r a, l=0
r=n-1, n a.
[l, r]: mid=(l+r)/2. x a[mid] ( l r). : x==a[mid], x<a[mid],
x>a[mid]. x a,
. l, r. , x<a[mid], ,
mid, r, r=mid-1, .
202

int BinarySearch(const double *a, const int n, const double x)


{
int l,
/* */
r,
/* */
mid;
/* [l, r]
*/
l = 0;
r = n 1;
while (l < r)
{
mid = (l + r) / 2; /* [l, r] */
if (a[mid] == x)
l = r = mid;
else
if (a[mid] < x)
l = mid + 1;
else r = mid - 1;
}
return (a[l] == x);
}

203

2

a n
.
1.
. i-
(i = 0, 1, , n-2)
i, i+1, , n-1,
i- ,
.
: O(n2).
void SelectionSort(double *a, const int n)
{
int i,
/*
*/
j,
/*
*/
k;
/* */
double min;
/* */
for(i = 0; i < n-1; i++)
{
k = i; /* */
min = a[i]; /* */
for(j = i + 1; j < n; j++)
if (a[j] < min)
{
k = j;
min = a[j];
}
a[k] = a[i];
a[i] = min;
}
}

204

, ,
.
1.1.
a. a index ( , a) ,
,
a . , a
30, 10, 20, 50, 40,
index :
1, 2, 0, 4, 3.
,
a,
index.
void CreateIndex(const double *a, int *index, const int n)
{
int i, j, k, buf;
double min;
/* index
a:
*/
for(i = 0; i < n; i++)
index[i] = i;
for(i = 0; i < n-1; i++)
{
k = i;
min = a[index[i]];
for(j = i + 1; j < n; j++)
if (a[index[j]] < min)
{
k = j;
min = a[index[j]];
205

}
buf = index[k];
index[k] = index[i];
index[i] = buf;
}
}
1.2.
, , ()
a.
, : i-
(i = 0, 1, , n-2)
i, i+1, , n-1,
i- .
void InterMix(double *a, int n)
{
int i, k;
double buf;
for(i = 0; i < n-1; i++)
{
k = i + rand()%(n - i);
buf = a[i];
a[i] = a[k];
a[k] = buf;
}
}
2.
, ,

206

.
i-1 (i 1) . i-1 , i- , .
i- . i .
a .
i = 1, 2, , n-1.
: O(n2).
void InsertSort(double *a, int n)
{
int i, j;
double buf;
for (i = 1; i < n; i++)
{
buf = a[i];
j = i - 1;
while (j >= 0 && a[j] > buf)
{
a[j+1] = a[j];
j--;
}
a[j+1] = buf;
}
}
3.
.
. , .
, ,
, ,
.
207

flag , , . ,
.
, . , , r.
O(n2), n-1.
void BubbleSort(double *a, const int n)
{
int i, r, flag;
double buf;
flag = 1;
r = n;
while(flag)
{
flag = 0;
for(i = 1; i < r; i++)
if (a[i] < a[i-1])
{
buf = a[i];
a[i] = a[i-1];
a[i-1] = buf;
flag = 1;
}
r--;
}
}

208

4.
. . ,
.
void ShaikerSort(double *a, int n)
{
int l, r, i, k;
double buf;
k = l = 0;
r = n - 2;
while(l <= r)
{
for(i = l; i <= r; i++)
if (a[i] > a[i+1])
{
buf = a[i];
a[i] = a[i+1];
a[i+1] = buf;
k = i;
}
r = k - 1;
for(i = r; i >= l; i--)
if (a[i] > a[i+1])
{
buf = a[i];
a[i] = a[i+1];
a[i+1] = buf;
k = i;
}
l = k + 1;
}
}

209

5.
. x,
, ,
, , ,
. . ,
x, ai > x.
,
x, aj < x.
ai aj .
. QuickSort .
O(n * log2 n).
void QuickSort (double *a, int l, int r)
{
int i, j;
double x, buf;
i = l;
j = r;
x = a[(l+r)/2];
do
{
while (a[i] < x)
i++;
while (x < a[j])
j--;
if (i <= j)
{
buf = a[i];
a[i] = a[j];
a[j] = buf;
i++;
j--;
}
210

} while( i <= j);


if (l < j) QuickSort (a, l, j);
if (r > i) QuickSort (a, i, r);
}
6.
, ,
, .
,
.
a , n .
min max a. a[0],
a[1], ..., a[-1] a {min,
min+1, ..., max},
m = max min + 1. a m .
count m, count[i] min+i a, i = 0, 1, , m-1.
count[0] a,
min, count[1]
a, min+1 ..
count , a. count[0] min, count[1]
min+1 .. a:

O(n+m). , m n,
.
211

void CountSort(int *a, int n)


{
int *count, m, i, j, k, min, max;
count = NULL;
/*
a
*/
min = max = a[0];
for (i = 0; i < n; i++)
if (a[i] < min)
min = a[i];
else if (a[i] > max)
max = a[i];
m = max - min + 1; /* count */
/* count,

*/
count = calloc(m, sizeof(int));
/*
a
*/
for (i = 0; i < n; i++)
count[a[i] - min]++;
/* a */
k = 0;
for (i = 0; i < m; i++)
for (j = 0; j < count[i]; j++)
a[k++] = i + min;
free(count);
}

212


:
1. , . . : . / . . , . . . , 1995. 169 .
2. . .
3. . , / . ,
. . . : , 2001. 736 .
4. , . / . ,
. . . : , 2001. 352 .
5. . /++.
6. , . . / . . . : , 2009. . 1, 2.
7. , . . ++ /
. . . . : , 1999. 288 .
:
8. / . . . ., 1992.
9. , . / . . . :
, 1989.
10. , . . ++
/ . . , . . . :
, 2001.
11. , . . /
. . . . : , 2003.
12. , . . . / . . . ., 2003.
13. , . / . .
. : , 1976-1978. . 1-3.
14. , . / . . . : , 1988.
15. , . / . . . : , 2004.
16. , . /++. / . , . , . . . : , 2001. 640 .
17. , . : / . .
. : , 2004.

213

..
- ..
..
3.03.11.
6084/16. . . . 12,4. .-. . 10,4.
100 . 18 /
-




432017, . , . . , 42

214

Вам также может понравиться