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

+ +


++
I.

..

2005
. + + , I.

. 2 0 0 5 . . . 2 -
.

3- C/C++,
-. . i

C/C++,
, .
.

1.
2. 10
3. 17
4. 24
5. 30
6. 37
7. 45
8. while do..while. 53
9. 60
10. 67
11. 75
12. 81
88

!

,
.
, 25
,
, , , .
, :
.
, 200
, ,
, .
.
,
- .
.
,
. , *;
1. .
2. .
3. .
.
- .
.
,
( , , ..).
-, ..
.
. , ,
:
+1300042774
+1400593419
+1200274027
,
.

. .

- .
(overpay) (basepay)
(grosspay),
:
LOAD basepay
ADD overpay
S T O R E grosspay
, ,
.
,

.
,
. ,
, . ,
, :
grosspay = basepay + overtimepay
,
. , ,
. ++ -
, ,
. , + +
.

++?

+ + -
. , ,
, ,
,
.
.
,
. + + ,
. + +
, ,
( main)
, ++
.
, .
+ + ,
.

+ +

70- ,
Bell T e l e p h o n e L a b o r a t o r i e s .

Unix. Unix PDPr-
7 PDP-11.
, , , , ,
BCPL. BCPL 1969 .
" "
. Unix + + ,
1974 - 7 5 U n i x Bell L a b o r a t o r i e s ,
,
, .
1978 . "
". , " "
"K&R" - , .
"K&R" P D P - 1 1 , I n t e r d a t a 8/32, Honeywell
6000 I B M 370. .
70-
8080 Z80 / .
Tiny-C (" ") - ,
. ,
Basic Microsoft. 1980.
Small-C (" ") / 8080.
Small-C, ,
Small-C. , )
Tiny-C. Small-C
, , ,
. Small-C
++ 8080. ,
.

BDS-C
/ , .

.
B D S - C , /
.
+ + . 1981 ., IBM PC,
.
IBM PC .

8080, IBM PC.

IBM P C .
1983 . (ANSI)
X3J11 .
, ,
. -
, I B M P C ,
. X3J11
. ,
.
IBM PC
1 ,
, ( Borland 3.1 IBM PC
).
++ - ,
ANSI ,
.
Borland + + 3.1

B o r l a n d + + 3.1
. . F i l e
:
New - ;
Load - ;
Save - ;
Save as - ;
Quit - .
, ,
. New ,
. . : ,
( N O N A M E 0 0 . C P P ) ,
( , Alt + ),
( ).
. - ( ,
Alt + F 3 ) . ,
. ,
- . F6.
( )
( ) ,
( F5)

.



,
-

1:1:

,
,


( )
Ctrl + F9. ,
. , .
:

, F3. ,

( ) F2.
Borland + + 3.1 , <
.
.
, , Shift
. , (.
Edit):
(Ctrl + I n s ) ;
(Shift + I n s ) ;
( + + ) ;
(Ctrl + D e l ) .
,
Ctrl + F 1 .
, Ctrl + Y.
( - )
Alt + B a c k s p a c e . : Shift + Alt
+ Backspace.


- H e l l o w o r l d !!! .
Borland ++ 3.1,
:

+ +

/* */
# include <stdio.h>

void main() //

printf("Hello world ! \n") ;

//

. "Hello world!",
,
Borland + +. ,
AU+F5.

.
/* */.
.
.
. .
//, , ,
. , <
.
. include
.
printf(), stdio.h - .
+ + , .
. ,
, - .
+ + , ,
++
. , ,
?
, , - .
, , ,
,
, ..,
:

#include <math.h>

math.h - , .
C/C++ .
.
:

void main()
{

main(), C / C + + ,
. main()
. .
- printf().
.
. .
(
). , printf(), ,
-"\". . -
, Hello w o r l d ! .
, .

. . . 7.30. - 2 ,
26 -
15, , , 45 .
-
. , , - 9 .
- (, ), -
. , ,
, ,
.
,
- .
, ,
.

1.
(,) ( ) .
2.
S = * .
3.
V=S*h.

:
1. . ,
, ,
. (
S,
). ,
, :
.
, ,
.
2. . ,
.
,
. *
3. . ,
, ,
.
-:
-;
;
(, ,
.).
- -
.
, -, .
2 . 1 . .
: S = .
- .
. -
:

- :
, ,
, ,
-, . ,
" " . " " (..
), (.. ), ,
, . . , .
.

,
. -
.

.
,
- , .

.
, .

- .
. ;
-. .

- " " .
.

. ,

.

. ,
, : (1) , (2)
, (3) , (4) , (5) .
.
,
: ( 1 ) , ( 2 ) , ( 3 ) , ( 4 ) ,
(5) .
.
.
.

.
2.2. - ,
, .
.
"". , ,
. ,
. .
.
, , .

,
. , "", ,
"". , ,
.

.
.

> ( ) , " "


(.. - ), "",
. nncjfa
. " " . , .
, ,
. ,
, ,
.
2.3. .
. 3 .
, . , \
, ,
.
. ?

-
. ,
,
.
, , ,
, ,
,
.
2.4. - ,
. + b =
2 .
2.1. - , printf(),
, ,
, (),
100 ( ) ( ) .

2.2. , - . -
, printf, ,
, - .

2.3. , b, . - ,
printf, ,
, , .

2.4. . - , printf,
, ,
, .

2.5. 4 . - ,
printf, ,
, .

2.6. - , printf,
, ,
.

2.7. 3 5 . - ,
printf, ,
, - 4 ,
.
3

- ,
, - .
.
- ,
. - , , ,
.
, , ,
.

BORLAND
- ,
.
BGI , *.bgi (BGI - Borland Graphics
Interface). ,
. egavga.bgi. ,
. O p t i o n s - > Linker - > Libraries...
G r a p h i c s library.

,
().
, .
, (pixel).
. , 640x480. ,
640 480 .
: 320x200, 640x480, 800x600, 1024x768 . .
(.. (0,0))
. X , ,
Y , , - .
X , Y .
, .. ,
, , (639, 4 7 9 ) .
(0,0) 639 X
( p i x e l d e p t h ) .
, , ,
, . ,
8 256 ( 2 = 2 5 6 ) .
16- 65536 .
4, 8, 16, 2 4 3 2 .
- 640x480 4 , ..
16 . VGA.
SVGA
(SuperVGA) . - , ,
E G A , C G A , 2 16 ,
320x200.

, ,
.
/* */
#include<conio.h>
#include<graphics.h> /
void main()
{ //
int gd, gm;
gd = DETECT;
initgraph(&gd, &gm, "z:\\bgi");
// .
getch() ;
closegraph();
} //
graphics.h -
. conio.h.
,
- getch(); .
, ,
.

int gd, gm;


gd = DETECT;

(
) . gd gm ( g r a p h i c s d e t e c t g r a p h i c s
m o d e ) . ( C G A , E G A , V G A ..).
DETECT ( ),
()
. .
, .

initgraph(&gd, &gm, "z:\\bgi") ;

initgraph() .
. - gd g m .
, egavga.bgi.
, .
, , , ..

closegraph();
closegraph().

. ,
, ?
. .
16 . ,
, ( 0 15),
.


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

, , :

setcolor(color) - .
. color 0 15.

putpixel(x, , c o l o r ) - (,) color.

line(xl, yl, 2, 2) - (xl,yl) (2,


2).

rectangle(xl, yl, 2, 2) - ,
, (xl,yl)
(2,2) .

circle(x, , ) - ( , ) .

(, , start, e n d , ) - (,)
s t a r t end.

setbkcolor(color) - . color
0 15.

setfillstyle(p, color) - .
0 12. - color, .
floodfill(x, , border) ,
border, (,) .

, graphics.h,
( F1).

3.1. , .

+ + :

#include <graphics.h>
#include <conio.h>
void main()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"z : W b g i " )

setbkcolor(15 ) ; // -
I/
setcolor(l) ; // , :-::/_
c i r c l e ( 100 , 100 , 3 5 ) ; //
setcolor(O);
circle(160,100,35)
setcolor(4) ;
circle(220,100,35)
//
setcolor(YELLOW) ; // ,
circle(130,140,35); //
setcolor(GREEN);
c i r c l e t 1 9 0 , 1 4 0 , 35 ) ;

getch( ) ;
closegraph();
}

, ?

3.2. ,

, ,

, .

delay(n) dos.h - ,

(1

= 1000 ).

+ + :

setcolor(WHITE);//
rectangle(10,10,175,470);
circle(90,90,70);
circle(90,235,70) ;
circle(90,380,70);

Betfillstyle(1,RED);// -
floodfill(90,90(WHITE) ; //

delay(2000);// 2

setfillstyle(1,BLACK);//
floodfill(90,90,WHITE);

setfillstyle(1,YELLOW); // -
floodfill(90,235,WHITE); //

d e l a y ( 2 00 0 ) ; // 2
setfillstyle(1,BLACK); // -
floodfill(90,235,WHITE);

setfillstyle(1,GREEN); // -
floodfill(90,380,WHITE); //

3.3. ,
.

setcolor(RED); / /
arc(200,200,20,190,45);//
arc(200,170,170,340,45);//
c i r c l e ( 2 3 0 , 1 8 0 , 1 ) ; //
line(220,210,220,160);///
line(154,205,154,163);///
delay(3000) ; // 3
3. .
3.1. ,
.

3.2. , :

3.3. , -
:

3.4. , , .

3.5. , ,
, .
4


?
, ++, .

. ,
. + + :
: Z z;
: 0, 1,2, 3, 4, 5, 6, 7, 8, 9;

:
+

-
*

/ []
=
{}
< %
> &
!
#
? ~
1
? \

?

( )
. <Enter>,
,
.
,
.
,
.

, ,
(rt, first, huge, _ ) .
.
,
.
( ),
,
.
:
asm do if static volatile _ds
auto double int struct while far
break else long switch _ss pascal
case enum register typedef interrupt _es
char extern return union _cs huge
const float short unsigned cdecl export
continue for signed void near
default goto sizeof
,
; , ,
.
(, , )
,
.
, , .. ,
, .
.
, ,
- , ,
, . ,
,
-.
:

_ ;

: int ; long double b;


,
:
_ ! , 2 ;

: int ,,;

,
,
:

_ =;
: int =5;

_ 1=1, 2=2;
: int = ^ = 9;

_ !, 2=;
: int ,=10;


,
, , , .
char int,
-128 127, .
char ASCII (. 1),
.
int 16-
2 , 32- - 4 . 16-
, , Borland + + 3.1,
-32768 32767. ,
, 7 15, , 0
1 .
4 3 2 1
d
, , 25 = 1*2 + 1*2 + 0*2 + 0*2 + 1*2 = 11001b.
,
, , 1,
,
0. , char 127
01111111, -127 10000001.
unsigned,
.
: unsigned char - 0 255

unsigned int - 0 65535.
() float double.
, ,
3956.768, : 0.3956768+4, 0 . 3 9 5 6 7 6 8 * 10 .
, , , -
, .
:
1 2 3
. , 1 / 2 + 1 / 4 + 1 / 8 + . . . 1/2 + 1 / 2 + 1 / 2 + . . . ,
1 , , -
(, 10), , 1
, 1/2 - ,
0, ,
1 . ,
1 -)_1

l/2 ,
2(-1) .
float double ,
, , , ,
.
short long,
, .
short int 32-
, 16- ,
, .
:

, int
, , u n s i g n e d u n s i g n e d int .
, - , ,
, const.
: const int =5;

.


, , , ,
. , ,
: , , , .
. ,
, , , ,
,
, , . ,
.
- %.
.
++ ,
.
1.
(+ + ) ( + + ).
, .
, ,
.
. ,
,
= ;
.
,
, :
=2 * + 8 ;
, :

= = 5;
5, - ( 5).

, ,
, , , - .
:
= *3;
:
* = 3;
. , ,
, .
, .
,
, ( ) .
, ,
. ,
.
:
(_) ;
:
(int) ; *

. ,
,
s
, , .
.
char , ,
c h a r , int.
cout
iostream.h, c o u t < >
, clrscr()
conio.h:
4.1.
#include <conio.h>
ttinclude <iostream.h>

void main()
{ const int s=2; //
//
int =3;
char =4;
float z=3.14;
clrscr();//

z- z - a ; t l x 6.14
cout<<z<<endl; // << e n d l -

=s;//c 5
cout<<(int)c<<endl;

az-a; // 9
cout<<a<<endl;

+ + ; / /
cout<<(int)c<<endl ;

= c/s;//c 3
cout<<(int)c<<endl ;

a=a%c;//
cout<<a<<endl ;

a+=10;// 10
cout<<a<<endl;

getch();
}
4.
4.1
:

4.2
,, F :
5


- . -
. -
.
,
.

. printf()
?
1. : 1 100 .
2. : 1 = 100 .
3. : 1.00 =
100.00 .
4. ,
: 1.00 =
100.00 . *
, , (
). .
-
.
, , .
: , , 12 ., 20 . - .
:
- 12 .
- 20 .
++ printfQ.
stdio.h :

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

5.1. :
- 12 .
- 20 .

int =12, =20;


p r i n t f ( " - %i . \ - %i . " , , ) ;


:
1. , (
1 , , -
);
2. (\)- ,
. - .
:
\ - ;
\ - ;
\n - ;
\t - ;
\\ - \;
\" - ";
3. (% i) -
, .
%. ,
. :
% - ( char);
%s - ( c h a r ) ;
%d %i - ( int);
%f - ( float
double), , 3.141593. 6 (
).
%4.2f , 4 ,
. ,
, : %.2f;
% g % G - , . ..
5.2300 : 5.23.
1.

char a = 6 8 ; int a = 6 8 ; double a=68.15900;

printf("%c",a); D D -

printf("%i",a); 68 68 0
printf("%f',a); 68.159000
printf("%4f',a); 68.159000
printf("%4.1f',a); 68.2
printf("%.2f',a); 68.16
printf("%g'; - 68.159
,
.
, ,
,
. 68.159,
, : 68.16.
% , .
, %% %.
printf , ,
. , ,
, . ,
.
5.2.

int = 10, =20, z;


char ch;
float fl, f2;
double d;
z = x + y
ch =
fl = 3.14;
f2 = fl * 2.5;
d = 0.5;
printf( " \" );
printf( " = %i \", );
printf( " z %3 %31 ", , z ) ;
printf( " % % % ", ch, ch, ch );
printf ( "\t \n \ " };
printf( "%f %f %.2f ", fl, f2, d );

:

= 10
z 20 30
* * *



3.140000 7.850000 0.50

5.3. :
. , ,
, , , .
.

int =10, b=5, =2, d=3, h=3; //


float SI, S2; / / S I - , S2 -
SI = 0.5 * * h; *
S 2 = a * b - 3 * ( c * d ) ;
printf(" %2.1f \n", S 1 ) ;
printf(" %2.1f \n", S 2 ) ;

++ , - .
.
( conio.h):
textcolor(color) - . color
0..15;
textbackground(color) - .
, color;
gotoxy(x, ) - .
80x50, .. 80 ,
50. , .
5.4. , -

textbackground(LIGHTGRAY);
textcolor(YELLOW);
gotoxy(40,25);
printf("");

. scanf()
.
. ,
? ,
, , , d, h.
.
++ scanf
stdio.h. :

scanf (" ", &_1, &_2, ... &_) ;

int ;
scanf("%i", &); ?

printf.
, ..
. .
scanf ,
. '&' - .
scanf:
% - ;
%d %i - int;
%D %1 - long;
%f - 4Hcra(float);
%s - .

5.5. 3 ,
.
int , , z;
printf(" : " ) ;
scanf("%i", & ) ;
printf(" : " ) ;
scanf("%i %i", &, & z ) ;
^ p r i n t f (" = % i " , + + z) ;

5.6. ,
. : 1 .
float price, mass; //
printf(" 1 : " ) ;
scanf("%f", &price);
printf(" : " ) ;
scanf("%f", Smass);
p r i n t f ( " %.2f . \n ! price * mass);


++ , , /
. . ,
. ,
.
iostream.h. : cin cout -
.
. :

int ;
cin ; //
++;
c o u t << ; //

, :

int x l , 2, ;
c i n xl 2 ;
x l += 2 + ;
c o u t << " = " xl << "";

xl, 2 ,
" = ", , xl,
. ,
:
c o u t << " = " << xl <<endl;
, ,
.

34
5.

5.1. ,
, ,
- .
< >
< >
< >

7^. 5.2. , :

5.3. , -
= 3.140000 :
1)3 2)3.1 3)3.14 4)3.140

5.4. ,
, ,
.
:
.
:
(.) -> 10
() -> 1 0 0 0
(/) -> 3 7 , 3 *

: 373 000

5.5. ,
. , 12,5
12 . 50 .
:
.
-> 3 2 , 7

32,7 . - 32 . 70 .

5.6. . ,
.

5.7. ,
, , .
:
( )
-> 150
150 - 2 . 3 0 .

5.8. .
( % ) ( ) .
:
.
:
(.) -> 2 5 0 0
() -> 30
() - > 2 0
: 41,10 .
: 2541,10 .

5.9. , .
.

5.10. , ,
s
.

5.11. , , ,
. ( )
.
6

, ,
. , - .
:
2
> 0, = ;
< 0, = 2 + 1. ,
:
> 0, = ;
= 2 + 1.


,
.
++
if. :

if ( )
_1;
else
_2;

if (), else () - , _1 _2 -
. , ,
_1, ( ) - _2.
: " , _1, ( ) -
_2". 1 2 .
, .
, ++ :

if ( > 0)
= * ;
else
= 2 * + 1;
, .
- .
. : "" -
, , "" - .
, , :


> >7
>= >=8
< <7
<= <=8
== ==9
!=8

6.1. , -
.
/* */
f l o a t , , , max;
p r i n t f (" ! : \ " ) ;
scanf ("%f %f %f ", &a, &b, & c ) ;
i f ( a > b && a > )
p r i n t f ( " ! V n " ) ;
else
i f (b > c)
p r i n t f ( " ! \ n " ) ;
else
p r i n t f ( " ! \ " ) ;

.
3
: " > 10 < 40, , - 2
* + 3"? .
- "", "", "".
: && - "", || - "", ! - "".
: . ++
: - , -
. , , 0
1 - .
5 10, > 5
< 10. ++: > 5 && < 10.
5 10 :
< 5 > 10. ++: < 5 || > 10 , !( > 5 && < 10)
1

6.2. > 5 && < 10, < 5 || > 10,


!(<5), , =5.
int =5;
printf("%i\n",>5&&<10);
printf("%i\n",x < 5 | | x > 10);
printf("%i\n",!(x<5));
:
0
0
1

J
" > 10 < 40, x , - 2 * + 3"
:

if ( > 1 0 & & < 4 0 ) = * * ;


else = 2 * + 3;

(>, <, = ..).


, .
, , , . 0, .
,

if ( + 1) 1;
else 2;

. + 1 , 1, - 2.
>
. else
if, , :

if ( == )
if ( < d) = 5;
else = 12;
else = 34;
:


.
, :
if ()
{// ,
_1;
2;

_;
}
else
{// , .
_1;
_2;

__;
}
,
.
.
, :

if () _1;

, !, ( )
_1 . *
if :

++ .
- . :

= ? ^! : _2;

, , ,
_1, , - _2, .
?: if , if
, ?: .
,
. if :

int a, b, max;

if ( > b) max = ;
else max = b;

, ?:

int a, b, max;

max = > b ? : b;

, max , - .
++ .
, , , .
, ( = 5 + 3) 8.
, : (( = 5 + 3) < 10),
.
6.3. = 1/
. = 0, .

/* = 1/ */
float , ;
printf (" : \ " ) ;
scanf ("%f", & ) ;

if(x == 0) printf(" !\");


else *
{
= 1/;
printf(" = %.2f, = %.2f \", , ) ;
}

switch
if .
, .
, if. ++
: switch (),
.
, :

switch( )
{
case _1 : _1; break;
case _2 : _2; break;

case N : N; break;
default : _+1;
}

switch, case, default break.


. .
( ).
. ,
( ).
default , ( default
). default ,
, .
break,
switch.
switch, if, .
6.4. - .
. 2 ,
, , , ,
.

float a, b, res=0;
int ch;
scanf("%f %f", &a, & b ) ;
printf(" 1. \ n " ) ;
printf(" 2. \ n " ) ;
printf(" 3. \ n " ) ;

41
printf(" 4. ) ;
scanf("%i",Sen); //
switch( ch ) // ch
//
{
case res = a + b break;
case res = a - b break;
case res = a * b break;
case res = a / b break;
printf( '" ! ") ;
}
printf(" : %2.2f", r e s ) ;
.

if
6.1. , : ( )
- 100. , .
:
: 1 7 0 68
2,00 .
6.2. -.
:
-?
<Enter>
->1705
, - 1703.

6.3. .
:
?
->1939
.
6.4. , .
10 % , 1000 .
:
:
->1200
10%.
1080,00 .
6.5. 3 . ,
.
:
3 : 8 5 9
5 8 9

6.6. + b = 0.
2
6.7. + + = 0.

6.8. 60- , 12-


, : , , ,
. : , , , ,
, , , , , , .
. , 1984 -
.

6.9. ,
. ,
, - . ,
,
5 .

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

case
6.11. ,
.
:
: 11
.
6.12. , ( 1
999), (21 , 332 , 890 ).

6.13. , .
:
( ):31 12 2 0 0 4
!
!
1.1.2005

44
7


. , world!!! ,
, printf(),
printf() .
. ++
, .
,
.

:
for - .
while - ();
do while - ();

:
;

, break. ,
.
, " ".

for
,
, for.
- .
.
. ,
- (i =
0). , (i < 10).
, ,
. ,
. ,
, . (
) (i < 10). ,
.

.
:
;
;

.

45
7 . 1 . - , 50
.

"".
. i 50 ( i < 50).
- . ,
50 ( 0 4 9 . ).
,
i . (i < 50).
, . ,
, - "".
for :
*
for -zzz . . ::_1; ssqp :_2 ; 3)
{

for - . ,
.
:= : - . ,
, ,
.
=.::5_2 - . ,
. ,
.
5::-:::5_ - .
, , - 1, :
int i; s

for(i=0;i<50;i++)
{
printf{"*");
}
; for.
. i 50,
.
7.2. , N ,
.
int i,N,S=0,a;
/* i- ;
N- ;
S- ;
- .
*/
s c a n f & N ) ;
for(i=0;i<N;i++)
{
scanf("%i",&a); //
// ,
//.
S+=a; // , S=S+a;
}
printf("%i", S);//

, , , N=3
5,7 9.
i S
0 5 5 (0+5)
1 7 12 (5+7)
2 9 21 (12+9)

7.3. , yi = 2 + 1 = "
=0, 1, 2, 3, 4.

int i,yl,y2;
for(i=0;i<5;i++)
{
yl=i*i;
y2=2*i + 1;
printf ("x= %i yl= %i y2=%i\n",i,yl,y2! 9-
}

i(x) yi 2
0 0 1
1 1 3
2 4 5
9 7

4 16 9

1 2 3 4
, ,
. .
7.4. ,
0 10 . '

int i;
for(i=10;i>=0;i-=2)
{
printf("%i %i\n",i,i*i);
}

47
:
10 100
8 64
6 36
4 16
2 4
0


. , ,
, .
.
7.5. 5
]:
S
int i, j;
for (i = 0, j = 1 0 ; i < 5 ; i++, j-= 2)
{
printf("%i %i \n", i, j ) ;
}

i j.
i < 5. printf.
, .
i j ,
: i++ j = j - 2, .
for ,
. 7.5 :
int i=0, j = 10;
for (; i < 5; i++, j-= 2)
{
s
printf("%i %i \n", i, j ) ;
}
:
int i=0, j=.10;
for (; i < 5; )
{
printf("%i %i \n", i, j ) ;
i++;
j 2;
}
, .
i j :

i j
0 10
1 8
2 6
3 4
4 2


, ;-
. for( ; ; ), ,
, .

48
, , ,
, .
break
continue.
break , a continue
, .
7.6. , (
). ,
. ,
.

LONG pwd=12 34 5 , pwdl;


for(;;) //
{ printf(" :")
scanf("%I",&pwdl);
if(pwdl!=pwd) //
{
printf(" ! ! \ " ) ;
continue; //
}
printf(" ! ! \ " ) ;
break; //
}

,
, . ,
.
,
-
. Ctrl + Break.
,
.

.
, , .
.
7.7. , 3 ( 7
) 0 10
.
:
0 2 8 3 2 7 9 3 | 34
3 1 5 7 0 9 3 8 | 36
5 8 9 0 2 4 8 7 | 43
,
randomize(),
stdlib.h.
(). -
, ( - ).
# include <stdlib.h>

int a,s=0;
randomize(); //
for(int i=0;i<3;i++) //
{ for(int j=0;j<8;j++)//
{
printf("%2i",a=random(10));
9

s+=a; //
}
printf("I %i\n",s);
s=0; // ,
//
//

50
7.
7.1. , .

7.2. ,
.
:

1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

7.3. ,
.
.
:
.
:12
12 156.

7.4. ,
1,3,5,7.... .
:
1,3,5,7...
: 4
4 16. ,

2
7.5. , '/ + / + 3/+- - + N / n + i
N .
:
N: 5
3.55

7.6. , ( )
2
= 5 - + 2 -1 1 0,2.

7.7. 1 ,
. .
, 3! = 6 0! = 1. , ,
.
:
.
, : 7
71 = 5040.

7.8. ,
0 20. 10 .
.

51
, , : 10
9 - , 8 / 7 - , 6 / 5 -,
5 - .
:
:
15 11 = 1 6 5
11 X 5 = 55
3 8 = 25
! 3 * 8 = 24
7 13 = 91
8 4 = 32
12 13 = 156
18 19 = 3 4 2
2 1 = 2
5 19 = 95
8 8 = 64
: 9 - .

52
8

while do..while
, ,
while do...while.

while >
, ,
. , ,
. ,
while.
- while :

. ,
, - .
-, ,

:
while ()
{
//
/ /
}
while .
, . ,
, .
, .
, .
8.1. , = ,
= 2, 4, 6, 8, 10.
++:
i n t , ;
= 2;
while ( <= 10)
{
= * ;
p r i n t f ( " = %i = %i \ " , , );
= + 2;
}
:

X
2<= 10 . 4

4 4<=10 16
6 6 <= 10 36
8 8 <= 10 64
10 10 <= 10 100
12 12 <= 10

,
, :

while () ;

i n t =0;
w h i l e (n<10) printf("%i",n++);

8.2. ,
, .

long n,i=10,k=l;
s c a n f & n ) ;
while(n/i!=0)
{
k++;
i*=10;
}
cout<<kendl;
10, 100, 1000 ..,
.
, .
54
:
= 1234
i (n/i!=0)
10 1234/10 ! = 0 2
100 1234/100 ! = 0 3
1000 1234/1000 ! = 0 4
10000 1234/10000 != 0

do...while
do...while while, ,
(
), .
().
- do...while :

,
. , ,
.
do...while do, ,
while.
.
do
{
//
/ /
}
while ();

while, ,
.

do ; while ();
:
i n t n=0;
d o p r i n t f ("% i " , n + + ) ; while (n<10);

8.3. , = ,
= 2, 4, 6, 8, 10.
++:
i n t , ;
= 2;
do
{
= * ;
printf("%f,%f \n", , ) ;
+= 2 ;
}
while ( <= 10);

= 2.
:
1. ;
2. ;
3. 2
.
, <= 10 .
:

X +=2
2 4 4 4<= 10
4 16 6 6<= 10
6 36 .8 8 <=10
8 64 10 10 <=10
10 100 12 12 <= 10

8.4. , ,
. 4,
.

long ,1;
long i;
do
{
1=1;
i=10;
cout<<" :"<<endl;
cin>>n;
w h i l e ( n / i =0)
{
1++;
i*=10;
}
if(11=4)
c o u t " ! " e n d l ;
}
while(1!=4);

- while do...while.

s
.
, - ,
. do...while while
- .
, ,
.
:
int i=8,n;
while(i>0)

n++ ;
}
,
, ,
. :
int i=0;
while(1==1)
{
i++;
if(i==8)
break;
}
, i ,
break.

57
8. while do..while
8.1. }, ,
, , .

:
: z

* ***** ***** * 4
** **** **** **
*** *** *** ***
i|t jfc }ft 3ft ** ****
$z % * *****
1 2 5 1

8.2. ,
,
, 0.

8.3. ,
. .
.
: 12345
: 120

8.4. . ,
1 10 5 .
.
.
1 10.
5 .
.
-> 5
.
->3.
! !

8.5. ,
(
). , 0.
8.6. 5 . ,
( - ).
. , 0 0. - ,

.
.


1 8,50
2 34,70
3 9,90
4 11,40
5 20,90
:
1 - 2
5 - 3
1 - 5
3 - 8
4 - 5
0 - 0
:


1 8,50 7 59,5
2 34,70 0 0
3 9,90 8 79,2 258,4
4 11,40 5 57
5 20,90 3 62 , 7

8.7. X (|| < 1) N (N > 0).


2 3 N_1 N
X - /2 + /3 - ... + (-1) X /N.

8.8. , 16 ,
.

8.9. , ,
. ,
. .
.

8.10. ,
. , - .
:
*
- * *
* * - -
-
- *

8.11. ,
2
= -2.4 + 5 - 3 -2 2.

8.12. , , .
,
.
9



. , :
5, 10, -3, 72, 0, 27, 34
:
a s , v d , l o p , f, k o k , is, d d r .
, (,
, ), :
, 2, 3, 4, 5, 6
.
, , , , ,
. : " 2 ,
!".

. - ,
.
.
? , ,
:
:
0, 2, 3, 4, 5, 6;
:
(0), (1), (2), (3), (4), (5), (6);
:
{Ai}, 1=0,1,2, - .
++ :
[0], [1], [2], [3], [4], [5], [6],
- , , , - . ,
.
,
, .
(.. ), .
.
, ,
. :
[ ] ;
, :
int [ 7 ] ;
0,
[6]. ++ , ..
[9], , ,
, . ,
10 .
7 , , 10 , .
.
, ,
. ,

60
.
.
:
int [10], b[17], [ 5 0 ] ;
char str[40], c h [ 5 ] ;
(.. )
. :
int i ?
int m a s [ i ] ;
, i - , ,
.
: *
const int n = 10;
int m a s [ n ] ;

int m a s [ 1 0 ] ;
, ..
:
double arg[3] = {3.56, 7.32, - 0 . 0 1 } ;
arg[0] = 3.56, arg[l] = 7.32, arg[2] = -0.01.
(
):
char str[] = {'','b','','m','','f'};
str 6 , .. 1 (
:
char), . str[0] = ', str[l] = 'b', ... str[5] = .
,
. ,
:
[3] = [5] + 1; *
sum += a [ k ] ;
f1 = mas[2 * i + 1] ;

.
9.1. , - ,
.
int i;
int m a s [ 1 0 ] ;

for( i = 0; i < 10; i++ )


scanf( &mas[i] );
9.2. , -
:
int i;
int mas[10] ;

for( i = 0; i < 10; i++ )


printf{ " % i " , mas[i] );

9.3. , 5 .
,
.
, ,
, .
, 9, 9 ,
, .

61
int A[5],i,Summa=0,pos,max;//i -
//Summa -
//max -
//pos -
for( i=0;i<5;i++) //
{
scanf("%i",&A[i]);
}
max = A[0];//,
pos=0;
for( i=0;i<5;i++)
{
8
Summa+=A[i];
if (A[i] > max )// ,
{ //,
max=A[i]; //
pos=i+l; //
}
printf("CyMMa=%3i =%31 =%31\",Summa,max,pos);


, , -
. ++ -
. , .
- .
. - ,
. - ,
. , ,
.

:
[!] [ 2] ... [ N ] ;
, , ,
:
int [][ ];
double b[4][4][4];
. ,
- . - .
, ,
().
[ ] [ ]
:
[0][0] = 10;
a[j][i] = a[i][j] + 4;
z = a[3][4]++;.
. ,
, . - .
:
[3][0][1] = 3.14;
f = b[i][j][k] + b[i][j][k];
b[0][0][3] *= 7.34;
(
). "" , 4- :
- . - -
- 5- .
. - . 6-
. . ,
"", , ,
, , , "".
.
,
:
int mas[2][3] = {{7, 6, 33}, {-4, 56, 0 } } ;
int arr[][] = {{4, 5, 7 } , {2, 4, 6 } } ;
.
int [2][3],
: [0][0], a[0][lj, [0][2], [1][0], [1][1],
[1][2]. ,
.
.
- ( ),
- (
). :
int i, j ;
int bob[10][20];

for(i = 0; i < 10; i++)


{ // _1
for(j = 0; j < 20; j++) // _1
p r i n t f %3i " , b o b [ i ] [ j ] ) ; // _2
printf(" \ n " ) ; // _1
}
bob .
i 20 , .. i-
.
, i .
9.4. ,
- , .

/* */
const int n = 10; //
int a[n][n], i, j ;
for(i = 0 ; i < n; i++)
forfj = 0; j < n; j++)
a[i][j] = (i + 1) * (j + 1 ) ; //

63
for(i = 0 ; i < n; i++)
{
f o r ( j = 0 ; j < n;
printf("%4i", a[i][j]); // :.;";: = :-:a
printf("\n") ;
}
. . :
1 2 3
5 6 1 1, 6 0 ( ) .
# 9 0 3, 6 8 ( ) - .

9.5. , -
5x5 , , fi
.

/* */
c o n s t i n t n = 5; //
i n t a [ n ] [ n ] , i , j , tmp;

randomize();
for(i = 0 ; i < n; i++)
{
for(j = 0; j < n; j++)
{
a =
[i][j] random(100); //
p r i n t f ("%3i", a [ i ] [ j ] ) ; //
}
printf("\");
}

for(i = 0 ; i < n; i++)


{
tmp = a [ i ] [ i ] ; ?. - t m p
a[i][i] = a[i][n-i-1];
a [ i ] [ n - i - 1 ] = tmp;
}

64

9.

9.1. ,
7 .
.

9.2. ,
. 0,
.

9.3. ,
.
.
:
: 3
:
[0]->1
[1]->0
[2]->2
: 2
: 1.5
9.4. , ,
.
:
: 12 3 4 5 8 9
: 5
.

9.5. , ,
.

9.6. ,
.
.

9.7. ,
.

9.8. , ,
.
.
:
: 3
: 3
:
< >
1 2 3
4 5 6
7 8 9

9.9. ,
4x4 . .
9.10. ,
. .

9.11. .
, .
, ,
.
:
.
. - 1 .
123 : 0 : 0
654 : 2 : 2
954 : 1 : 1
658 : 3 : 3
*** !***

9.12. , ,
. .

9.13. , ,
. .

9
10

,
. . ,
, ,
.
, ,
, .

, ,
(, , ..) .

,
- ASCII (. ASCII-, 1). ASCII
(American Standart Code for
Informational Interchange))). .
ASCII .

, .
( char) ++.
++:

char a,b,c;

,
, , char:

char symb='A' ;
: s
char symb = 65;

,
( ASCII)
-
gercharQ. stdio.h :

int getchar();

.
ASCII- . ,
, ( char)
, :

char ch;
ch = getchar ( ) ;

, putchar(),
stdio.h. :
putchar (ch); , ch.
putchar (6 8 ) ; ASCII- 68. ''.
putchar(32+4) ; ASCII- 36. '$'

, getchar()
<Enter>. . getch()
getche() . :

int getch();
int getche() ;

(
<Enter>). , getcheQ
, a getch() - .
conio.h.
, scanf():
char symb;
scanf("%c",&symb);

printf():
char symb='A';
printf("%c - %i ", symb, symb); //
.

, ,
,
.
10.L , ,
, .
13 (
<Enter>).

int counter = 0 ; // "N"


char ch = ' ' ; /./
while (ch 1= 13)
{
ch = getche();
if(ch == 1 N 1 || ch == 1
n')
counter++;
}
printf("\n 'N' = %i \n", counter);

10.2. .
,
:

char ch;
for(ch = 'A'; ch <= 'Z'; ch++)
putchar(ch);

, char.
ASCII 0,
'\0'. ,
,
. char , :
char str[80];
, 79.
(
68
), .
, :

char str[6] = { ','','1','1',''};
:
char str[6] = "Hello";

, :
char str[] = {

','','1','1', '' };
:
char str[] = "Hello";
, :

STR[ 0]

/ /
1 1 0 \0

,
.
, 255 ,
. ,
, . ,
:
char s t r [ 2 5 5 ] = "";
:
char s t r [ 2 5 5 ] = " " ;
.
10.3. ,
.
int i=0;
char symb[255];
scanf("%s",&symb);
while(i! = ' ' )
{
printf("%c",symb[i]);
i++;
}
10.4. ,
.
int i=0;
char str[255 ] ;

scanf("%s",Sstr);
while(str[i]! = ' \ 0 ' )
{
if(str[i]>='a') str[i]-=32; / / A S C I I ,
i++; // - 32
}
printf("%s",str);

-, stdio.h:
gets() - : ,
char str[120 ] ;
gets(str);
puts() - :
char str[120];
puts(str);
69

, , .
, , 8 (.. 1 ),
, . ++
, ..
.
.
-
. - ,
(, , ). ,
:
*;
, , -
.
. ,
: *
int *, *, *;
char *d;
char *d , , d, char.
(, ,
),
& ( ), :
int ;
int *;
= &;
. &
; &( + 5) &27 .
, , , ?
*. .
, ,
, . , = &; = *; = .
. - , ..
int *;, * ,
, . ,
:
* = 7;
* *= 5;
(*)++;
7 ,
m ,
g. , .. * ++
. , , g = 5,
(*g)++ , *g = 6, a *g++ g (
++ g, *g ).
.
- , ++; ;
. . + (
- , - ) n- , .
(int, char, float ..). *
?
, ,
, ,
, . , -
. ,
, ,
.
,
. ,

. , , ,
,
, . ,
:
int arr[4] = {1,2,3,4};
:
int *;
: *
= &[0];
:
= ;
, 2:
printf("%i ", arr[2]);
:
printf("%i ", * (+2));
:
printf("%i ", [ 2 ] ) ;

10.5.
[ 4 ] , ,
,
, , .
int [4] = {1,2,3,4};
int *;
=;
p r i n t f ( " % i %i %i \n", a r r [ 2 ] , *(p+2), p[2]);

.
,
,
, . ,
, , ,
. ,
.
:
char *s="hello";

char s[]="hello";
,
.
, ,
, ,
, . ,
'\0', ,
,
. , ,
. >
:
char str[255];
printf(" : " ) ;
scanf("%s", & s t r ) ;
printf(" = %s \n", s t r ) ;
printf(" = %c \n", str[4]);
printf(" % s " , str + 1 ) ;

. "Hello".
"Hello", ''.
str, 2- ('')
('\0'). : "ello".
scanf ,
. .. - str -
, scanf("%s", &str[0]) : scanf("%s", &str).
, / - %s,
%.
10.6. , ,
,

const char password[] = "u8h5nkv888"; // -


char pass[255]; //
do
{
printf(" : " ) ;
scanf("Is", &pass);
} // pass
while(strcmp(pass, password) != 0 ) ; // password,
printf(" ! \ " ) ;

strcmp(strl, str2),
. , .
while(pass != password), ,
.. , .. passs password.
, , .
string.h,
. :

1. strcpy(sl,s2) - s2 s i . s i ;

2. strucpy(sl,s2,n) - s2 s i . s i ;

3. strcat(sl,s2) - s2 si. s i ;

4. strcmp(sl,s2) - si s2;

5. strlen(sl) - s i ;

6. s3 = strstr(sl,s2) - s3 si
, s2. *
10.7. ,
. , ,
, . *

char result[255];
1
char space = \ sl[] = "Russian", s2 = "Federation";
strcpy(result,s1);
strcat(result, s p a c e ) ;
strcat(result, s 2 ) ;
printf("%s\n", r e s u l t ) ;

73
10.

10.1. , ,
.
:
?

, !

10.2. , ASCII 15
.
.

10.3. , ,
. , <Esc>.

10.4. ,
.
: ,
:

10.5. . ,

:
:\windows\system32\activeds.dll
activeds.dll
10.6. , ,
.
:


10.7. , ,
.
:
27,5
s

10.8. , ,
.
:
:
:

:

10.9. , .
11

,
, , ,
.
. ,
, ,
, , .
: -,
. -, ,
. .
, ,
. -,
,
#include.
:
1. . ++ ,
.
2. , ,
.
3.
. ,
. ,
, Vasja(), .
Cube().
4. main()
.

/* */
__ ( )

;
};

/* */
main()
{
m a i n ;
}

,
.
- , .
main.
main(). .
11.1. , , 10
.

#include <stdio.h> '


#include <conio.h>

void Stars() // Stars


{
int i; //
for (i = 0; i < 10; i++)
putchar();
printf ("\n") ;
};

void main() // -
{
clrscr();
Stars (); // Stars()
printf(", ! " ) ;
Stars ( ) ; // Stars()
}

Stars(), 10
. .
void - . .
, .
.
main(). main()
i, ,
.
Stars() : ,
10 , , .
, , -
. .
:
int ; //

{
,
void Stars ()
, // Stars()

15
int i; //
for (i = 0; i < n; i++) // : n
putchar( 1 * ' ) ;
printf("\n");
};

void main() // -
{
clrscr();
n = 5;
Stars(); // Stars()
'printf(", ! " ) ;
n = 15;
Stars(); // Stars()
}

Stars()
, .
, .
, , , .
: .

dude, .
, , .
,
?
.
,
.
. , , ,
.. :

void Stars(int n ) / / Stars()


{
int i ; //
for (i = 0; i < n; i++) // : n
putchar(**');
printf("\n");
};

void main() // -
{
int z = 7; // z
clrscr();
Stars(5); / / Stars() 5
printf(", ! " ) ;
Stars(z + 3 ) ; / / Stars() 10
}

Stars() ,
, .
. ,
. Stars()
.
- ,
. , ,
.
return.

11.2. , ,
, .
float Square(float ) // Square()
{
float res;
res = * ;
return res;
};

void main()
{
float a, b, ; // z
clrscr{);
a = 5.5;
b = Square(a); // a
= Square(b + 3 ) ; // b + 3
p r i n t f ( " = %.2f", ) ;
}
Square() float.
. res
. return
Square().
(
), .
11.3. , ,
, .

float Average(int , int ) //


{
return ( + ) / 2 . 0 ;
};

void main()
{
int a, b;
printf(" : " ) ;
scanf("%i % i " , &a, & b ) ;
printf(" = %.lf \n", Average(a, b ) ) ;
}

.
, : float Average(int , ). ..
.
return. ,
:
int max(int a, int b)
{
if (a > b) return a;
else return b;
}

,
main(), :

int max(int a, int b ) ; // max

float Square(float x ) ; // Square()

void main() // main()


{
float a, b, c;
clrscr();
a = 5.5;
b = Square(a)?
= Square(b + 3 ) ;
printf(" = %.2f", ) ;
};

int max(int a, int b) // max()


{
if (a > b) return a;
else return b;
}
float Square(float x) // Square()
{
float res;
res = x * x;
return res; *
};

:
? ,
, .. .

11.4. , ,
.

/* */
void OutText(char *str) // char
{
int i = 0;
while(str[i] != *\0') // : ,
{
printf ("%", str [i] ).;
sound(500); ,
delay(100);
nosound();
i++;
>
}

void main()
{
char name[20];
textcolor(14);
textbackground(0);
clrscr() ;
OutText(" ! \ n " ) ;
OutText(" OutText() \h");
scanf("%s", S n a m e ) ;
OutText(name);
OutText(" \ n " ) ;
getch();

OutText(),
char (.. ). while()
, , '\0'.

8(1(), dos.h, - ,
(, "" 440 ).
0.5 , .
nosound(),
.
11.

11.1. ,
: >, < =.

11.2. , , '
pow().

11.3. ,
.

11.4. , 8
. .

11.5. - , ,
, :
.- . .

- . 3--..
.. . -.- .-..

- -. 0
.-. ... - ..-
X .... -.-. .


. . ~

11.6. ,
4 .

11.7. , .
.

11.8. , 10 ( 0 9)
. .

11.9. 11.7, -,
30 .
12

, .
.
++ stdio.h.
: (
). - , .
*.txt, *.ini, *.bat, *.log .. , ,
, .
*., *., *.pas, *.asm ..
, WordPad'oM. , , ,
, .., (). , *.,
*.com, *.avi, *.dat, *.bmp - . *.doc
, .. , ,
, , .
, .
fopen. .
,
. :

FILE *ist;

FILE - , stdio.h, ist -


. fopen :

ist = (" ", " ");

, , :
"c:\\my_file.txt", "a:\\doc\\data.txt". ,
, ,
"temp.txt".
" " - ,
:
"rt" - ;
"wt" - (
, ); *
"at" - ;
"rt+" - ;
"wt+" - .

(read) - , w (write) -
, a (add) - . t (text) - ,
. , t b
(binary). , ist = fopen("max.dat", "rb") - max.dat
. fopen
(, max.dat ),
NULL. fopen
. , :
if(ist = fopen("max.dat", "rb")==NULL) printf(" ! " ) ;
.
fclose.
, ,
fclose(ist);

,
( stdio.h): 9

1. putc :

putcfch, i s t ) ;

ch - ( char), ist - ,
fopen.

2. getc :

ch = getc(ist) ;

ch char, ist - ,
ch. ,
.
EOF (End Of File) -
.

3. feof :

status = feof(ist);

ist - , status - int,


.
0.

4. fputs .
puts ,
. :

FILE *ist;
i s t = fopen("my.txt", wt") ;
fputs(", !" i s t ) ;
fclose(ist);

, ist ", !".


5. fgets .
gets ,
. :

FILE *;
char str[50 ] ;
= fopen (": W s t o r y . t x t " , "rt") ;
fgets(str, p ) ;
fclose(p);

82
6. fprintf , printf,
. ,
:

FILE *;
int = 10;
= f o p e n (" : W d a t a . t x t " , " w t " ) ;
fprintf(p, " x = %i", x);
fclose(p); *

,
, printf, a:\data.txt.

7. fscanf scanf ,
- ,
, .

FILE *;
char str[100];
int , ;
= fopen("noname.cpp", " r t " ) ;
fscanf(p, "Is %i %i", &str, &x, & y ) ;
fclose(p);

( )
.

8.
, . ,
, 100 ?
?
fseek,
. :

fseek(ist, count, access);

ist - , count -
, access - .
access :

0 - ; ,
1 - ;
2 - .

:
/* 1 0 - ist */
fseek(ist, 10, 0 ) ;
ch = getc(ist);

/ * 5- */
fseek(ist, - 5 , 2 ) ;
ch = getc(ist);
/* 10 */
for(i = 0; i < 10; i++)
{
fseek(ist, 2, 1 ) ;
fscanf(ist, "%c", & c h ) ;
printf("%c", c h ) ;
}
12.1. , ,
, .
int main() *
{
FILE *f;
char name[15]; //
int age; //
int weight; //
f = fopen("x:\\<Login_Name>\\database.txt", "wt+);
// x:
if (f == NULL)
{
printf(" ! \ n " ) ;
return 1; //
}

/* */
printf(" : " ) ;
scanf("%s", &name);
printf(" : " ) ;
scanf("%i", &age); *
printf(" : " ) ;
scanf("%f", &weight);

/* */
fprintf(f, "%s \n %i \n %f", name, age, weight);

fclose(f); //
}

12,2. , ,
.

int main()
{
FILE *f;
char name[15]; //
int age; //
float weight; //

(f == NULL)
{
printf("He ! \ n " ) ;
return 1; //
}

/* */
fscanf(f, "%s %i %\", Sname, Sage, Sweight);
/* */
printf(" : %s \n", n a m e ) ;
printf(" : %i \n", a g e ) ;
printf(" : %f \n", weight );

fclose(f); //
return 0;
}

12.3. ,
:

/* */

FILE *f; //
char ch; //

f = fopen(":\\<Login_Name>\\my.txt", "rt");

while (feof (f) ===0) // : f,


{
ch = getc(f);
printf("%", c h ) ;
}

fclose(f);
1 2 .

12.1. , , -
, , ,
. ,
.

12.2. , numbers.txt
5 .

12.3. , ,
numbers.txt.

12.4. , phones.txt ,
.
.

12.5. ,
phones.txt
( ) .

12.6. ,
, ( 12.4.)
( 12.5.). : ,
**** ****
1.
2.
3.
:

12.7. ,
EUR/USD ( , )
.
.

12.8. , ,
, .
.
1. ASCII



1. ., . " "".
2. . " C/C++. 1".
3. . " - ".
4. . " Turbo ".
5. ., . " ++".
6. . " ++".
7. ., . " ".
8. . " ".
9. . "Turbo Pascal ".
10. . "Turbo Pascal ".
11. X., . " ++".

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