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

..

++:

2009

..

++:

2009

621.3
.. ++:
: . / .. .
: , 2009. 251 .
, C C++.
7 .
.

210300 , 210302 , .
210302 (
), .
. 17. . 30. .: 19 .
..
: (. .
. , . .. );
. .-. ..
c .., 2009

c .


, 2009


, . , , 3-, , .. ,

.
.


++. /++.
, , .
-,
( )
. ++. , , . ,
, .
. ,
.

.
3

, . 1, 3, 4,
6, 10 . , .
/++. 2, 5, 7, 8, 9, 11, 12
, .
,
,
, .
. , [1] [19] .
!!!

1.
1.1.


, ,

: .
,
.

. 1. : 1

. 1.
, 2 , 3
,
, 4 .

.

(). , , .
, ,

() , .
,

( ). .
.
.
,
. , ,

- . .

.
,
.
, ,
. . , ,
.

1.2.

, , ,
.
. ,
.

.
.
: .


(, exe).

, . . ( ,
). .
() .
,

7

, .
(), , . , , ,
. , , . ,
, ,
, , ..
1.3.

,
- . , .
++ [3]: , , .,;:/{}\<>~-=?+^!*%($)#|["]@& ,
. ++ . . .

1.4.

:
1)
.
2) .
3) .
++.
1.4.1.

, . , Windows , Linux , Midnight Commander. ,
c, ++ cpp.

Windows ( , , , ).
Linux .
1) ().
2) Midnight Commander
mc.
3) , , , /home/user/prog.
9

4) Shift+F4.
Midnight Commander .
5) .
6) (F10 Esc) .
, Midnight Commander F4.
1.4.2.
. .
. GCC
G++ ++. .

gcc __,
g++ __,
, . ,

. ,


. Enter. 10

,
.
Windows a.exe,
Linux a.out. gcc g++

-o __,
,
g++ -o programma.exe

main.cpp.

. , .
.

. gcc -,

. ,
g++ -

main.cpp

main.o
. gcc (g++),
, ,
g++ main.o.

11

, .
,
, ,
,
main.cpp:7: error: ...
. , .
1.4.3.
. . . . Linux:
:
./__ Enter.

, ,
, . -, .

12

1.5.

, , , . ,
. .

.
.
1.5.1. Code::Blocks
Code::Blocks . Code::Blocks
Windows, Linux.
. 2. 1 . 2 , 2
3 .
.
Code::Blocks
File, New, Project. , . 3. , . , . Console
application. Go
( ++),
13

. 2. Code::Blocks
.
. 4, . Compiler
( GNU GCC Compiler).
Create Debug configuration Create Release
configuration () . ,
, ..
. 14

. 3.
.
( Project, Properties..., Build targets). , . 4,
Debug Release,
.
Finish Code::Blocks .
.cbp main.cpp.
main.cpp
#include <iostream>
using namespace std;

15

. 4.
int main()
{
cout << "Hello world!" << endl;
return 0;
},
.
!
.
, ( Project,
Properties..., Build targets).
Build, Build Ctrl+F9.
16


, c
obj bin.
. obj ,
bin .
Code::Blocks,
Code::Blocks ( Run
Ctrl-F10 Build and run F9).
1.5.2. Kdevelop
Liunx Kdevelop.
Kdevelop
Code::Blocks. Kdevelop
. 5.
.
Kdevelop , .
,
. 6. ,
, . Simple
Hello World program. , ,
.., .
. 17

. 5. Kdevelop
.cpp
src
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
using namespace std;
18

. 6.

int main(int argc, char *argv[])


{
cout << "Hello, world!" << endl;
return EXIT_SUCCESS;
}.
.
Kdevelop , F8. Kdevelop ,
19

Makefile .
Kdevelop Makefile .
,
c obj bin.
. obj ,
bin .

Kdevelop, Kdevelop ( , , Shift+F9).

20

2. 1


++
:
/++;

, .
2.1.

++

XX . . ++ - . ++ , . ++ .
. ,
.
2.1.1.
(++) .
.
21

#. include
.
, ,
#include <stdio.h>
stdio.h, stdio. h ,
. stdio
, stdio.h
. .
- ,
.
. .
.
, main, . main ,
. , , main. C
main .
, , .
.

,
.
22

#include <stdio.h>
int main(void)
{
printf(!\n);
return 0;
}
int main(void) main. void , main
. int , main .
return
. .
, :
()
{
;
}.

.
printf. ! \n
,

. return
, .
.
,
, 23

[3].
-
. (++) .
, ,
. , .
.
.

. /* */. ++ . // .
.
, , . .
.
, , . , . ().
,
:
int i,j,k;
float x,y; .

24


i,j,k x y. , :
int i=1,j=10,k=500;
float x=3.14,y; .
,
;

1=1,2=2,...; .

. -
. .
,
.
- , , . . . ,
. ,
.
2.1.2.

.
. (++)
, .
25

. , , ..
5 , .
1. char 1 .
.
2. int
2 .
3. float 4 .
4. double . 8
.
5. void ,
, , ,

.
,
: signed , unsigned , long , short .
.

( ).
int, signed short
. ,

int M;
short int M;
26

signed int M;
signed short int M; .
int 2 -32768 32767. unsigned int 2 ,
0 65535.
, , , . long int. 4
-2147483648 2147483647. unsigned long int 4
0 4294967295.
. 1.
1

int
signed int
-32768..
short int
..32768
signed short int

unsigned int
0..65535

unsigned short int

long int
-2147483648

..

signed long int


2147483647

unsigned long int 0..4294967295

27

.
: float,
double long double. , .
. 2.
2

float

3.4 1038
3.4 1038

double

1.7 10308
1.7 10308

long double

3.4 104932 10
1.1 104932

char.

1 . , ,
, : 5, d.

.
signed
unsigned. char singed.
-128 127, . 3.

28

3


char signed char
-128..127
1
unsigned char
0..255
1

0 31 (. ),
, .
(++) ,
. 4.
4

\b
\f
\n
\r
\t
\v
\
\
\\
\0
\a

BS,
,
,



2.1.3.
++
. =, , pi=3.14;. ++ , . 5.
29

5


A=A*B
A = A / (B + 4)
= A %
A=A+B
A=A-B

C++
A *= B
A /= B + 4
A%=B
A += B
A -= B

2.1.4.
( ). .
( ) ( ).
. 6 , ++ .
6
++
&
*
+

~
!
++

*
/
%
<<






( )

: -, : -
: -, : -

()
()

30

>>
&
|
&&
||
=
*=
/=
%=
+=
-=
<<=
>>=
&=
^=
|=
<
>
<=
>=
==
!=

. 6
,



2.1.5.
- stdio.


#include <stdio.h> .
printf:
printf (" ",1,2,...);

31

:
1) , ;
2) . 5;
3) .

1,
2, ... %,
.
printf(" %f\n", pi);
f.
printf %f pi, .
:
%d ;
%f ;
%c ;
%s .
++ -
.

#include <iostream.h>
cout, .
. :
32

cout << "!!!\n";


cout << " " << pi << \n;

//.

<<
.
2.1.6.

scanf:
scanf (" ",1,2,...); .
scanf printf,
. , , & , ,
scanf("%d%f ", &x,&y);

//.

++
cin,
, ,
cout << " ";
cin >> x;
cout << "\n"; //.
2.1.7.
3 : , .
(), , , .
.
33




. , .
. .
.

.
.
( ) ( ) .
, .
#include <iostream.h>
float sum,
//
srok,
//
stavka,
//
dohod;
//
int main(void)
{
cout << " \n";
cout << " :\n";
cout << " () ->";
cin >> sum;
cout << "\n";
cout << " () ->";
cin >> srok;
cout << "\n";
34

cout << " ->";


cin >> stavka;
cout << "\n";
dohod=(sum*stavka/100)*srok/365;
sum=sum+dohod;
cout << "\n";
cout << "-------------------------\n";
cout << ":" << dohod << "\n";
cout << " :" << sum << "\n";
} // .
#include <iostream.h>
iostream
. sum, srok,
stavka, dohod float. main,
int main(void). ,

.
cout << " \n";
cout << " :\n";
. \n.
, ,
.
.
cout << " () ->";
35


cin >> sum;
, .
sum.
cout << "\n";
\n, .
cout << " () ->";
cin >> srok;
cout << "\n";

cout << " ->";
cin >> stavka;
cout << "\n";
//.
srok
stavka .

stavka srok

,
100
365
,
, :
dohod = sum

dohod=(sum*stavka/100)*srok/365;
sum=sum+dohod;
//.
36

:
cout
cout
cout
cout

<<
<<
<<
<<

"\n";
"-------------------------\n";
":" << dohod << "\n";
" :" << sum << "\n";

//.

2.1.8. .

(++) if switch.
if :
if () 1; else 2;

//.

if . , 1,
2. -, if . 7.
. , ,
, .
if:
if () 1;

//.

1,
if . - if . 8.

37

. 7. -

. 8. -
, {. . . }:
{
11;
12;
...
1N:
};
//.
.
38

.
.
, .
:
#include <iostream.h>
#include <math.h>
double a,b,c,p,s;
int main(void)
{
cout << " \n";
cin >> a >> b >> c;
if ((a+b<c)||(a+c<b)||(b+c<a))
cout << " \n";
else
{
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
cout << " " << s << "\n";
};
}
//.
, ,
sqrt math.
2.1.9.

: , ,
( ). 39

. ,
. -
. 9.
. - . 10.
C (C++) .
1) while
while () ;

//.

,
. . ,
, . ,
- . . while . .
.
100 , . .
. max min , . .
40

. 9. -

. 10. -

41

max min , .
, max min.
.
j .
j 1. j 1,
j < 100.
, , . 11 -.
#include <iostream.h>
float x,min,max;
int j;
int main (void)
{
cout << !;
cin >> x;
min=x; max=x;
j=1;
while (j<100)
{
cin >> x;
if (x<min) min=x;
if (x>max) max=x;
j++; // i=i+1
};
cout << << max-min << \n;}

42

. 11. -
43

2) do while
do
{ }
while ();

//.

.
,
, - . . do ... while .
.
.
100 , . .
, .
#include <iostream.h>
float x,min,max;
int j;
int main (void)
{
cout << " !";
cin >> x;
min=x; max=x;
j=1;
do
{cin >> x;
44

if (x<min) min=x;
if (x>max) max=x;
j++;}
while (j<100);
cout << " " << max-min << "\n";
}
- . 12.
3)
. :
For (1;2;3) ;

//.

1-3 .

. 1
. 2 , 3
, ,
for (i=1; i<=100;i++) {...}

//.

.
100 , . .

.
#include <iostream.h>
45

. 12. -
46

. 13. -

47

foat x,min,max;
int j;
int main (void)
{
cout << !;
cin >> x;
min=x; max=x;
j=1;
for (j=2; j<=100; j++)
{
cin >> x;
if (x<min) min=x;
if (x>max) max=x;
};
cout << << max-min << \n;
} //.
min
max , j
2 100. j . -
. 13.
2.1.10.
(. . 7).
[1] [13].

48

7


math
acos
asin
atan
cos
sin
tan
cosh
sinh
tanh
exp
log
log10
pow pow(x,y)=xy
sqrt
ceil
fabs
floor
stdlib
atof float
atoi int
atol long int
rand
abs
labs

2.2.

.
1. , .
2. , .
3. , .
49

4. .
5. -.
6. .
2.2.1.
1.
P Q <1000000
, ,
. ,
1.
2.
1-,5-,10-,50-
. ,
.
3.
,
, 1.
, ,
1000.
4.
, , . , ,
1000.
5.

M(x,y). , , -

50

, . 14.

. 14. 5

6.

M(x,y). , , , . 15.
7.

s ,
r
q
p

S = 3 + 6 + 9 + ... + 96 + 99.
8.
,
S = cos (1 + cos (2 + cos (3 + ... + cos (39 + cos (40)) ...))) .

51

. 15. 6
9.
,
, R > 0 A(x,y).
10.
2 9. ,
.
11.
1+2+3+4+...+N .
,
N (N + 1)/2. N .

52

12.
1 + 3 + 5 + 7 +
... + (2N 1). , N 2 .
N .
13.
2 + 4 + 6 + 8 + ... +
2N . , N (N + 1). N .
14.
12 + 22 + 32 + 42 +
...+N 2 . , N (N + 1)(2N + 1)/6. N .
15.
12 + 32 + 52 + 72 +
... + (2N 1)2 . , N (4N 2 1)/3. N .
16.
13 + 23 + 33 + 43 +
...+N 3 . , N 2 (N +1)2 /4.
N .

53

17.
13 + 33 + 53 + 73 +
... + (2N 1)3 . , N 2 (2N 2 1). N .
18.
14 + 24 + 34 + 44 +
...+N 4 . , (N 2 + N )(2N + 1)(3N 2 +
3N 1)/30. N .
19.

X
i=1

i2 = 1 +

1
1
1
+ 2 + 2 + ...
2
2
3
4

,  = 104 , .
2 /6. .
20.

X
1
1
1
(1)i1 i2 = 1 2 + 2 2 + ...
2
3
4
i=1

54

,  = 105 , .
2 /12. .
21.

(2i 1)2 = 1 +

i=1

1
1
1
+ 2 + 2 + ...
2
3
5
7

,  = 104 , .
2 /8. .
22.

X
(1)i+1
i=1

2i 1

=1

1 1 1
+ + ...
3 5 7

,  = 104 , . /4. .

55

23.

sin(x)

X
x3 x5
x2i1
sin(x) =
(1)i1
=x
+
...
(2i 1)!
3!
5!
i=1

x, . ,  = 104 .
24.
ex

X
xi
x2 x 3
x
e =
=1+x+
+
+ ...
i!
2!
3!
i=0
x, . ,  = 103 .
25.

cos(x)
cos(x) =

X
i=1

(1)i

x2i
x2 x4
=1
+
...
(2i)!
2!
4!

x, . ,  = 104 .
56

26.

sh(x)
sh(x) =

X
i=1

x2i1
x3 x5
=x+
+
+ ...
(2i 1)!
3!
5!

x, . ,  = 103 .
27.

ch(x)

X
x2 x4
x2i
=1+
+
+ ...
ch(x) =
(2i)!
2!
4!
i=0

x, . ,  = 103 .
28.

sin(x)/x

x 2 x4
sin(x) X
x2i
=
(1)i
=1
+
...
x
(2i
+
1)!
3!
5!
i=0
x, . ,  = 104 .
57

3.

3.1.

, . , . , ,
, ,
. , ,
.
.
,
. . ++ . .
, .

. , , void

58

.
3.1.1.

( )
{
;
} //.
, .
, ,
int.
,

. ,
. , , .
:
float diskr(float a, float b, float c)
void podprogramma() //,
int prog1() //.

//,

()
return. . return
,
. return , , .
59

,
return. .
3.1.2.
() , ,
.
.
, .
: , . ,
.
, , .
: .
. ,
. ( ) .
, . , .
, ,
.
, .
, - . 60

. .

:
,
.
3.2.


. ,
.
. ,
. , . :
*_;

//.


(++) . , . * , . :
char *ch;
int *temp, i, *j;
float *pf, f;
//.

61

ch,temp,j,pf, i
int f float.
: &
*. , . .
, . & () .
* () .
, AND ,
, .
, ,
. :
# include <iostream.h>
int main (void)
{
//
float x= 12.3, ;
// p
float *p;
//
=&;
//
// ,
//
=*p;
//
cout << "x=" << x << " y=" << y << endl;
62

// 1 ,
//
(*)++;
//
cout << "x=" << x << " y=" << y << endl;
// 1 ,
//
=1+(*)*;
//
cout << "x=" << x << " y=" << y << endl;
} // .
, , :
# include <iostream.h>
int main(void)
{int x=12;
int *p, *g;
p=&x;
g=p;
printf("%p\n",p);
printf("%p\n",g);
printf("%d\n%d\n",x,*g);
} // .
% printf(), .
void, . void
. 63

void. , :
void *pv;
float f, *pf;
pf=&f;
pv=pf;
pf=(float*)pv; // .
pv (float*),
float.
.
(int *), , , ,
=&. , : p=(int*)&x;
.

: .
. :
#include <stdio.h>
int main(void)
{
int *p;
int x=12;
=&;
printf("%p\n%p",p,++p);
} //.
64

,
++ 2,
1. ,
,
( 2 ). , ++
,
.
. 4000 .
=+5; 4010.
=+n; :
=+n*_____.
.
.
, . . ,
, . .
. 6 . p<g " ,
, , g.
, [4].
:
int **point;

//.

point int. , , point, **point. :


65

#include <stdio.h>
int main(void)
{
int i;
int *pi;
int **ppi;
i=12;
pi=&i;
ppi=&pi;
printf("i = %d pi = %p ppi = %p
**ppi = %d\n",i,pi,ppi,**ppi);
} //.
,
, . . ,

NULL. .
3.3.

. ,
. ,
.

. , . , 66

, . ,
swap(), , :
void swap(int a, int b)
{
int tmp=a; a=b; b=tmp;
} //.
. , ,
. . .
.
, . swap() :
void swap(int *a, int *b)
{
int tmp=*a; *a = *b; *b=tmp;
} //.
x y swap(&x,&y).

.
swap()
void swap(int &a, int &b)
{
int tmp=a; a = b; b=tmp;
} //.
67

x y:
swap(x,y). [2].
3.4.


. new:
= new ;
,
int *p; //
p = new int; // .
new , ,
.
,
*p = 155; // .
delete:
delete ;
,
delete p; // .
, ,
, .
68

3.5.

, .
, N
.
int factorial(int n)
{
int a;
if (n==1) return 1; //
a = factorial(n-l)*n; //
return a;
} //.
,
. , . , , . . ,
, .

69

4.

4.1.

. , . .
, . . .
,
. 34 . func.cpp

float fun_dohod(float sum, float srok, float stavka)
{
return (sum*stavka/100)*srok/365;
} //.
func.h ,
fun_dohod.
float fun_dohod(float,float,float); //.

.
main.cpp main,
fun_dohod

70

#include <iostream.h>
#include "func.h"
float sum,
//
srok,
//
stavka,
//
dohod;
//
int main(void)
{
cout << " \n";
cout << " :\n";
cout << " () ->";
cin >> sum;
cout << "\n";
cout << " () ->";
cin >> srok;
cout << "\n";
cout << " ->";
cin >> stavka;
cout << "\n";
dohod=fun_dohod(sum, stavka, srok);
sum=sum+dohod;
cout << "\n";
cout << "-------------------------\n";
cout << ":" << dohod << "\n";
cout << " :" << sum << "\n";
} //.
fun_dohod func.cpp #include "func.h".

71

g++ - func.cpp
g++ - main.cpp.
func.o main.o. nm.
nm func.o
, fun_dohod.
...
00000000 T __Z9fun_dohodfff
, func.o . , .
nm main.o
main.o.
...
U __Z9fun_dohodfff
...
000000fe
00000004
00000008
00000000

T
B
B
B

_main
_srok
_stavka
_sum

main 000000fe, fun_dohod ,


72

. ,
, .

nm a.exe
,
...
00401640 T __Z9fun_dohodfff
...
004013ee T _main
...
.
4.2.


:
. , , , , .
.
make.
,
Makefile . Makefile

73

: , . ,
.
- ,
.
: , . , , ( )
.
(- ) .
, , . , . Makefile.
# Makefile
prog:

main.o func.o
g++ -o prog.exe main.o func.o

main.o: main.cpp
g++ -c main.cpp
func.o: func.cpp
g++ -c func.cpp
clean:
rm -f

*.o prog.exe

: prog, main.o, func.o, clean.


74

prog
prog.exe.
main.o func.o, .
prog ()
g++ -o prog.exe main.o func.o.
. main.o func.o . , . , clean .

prog.exe. Linux.
make Windows Linux,
rm.
Makefile make ,
make prog

make clean.
make ,
.
, ,
Makefile. 1.5.1 1.5.2
Makefile.
75

5.

:
++;
.
5.1.

(, ,
..), .
, .
: , , .
:
__

_[];

,
int a[100],b[100],c[100],d[100];
4 a,b,c d. ,
0 99.
, 0. :
int x[10] = {20, 2, 5, 89, 7, 5, 3, 6, 1, 4};

76

5.1.1.

, (
) . : M[5] ; M[k] k;
M[k1+5] k1+5; M[abs(i)] .
,
mas[2]=34;
mas 2
34.
cout << mas[5] << endl;
mas
5.
. , :
int x[10], y[10];

:
x=y; //.
. .
, .
.
77

for
.

for (i=1;i<=25;i++) mas[i]=0;
.

. , ,
. .
, .
.
t0 ... t30. . .
#include <iostream.h>
int main(void)
{
float t[31];
//
int i;
// for
float s;
//
for (i=0;i<=30;i++)
//
{
cout << " " << i;
cin >> t[i];
};
s=0;
//
78

for (i=0;i<=30;i++) s=s+t[i]; //


cout << " " << s/31;
} // .
5.2.

. ,
, ,
. ,
int t[31]; t ,
pl=t; pl=&t[0]; .
8- t
t[7] *(t+7).
. ,
, .

#include <iostream.h>
int main(void)
{
float t[31];
//
int i;
// for
float s;
//
for (i=0;i<=30;i++)
//
{
cout << " " << i;
cin >> *(t+i);
};
s=0;
//
79

for (i=0;i<=30;i++) s=s+*(t+i); //


cout << " " << s/31;
} // .
5.3.

, . , , :
cout << " :\n";
for (i=0;i<=99;i++)
{
cout << "x1[" << i << "]=";
cin >> x1[i];
}; //.
.
.
rand().
rand() stdlib
0
RAND_MAX, .

. srand(unsigned long int), rand(). srand()
c .
80


, .
srand() . , , , srand(time(NULL)). time() time. , 0 N

#include<stdlib.h>
#include<time.h>
...
srand(time(NULL));
for (i=0;i<=99;i++)
x1[i]=rand()%(N+1);

//
//
//


[0, N ]. [a, b],
:
x1[i]=a+(b-a)*1.0*rand()/RAND_MAX; //.
rand()/RAND_MAX [0, 1].
C (C++)

. 1.0. , .
81

y = a + (b a)x x [0, 1] y [a, b].


5.4.

, , , .
-100 100, .
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int m[30];
//
int i;
// for
int n;
//
srand(time(NULL));
//
for (i=0; i<=29;i++)
//
m[i]=rand()%201-100;
n=0;
//
for (i=0; i<=29;i++)
if (m[i]>0)
//
n++;
// 1
cout << << n << -;
return 0;
} // .
, n 82

. . n .
5.5.

, ()
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
float m[30]; //
int i;
// for
float max;
//
int t;
// () .
srand(time(NULL)); //
for (i=0; i<=29;i++)
//
m[i]=10.0*rand()/RAND_MAX;
// , 1-
max=m[0];
t=0;
for (i=1; i<=29;i++) //
if (m[i]>max)
//
{
max=m[i];
83

t=i;
};
cout << " = " << max;
cout << " " << t;
return 0;
} // .

. 16. -

. max ,
,
t. ,
84

( 0) : max=m[0]; t=0;. max.


,
max ,
t . . 16
- .

,
. .
5.6.

()

,
..
() . , .. .


.
, .
.
x n
. ,

x[0] < x[1] < ... < x[n 1].
.

85

.
,
,
, ..
, :
1) ;
2) ;
3) ;
4) ..

,
.
. i..n. i
1, 2, 3 . n-1. t
i. x
y :
1) v=x; x=y;
y=v;
2) x=x+y;
y=x-y; x=x-y; .
. 17 -
.

86

i=0, n-2, 1


m[i]..m[n]
min=m[i]; t=i

m[t] m[i]

k=i+1, n-1

m[k]<min

min=m[k]; t=k

. 17. -

.
. , . ,
, ..
. . 18 - (m 0, n -
87

).

. 18. -


5 2 7 4 3 1 8 6.
5 2, 7 4, 7 3, 7 1, 8 6:
2 5 7 4 3 1 8 6,
2 5 4 7 3 1 8 6,
2 5 4 3 7 1 8 6,
88

2 5 4 3 1 7 8 6,
2 5 4 3 1 7 6 8.

8. 5 4, 5 3,
5 1, 7 6:
2
2
2
2

4
4
4
4

5
3
3
3

3
5
1
1

1
1
5
5

7
7
7
6

6
6
6
7

8,
8,
8,
8.

4 3, 4 1:
2 3 4 1 5 6 7 8,
2 3 1 4 5 6 7 8,
3 1:
2 3 4 1 5 6 7 8,
2 1 3 4 5 6 7 8,
, , 2 1:
1 2 3 4 5 6 7 8.
5
.
,
n 1.
.
, . (C. Hoare), . (,
89

. 19. -
90

), . , (
) , ,
, .
- . . .
.

.

, , .
, .
. ,
.
,
, , . -
. 19.
Left ,
Right , . 91

. 20. , 1

92

:
, J
Y , . K J ,
(K + J)/2 . , , ,
. 20, Y = 5.
,
. 1 K , m[K] Y . J 1 ,
m[J] Y . , . 20,
m[1]=8 m[8]=0. K J,
,
1 K 1 J. K J, 1 K
, m[K] Y ,
J 1 ,
m[J] Y . , . 20,
m[3]=9 m[6]=3.
, : m[4]=5 m[5]=1.
, J K. . 20 J=4, K=5. J K 2
[Left, J] [K, Right].
.
. 21
.
( 0,1,2; 3,4; 5,6; 7,8.9),
.
93

. 21. , 2

, ,
. , (
), 8 7 8 9 8, ,
94

m[Left]=m[Right]=Y.
Y = m[(K + J)/2]
. .
5.7.

,
. C

, . , , n , :
int n;
cin >> n //
...
int *massiv = new int[n]; // .
massiv
n .
,
massiv .

*(massiv+k), massiv[k].
,
. .

.
95

, , .
,
,
.
g mas,
, g+1 , 1 . :
float mas[100];
//
int i;
// for
int n;
//
..............................
for (i=g; i<=n-1; i++) mas[i]=mas[i+1];
n--;
// n 1.
g
.
g
,
1.
g:
for (i=n-1; i>=g; i--) mas[i+1]=mas[i];
mas[g]=...
n++;
// n 1
5.8.

, ( ) . 96

, float x2[10][20] , 10 20 .
.
float x3[10][20][5] 1000 .
2.
, . , . , . C++
, .

( , ). .
for,
for (i=0; i<=9; i++)
for (j=0; j<=19; j++)
A[i][j] = 0;
//.
.
1.
( ) .
#include <iostream.h>
97

#include <math.h>
int main(void)
{
int p[9][9];
int i, j;
for (i=0; i<=8; i++)
//
for (j=0; j<=8; j++)
//
p[i][j]= (i+1)*(j+1);
for (i=0; i<=8; i++)
{
for (j=0; j<=8; j++) cout << p[i][j] << "\t";
cout << endl; // .
}
}
2.
b[10][10] , [-10,10].
, k.
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int b[10][10];
int i, j, k;
srand(time(NULL));
for (i=0; i<=9; i++)
{
for (j=0; j<=9; j++)
98

{
b [i][j]=rand()%21-10;
cout << b[i][j] << " ";
};
cout << endl;
};
cout << " k";
cin >> k;
for (i=0; i<=9; i++)
for (j=0; j<=9; j++)
if (b[i][j]>k) cout << b[i][j] << "\t";
cout << endl;
} //.
3.
10 10,
[1,100].
, .
,
. , , ,
.
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
float A[10][10];
int i, k;
99

float S;
S=0;
srand(time(NULL));
for (i=0; i<=9; i++)
{
for (k=0; k<=9; k++)
{
A[i][k]=(99.0*rand()/RAND_MAX)+1.0;
cout << A[i][k] << " ";
if (k>i) S=S+A[i][k];
};
cout << endl;
};
cout << "
" << S;
} //.
5.9.

, . 10 int *x[10];.
. ,
: [4]=&;.
, ,
*[4].
C . 10 p:
int *p[10];

100

C
:
for (i=0; i<=8; i++)
{p[i]=new int[20];}

//
// .

. p ,

. p[k]
*(p+k)
k. *(p[k]+m) *(*(p+k)+m) m k, , k m.
, ,
.
5.10.

.
1.
2.
3.
4.
5.
6.

, .
, .
, .
.
-.
.

101

5.10.1.
1.
1. , .
.
2.
. [1,1] 1-
, 2- , 1- , .. , () 5 . , .
2.
1. ,

. .
2. a[n][m]
, a[i][j] = a[i][j] * arccos(i / (i + j)).
. .
3.
1. d[n] d[i] = exp(i/d[i]).
, 3.
2. a[m][n]
102

a[i][j] = ij , i j a[i][j] = j i
. m[3][3],
.
4.
1. a[n] f[n],
f [i] = ln(a[i])/arctg(ei/10 ).
, .
.
2.
5 .
5.
1.
, 5- ,
6- .
2. b[n][m] 4 , .
,
b[0,0].
6.
1. ,
. ,
.
2.
.
.
103

7.
1. .
2. 33,
. .
8.
1. .
. .
2. a[m][n]
f[m*n] ,
, , .
9.
1. .
2. , .
10.
1. ,
, . .
2. , 3
7.

104

11.
1. , .
2. , . ,
, , . .
12.
1. . .
2.
33.
,
[1][1].
13.
1. 55. ,
.
2.
20 .
14.
1. 55. .
2. 20 .

105

15.
1. N
N . , .
2. N .

. ,
.
16.
1. .
, , .
2. .
17.
1. N M , ,
, .
2. 90 .
18.
1. , N . ,
?
2. , , .. ( ) .

106

19.
1. , N .
.
2. [10][10]
.
20.
1. , N . ,
.
2. [m][n], ,
3,2. .
21.
1. , N .
, .
2. ( )
1
1
1
1
1
1
1

0
1
2
3
4
5
6

0
0
1
3
6
10
15

0
0
0
1
4
10
20

0
0
0
0
1
5
15

0
0
0
0
0
1
6

0
0
0
0
0
0
1

0
0
0
0
0
0
0

22.
1. [15].
.
107

2. S[m][n] .

.
23.
1. [m]
, .
2. [m][n]. .
.
24.
1. [10]. ,
.
2. [m][m].
, .
.
25.
1. [n],
. .
2. [m][m].
, .
.

108

26.
1. [K],
. ,
, ,
.
2.
, .
27.
1. Y[n],
. , ,
0, .
2. .

109

6.
-

6.1.

. . . , , , .
: ,
, , ,
.. .
. :
struct {
1 1;
2 2;
...
}; //.
, ,
. , ,
, , .
struct student {
char fio[30];
int kurs;
char group[7];
110

};

//.

, .
, . , .
. student,
student stud1, stud2; //.
stud1 stud2. .
.
student *p;

new:
p = new student; //.
, ,
kurs stud2,
cout << stud2.kurs;

//.

,
(*p).kurs.
111

p->kurs kurs ,
p.
,
.
( ) .
.
6.2.

, ( ) (). , .

class {
1 1;
2 2;
...
1
2
...
};
//.
private, public, protected.
public ,
, private , , protected
, .
112

.
.
float
class CComplex {
public :
float x;
float y;
}; //.
, .
class CComplex {
public :
float x;
float y;
float Modul(void)
{return sqrt(x*x+y*y);}
float Argum(void)
{if (x==0) return 3.14159/2;
else return atan(y/x);}
}; //.

. . ,
. , ,
class CComplex {
public :
113

float x;
float y;
float Modul(void);
float Argum(void);
};
complex.h.
complex.cpp :
#include "complex.h"
float CComplex::Modul(void)
{return sqrt(x*x+y*y);}
float CComplex::Argum(void)
{if (x==0) return 3.14159/2;
else return atan(y/x);} //.
,
. ,
. . ,
CComplex A;

//.

. . -

, . . , , ,
114

. . . . ,
. ,

. , .
, . a b. x, y.
move,
. :
class TPoint {
public :
int a;
int b;
void move(int x,y);
};
TPoint::move(int x,y)
{a=x; b=y;} //.
, .
int visible. visible=0, , . show hide.
115

class TVisiblePoint
public :
int visible;
void show(void);
void hide(void);
};

: public TPoint {

TVisiblePoint::show()
{visible = 1;}
TVisiblePoint::show()
{visible = 0;} //.
TVisiblePoint TPoint.
public , TVisiblePoint
TPoint.
, , ,
. , TVisiblePoint, ,
. ,
.

class TColorVisiblePoint
public :
int color;
void setcolor(int c);
};

: public TVisiblePoint {

TColorVisiblePoint::setcolor(int c)
{color = c;} //.
116

setcolor()
. .
TColorVisiblePoint

ExamplePoint;

:
ExamplePoint.x=10;
ExamplePoint.y=10;
ExamplePoint.show();
ExamplePoint.move(50,50);
ExamplePoint.setcolor(5);

//
//
//
//
//

,
-
. ,
. ,

. ,
. ,
.
6.3.

, . ,
. . , .
TColorVisiblePoint
117

TColorVisiblePoint::TColorVisiblePoint(int X,Y,C)
{x=X; y=Y; color = C;} //.
.
, :
//
TColorVisiblePoint *ExamplePoint =
new TColorVisiblePoint(10,10,5);
ExamplePoint->show();
//
ExamplePoint->move(50,50); // .
, . -,
, ~.
, .
.
6.4.

, ,

-
,
- . , .

. , , (public) (private

118

protected). , . , . , ,
. , , .
, .
, ,
.
, .
, .. -
.
. , .
.
, .

.
,
[6] [9].

119

7. 3
,
:
;
.
7.1.

7.1.1.

. .
. char
. (string). ,
char.
char, .
\0.
. ,
N , N+1 . ,
int str[12], , 11 , .
,
.
, . , " ". \0.
120

, CodeBlocks :
... C o d e B l o c k s \0 ...
. scanf()
%s. ,
scanf() .
gets(), stdio.h. gets()
, . Enter.
.
.

printf() puts(). . puts()
.
printf()
. :
# include <stdio.h>
//
int main(void)
{
char str[80];
printf(" 80 ");
gets(str);
printf(" %s\n",str);
printf(" ");
scanf("%s",str);
printf(" ");
puts(str);
} //.
121

, string.h.
.
strcpy(sl, s2) s2 s1. s1
,
s2. , , ,
, .
strcat(sl, s2) s2
s1 , s1, s2
. , s1,
s2.
strcpy(), strcat() .
:
# include <stdio.h>
# include <string.h>
int main(void)
{
char s1[20], s2[20];
strcpy(sl," Hello,");
strcpy(s2," World !");
puts(sl); puts(s2);
strcat(sl,s2);
puts(sl);
}

//
// s1="Hello,"
// s2="World !"
// Hello, World !
// s1="Hello, World !"
// Hello, World !
//.

strcmp() strcmp(sl, s2).


s1 s2 , , . .
122

. . s1
s2, strcmp() , .
strlen(s) s,
. ,
strlen("hello") 5.
7.1.2.
, .
.
, . , [4]:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
//
char *ext[]={"exe","com","dat","c","pas","cpp"};
char ch, s1[80];
for( ; ; ) //
{do
{
//
printf(" :\n");
printf("l. exe\n");
123

printf("2. com\n");
printf("3. dat\n");
printf("4. c\n");
printf("5. pas\n");
printf("6. cpp\n");
printf("7. Exit\n");
printf(" : \n");
// 1-7
ch=getche();
printf("\n"); }
// 1-7
while ((ch<1)||(ch>7));
//
if ( ch == 7 ) break;
strcpy(s1,"dir *.");
strcat(s1,ext[ch-49]);
system(s1);
}
} //.
dir (
) system().
, .
.

. . 1 7 49 ... 55. , 49
exe
0.
124

cpp ,
6, 54. 54 49 ext.
[4]:
char *errors[]= {"Cannot open file",
"Cannot close file", "Allocation error",
"System error" }; //.
, ,
. ,
.
7.1.3. string ++
++ string, .
string ,
.
#include <string>
string
string stroka( " \n" );

string *stroka = new string( " \n" );
125

//.


. , .

stroka = " \n";

//.


cout
cout << stroka;

//.

printf ,

printf(" %s", stroka.c_str());

//.

c_str() .
, ,
stroka[5]=!;

//.


string. :
begin()
,
end() , ,
append()
,
at() , ,
126

c_str()
,
clear() , ,
compare() ,
data() ,
empty() ,
erase() ,
find()
,
find_first_not_of()
, ,
find_first_of() , ,
find_last_not_of()
, ,
find_last_of() ,
,
getline ,
insert(, )
,
length
max_size ,
rfind ,
size .
127

replace
( ).
.
replace(stroka.begin(),stroka.end(),5,*);

//.

5 .
7.2.

7.2.1.
. stdio ,
, .
FILE,
. ,
f FILE.
FILE *f;

//.

. fopen(). fopen()
FILE . fopen()
. ,
. ,
. r (read) , . w (write)
. ,
128

. a (append) . , . t b
.
.
FILE
f1 =
f2 =
f3 =

*f1, *f2, *f3;


fopen("temp.txt", "rt");
fopen("z:/Prog/file.cpp", "w");
fopen("z:\\Prog\\file.cpp", "at");

temp.txt , .
file.cpp .

/, ( ) .
, ,
,
fopen() NULL. :
#include <stdio.h>
...
FILE *f = fopen("file.txt", "rt");
if (f == NULL)
{ printf(" !!!"); }
...
c , . printf()
perror()
129

perror(" !!!"); //.



!!!, .
7.2.2.
-
. , , , (
FILE *). ,
. . ,
EOF.
, ,
. ,
int 4 . .
, ,
.
( ), .
() .
,
.

130

7.2.3.
.
fread(), 4 : , , , , . fread()
. ,
, 512
float. mas, , kolvo
unsigned int kolvo;
float mas[512];
FILE *f; //.

kolvo = fread(mas, sizeof(float), 512, f);
sizeof(float) , kolvo , . 512 , .
.
fscanf().
.
fscanf() scanf(),
131

scanf ( ,
" ",1,2,...); //.
fscanf():
int K, kolvo;
float X, Y;
char C;
char S[80];
FILE *f;
. . .
fscanf(f,
fscanf(f,
fscanf(f,
fscanf(f,
fscanf(f,
. . .

"%d", &K);
"%f", &X);
"%c", &C);
"%s", S);
"%f%f", &X, &Y);

kolvo ,
.
7.2.4.
.

fwrite(). 4 :
,
, , , . fwrite() . , ,
512 float mas
unsigned int kolvo;
132

float mas[512];
FILE *f; //.

kolvo = fwrite(mas, sizeof(float), 512, f);

//.

sizeof(float) , kolvo ,
. 512 ,
.
.

fprintf(). . fprintf() printf(),
.
printf ( ,
" ",1,2,...);

//.

:
1) , ;
2) . 5;
3) .

1,
2, ... %,
.
133

fprintf(f," %12.3f\n", y);


f. ,
printf() %12.3f
y, .
y 12
, 3 .
7.2.5.

.
fclose(), ,
fclose(f);

//.

,
-1.

if (fclose(f) < 0)
{
printf(" !!!");
} //.
7.2.6.
.
.
fseek().
:
134

FILE *f, , , . , : SEEK_CUR=1


,
SEEK_SET=0 ,
SEEK_END=2 . :
fseek(f, 0, SEEK_SET);
fseek(f, -8, SEEK_END);
fseek(f, 5, SEEK_CUR); //.
,
,
5 .
ftell() , ,
N=ftell(f);

//.

feof() ,
. , .
7.2.7. -
.
.

, .
.
.

135

. FILE * (
).
, : *stdin , ; *stdout
, ; *stderr
, .
,

printf("");
fprintf(stdout,"");

//.

7.2.8. -
-
stdio . .
remove filename
int remove(char *filename);

//.

rename old new


int rename(char *old, char *new);
. 8 - .

136

8
-
fgetc
fgets
fputc
fputs
getc
putc
putw
getw
gets
getchar
putchar
puts


C A, int fgetc(FILE *A)
s n
A, char *fgets(char *s, int n, FILE *A)
A, int fput(int , FILE *A)
S A, int fputs(char *S,
FILE *A)
C A , int getc(FILE *A)
A, int putc(int , FILE *A)
A W, int putw(int W, FILE
*A)
A , int getw(FILE *A)
stdin
S stdin, char
*gets(char *S)
stdin, int getchar(void)
stdout
stdout, int putchar(int )
S stdout, int puts(const char *S)

7.2.9. -
++ - -.
C++ iostream , -. ios. istream ostream .
iostream. cin istream cout, cerr, clog
ostream. >>. -

137


<<.
-
istream, ostream iostream
ifstream, ofstream fstream . . , . cout
hex,
cout << "N=" << hex << N << endl; //.
oct dec.
precision(m), m .
m=0 (
). -
[1] . 630 672.
- fstream. ifstream

ifstream fileIn("d:\\text\\fileIn.txt");

//.

, ofstream
ofstream fileOut("d:\\text\\fileOut.txt");

//.

fileIn fileOut cin cout, ,


138

for(k=1;k<=100;k++) {fileOut << x[k];}

//.

, , () .
ios::app , ios::ate
,
ios::trunc ( ), ios::nocreate ,
, ios::noreplace ,
. , ,

ifstream fileOut("d:\\text\\fileOut.txt", ios::app);

close().

[2] . 505542.
7.3.

.
1.
2.
3.
4.
5.
6.

, .
, .
, .
.
-.
.

139

7.3.1.
1.

xn = xn1 /2 + a/2xn1 , x1 = 1,
x a.
, .
2.
,
.
. .

.
3-18.
. : , , .
.
3. , .
4. , .
5. ,
,
.
6. ,
, .
7. ,
.
8. ,
140

.
9. ,
.
10. , .
11. ,
.
12. ,

.
13. ,

.
14. ,
, .
15. ,
, .
16. ,
, .
17. ,
,
.
18. ,

.
19-27.
.
: , , , .
141

.
19. , (
).
20. ,
.
21. ,
.
22. ,
.
23. ,

.
24. ,
,
.
25. ,
.
26. ,
, .
27. , ,
.

142

8. 4


:

;
PDCurses;
;
.
- .
, , . , ,
, , .
8.1.

PDCurses

Unix- ncurses.
Windows PDCurses.
,
GCC. ,
GCC Windows MinGW : c:\MinGW, c:\Program Files\MinGW
c:\Program Files\CodeBlocks\MinGW. PDCurses,
143

#include <curses.h>
,
:
chtype , PDCurses
( , );
bool (
FALSE TRUE);
SCREEN ,
( , ; Windows
,
Alt+Enter);
WINDOW , ( ).
PDCurses :
(screen), (window) (sub-window).
, . .
PDCurses :
...
#include <curses.h>
...
initscr();
pdcurses
endwin();
...
144

initscr() ,
.
stdscr, .
PDCurses. endwin().
#include <curses.h>
int main(void)
{
// (
// PDCurses)
initscr();
// y=10 x=30
move(10,30);
printw("Hello world !!!"); //
refresh(); //
getch(); //
endwin(); // PDCurses
} //.
:
move(y,x) ,
, , Y
;
printw("")
;
refresh() .
PDCurses
, ,
, ,
refresh().
145

, ncurses Unix- . Windows pdcurses.a . , CodeBlocks, Settings, Compiler


and debugger Linker settings Link libraries ( C:\Program
Files\CodeBlocks\MinGW\PDCurses\win32\pdcurses.a).
8.2.

PDCurses

-, (++) PDCurses. , PDCurses initscr()



stdscr. , PDCurses.
8.2.1.
stdscr :
printw() printf();
addch(chtype X) putchar
X
.
,
.

146

insch(chtype X) X ,

.
addchnstr(chtype *S, int n) n S .
chtype *S
chtype. n=-1, .
addstr(char *S) S
. S
char.
insnstr(char* S, int n) n
S , ,
. n=-1, .
insertln() .
chtype char (A_BLINK), (A_BOLD), (A_NORMAL), (A_DIM), (A_UNDERLINE), (A_REVERSE), . ,
chtype C=a|A_BOLD;
a
.
8.2.2.
. has_colors() 147

, . , .
start_color() . . 0 . 1
. :
COLOR_BLACK ,
COLOR_RED ,
COLOR_GREEN ,
COLOR_YELLOW ,
COLOR_BLUE ,
COLOR_MAGENTA ,
COLOR_CYAN ,
COLOR_WHITE .
init_pair().
, , :
// 1
init_pair(1, COLOR_RED, COLOR_WHITE);
// 2
init_pair(2, COLOR_YELLOW, COLOR_BLUE);
..
.
,

chtype C=a|COLOR_PAIR(1);

//.

a
1 .
148

, .
.
int attron(int A) A.
, , , PDCurses. . ,
2
, COLOR_PAIR(2),
A_BOLD
attron(COLOR_PAIR(2));
attron(A_BOLD);
printw("!!!");
int attroff(int A) A.
void bkgdset(int A) .
clear().

, .

.
#include <curses.h>.
int main(void)
{
//
initscr();
//
start_color();
149

//
// 1
init_pair(1, COLOR_RED, COLOR_GREEN);
//
attron(A_BOLD);
//
attron(COLOR_PAIR(1));
//
move(10, 15);
//
printw(" !!!\n");
//
attroff(A_BOLD);
//
attron(A_BLINK);
//
move(15, 15);
//
printw("PDCurses");
//
refresh();
//
getch();
// PDCurses
endwin();
}
8.2.3.

:
scanw() scanf();
150

getch(void) getchar()
;
getstr(char *S) S;
getnstr(char *S, int n) S, n .
getch(). , . -.
,
,
, , Shift, Ctrl, Alt.
, .
PDCurses,
,
. -
,
Enter.
cbreak().
nocbreak() .
int keypad(WINDOW *win, bool bf);
. ,
. stdscr. bool
151

TRUE ( ) FALSE ( ).
,
,
.
getch() int, char. , 32 127,
ASCII (. ). 128
255 ( : CP866, CP1251 KOI8-R).
.
PDCurses , . :
KEY_DOWN = 0x102 ,
KEY_UP = 0x103 ,
KEY_LEFT = 0x104 ,
KEY_RIGHT = 0x105 ,
KEY_HOME = 0x106 Home,
KEY_BACKSPACE = 0x107 Backspace,
KEY_DC = 0x14a Delete,
KEY_IC = 0x14b Insert,
KEY_ENTER = 0x157 Enter,
KEY_END = 0x166 End,
KEY_NPAGE = 0x152 Page Down,
KEY_PPAGE = 0x153 Page Up,
KEY_F(n) , n 0 63.
curses.h.

152

noecho() , echo()
.
halfdelay(int time)
time ( ). , getch()
ERR=-1. nocbreak().
. ,
, Esc ( Esc
27).
#include <curses.h>
int main(void)
{
//
initscr();
//
start_color();
// 1
init_pair(1, COLOR_RED, COLOR_GREEN);
//
bkgdset(COLOR_PAIR(1));
clear();
//
keypad(stdscr,TRUE);
//
refresh();
//
int C;
do
153

{C=getch();
//
clear();
//
move(10,15);
//
printw("%d",C); //
refresh();}
//
while (C!=27);
// Esc
// PDCurses
endwin();
return 0;
}
8.2.4.

move(y,x) .
( , ) . PDCurses , .
.
y, x.
PDCurses .
getyx(WINDOW *win, int y, int x)
win, ,
stdscr.

int getmaxx(WINDOW *win),


int getmaxy(WINDOW *win)
154

.
().
#include <curses.h>
int main(void)
{
//
initscr();
//
keypad(stdscr,TRUE);
//
refresh();
//
int C;
//
int x=10;
int y=10;
move(y,x);
//
int maxx=getmaxx(stdscr);
int maxy=getmaxy(stdscr);
do
{C=getch(); //
//
if ((C==KEY_LEFT)&&(x>0)) x--;
if ((C==KEY_RIGHT)&&(x<maxx)) x++;
if ((C==KEY_UP)&&(y>0)) y--;
if ((C==KEY_DOWN)&&(y<maxy)) y++;
move(y,x);
//
refresh();}
//
155

while (C!=27); // Esc


// PDCurses
endwin();
return 0;
}
8.3.

PDCurses

PDCurses . . PDCurses ,

stdscr .
-, .
.
,
w (, wprintw printw)
.
8.3.1.
.
WINDOW *newwin(int lines, int cols, int y, int x)
(x,y),
lines cols .
WINDOW *subwin(WINDOW *parent,
int lines, int cols, int y, int x)
156

(x,y)
, lines cols
, parent .
WINDOW *derwin(WINDOW *parent,
int lines, int cols, int y, int x)
lines cols
(x,y) parent.
int delwin(WINDOW *win)
/ win.
int mvwin(WINDOW *win, int y, int x)
win (x,y)
.
int mvderwin(WINDOW *win, int y, int x)
win (x,y) .
. ,
.
PDCurses
#include <curses.h>
int main(void)
{
//
initscr();
157

//
start_color();
// 1
init_pair(1, COLOR_RED, COLOR_GREEN);
init_pair(2, COLOR_RED, COLOR_BLUE);
//
bkgdset(COLOR_PAIR(1));
clear();

int maxx=getmaxx(stdscr);
int maxy=getmaxy(stdscr);

WINDOW *topwin=newwin(3,maxx,0,0); //.
,
. topwin
2 ,
wbkgdset(topwin,COLOR_PAIR(2));
wclear(topwin);
.
topwin.
//
wattron(topwin,COLOR_PAIR(2));
//
box(topwin,|,-);
158

// 25,1
wmove(topwin,1,25);
//
wprintw(topwin," "); //.
, .
//
WINDOW *mildwin=newwin(5,10,5,30);
//
wbkgdset(mildwin,COLOR_PAIR(2));
//
wclear(mildwin);
//
wattron(mildwin,COLOR_PAIR(2));
//
box(mildwin,*,*); //.
- mildwin c.

mildwin.
//
WINDOW *submildwin=derwin(mildwin,3,8,1,1);
//
wbkgdset(submildwin,COLOR_PAIR(1));
//
wclear(submildwin);
//
wattron(submildwin,COLOR_PAIR(1)); //.

,
159

Esc ( 27).
submildwin c .
//
keypad(submildwin,TRUE);
//
refresh();
// topwin
wrefresh(topwin);
// mildwin
wrefresh(mildwin);
// submildwin
wrefresh(submildwin);
//
int C;
do
{ // submildwin
C=wgetch(submildwin);
// submildwin
wclear(submildwin);
//
wmove(submildwin,1,2);
//
wprintw(submildwin,"%d",C);
// submildwin
wrefresh(submildwin);
}
while (C!=27); // Esc
// PDCurses
endwin();
return 0; //.

160

PDCurses
.
PDCurses
.
8.3.2.
, . . , .
panel.h.

PANEL *new_panel(WINDOW *win)


,
, ,
PANEL *panel1;
panel1 = new_panel(topwin);
, PDCurses:
int del_panel(PANEL *panel1)
panel1. , , . .
WINDOW *panel_window(const PANEL *panel1)
panel1.
int hide_panel(PANEL *panel1)
161

. .
int show_panel(PANEL *panel1)
.
.
int top_panel(PANEL *panel1)
, .
int bottom_panel(PANEL *panel1)
.
int move_panel(PANEL *pannel1, int y, int x)
, (x,y).
int replace_panel(PANEL *panel1, WINDOW *win)
panel1 win.
PANEL *panel_above(const PANEL *pan)
,
. , 0.
PANEL *panel_below(const PANEL *pan)
,
. , 0.
void update_panels()
.
doupdate().
162

8.4.

PDCurses

PDCurses . mousemask(),
.

, , . , ,
BUTTON1_CLICKED ,
BUTTON1_DOUBLE_CLICKED
,
BUTTON1_TRIPLE_CLICKED
,
ALL_MOUSE_EVENTS
.
mousemask() ,
, NULL.
, ,
KEY_MOUSE = 539.
.

int C;
keypad(stdscr,TRUE);
do
C=getch();
while(C!=KEY_MOUSE);

163

stdscr
.
. , , , ..
PDCurses MEVENT,
nc_getmouse(MEVENT *) . MEVENT ,
, .
:
., ..

int C;
keypad(stdscr,TRUE);
do
{
move(0,0);
C=getch();
if (C==KEY_MOUSE)
{
clear();
MEVENT M;
nc_getmouse(&M);
printw("%d
%d",M.x,M.y);
}
}
while(C!=27);
164

.
,
.
,
.
, . .
.
.
,
,
//
WINDOW *win[3]; //.
main
, ,
//
initscr();
//
start_color();
//
init_pair(1, COLOR_WHITE,
init_pair(2, COLOR_WHITE,
init_pair(3, COLOR_WHITE,

1, 2, 3
COLOR_GREEN);
COLOR_BLUE);
COLOR_RED);
//.

1
, 2 , 3 .
,
165

//
bkgdset(COLOR_PAIR(1));
clear();
//
int maxx=getmaxx(stdscr);
int maxy=getmaxy(stdscr);

//
WINDOW *topwin=newwin(3,maxx,0,0);
//
wbkgdset(topwin,COLOR_PAIR(2));
//
wattron(topwin,COLOR_PAIR(2));
//
wclear(topwin);
//
box(topwin,|,-);
// 25,1
wmove(topwin,1,25);
//
wprintw(topwin,"No click!!!"); //.
topwin 3
maxx (0,0).
2 ( )
. No click!!!.
,
win.
int k;
//
for(k=0;k<=2;k++)
166

{
// k
win[k]=newwin(10,25,5,k*25+k);
wbkgdset(win[k],COLOR_PAIR(2));
wclear(win[k]);
wattron(win[k],COLOR_PAIR(2));
box(win[k],|,-);
} //.
10 25 .
, y
5, x x = k 25 + k, k .
. , ,

,
//
refresh();
// topwin
wrefresh(topwin);
// win
for(k=0;k<=2;k++) {wrefresh(win[k]);}
//
mousemask(ALL_MOUSE_EVENTS,NULL);
//
keypad(stdscr,TRUE);
active :
int C;
int active=0;

//.
167


:
// Esc
do
{
//
C=getch();
//
if (C==KEY_MOUSE)
{
//
MEVENT M;
nc_getmouse(&M);
//
if ((M.y>=5)&&(M.y<15)) {active=M.x/25;}
//
wmove(topwin,1,25);
wprintw(topwin,"Active%d %d %d",active,M.x,M.y);
wrefresh(topwin);
//
repaint(active);
}
}
while(C!=27); //.

5 y < 15, .
. ,
y = 10, x = 38
, 38/25 = 1.
168

win. repaint(int a). a .


.
void repaint(int a)
{
int k; //
//
for(k=0;k<=2;k++)
{
if (k==a) //
{
// 3
wbkgdset(win[k],COLOR_PAIR(3));
//
wclear(win[k]);
//
box(win[k],|,-);
//
wrefresh(win[k]);
}
else
{
// 2
wbkgdset(win[k],COLOR_PAIR(2));
//
wclear(win[k]);
//
box(win[k],|,-);
//
wrefresh(win[k]);
169

}
}
}

//.

k
. ,
3.
2. .
#include <curses.h>
//
WINDOW *win[3];
//
void repaint(int a)
{
int k; //
//
for(k=0;k<=2;k++)
{
if (k==a) //
{
// 3
wbkgdset(win[k],COLOR_PAIR(3));
//
wclear(win[k]);
//
box(win[k],|,-);
//
wrefresh(win[k]);
170

}
else
{
// 2
wbkgdset(win[k],COLOR_PAIR(2));
//
wclear(win[k]);
//
box(win[k],|,-);
//
wrefresh(win[k]);
}
}
}
//
int main(void)
{
initscr(); //
start_color(); //
//
init_pair(1, COLOR_WHITE, COLOR_GREEN);
init_pair(2, COLOR_WHITE, COLOR_BLUE);
init_pair(3, COLOR_WHITE, COLOR_RED);
//
bkgdset(COLOR_PAIR(1));
clear();
int maxx=getmaxx(stdscr);
int maxy=getmaxy(stdscr);

171

//
WINDOW *topwin=newwin(3,maxx,0,0);
wbkgdset(topwin,COLOR_PAIR(2));
wclear(topwin);
wattron(topwin,COLOR_PAIR(2));
box(topwin,|,-);
wmove(topwin,1,25);
wprintw(topwin,"No click!!!");
// win
int k;
for(k=0;k<=2;k++)
{
win[k]=newwin(10,25,5,k*25+k);
wbkgdset(win[k],COLOR_PAIR(2));
wclear(win[k]);
wattron(win[k],COLOR_PAIR(2));
box(win[k],|,-);
}
//
refresh();
wrefresh(topwin);
for(k=0;k<=2;k++) {wrefresh(win[k]);}
//
mousemask(ALL_MOUSE_EVENTS,NULL);
//
keypad(stdscr,TRUE);
int C; //
int active;
//
172

do
{
C=getch();
if (C==KEY_MOUSE)
{
MEVENT M;
nc_getmouse(&M);
if ((M.y>=5)&&(M.y<15)) {active=M.x/25;}
wmove(topwin,1,25);
wprintw(topwin,"Active%d %d %d",active,M.x,M.y);
wrefresh(topwin);
repaint(active);
}
}
while(C!=27);
endwin();
return 0;
}
8.5.

.
1.
2.
3.
4.
5.
6.

, .
, .
, .
.
-.
.

173

8.5.1.
1.
, .
- .
2.
,
. - .
3.
, .
-
.
4.
, .
- .
5.
,
, .
6.

. .

174

7.
, .
8.

(- ) .
9.

(- ) .
10.

(- ) .
11.

(, ) .
12.
.
.
, .

175

13.
.
.

, . , .
14.

33. .
15.

. F12
.

176

9. 5

:
;
;
.
9.1.

x y
y = f (x),
.
(xk , fk ), fk = f (xk ), k = 0, n, n + 1 . f (x)
( ) [a, b].

g(x) f (x). g(x)
g(x) = Pn (x) = a0 + a1 x + a2 x2 + ... + an xn .

(1)

Pn (x) ak , k = 0, n. , Pn (x)
, -

177

ak :

a0 + a1 x0 + a2 x20 + ... + an xn0 = f0 ,

2
n

a0 + a1 x1 + a2 x1 + ... + an x1 = f1 ,

...

a0 + a1 xk + a2 x2k + ... + an xnk = fk ,

...

a0 + a1 xn + a2 x2n + ... + an xnn = fn .

(2)

(2) ak (xk , fk ), (1),


. (2)
, . .
(1)
Pn (x) =

n
P
k=0

fk

(x x0 )...(x xk1 )(x xk+1 )...(x xn )


=
(xk x0 )...(xk xk1 )(xk xk+1 )...(xk xn )
n
Y
(x xj )
n
P
j=0,j6=k
=
fk Y
.
n
k=0
(xk xj )
j=0,j6=k


,
:
.
.
178

Pn (x) f (x)
.
,
xk .
fi,j =

fj fi
,
xj xi

i, j = 0, n,

i 6= j.

, f0,1 , f1,2 ,..., fn1,n ,


f0,1,2 =

f1,2 f0,1
,
x2 x 0

f2,3 f1,2
,
x3 x 1
...
fn1,n fn2,n1
fn2,n1,n =
.
xn xn2
(k+1)- k:
f1,2,3 =

fj,j+1,...,j+k,j+k+1 =

fj+1,j+2,...,j+k+1 fj,j+1,j+k
.
xj+k+1 xj


( x0 )
Pn (x) = f0 + f0,1 (x x0 )+
+f0,1,2 (x x0 )(x x1 ) + ...+
+f0,1,...,n1,n (x x0 )(x x1 )...(x xn1 )
179

(3)

(
xn )
Pn (x) = fn + fn1,n (x xn )+
+fn2,n1,n (x xn )(x xn1 ) + ...+
(4)
+f0,1,...,n1,n (x xn )(x xn1 )...(x x1 ).
. . , xn+1
(3)

f0,1,...,n,n+1 (x x0 )(x x1 )...(x xn1 )(x xn ).


(3) . , f (x0 ),
f0,1 , f0,1,2 .. ,
. xk fk n+1 ,
. 9.
. fn1,n = (fn fn1 )/(xn
xn1 ) fn , .
f,
3 9. fn2,n1 = (fn1 fn2 )/(xn1 xn2 )
fn1 .
(180

4) ..
for(k=n;k>=1;k--)
{f[k]=(f[k]-f[k-1])/(x[k]-x[k-1]);}
f 5 . 9.
9
1
2
3
4

x0 x1 x2
f0 f1 f2
f0 f1 f2
f0 f1 f2

5 f0 f0,1 f1,2
6 f0 f0,1 f0,1,2
7 f0 f0,1 f0,1,2


... xn1
xn
... fn1
fn
... fn1
fn1,n
... fn2,n1
fn1,n
...
... fn2,n1
fn1,n
... fn2,n1 fn2,n1,n
...
... f0,1,...,n1 f0,1,...,n1,n


f[2]...f[n]:
for(k=n;k>=2;k--)
{f[k]=(f[k]-f[k-1])/(x[k]-x[k-2]);}

//.

j j-1 f[j]...f[n]:
for(k=n;k>=j;k--)
{f[k]=(f[k]-f[k-1])/(x[k]-x[k-j]);}

181

//.

f
, ( 7).

, (x xi )
Pn (x) = f0 + (x x0 )[f0,1 + (x x1 )[f0,1,2 + ...+
+(x xn2 )[f0,1,...,n1 + (x xn1 )f0,1,...,n ]]...].
9.2.

WinBGIm

(
)
,
.
.
WinBGIm WinBGI Borland.
.

.
graphics.h
#include <graphics.h>
...
int gd, gm; //.
gd (graphic driver)
() , gm (graphic mode)
182

. gd gm . gd gm.

void detectgraph(int *graphdriver, int *graphmode);
. 10.
VGA 9 , . 11.
10

DETECT 0 ()
CGA
1
MCGA
2
EGA
3
ECGA64
4
EGAMONO
5
IBM8514
6
HERCMONO
7
ATT400
8
VGA
9
PC3270
10

183

11
VGA

VGALO
0
640x200
VGAMED
1
640x350
VGAHI
2
640x480

16
16
16

void initgraph(int *graphdriver,


int *graphmode, char *path);
path .
path . initgraph . ()
void closegraph(int w=ALL_WINDOWS); //.
, w , . :
CURRENT_WINDOW ( )
ALL_WINDOWS ( , ).


.
184

#include <iostream.h>
#include <graphics.h>
int main()
{
int gd, gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
getch();
closegraph();
return 0;
} //.
WinBGIm . ,
. 12.
9.2.1. ,


. , Y .
. .
( , ).

int getmaxx(void);
int getmaxy(void);

//.

185

12

BLACK
0
BLUE
1
GREEN
2
CYAN
3
RED
4
MAGENTA
5
BROWN
6
LIGHTGRAY
7
DARKGRAY
8
LIGHTBLUE
9
LIGHTGREEN
10
LIGHTCYAN
11
LIGHTRED
12
LIGHTMAGENTA
13
YELLOW
14
WHITE
15

void moveto(int x, int y);
void moverel(int dx, int dy);

//.

, dx dy .

int getx(void);
int gety(void);

//.
186

9.2.2.
WinBGIm

void putpixel(int x, int y, int color);


x, y , color .
,
putpixel(getmaxx()/2, getmaxy()/2, GREEN);
.

line, lineto, linerel.
void line(int x1, int y1, int x2, int y2);
(x1, y1)
(x2,y2). , , line, .
void setcolor (int color);
,
setcolor(RED);
line(0,0,100,200);

//.

setbkcolor (int color)


.

int getcolor(void);
int getbkcolor(void);
187


,
unsigned getpixel(int x, int y);
.

void lineto(int x, int y);


x,y.

void linerel(int dx, int dy);



(x, y) (x+dx, y+dy).

void setlinestyle (int A, unsigned B, int C);
A ,
. 13.
13

SOLID_LINE
0

DOTTED_LINE
1

CENTER_LINE
2
-
DASHED_LINE
3

USERBIT_LINE
4
,

188

C .
:
NORM_WIDTH=1 ( 1 )
THICK_WIDTH=3 ( 3 ).
B ,
USERBIT_LINE ( B=0). .
, .

1111000011110000=0xF0F0,

1010101010101010=0xAAAA,
,
setlinestyle(SOLID_LINE,0,NORM_WIDTH);
line(30,30, 555,400);

setlinestyle(USERBIT_LINE,0x9898,THICK_WIDTH);
line(30,30, 555,400); //.
9.2.3.
,
, (x1,
y1) (x2, y2) .

void rectangle(int x1, int y1, int x2, int y2);


,
189

void bar(int x1, int y1, int x2, int y2);


.

void bar3d(int x1, int y1,


int x2, int y2, int d, int b);
(). d
, b .
9.2.4.

void ellipse(int x, int y, int a, int b,
int Rx, int Ry);
x, y , a, b
, , Rx, Ry .
0 360 .
. , ,
ellipse(100, 100, 0, 360, 50,50);
.

void fillellipse(int x, int y, int Rx, int Ry);


.

190

void setfillstyle(int style, int color);


style , color . . 14.
14

EMPTY_FILL
0

SOLID_FILL
1

LINE_FILL
2

LTSLASH_FILL
3

SLASH_FILL
4

BKSLASH_FILL
5

LTBKSLASH_FILL
6

HATCH_FILL
7

XHATCH_FILL
8

INTERLEAVE_FILL
9

WIDE_DOT_FILL
10

CLOSE_DOT_FILL
11

USER_FILL
12

void pieslice(int x, int y, int a, int b, int R);


. x, y ,
a b.

191

void sector(int x, int y, int a, int b,


int Rx, int Ry);
. x, y
, a b , Rx,
Ry
.
9.2.5.
outtext()
outtextxy().
void outtext(char *string);
, . ,
outtext(" Enter");

//.

,
void outtextxy(int x, int y, char *string);
x, y . ,
outtextxy(50,100," Enter");//.
,
.
sprintf (char *S, " ", ...);
192

ptintf(), S, . ,
x=3.14159;
char *S;
sprintf(S, "%f",x)
outtextxy(20, 40,S);

//.

void settextstyle(int font, int B, int size);



. font
( DEFAULT_FONT=0), b ( b=0 b=1), size
: size=1
88 , size=2 1616 . , , . 15.
15

DEFAULT_FONT
0
TRIPLEX_FONT
1
SMALL_FONT
2
SANS_SERIF_FONT
3
GOTHIC_FONT
4
SCRIPT_FONT
5
SIMPLEX_FONT
6
TRIPLEX_SCR_FONT
7
COMPLEX_FONT
8
EUROPEAN_FONT
9
BOLD_FONT
10
193


(0,0)
void cleardevice(void); //.
9.3.

, f (x) = x sin(x).

#include <graphics.h>
#include <math.h> //.
main()
int gd, gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");

setbkcolor(WHITE);
cleardevice(); //.
XOY xoy, .
xoy (X0,Y0) XOY. ox ,
oy ,
edx, edy. (X,Y) xoy:
194

. 22.
x = (X X0)/edx , y = (Y 0 Y )/edy (. . 22). , (x,y)
: X=X0+edx*x, Y=Y0-edy*y.
, .
(X0,Y0)
int X0=getmaxx()/2;
int Y0=getmaxy()/2;

line(0,Y0,getmaxx(),Y0);
line(X0,0,X0,getmaxy());

//.

,
195

int edx=20;
int edy=15;
// -
setcolor(LIGHTBLUE);
//
int k;
//
//
for(k=0;edx*k<X0;k++)
{
line(X0+k*edx,Y0-3,X0+k*edx,Y0+3);
line(X0-k*edx,Y0-3,X0-k*edx,Y0+3);
}
//
for(k=0;edy*k<Y0;k++)
{
line(X0-3,Y0+k*edy,X0+3,Y0+k*edy);
line(X0-3,Y0-k*edy,X0+3,Y0-k*edy);
} //.
. 3

setcolor(LIGHTRED);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);

//.

: a,b
ox, x,y
, delta ox
float a,b,x,y,delta;
//
delta=0.1;
196

//
a=-15; b=15; //.

(a,f(a)). ( ).
//
x=a;
y=x*sin(x);
//
moveto(X0+edx*x,Y0-edy*y); //.

(x,f(x))
.
, , .
//
for(x=a;x<=b;x=x+delta)
{
y=x*sin(x);
lineto(X0+edx*x,Y0-edy*y);
}; //.
.
//
settextstyle(SANS_SERIF_FONT,0,2);
//
outtextxy(180,10," f(x)=x*sin(x)");
,
. 23.
197

#include <graphics.h>
#include <math.h>
int main()
{
//
int gd, gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
// -
setbkcolor(WHITE);
//
cleardevice();
// -
setcolor(BLACK);
//
int X0=getmaxx()/2;
int Y0=getmaxy()/2;
//
line(0,Y0,getmaxx(),Y0);
line(X0,0,X0,getmaxy());
//
int edx=20;
int edy=15;
// -
setcolor(LIGHTBLUE);
//
int k;
//
//
for(k=0;edx*k<X0;k++)
{line(X0+k*edx,Y0-3,X0+k*edx,Y0+3);
line(X0-k*edx,Y0-3,X0-k*edx,Y0+3);}
198

//
for(k=0;edy*k<Y0;k++)
{line(X0-3,Y0+k*edy,X0+3,Y0+k*edy);
line(X0-3,Y0-k*edy,X0+3,Y0-k*edy);}
// -
setcolor(LIGHTRED);
// -
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
//
float a,b,x,y,delta;
//
delta=0.1;
//
a=-15; b=15;
//
x=a;
y=x*sin(x);
//
moveto(X0+edx*x,Y0-edy*y);
//
for(x=a;x<=b;x=x+delta)
{y=x*sin(x);
lineto(X0+edx*x,Y0-edy*y);};
//
settextstyle(SANS_SERIF_FONT,0,2);
//
outtextxy(180,10," f(x)=x*sin(x)");
getch(); //
//
closegraph();
return 0;
} //.
199

. 23.
9.4.

n+1 (x, y), f (x) . x


n+1 [a, b]. Pn (x).
:
1. f (x)
[a, b].
x y,
-
.
2. 200

3.
4.

5.

6.

n + 1 .

Pn (x) .
f (x) Pn (x) [a, b+]. > 0 ,
x 1.52
, .

(4,5,7,10 ) .


.
16

1
2
3
4
5
6
7
8

f (x)
ex
ex
sin(x)
cos(x)
tg(x)
1/x
2
1/x


[a, b]

f (x)
[a, b]
2
[1, 1]
9
1/(1 x )
[2, 5]
[2, 2]
10
x2 ln(x)
[1, 5]
[/2, /2] 11
ln(|x|)
[2, 2]
[0, 4]
12
| sin(x)|
[/2, /2]
[/4, /4] 13
ln(x2 + 1)
[2, 2]
|x|
[1, 3]
14
e
[1, 1]
3
[1, 3]
15
x
[0, 3]
[0, 5]
15 cos(x) sin(x)
[4, 4]

201

10.



Qt


(Graphics User Interface, GUI).

GUI. , , Windows
API (Application Programming
Interface) . ,
.
: MCF (Microsoft Foundation Classes), VCL (Visual
Component Library) Borland .
Qt
. -
. , Qt
. - Qt, .
Qt,
. Qt
[17, 18, 19].
Qt .

[6, 7, 8, 9].
202

10.1.


, .
. . .
. :
, ..
,
.

Qt QApplication. Qt exec()
. Qt-:
#include <QApplication>
...
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
...
return app.exec();
} //.
#include <QApplication> QApplication.h. 203

- QtGui.h. main . , , .
, . argv, arg. . QApplication app(argc, argv); app
QApplication.
argv argc.
. Qt-
-style=. Windows, CDE,
Motif, Plastique Cleanlooks . exec()
QApplication . , . .
10.1.1. Qt
Qt .
, ,
.
. 204

:
1. . Qt Qt
Command Prompt
.
Far manager
Linux.
2. qmake -project.
pro.
3. qmake.
Makefile.
4. make .
. . IDE
Kdevelop Linux.
Code::Blocks
QtWorkBench. ,
. Qt
.
10.1.2.
, (, , ..) . Qt
. ,
QLabel ().
,
- .
205

QLabel:
QLabel Lab("Hello!!!");
resize show
Lab.resize(50,30);
Lab.show(); //.

#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QLabel Lab("Hello!!!");
Lab.resize(50,30);
Lab.show();
return app.exec();
} //.

.
QPushButton,
.
#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QPushButton *Btn = new QPushButton("Button");
Btn->resize(50,30);
Btn->show();
return app.exec();
} //.
206

QPushButton new.
Btn. QWidget.
.
QWidget
#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget *wdt = new QWidget();
wdt->resize(300,300);
wdt->setWindowTitle("!!!Window!!!");
wdt->show();
return app.exec();
} //.
300 300 !!!Winwow!!!.
. , ,
.
QPushButton
QPushButton *btn1 = new QPushButton("Button1");
QPushButton *btn2 = new QPushButton("Button2");
wdt
btn1->setParent(wdt);
btn2->setParent(wdt);

//.
207


btn1->move(10,10);
btn2->move(100,10);
wdt->show(); //.
, :
#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget *wdt = new QWidget();
wdt->resize(300,300);
wdt->setWindowTitle("!!!Window!!!");
QPushButton *btn1 = new QPushButton("Button1");
QPushButton *btn2 = new QPushButton("Button2");
btn1->setParent(wdt);
btn2->setParent(wdt);
btn1->move(10,10);
btn2->move(100,10);
wdt->show();
return app.exec();
} //.
. 24. , ,
,
.
10.1.3.
,
, :
208

. 24.
. . ,
.
Qt : QHBoxLayout (
), QVBoxLayout ( )
QGridLayout ( ). .
, . : ,
209

QApplication app(argc, argv);



QWidget *wdt = new QWidget();

QPushButton *btn1 = new QPushButton("Button1");
QPushButton *btn2 = new QPushButton("Button2");

QLineEdit *edt1 = new QLineEdit("Text1");
QLineEdit *edt2 = new QLineEdit("Text2");

QLabel *lb = new QLabel("Text Label");



QHBoxLayout *hlayout1 = new QHBoxLayout;
QHBoxLayout *hlayout2 = new QHBoxLayout;

QVBoxLayout *vlayout = new QVBoxLayout;

//.

,
:
wdt->resize(300,150);
wdt->setWindowTitle("!!!Window!!!");
lb->setAlignment(Qt::AlignCenter);

210

, , .
edt1 edt2 addWidget()
hlayout1, btn1 btn2
hlayout2
hlayout1->addWidget(edt1);
hlayout1->addWidget(edt2);
hlayout2->addWidget(btn1);
hlayout2->addWidget(btn2);

//.

: hlayout1 , lb
hlayout2
vlayout->addLayout(hlayout1);
vlayout->addWidget(lb);
vlayout->addLayout(hlayout2);

//.

, addLayout(). vlayout
wdt setLayout()
wdt->setLayout(vlayout);

//.

. 25
, .
. [17, 18, 19].

211

. 25.

#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QWidget *wdt = new QWidget();
QPushButton *btn1 = new QPushButton("Button1");
QPushButton *btn2 = new QPushButton("Button2");
QLineEdit *edt1 = new QLineEdit("Text1");
QLineEdit *edt2 = new QLineEdit("Text2");
QLabel *lb = new QLabel("Text Label");
QHBoxLayout *hlayout1 = new QHBoxLayout;
QHBoxLayout *hlayout2 = new QHBoxLayout;
QVBoxLayout *vlayout = new QVBoxLayout;
wdt->resize(300,150);
wdt->setWindowTitle("!!!Window!!!");
lb->setAlignment(Qt::AlignCenter);
hlayout1->addWidget(edt1);
hlayout1->addWidget(edt2);
212

hlayout2->addWidget(btn1);
hlayout2->addWidget(btn2);
vlayout->addLayout(hlayout1);
vlayout->addWidget(lb);
vlayout->addLayout(hlayout2);
wdt->setLayout(vlayout);
wdt->show();
return app.exec();
//.

10.1.4.
,
, QWidget. TForm. , ( , ) .
TForm
form.h:
#include <QtGui>
#ifndef F_H_INCLUDED
#define F_H_INCLUDED
class TForm : public QWidget
{
public :
TForm(); //
private :
//
QPushButton *btn1;
213

QPushButton *btn2;
QLineEdit *edt1;
QLineEdit *edt2;
QLabel *lb;
QHBoxLayout *hlayout1;
QHBoxLayout *hlayout2;
QVBoxLayout *vlayout;
};
#endif // F_H_INCLUDED.
,
TForm. #ifndef, #define,
#endif
.
define ( ),
,
#define PI 3.14159
PI 3,14159.
.
ifndef
. ,
#ifndef NAME #endif , NAME .
TForm QWidget.
TForm(). ,
, .. 214

, .
form.cpp:
#include "form.h"
TForm::TForm()
{
btn1 = new QPushButton("Button1");
btn2 = new QPushButton("Button2");
edt1 = new QLineEdit("Text1");
edt2 = new QLineEdit("Text2");
lb = new QLabel("Text Label");
hlayout1 = new QHBoxLayout;
hlayout2 = new QHBoxLayout;
vlayout = new QVBoxLayout;
lb->setAlignment(Qt::AlignCenter);
hlayout1->addWidget(edt1);
hlayout1->addWidget(edt2);
hlayout2->addWidget(btn1);
hlayout2->addWidget(btn2);
vlayout->addLayout(hlayout1);
vlayout->addWidget(lb);
vlayout->addLayout(hlayout2);
this->setLayout(vlayout);
} //.
.
. , , this. this
215

, (
). , , . , .
, ,
main.cpp:
#include <QtGui>
#include "form.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
TForm *wdt = new TForm();
wdt->resize(300,150);
wdt->setWindowTitle("!!!Window!!!");
wdt->show();
return app.exec();
} //.
wdt TForm .
,
.
.
.

216

10.2.

Qt
, . ,
. . ,
.
.
, , ,
.
Qt ,
. Q_OBJECT private
( ), :
class TForm : public QWidget
{
Q_OBJECT
...
signals :
void NameSignal(void);
...
}; //.
NameSignal .
- , , , 217

..
emit NameSignal();

//.

, , ,
class TForm : public QWidget
{
Q_OBJECT
...
signals :
void NameSignal(void);
...
public :
void SendNameSignal(void)
{
emit NameSignal();
}
...
}; //.
. , .
.
.
connect() QObject
Qt
218

connect(Sender, SIGNAL(NameSignal()),
Receiver, SLOT(NameSlot())); //.
Sender , , NameSignal() , Receiver , NameSlot() . .
14
1, N 12 + 22 + 32 + 42 + ... + N 2 .
N.
N edt1.
btn1
edt2. lb ,
.

TForm Btn1Click(). form.h
#include <QtGui>
#ifndef F_H_INCLUDED
#define F_H_INCLUDED
class TForm : public QWidget
{
Q_OBJECT
public :
TForm();
public slots :
void Button1Click(void);
219

private :
QPushButton *btn1;
QPushButton *btn2;
QLineEdit *edt1;
QLineEdit *edt2;
QLabel *lb;
QHBoxLayout *hlayout1;
QHBoxLayout *hlayout2;
QVBoxLayout *vlayout;
};
#endif // F_H_INCLUDED.
Q_OBJECT . :
clicked() btn1 Btn1Click(), clicked() btn2 close() , .
TForm::TForm()
{
btn1 = new QPushButton(
QString::fromLocal8Bit(""));
btn2 = new QPushButton(
QString::fromLocal8Bit(""));
edt1 = new QLineEdit("0");
edt2 = new QLineEdit("0");
lb = new QLabel(
QString::fromLocal8Bit(" N"));
hlayout1 = new QHBoxLayout;
hlayout2 = new QHBoxLayout;
vlayout = new QVBoxLayout;
220

lb->setAlignment(Qt::AlignCenter);
hlayout1->addWidget(edt1);
hlayout1->addWidget(edt2);
hlayout2->addWidget(btn1);
hlayout2->addWidget(btn2);
vlayout->addLayout(hlayout1);
vlayout->addWidget(lb);
vlayout->addLayout(hlayout2);
this->setLayout(vlayout);
//
connect(btn1,SIGNAL(clicked()),
this,SLOT(Button1Click()));
connect(btn2,SIGNAL(clicked()),
this,SLOT(close()));
} //.

QString::fromLocal8Bit(). , Qt
Unicode, ( Windows
CP1251, Linux koi8-r). fromLocal8Bit()
QString
QString.
.
N. .
edt1 text(),
QString. QString
: toInt(), toDouble(). 221


N=edt1->text().toInt();
edt1->text()
QString, .toInt()
.

for(k=1;k<=N;k++) {s=s+k*k;};

//.

s.
edt2 . tmp
QString setNum()
s tmp. , ,
setText() edt2
tmp lb
QString tmp;
tmp.setNum(s);
edt2->setText(tmp);
lb->setText(
QString::fromLocal8Bit(""));

void TForm::Button1Click(void)
{
int N;
N=edt1->text().toInt();
int k;
int s=0;
for(k=1;k<=N;k++) {s=s+k*k;};
222

QString tmp;
tmp.setNum(s);
edt2->setText(tmp);
lb->setText(
QString::fromLocal8Bit(""));
} //.
, .
10.3.

QtDesigner

Qt Qt Designer,

. Qt Designer.
: , , ,
. Qt Designer
, . . .
Qt Designer , .
Qt Designer , .. Form,
. .
:
btn1 btn2;
223


Lay Out Horizontally;
edt1
edt2;

Lay Out Horizontally;
lb;

Lay Out Vertically;

Lay Out Vertically.
forma.ui.
UiForm.
Qt forma.ui ui_forma.h UiForm
++. ,
Qt Designer . -,

QWidget, UiForm. form.h
#include <QtGui>
#include "ui_forma.h"
#ifndef F_H_INCLUDED
#define F_H_INCLUDED
class TForm : public QWidget, private Ui::Form
{
Q_OBJECT
public :
TForm();

224

public slots :
void Button1Click(void);
};
#endif // F_H_INCLUDED.
-, setupUi(this). form.cpp

#include "form.h"
void TForm::Button1Click(void)
{
int N;
N=edt1->text().toInt();
int k;
int s=0;
for(k=1;k<=N;k++) {s=s+k*k;};
QString tmp;
tmp.setNum(s);
edt2->setText(tmp);
lb->setText(QString::fromLocal8Bit(""));
}
TForm::TForm()
{
setupUi(this);
connect(btn1,SIGNAL(clicked()),
this,SLOT(Button1Click()));
connect(btn2,SIGNAL(clicked()),
this,SLOT(close()));
}

225

, Qt Designer
.
, Qt.
.
Qt [17, 18, 19].

226

11. 6

:
;
Qt.
11.1.

,
, .

Zb
I=

f (x)dx

(5)


, . ,
.
.
, , N h = (b a)/N
a = x0 , x1 , ..., xN = b. ,
. ,
227

(. . 26). . , ,
.

. 26.

k Sk = hf (xk ) = hf (a + hk).

N 1
ba X
f (a + hk)
I
Sk =
N k=0
k=0
N
1
X

(6)

N
X
k=1

Sk =

baX
f (a + hk).
N k=1

(7)

( . 27), .
228

. 27.
k
Sk = h[f (xk ) + f (xk+1 )]/2 = h[f (a + hk) + f (a + h(k + 1))]/2,

N
1
X

N 1
ba X
I
Sk =
[f (xk ) + f (xk+1 )] =
2N k=0
k=0

"N 1
#
f (a) + f (b)
ba X
.
f (a + kh) +
=
N
2
k=1

(8)


(6) (8) .

(5)
Zb
I=

f (x)dx =
a

N
1
X

xi+1
Z
f (x)dx.

k=0 x
i

229

(9)

gk (x), .
f (x) gk (x) = f (xk )
gk (x) = f (xk+1 ),
(6) (7). , (xk , f (xk )) (xk+1 , f (xk+1 )),
(8).
I

N
X

Ak f (xk ),

(10)

k=0

Ak .
, (10).
( )

N/2
N/21
X
X
ba
4
f (x2k1 ) + 2
f (x2k ) + f (a) + f (b) .
I
3N
k=1
k=1
(11)
N .

N

baX
Hk f (xk ),
I
2 k=1

xk =

a+b ba
+
tk
2
2

(12)


tk , N
[1, 1]. tk
Hk N . 17.
230

17
N
N
N
N
N


=1
t1 = 0.5
H1 = 2
= 2 t1 = t2 = 0.577350269
H1 = H2 = 2
= 3 t1 = t3 = 0.774596669 H1 = H3 = 0.555555555
t2 = 0
H2 = 0.888888888
= 4 t1 = t4 = 0.861136311 H1 = H4 = 0.347854845
t3 = t2 = 0.339981043 H2 = H3 = 0.652145155
= 5 t1 = t5 = 0.906179846 H1 = H5 = 0.236926885
t2 = t4 = 0.538469310 H2 = H4 = 0.478628670
t3 = 0
H3 = 0.568888888
11.2.


, . .
.
11.2.1.
1.
3.
5.
7.

R2

(1 2|x|)dx.

2
R5

xdx.

0
R3
3
R4

exp(x2 )dx.

2.
4.
6.

9.

2
R4
1
R

| cos(2x)|dx.
ln(x)dx.
cos2 (x)dx.

ln(x2 + 1)dx.

R8

8. (x2 + x)dx.

R5

R2

ln(x)dx.

10.

R2
2

231

(x3 + x2 + 1)dx.

11.
13.
15.

R1
4
R

ex dx.

12.

R10

x exp(x2 )dx.

sin(x)dx.

0
/2
R

cos(x)dx.

14.
16.

R0
10
R5

(x2 + |x|)dx.

/2

232

x3 exp(x2 )dx.

12. 7


:
;
Qt.
12.1.

, , .
.
.

f (x) = 0,
f (x) . , ,
x3 5x2 + 2x = 5
f (x) = x3 5x2 + 2x + 5.
x f (x) = 0 .

f (x) ,
f (x).
:
1. , ..
233

.
2. .
, , , .
, , f (x). ,
[a, b] f (a)f (b) < 0. f (x) = x3 5x2 + 2x + 5 ,
. ,
f (x):
3x2 10x + 2 = 0.
: x1 3.12, x2 0.21. f (0.21) > 0, f (3.12) < 0, f (a)f (b) < 0,
[0.21; 3.12] .
f (x) , . . , x (; 0.21)
x (3.12; +) f (x) . ,
x, , x = 2,
f (2) < 0 [2; 0.21] .
x = 5, f (5) > 0.
[3.12; 5] .
f (x) , , . a b,
, .
[a, b] f (a)f (b) < 0.
234

, ,
xk .
: |f (xk )| < |xk xk1 | < .

,
, , = 106 ,
f (x) . : ( ), .
12.1.1.
, f (x),
[a, b], b > a.
, , f (a)f (b) < 0.

:
1) d = (a + b)/2,
2) f (a), f (b), f (d),
3) f (a)f (d) < 0,
[a, b]. b = d;
a = d (. . 28).
4) |b a| > , 1.
|ba| < ,
, , .
12.1.2.
, , .
235

. 28.
:
1) (a, f (a))
(b, f (b)).
d = a(ba)f (a)/[f (b)
f (a)] . [a, b] 2 ,
2) f (a), f (b), f (d),
3) f (a)f (d) < 0, [a, b]. d
(b = d); d
(a = d).
4) |b a| > , 1.

. 29.

236

|b a| < ,
, , .
12.1.3.
f (x) = 0
x = (x).

(13)

(13) y = (x)
y = x. x0 , [a, b], (13),
x1 = (x0 ).

(14)

x1 (13) :
x2 = (x1 ).

(15)

n
xn = (xn1 ).

(16)

xn n
, .
12.1.4. ()
1) x0 (
a, b).
2) x0 f (x).
x1 = x0 f (x0 )/f 0 (x0 ) .
237

. 30.
3) x1 , x2 = x1
f (x1 )/f 0 (x1 ).
4) n: xn = xn1 f (xn1 )/f 0 (xn1 ).
xn n
,
.
, x3 5x2 + 2x = 5 .
#include <iostream.h>
#include <math.h>
// ,
float f(float x)
{return x*x*x-5*x*x+2*x+5;}
float eps=1e-6; //
float a,b,d,res;
238

int main()
{
//
//
cout << " : ";
cin >> a >> b;
if (f(a)*f(b)>0)
{cout << " !";}
else
{
do //
{
d=(a+b)/2; //
//
if (f(a)*f(d)<0) {b=d;} else {a=d;}
}
while (fabs(b-a)>eps);
//
res=(b+a)/2;
cout << " " << res << endl;
cout << "" << f(res) << endl;
}
return 0;
} //.
,
, , , :
a=-6; b=a+0.1;
while (f(a)*f(b)>0)
{
239

a=b;
b=b+0.1;
} //.
12.2.

, . .
12.2.1.

1) x3 x = 2x2 2
3) x3 = 4x(x 1)
5) x2 = ln(x)
7) x2 = 2 sin(x) + 1
9) x3 3x2 5x = 12
11) ln(x2 + 1) = 1
13) arctg(2x2 2) = 1
15) x4 + x = 2
17) x4 + 5x = 3 ln(x)
19) x2 + sin(x) = 2 + x
21) x2 + cos(x) = 7 + 4x
23) x4 + cos(x) = 5
25) 3x3 + 2x2 = 3x + 1

2) x3 + 12 = x(3x 4)
4) x3 x2 8x = 12
6) ln(x) + 2 = x3
8) x2 = 2 cos(2x) + 1
10) x3 = 2 sin(3x) + 1
12) ln(x4 + 1) = 2
14) 2x3 = 4x(x + 1)
16) x4 + 5x = 3
18) x2 + sin(x) = 2 + x
20) x2 + sin(x) = lg(x) + x
22) x2 + cos(x) = 7 4x
2
24) 3x3 + 2x
= 5x + 2
3
26) x 10 x = 5

240


++,
.
, . .
++. , , , .
, .

++.
PDCurses, WinBGIm
Qt.
- ++. ( ),
: , , .

/++,
.

241

242

243

244

245

246


1. , . C++ /
.. , .. ; . . . . . .: , 2000. 1021 .
2. ++ 21
/ . , . . .: , 2006. 784 .
3. ++ / .. . .:
, 2003. 512 .
4. .. :
. / .. , .. . .: , 2001. 86 .
5. .. /++. : / .. , .. . .:
, 2003. 240 .
6. .. ++ -
/ .. . :
, 1997. 478 .
7. , , . C++ / .. . .:
, 2003. 336 .
8. , . ++ /
. . .: , 2001. 590 .
9. , . C++ : /
. . . . . .: ,
2000. 320 .
10. . / . ,
. . . .; . .. . .:
, 2001. 351 .
247

11. , . C++. / ; . .
. . ; . .
. . .: -, 2007. 1098 .
12. , . C++ /
. ., .: : , 2006. 444 .
13. , . C++:
/ . ; . . . . .: ,
2004. 727 .
14. ,
.

UNIX / . //
http://lib.ru/CTOTOR/book.txt
15. , .
Linux / .. , .. . .:
, 2007. 544 .
16. .. . -
C++: . / .. . .: , 2004. 464 .
17. . QT4: GUI ++ /
. , . . .: -, 2007.
736 .
18. . Qt4.
++ / . . .: -, 2007. 880 .
19. .. Qt 4 / .. . .:
-, 2008. 608 .

248


. . . . . . . . . . . . . . . . . . . . . . . . . .
1. . . . . . . . . . .
1.1. , ,
. . . . . . . . . . . . .
1.2. . . . . . . . . . . . . . . . . . . . .
1.3. . . . . . . . . . . . . . . . . . . .
1.4. . . . . . . . . . . .
1.5. . .
2. 1

++ . . . . . . . .
2.1. ++ . . . . . . .
2.2. . . . . . . . . . . . . . . .
3. . . . . . . . . . . . . . .
3.1. . . . . . . . . . .
3.2. . . . . . . . . . . . . . . . . . . . . . .
3.3. . . . . . . . . . .
3.4. . . . . . . . .
3.5. . . . . . . . . . . . . . . .
4. . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.1. . . . . . . . . . . . .
4.2. . . . . . . . . .
5. 2
. . . . . . . . . . . . . . . . .
5.1. . . . . . . . . . . . . . . .
5.2. . . . . . . . . . . .
5.3. . . . . . . . . . . . . . . .
249

3
5
5
7
8
9
13

21
21
49
58
58
61
66
68
69
70
70
73
76
76
79
80

6.

7.

8.

9.

5.4. , . . . . . . . . . . . . . . . . . . . . . 82
5.5. . . . . . . . . . . . . . . . . . 83
5.6. () . . . . 85
5.7. . . . . . . . . . . . . . . . . . . 95
5.8. . . . . . . . . . . . . . . 96
5.9. . . . . . . . . . . . . . . . . 100
5.10. . . . . . . . . . . . . . . . 101
- . . . . . . . . . . . . . . . . . . . . . . . 110
6.1. . . . . . . . . . . . . . . . . . . . . . 110
6.2. . . . . . . . . . . . . . . . . . 112
6.3. . . . . . . . . . . 117
6.4. , , . . 118
3
, . . . . . . . . . . . . . 120
7.1. . . . . . . . . . . . . . . . . 120
7.2. . . . . . . . . . . . . . . . . . 128
7.3. . . . . . . . . . . . . . . . 139
4
C . . . . . . . . . . . . . . . . . . . . . . . 143
8.1. PDCurses . . . . . . . . . . . . . . . 143
8.2. PDCurses . . . . . . . 146
8.3. PDCurses . . . . . . . . . . . . . 156
8.4. PDCurses . . . . . . . . . . . 163
8.5. . . . . . . . . . . . . . . . 173
5
. . . . . . . . . . . . . . . . . . . . . . . . . . . 177
250

9.1. . . . . . . .
9.2. WinBGIm . . . . . .
9.3. . . . . . . . .
9.4. . . . . . . . . . . . . . .
10. Qt . . . . . . .
10.1. . . . . .
10.2. .
. . . . . . . . . . . . . . . . .
10.3. QtDesigner .
11. 6
. . . . . . . . . . . .
11.1.
11.2. . . . . . . . . . . . . . .
12. 7
. . . . . . . . . . . . . . . . . . . . . . . . . . .
12.1. . . . . . . . . . . . . . . . . . . . . . . . . .
12.2. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . .

251

.
.
.
.

177
182
194
200

. 202
. 203
. 217
. 223
. 227
. 227
. 231

. 233
.
.
.
.
.

233
240
241
242
247

C++:

07.07.2009.
6084/16. .
. . . 15,8. .-. . 14,5. 100 .
______

394026 , ., 14