Академический Документы
Профессиональный Документы
Культура Документы
Devcpp 1
Devcpp 1
. , 1995-2009
I.
1.
......................................................................................... 3
2.
............................................................................................................... 7
3.
.................................................................................. 35
................................................................................................................... 38
9.
................................................................... 31
? .............................................................................................. 31
.................................................................................................. 34
............................................................................................................... 35
8.
Dev-C++ ............................................................................... 27
........................................................................... 31
7.
? ............................................................................................... 19
(for) ................................................................ 19
(while) ........................................................................................ 20
(do while) .................................................................... 22
..................................................................................... 23
............................................................... 24
................................................................................. 27
6.
? ........................................................................................ 14
if else............................................................................... 14
...................................................................................................... 16
switch ( ) ............................................. 17
....................................................................................................................... 19
5.
..................................................................................... 7
( )..................................................... 7
.................................................................................... 9
................................................................................ 11
................................................................................................... 14
4.
? ........................................................................... 3
................................................................................... 3
................................................................................. 4
............................................................................................... 4
? ....................................................................................... 5
................................................................................................ 5
............................................................................... 38
................................................................................................ 39
, ............................................................... 40
............................................................................................ 42
10.
................................................................................. 42
................................................................. 42
............................................................................. 43
............................................................................................................. 46
?................................................................................................ 46
http://kpolyakov.narod.ru
I.
11.
. , 1995-2009
.................................................................................................... 46
- ..................................................................... 48
.............................................................. 51
? .................................................................................. 51
........................................................................... 51
.................................................. 52
............................................................. 52
........................................................................................................... 53
http://kpolyakov.narod.ru
. , 1995-2009
1.
, . , , , ( )
. , ,
( , ).
, ,
1) ;
2) .
:
first.cpp
:
first.o
:
first.exe
?
, :
(),
;
;
( ) ( );
,
, , ( ).
: ()
, ,
.
, .
http://kpolyakov.narod.ru
. , 1995-2009
I.
*. *.cpp (
*.cpp , ++).
, , , .
(, ) *.o. , ,
(, ).
, (
*.a). *.exe, .
8 . :
main()
{
}
main (
, ). , main .
, , , exe-.
, - , ,
.
#include <stdio.h>
main()
{
printf("");
}
,
stdio.h
?
, ,
,
. ,
.
*.h ( C:\Dev-Cpp\include).
() 1 #include, .
, .
#.
http://kpolyakov.narod.ru
. , 1995-2009
.
#include.
printf.
, .
.
, ,
, ,
. ,
.
, , , -, (IDE integrated development environment).
,
( ).
Dev-C++ F9.
, ( , ). ,
, - .
,
, ;
,
.
, ,
,
. , .
#include <stdio.h>
#include <conio.h>
main()
{
printf("");
getch();
}
conio.h
//
/* */
?
getch().
conio.h.
http://kpolyakov.narod.ru
I.
. , 1995-2009
// .
/* ( ) */
( ). , .
http://kpolyakov.narod.ru
. , 1995-2009
2.
.
- . , : ( )
. .
- , . .
.
(
). , ( ) ,
-. , . ,
int ( integer ), 4 ;
, ( float floating point ) , 4
( char character ), 1
, , , .
.
(int, float char), . ,
. , , , .
.
int a;
// a
float b, c;
// b c
// ,
// x 4.56
char c, c2='A', m;
// , c2
// 'A'
( )
. .
.
http://kpolyakov.narod.ru
. , 1995-2009
I.
#include <stdio.h>
#include <conio.h>
main()
{
int a, b, c; //
printf ( " \n" ); //
scanf ( "%d%d", &a, &b );
//
c = a + b;
// ( )
printf ( ": %d + %d = %d \n",
a, b, c );
//
getch();
}
?
4 :
o ;
o ;
o ();
o .
(
, , ).
\n printf .
scanf.
,
o ,
http://kpolyakov.narod.ru
. , 1995-2009
o ,
c = a + b;
// a b c
printf
?
, ,
(
, , )
+
* /
%
,
,
abs(i)
fabs(x)
sqrt(x)
pow(x,y)
i
x
x
x y
http://kpolyakov.narod.ru
10
. , 1995-2009
I.
,
,
, 7/4 1. ,
. :
int i, n;
float x;
i = 7;
x = i / 4;
//
x = i / 4.;
//
x =(float) i / 4; //
n = 7. / 4.;
//
//
x=1,
x=1.75,
x=1.75,
n=1,
. a b ostatok,
:
ostatok = a % b;
,
() ,
.
,
,
, , ,
, .
:
x
( a + 5 * b ) * fabs ( c + d ) ( 3 * b c );
.
4x + 5
5x
y=
( 2x 15z )(3z 3) x + z + 3
y = (4*x + 5) / ((2*x 15*z)*(3*z 3)) 5 * x /
(x + z + 3);
,
:
i = i + 1;
, .
i .
http://kpolyakov.narod.ru
11
. , 1995-2009
: i, i.
()
i ++;
// ...
++ i;
i = i + 1;
()
i ;
// ...
i;
i = i 1;
, , .
- ( , -
), :
x += a;
x = x + a;
x -= a;
x = x - a;
x *= a;
x = x * a;
x /= a;
x = x / a;
x %= a;
x = x % a;
scanf printf
, . scanf,
, %d, %f %c ,
, .
printf , , , .
1234.
, , .
printf("[%d]", 1234);
[1234]
.
http://kpolyakov.narod.ru
12
. , 1995-2009
I.
printf("[%6d]", 1234);
1234]
printf("[%-6d]", 1234);
[1234
printf("[%2d]", 1234);
[1234]
6 , .
6 , .
2
,
.
, %d %c.
( ) : %f,
%e %g. %f.
printf("[%f]", 123.45);
printf("[%9.3f]", 123.45);
printf("[%-9.3f]", 123.45);
printf("[%6.4f]", 123.45);
[123.450000]
,
6 .
9 , 3
,
.
123.450]
[123.450
[123.4500]
9 , 3
,
.
6 (4
),
.
%e , , .
( ). ,
123.45 123.45 = 1.2345 10 2 . 1.2345
( 1 10), 2 (
10 ). %e ,
, . , e ( ).
printf("[%e]", 123.45);
[1.234500e+02]
, 6
.
printf("[%12.3e]", 123.45);
12 , 3
, .
printf("[%-12.3e]", 123.45);
[1.234e+02
1.234e+02]
12 , 3
, .
http://kpolyakov.narod.ru
13
. , 1995-2009
printf("[%6.2e]", 123.45);
6 (2
),
.
[1.23e+02]
%g ,
( ).
( ). .
printf("[%g]", 12345);
[12345]
printf("[%g]", 123.45);
[123.45]
printf("[%g]", 0.000012345);
[1.2345e-05]
printf("[%10.3g]", 12345);
1.23e+04]
printf("[%10.3g]", 123.45);
123]
printf("[%10.3g]",0.000012345);
1.23e-05]
, 6
.
10 , 3
, . ,
"%-10.3g".
http://kpolyakov.narod.ru
14
. , 1995-2009
I.
3.
.
. : , ,
. , .
:
if else
switch
if else
. .
:
, , . : , Max.
#include <stdio.h>
#include <conio.h>
main()
{
float A, B;
printf (" A B :");
scanf ( "%f%f", &A, &B );
if ( A > B )
{
printf ( " %f",
A );
}
else
{
printf ( " %f",
B );
}
getch();
}
#include <stdio.h>
#include <conio.h>
main()
{
float A, B, Max;
printf(" A B : ");
scanf ( "%f%f", &A, &B );
if ( A > B ) //
{
Max = A; //
}
else
{
Max = B; //
}
printf ( " %f",
Max );
getch();
}
http://kpolyakov.narod.ru
15
. , 1995-2009
?
:
if ( ) //
{
... // , ,
//
}
else
{
...
// , ,
//
}
, ,
else .
else , , if, .
, .
> <
,
>= <=
,
==
!=
, , ,
:
(: ,
, ...), :
if ( )
{
...
// ,
}
, :
#include <stdio.h>
#include <conio.h>
main()
{
float A, B, Max;
printf(" A B : ");
scanf ( "%f%f", &A, &B );
Max = A;
if ( B > A )
Max = B;
printf ( " %f", Max );
getch();
}
http://kpolyakov.narod.ru
16
. , 1995-2009
I.
,
; else if:
if ( A > 10 )
if ( A > 100 )
printf ( " ." );
else
printf ( " ." );
else
printf ( " ." );
, ( ) 2-3 ( ).
(, ..).
, . ,
25 40 (). :
#include <stdio.h>
#include <conio.h>
main()
{
int age;
printf ( "\n : " );
scanf ( "%d", &age );
if ( 25 <= age && age <= 40 ) //
printf ( " ." );
else
printf ( ", ." );
getch();
}
?
, :
o
_1
&&
_2
( )
_1
(0)
(0)
(1)
(1)
_2
(0)
(1)
(0)
(1)
_1 && _1
(0)
(0)
(0)
(1)
http://kpolyakov.narod.ru
17
. , 1995-2009
o (
)
_1
||
_2
_1
(0)
(0)
(1)
(1)
_2
(0)
(1)
(0)
(1)
_1 || _1
(0)
(1)
(1)
(1)
o ( )
!
,
A > B
! ( A <= B )
() :
o
o
o
o
o
,
,
>, <, >=, <=, ==, !=,
,
switch ( )
, if,
switch.
. , .
#include <stdio.h>
#include <conio.h>
main()
{
char c;
printf("\n :");
scanf("%c", &c); //
switch ( c )
//
{
case '': printf("\n"); break;
case '': printf("\n"); break;
case '': printf("\n"); break;
default: printf("\n !"); //
}
getch();
}
http://kpolyakov.narod.ru
18
I.
. , 1995-2009
?
switch , .
switch
( ). .
case,
;
, .
break switch. break, , , a
!
,
default ( , ).
, ,
, , switch :
case '':
case '':
printf("\n"); break;
case '':
case '':
printf("\n"); break;
.
http://kpolyakov.narod.ru
19
. , 1995-2009
4.
, 10 . , 10 printf, 200 ,
. .
- , .
(for)
( ),
- .
repeat . , .
.
, () . , .
repeat , for. -,
( ),
. , 10 .
#include <stdio.h>
#include <conio.h>
main()
{
int i;
for ( i = 1; i <= 10; i ++ )
{
printf("");
}
getch();
}
//
//
//
//
//
( )
( )
?
for ,
.
for .
for
:
o : ,
;
o , ; ,
; ,
(, ,
);
http://kpolyakov.narod.ru
20
. , 1995-2009
I.
o ( ).
, .
:
for ( i = 0; i < 10; i ++ ) {
...
; ,
.
, (
).
, ,
2-3 ( ).
?
, N 1 N
1 1
2 4
...
#include <stdio.h>
#include <conio.h>
main()
{
int i, N;
// i
printf ( " N: " ); //
scanf ( "%d", &N );
// N
for ( i = 1; i <= N; i ++)
// : i 1 N
{
printf ( " %d %d\n", i, i*i);
}
getch();
}
: N , i ,
1 N. N
scanf %d (
).
i = 1, i
(i ++). i <= N.
. .
(while)
, - , , .
: ,
http://kpolyakov.narod.ru
21
. , 1995-2009
( , ; , ). while, .
. , .
. 10 , , . ( ) ,
. , , , .
#include <stdio.h>
#include <conio.h>
main()
{
int N;
// ,
int count=0; // -
printf ( "\n N: " );
scanf ( "%d", &N );
//
// N
?
while ,
.
while .
while ,
. ( ),
.
> <
>= <=
==
!=
,
,
, ( ).
(), ; ,
.
, , ,
:
while ( 1 ){ ... }
//
http://kpolyakov.narod.ru
22
. , 1995-2009
I.
while ( 0 ){ ... }
//
; ,
.
, (
).
, ,
2-3 ( ).
(do while)
, ,
, .
( ,
). ,
: , , . , .
. . , .
(
fool proof).
, . ,
, .
, ( 10)
.
#include <stdio.h>
#include <conio.h>
main()
{
int N, sum; // sum -
sum = 0;
//
do {
//
printf ( "\n :" );
scanf ( "%d", &N );
}
while ( N <= 0 ); // N <= 0
while ( N > 0 ) {
sum += N % 10;
N /= 10;
}
printf ( " %d\n", sum );
getch();
}
?
dowhile , .
http://kpolyakov.narod.ru
23
. , 1995-2009
do, .
while, , ; , .
( ),
, .
(), ; ,
.
; ,
.
, (
).
, ,
2-3 ( ).
, . break.
, (
) continue.
. ,
. ,
, , .. ,
.
, , .
, ,
.
.
while(1){...} (, ).
break.
, , . continue.
#include <stdio.h>
#include <conio.h>
main()
{
int A, B;
while ( 1 ) // , break!
{
printf ( "\n :" );
scanf ( "%d%d", &A, &B );
if ( A == 0 && B == 0 ) break; //
if ( B == 0 ) {
printf ( " " );
continue; //
}
http://kpolyakov.narod.ru
24
. , 1995-2009
I.
printf ( " %d %d", A/B, A%B )
}
getch();
}
?
,
(, ), , break:
while ( 1 ) {
...
if ( )
...
}
break;
. 20
1 2 3 4
S = + +L
2 4 8 16
, .
,
;
;
2 ;
(, ..).
zc
,
d
z, c d ( )
ai =
i
z
c
d
1
1
1
2
2
-1
2
4
3
1
3
8
4
-1
4
16
5
1
5
32
S 0; ;
http://kpolyakov.narod.ru
25
. , 1995-2009
z, c d ( ):
z = 1, c = 1, d = 2.
20 :
o ;
o z, c d .
#include <stdio.h>
main()
{
float S, z, c, d;
int i;
S = 0; z = 1; c = 1; d = 2; //
for ( i = 1; i <= 20; i ++ )
{
S = S + z*c/d;
//
z = - z;
// z, c, d
c ++;
d = d * 2;
}
printf(" S = %f", S);
}
, .
.
1 2 3 4
S = + + L,
2 4 8 16
, 0,001.
, . , , while ( do-while). .
#include <stdio.h>
main()
{
float S, z, c, d, a;
S = 0; z = 1; c = 1; d = 2; //
a = 1;
// , 0.001
while ( a >= 0.001 )
{
a = c / d;
S = S + z*a;
z = - z;
c ++;
d = d * 2;
}
printf(" S = %f", S);
}
//
//
// z, c, d
http://kpolyakov.narod.ru
26
I.
. , 1995-2009
, a ( ) 0,001. ,
, , 0,001.
, a ,
.
http://kpolyakov.narod.ru
27
. , 1995-2009
5.
Dev-C++
?
.
debugging ( ) ,
Mark II 1940 (), - .
:
(, printf print); , , ;
, , ;
( , , );
,
, .
, , . , ,
. .
. ?
-, , ,
( ). -,
, , . , , 2- , 3- , .
(1, 2 3) ?:
main()
{
int i, X;
printf(" :\n");
scanf("%d", &X);
printf(" X=%d\n", X);
// 1
for(i=1; i<10; i++)
{
printf(" : i=%d, X=%d\n", i, X); // 2
...
}
printf(" : X=%d\n", X);
// 3
...
}
1 , X .
http://kpolyakov.narod.ru
28
I.
. , 1995-2009
2 . , .
3 X .
, , -.
:
. -
, ,
. , , .
, //,
, /* */ (
- ):
main()
{
int i, X;
printf(" :\n");
scanf("%d", &X);
// X *= X + 2;
for(i=1; i<10; i++) X *= i;
/* while ( X > 5 ) {
i = i * X;
} */
...
}
.
,
( Ctrl+F5)
.
.
Dev-C++ GDB.
,
,
.
. . ,
Ctrl+F5 ,
.
,
, F8.
http://kpolyakov.narod.ru
29
. , 1995-2009
. (
), F7.
(
). Shift+F7.
Ctrl+F7 . Ctrl+Shift+F2.
,
, .
.
.
, .
, ,
, .
, ( ). () , , . ,
N=5 (, 5 ). .
#include <stdio.h>
main()
{
int
N, i, count = 0;
printf(" ");
scanf("%d", &N);
for ( i = 2; i <= N; i ++ )
if ( N %i == 0 )
count ++;
if ( count == 0 )
printf(" ");
else printf(" ");
N
5
i
?
2
3
4
5
count
0
, , N
, , count . ,
http://kpolyakov.narod.ru
30
I.
. , 1995-2009
, .
i < N.
( )
. , ,
, , 1 2. , , , ,
.
http://kpolyakov.narod.ru
31
. , 1995-2009
6.
. ,
, . , .
#include <graphics.h>
#include <conio.h>
main()
{
initwindow ( 400, 300 ); // 400 300
// ... ()
getch();
//
closegraph();
}
//
?
graphics.h.
initwindow , .
.
closegraph.
- . (0,0)
, .
, (0,0),
.
X , Y ( ).
x
, y .
y
x
( x, y )
http://kpolyakov.narod.ru
32
. , 1995-2009
I.
16 :
0
1
2
3
4
5
6
7
BLACK
BLUE
GREEN
CYAN
RED
MAGENTA
BROWN
LIGHTGRAY
8
9
10
11
12
13
14
15
DARKGRAY
LIGHTBLUE
LIGHTGREEN
LIGHTCYAN
LIGHTRED
LIGHTMAGENTA
YELLOW
WHITE
-
-
-
-
-
, ( True Color, ).
: (R) , (G) (B).
0 255 (256 ), , 2563 = 16 777 216 . COLOR, R , G B ( ). ,
, :
COLOR(0,0,0)
COLOR(255, 0, 0)
COLOR(0, 255, 0)
COLOR(0, 0, 255)
COLOR(255, 255, 255)
COLOR(100, 100, 100)
COLOR(255, 0, 255)
COLOR(0, 255, 0)
setcolor :
setcolor ( 10 ); // -
( , ), ,
- . :
setcolor ( COLOR(255,0,255) ); //
, ( )
.
.
putpixel:
putpixel ( x, y, 14 );
// (x,y)
getpixel :
n = getpixel(x, y); // (x,y) n
line:
line ( x1, y1, x2, y2 );
// (x1,y1)-(x2,y2)
: ( )
(x1,y1) moveto, (x2,y2) lineto:
http://kpolyakov.narod.ru
.
moveto ( x1, y1 );
lineto ( x2, y2 );
33
. , 1995-2009
// (x1,y1)
// (x2,y2)
lineto
(x2,y2).
:
setcolor(12);
//
moveto (x1, y1); //
lineto (x2, y2); //
lineto (x3, y3); // ..
lineto (x4, y4);
lineto (x5, y5);
(x1, y1)
(x5, y5)
(x2, y2)
(x3, y3)
(x4, y4)
( ).
setcolor, rectangle:
setcolor ( 9 );
rectangle (x1, y1, x2, y2);
(x1, y1)
(x2, y2)
(x1, y1)
bar. , setfillstyle:
(x2, y2)
setfillstyle ( 1, 12 ); // 1, 12
bar (x1, y1, x2, y2);
setfillstyle :
0
1
3,4,5,6
7,8
9,10,11
.
, circle:
setcolor ( COLOR(0,255,0) ); //
circle ( x, y, R );
circle
. , , :
circle ( 200, y0+20, R );
- , .
floodfill:
(x, y)
setfillstyle ( 1, 11 ); // 1, 11
floodfill (x, y, 0);
// 0
http://kpolyakov.narod.ru
34
I.
. , 1995-2009
(x,y) (!) .
, , , . , floodfill.
(x, y)
outtextxy .
(x,y) .
setcolor:
setcolor ( 9 );
outtextxy ( x, y, "" );
,
. .
#include <graphics.h>
#include <conio.h>
main()
{
initwindow (440, 300);
setfillstyle (1, 9);
bar (100,100,300,200); //
setcolor (13);
//
rectangle (100,100,300,200);
moveto (100,100);
// -
lineto (200, 50);
lineto (300,100);
setfillstyle (1, 14);
//
floodfill (200, 75, 13);
setcolor (15);
circle (200, 150,50);
//
setfillstyle (1, 10);
floodfill (200,150, 15); //
setcolor (12);
outtextxy (100, 230, "Sharik's house.");
getch();
closegraph();
}
(200, 50)
(100, 100)
(300, 200)
Sharik's house
http://kpolyakov.narod.ru
35
. , 1995-2009
7.
(, ). ,
.
, ,
- , .
,
(, ) ( , ). , () ( ).
(x,y) ,
(x, y-60)
100 60. : (x,y-60) (x+100,y). ,
60
Y .
Tr, .
(x, y) 100 (x+100, y)
:
Tr ( x, y, c );
c .
, (, -) ( ). , , . :
void Tr ( int x, int y, int c )
{
moveto ( x, y );
//
lineto ( x, y-60 );
//
lineto ( x+100, y );
lineto ( x, y );
setfillstyle ( 1, c ); //
floodfill ( x+20, y-20, 15); //
}
, ,
( ) .
(), - , .
, ,
main, - . ,
, .
.
void Tr ( int x, int y, int c )
http://kpolyakov.narod.ru
36
I.
. , 1995-2009
void , - (, - ),
- 2. .
.
, . , .
( x y) ( c).
60
, , . , - (100,100)
() .
,
100
.
? ,
(100,100). , 100 60,
: (200,100) (200,160) .
:
#include <conio.h>
#include <graphics.h>
void Tr ( int x, int y, int c )
{
moveto ( x, y );
//
lineto ( x, y-60 );
//
lineto ( x+100, y );
lineto ( x, y );
setfillstyle ( 1, c ); //
floodfill ( x+20, y-20, 15); //
}
main()
{
initwindow (400, 300);
Tr (100, 100, COLOR(0,0,255));
Tr (200, 100, COLOR(0,255,0));
Tr (200, 160, COLOR(255,0,0));
getch();
closegraph();
}
. (, )
. ,
, .
Tr (100, 100, COLOR(0,0,255));
. (
). (100) x, .. ,
2
, - .
http://kpolyakov.narod.ru
37
. , 1995-2009
Tr,
.
,
, , , :
Tr (200, 100, COLOR(0,255,0));
Tr (200, 160, COLOR(255,0,0));
?
, : .
void. ,
, .
, . .
(int, float, char).
, .
, , , .
, .
,
.
(
).
, ..
; , , . , :
Tr ( 100 );
( ) , , 50 (2 25
), .
return,
.
return:
.
http://kpolyakov.narod.ru
38
I.
. , 1995-2009
8.
, ,
. : - ( ,
).
(), . - .
. , .
. , . , .
, , 10. 10, , ..
-, .
#include <stdio.h>
#include <conio.h>
int SumDigits ( int N ) //
{
//
int d, sum = 0;
while ( N != 0 )
{
d = N % 10;
//
sum = sum + d;
N = N / 10;
}
return sum;
// sum
}
//
main()
{
int N, s;
printf ( "\n ");
scanf ( "%d", &N );
s = SumDigits (N);
//
printf ( " %d %d\n", N, s );
getch();
}
?
, : .
(int, float, char, ..) , .
,
.
http://kpolyakov.narod.ru
39
. , 1995-2009
return .
return.
,
. .
, , .
, -
. . ,
, .
, 1 ( ) 0 (
).
:
, .
- , -
.
?
. N , . ,
.
http://kpolyakov.narod.ru
40
I.
. , 1995-2009
.
, .
#include <stdio.h>
#include <conio.h>
int Prime ( int N ); //
main()
{
int N;
printf ( "\n ");
scanf ( "%d", &N );
if ( Prime(N) )
//
printf ( " %d - \n", N );
else printf ( " %d - \n", N );
getch();
}
int Prime ( int N ) //
{
for ( int i = 2; i*i <= N; i ++ )
if ( N % i == 0 ) return 0; // !
return 1; // !
}
-. , .
. ,
.
: , , .
( ) , , .
#include <stdio.h>
-
#include <conio.h>
int MinMax ( int a, int b, int &Max )
{
if ( a > b ) { Max = a; return b; }
else
{ Max = b; return a; }
}
main()
{
int N, M, min, max;
printf ( "\n 2 ");
scanf ( "%d%d", &N, &M );
min = MinMax ( N, M, max ); //
printf ( " %d, %d\n", min, max );
getch();
}
http://kpolyakov.narod.ru
41
. , 1995-2009
, . , , -, .
& (,
), , . max
.
?
, , :
o return
o
,
(, a b MinMax,
N M ).
.
( , )
: &
, (
max ).
(
).
http://kpolyakov.narod.ru
42
I.
. , 1995-2009
9.
:
, #include
( ):
const N = 20;
, . ( ).
. , ,
.
, . , .
.
. , ,
(), ,
. .
#include <stdio.h>
int var = 0;
//
void ProcNoChange ()
{
int var;
//
var = 3;
//
}
void ProcChange1 ()
{
var = 5;
//
}
void ProcChange2 ()
{
int var;
//
var = 4;
//
::var = ::var * 2 + var; //
}
main()
{
ProcChange1();
// var = 5;
ProcChange2();
// var = 5*2 + 4 = 14;
ProcNoChange();
// var
printf ( "%d", var ); // (14)
}
http://kpolyakov.narod.ru
43
. , 1995-2009
?
.
,
, .
, :
,
;
, -
;
, , .
:
( ..);
-
.
, , .
, , .
?
? , ( ) :
#include <stdio.h>
main()
{
float x, y;
printf ("\n 2 ");
scanf ("%d%d", &x, &y);
printf (" %d ", x+y);
}
, (, ).
,
( )
http://kpolyakov.narod.ru
44
I.
. , 1995-2009
:
,
, . , , -.
, . , , , :
void Square ( int x, int y, int a );
void Kvadrat ( int x, int y, int a );
(
-). :
, , , .
25-30 ,
. ,
.
. .
,
, ( 1) ( 0).
//**********************************************
// ROMB
//
(x,y)
//
a, b
//
color, colorFill
//
1, , 0
//
//**********************************************
int colorFill )
{
if ( (x < a) || (x > 640-a) || (y < a) || (y > 480-b) )
return 0;
//-----------------------------------
setcolor ( color );
line ( x-a, y, x, y-b );
line ( x-a, y, x, y+b );
line ( x+a, y, x, y-b ); line ( x+a, y, x, y+b );
setfillstyle ( SOLID_FILL, colorFill );
floodfill ( x, y, color );
return 1;
}
http://kpolyakov.narod.ru
45
. , 1995-2009
(, ,
). , . :
2 3 .
.
o for, while, do-while
o if else
o switch
( ):
main()
{
int a = 1, b = 4, i;
for (i=1; i<2; i++)
{
if ( a > b )
{
b = a + i;
}
else
{
a = b + i;
}
}
printf ( "%d %d\n", a, b );
}
http://kpolyakov.narod.ru
46
I.
. , 1995-2009
10.
( animate ).
, , , ..
, :
,
. Esc.
, - (
) . , Esc.
, .
(x, y)
20 .
. ,
()
(x,y).
(x+20, y+20)
(x+20,y+20).
, , .
:
1) ;
2) ( 10-20 );
3) ;
4) ;
5) 1.
, ( Esc ).
. , .
, x y, color.
, .
void Draw( int x, int y, int color )
{
setfillstyle ( 1, color ); // , color
bar ( x, y, x+20, y+20 );
//
}
, .
http://kpolyakov.narod.ru
47
. , 1995-2009
, , ,
. , , ,
while ( ).
Esc. 400 400 . x
0 399, :
x + 20 < 400
,
.
Esc.
getch. :
1. , - ; kbhit, 0 ( ), , , .
if ( kbhit() ) { ... }
2. ,
, getch. . 1 , 256 0 255.
Esc (27),
, , .
Esc
27
Enter
13
32
:
#include <conio.h>
#include <graphics.h>
void Draw ( int x, int y, int color )
{
setfillstyle ( 1, color ); // , color
bar ( x, y, x+20, y+20 );
//
}
main()
{
int x, y;
//
initwindow (400, 400);
//
setfillstyle(1, COLOR(0,0,255)); // ,
bar (0, 0, 399, 399);
//
x = 0; y = 240;
//
/* */
closegraph();
//
}
http://kpolyakov.narod.ru
48
. , 1995-2009
I.
,
/* */
, , Esc.
while ( x + 20 < 400 ) //
{
if ( kbhit() )
// ...
if ( getch() == 27 ) break;
// Esc,
Draw ( x, y, COLOR(255,255,0) ); //
delay ( 20 );
// ()
Draw ( x, y, COLOR(0,0,255) );
//
x ++;
//
}
while , .
Esc . , -
( kbhit), ( getch) ,
Esc, break.
,
20 , delay 20, . x .
?
- , kbhit.
0, , ,
- .
, ( )
getch.
, delay.
. ,
.
: , . x y 1
dx dy, :
dx
dx
dx
dx
>
<
=
=
0,
0,
0,
0,
dy
dy
dy
dy
=
=
<
>
0
0
0
0
, . if, switch,
.
http://kpolyakov.narod.ru
49
. , 1995-2009
.
switch
if
1
, ()
. ,
. ,
2 , , ( -, ). , :
75
72
77
80
:
75, 77, 72 80 , K,
M, H P.
, , -. , getch.
( ) , ,
.
while(1).
break ( ).
while ( 1 )
{
Draw ( x, y, COLOR(255,255,0) );
code = getch();
if ( code == 27 ) break;
Draw ( x, y, COLOR(0,0,255) );
switch ( code ) {
case 75: x --; break;
case 77: x ++; break;
case 72: y --; break;
case 80: y ++;
}
}
//
//
//
//
//
//
//
//
//
//
Esc,
, , , . dx dy,
. , , ,
http://kpolyakov.narod.ru
50
I.
. , 1995-2009
code,
switch.
dx = 1; dy = 0;
//
while ( 1 )
//
{
if ( kbhit() ) {
//
code = getch();
//
if ( code == 27 ) break;
// Esc,
switch ( code ) {
//
case 75: dx = -1; dy = 0; break;
case 77: dx = 1; dy = 0; break;
case 72: dx = 0; dy = -1; break;
case 80: dx = 0; dy = 1;
}
}
Draw ( x, y, COLOR(255,255,0) ); //
delay ( 10 );
//
Draw ( x, y, COLOR(0,0,255) );
//
x += dx;
//
y += dy;
}
http://kpolyakov.narod.ru
51
. , 1995-2009
11.
, . , - . :
? , ,
, .. , ,
, .
( , ..) .
,
, .
.
.
.
, , ,
.
, ,
, , , .
(
), . , ,
, OX a b.
.
, ,
. , , .
( ,
) .
, ,
-
.
http://kpolyakov.narod.ru
52
. , 1995-2009
I.
(
stdlib.h , ):
n = rand();
0
RAND_MAX ( 32767)
srand ( m );
, m
[a,b].
(a=0),
: N , N,
[0,N-1].
int random ( int N )
{
return rand() % N; // [0,N-1]
}
( )
[0,N-1] .
[a,b]. ,
k = random(N) + a;
[a,a+N-1].
[a,b], b=a+N-1, N=b-a+1.
[a,b]
k = random(b-a+1) + a;
.
rand() RAND_MAX:
x = (float) rand() / RAND_MAX;
[0,1) ( ,
).
[0,1) 1, b-a. b-a a, .
[a,b)
x = rand()*(b-a)/RAND_MAX + a;
. ? :
http://kpolyakov.narod.ru
53
. , 1995-2009
, . , , .
x [0,399],
y [0,299] (x,y).
, , .
COLOR (R), (G) (B)
, [0,255].
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
int random (int N) { return rand() % N; } //
main()
{
int x, y, R, G, B;
initwindow (500, 500);
while ( ! kbhit () ) {
//
x = random(400);
//
y = random(300);
R = random(256);
// (R,G,B)
G = random(256);
B = random(256);
if ( getpixel(x,y) != 0 )
//
putpixel ( x, y, 0 );
//
else
// ...
putpixel ( x, y, COLOR(R,G,B) ); //
}
getch();
closegraph();
}
?
, - ,
kbhit, 0, , ,
. , ,
getch. , :
while ( ! kbhit() ) { ... }
, , getpixel.
http://kpolyakov.narod.ru