( )

---------------------------------------------------------------------------

 

 

- "" ,

.

,

.

 

, "x".

 

-----

/ x /

---------------

| |

| , |

| 12 |

---------------

 

.

.

 

x = 12 ;

 

" 12",

" 12",

" 12".

 

,

. .

 

 

x = x + 3;

 

.

, . :

 

1) " "

2) " 3"

3) " ",

.

 

: .

 

_ = _ ;

 

.

 

,

" ".

 

,

. , " "

! , .

 

:

 

x = x + 3 ;

 

x 12

 

.

 

x + 3

 

-----

/ x /

---------------

| 12 | |

--------|------

|

|

| ( 12) ""

|

V

x

12 + 3 ----> .

 

15.

x 12

( 12)

 

:

 

x = 15 ;

 

|

|

| ""

| ( , )

-----|

/ x / |

--------|------

| 12 V |

---------------

 

:

-----

/ x /

---------------

| 15 |

---------------

 

,

. "".

 

, Pascal Modula,

:= =

, = .

.

 

:

 

z = x * x + 2 * x;

 

:

 

z - .

x - - .

 

x * x " " ( ,

!)

 

x * 2 " "

 

+ .

 

.

, ,

, , :

 

= 1;

 

 

= 1;

 

"" "",

. .

,

.

 

, ( ..., -2, -1, 0, 1, 2, 3, ...),

:

 

int 1;

int 2;

 

:

 

int 1, 2;

 

int integer - "".

 

 

, .

,

.

 

/* */

int x, y; /* 0 */

 

/* , 2

*/

 

/* - . */

/* */

 

x = 3; /* 1 */

y = 4; /* 2 */

x = x + y; /* 3 */

y = y - 1; /* 4 */

x = y; /* 5 */

 

(, ) :

 

x y

/* 0 */

 

/* 1 */ 3

/* 2 */ 3 4

/* 3 */ 7 4

/* 4 */ 7 3

/* 5 */ 3 3

 

, ,

, .

 

x = y; x y .

, " "

. , x y

, .

 

----- -----

/ x / / y /

--------------- ---------------

| 3 *<--|--------<----|-- 3 |

--------------- 1) ---------------

2), 3) 4)

 

1) y 3 ( ).

2) x .

3) 3 x.

4) y 3.

 

:

 

printf("%d\n", x);

 

"".

 

:

 

x + y

x - y

x * y

x / y ( ; - )

x % y

 

5 / 2 2

5 % 2 1

 

:

 

x = x + 1; " 1" x++; ( ++x; )

x = x - 1; " 1" x--; ( --x; )

x = x + y; " y" x += y;

x = x * y; " y" x *= y;

x = x / y; " y" x /= y;

 

x++; x += 1;

 

* *

 

,

, .

 

1; |

2; |

3; |

4; V

 

 

if() ;

 

......

 

:

 

.

 

, ,

.

 

, ,

.

 

,

{ ... } -

" ".

 

if() {

1;

2;

...

}

 

} ( , ).

 

:

 

|

|

|

----------------

---| |----

| ---------------- |

| |

V V

| |

V |

------------ |

| | |

------------ |

| |

------->-------<-------

|

|

V

|

 

, "":

 

if() __;

else __;

 

" , " ( )

 

|

|

|

----------------

---| |-----------

| ---------------- |

| |

V V

| |

V |

------------------------- -----------------------

| __ | | __ |

------------------------- -----------------------

| |

------->-------<--------------

|

|

V

|

 

1:

 

if(x > 10)

printf(" \n");

 

2:

 

int x, y, z;

 

if(x < y) z = 1;

else z = 2;

 

:

 

( , , )

 

x < y

x > y

x <= y

x >= y

x == y

x != y

 

1,

0, .

 

, :

 

if() ....

 

- .

... -2, -1, 1, 2, 3, ... - .

 

.

 

, :

 

if(x != 0) ... ; if(x) ... ;

if(x == 0) ... ; if(!x) ... ;

---------------------------------------------------------------------------

 

:

 

int x, y, z;

 

if(x == 1){ y = 2; z = x + y; }

else { y = 1; z = x - y; }

 

---------------------------------------------------------------------------

 

:

 

if(x == 1){

printf(" 1\n");

if(y == 2){

printf(" 2\n");

}

} else {

printf(" 1\n");

}

---------------------------------------------------------------------------

 

,

:

 

if(x == 1)

printf(" 1\n");

else if(x == 2)

printf(" 2\n");

else if(x == 3){

printf(" 3\n");

y = 1;

} else

printf(" \n");

---------------------------------------------------------------------------

 

- , ==,

=

= " ", " ".

 

while (" , ")

 

while()

;

......

 

 

while(){

;

...

}

......

 

|

V

|

+------>--+

| |

| V

| ---------------------

| | |-------> ()

A --------------------- |

| | |

| V |

| ( ) |

| | |

| V |

| V

| | |

| | |

+-----<---+ |

|

+-------<---------------------+

|

V

 

:

int x;

 

x = 10;

while(x > 0){

printf("x=%d\n", x);

x = x - 1;

}

printf(".\n");

printf("x %d.\n", x); /* 0 */

 

"" , .

 

, -

- , .

 

", , "

 

.

 

< 10 ...;

...;

...;

 

:

 

if(1 && 2) ...; /* "" */

if(1 || 2) ...; /* "" */

if(! 1) ...; /* "" */

 

:

 

if(4 < x && x <= 12) ...;

 

 

if(4 < x <= 12) ...;

 

!

 

:

 

if(x < 3 || y > 4) ...;

 

if( ! (x < 3 || y > 4)) ...;

 

for (" ")

 

while.

,

" ", " N-".

 

" " " ".

 

int i;

 

i = a; /* */

 

while(i < b){

 

_;

 

i += c; /* */

}

......

 

 

int i;

 

for(i=a; i < b; i += c)

_;

 

_ i

a

a+c

a+c+c

...

 

i < b

 

 

for(i=1; i <= N; i++)

printf("i=%d\n", i);

 

i " ".

 

.

 

break (" ")

 

break

.

 

while(1){

1;

 

if(2)

break; ------->----+

|

2; |

} |

......<--------<---------+

 

 

for(i=0; 1; i++){

1;

 

if(2)

break; ------->----+

|

2; |

} |

......<--------<---------+

 

( ).

 

:

 

for(i=0; i < 20; i++){

printf("i=%d\n", i);

if(i == 7){

printf("break loop!\n");

break; /* */

}

printf("more\n");

}

printf("finished, i=%d\n", i); /* 7 */

 

, :

 

for(;;){ /* */

1;

 

if(2)

break; ------->----+

|

2; |

} |

......<--------<---------+

 

,

.

.

 

-

break ( - ) , .

 

while(1){

...

}

 

()

 

printf("");

 

.

 

printf("\n");

 

.

 

printf("1 2 ");

printf("3\n");

 

 

1 2 3

.

 

, \n,

.

 

printf("%d", x);

 

x.

%d

"

".

 

printf(" %d - -\n", x);

 

 

 

x ,

- -

 

( \n).

 

:

 

int x, y;

 

x = 12; y = 15;

printf(" %d, %d, .\n", x, y);

~~~~~~

 

.

" %d, %d\n" .

, %d.

_

 

_

 

~~~~

.

 

12_

 

%d

 

12, _

 

:

 

12, 15_

 

, \n.

, printf .

 

12, 15, .

_

 

,

:

 

printf(": %d\n", 12 + 3 * 5);

 

, :

 

int x, y, z;

 

x = 13;

y = 23;

z = 34;

 

printf("x=%d xx=%d\nzzz=%d\n", x, y - 1, z * 2 + 1);

 

,

:

 

x=13 xx=22

zzz=69

_

 

, ,

.

 

 

printf("x=%d\n y=%d\n", x, y);

 

x=13

y=23

_

 

y ,

\n !!!

.

 

 

,

,

( ).

 

,

.

 

 

- , -

.

 

- ,

.

.

 

:

 

int func(int x){

 

/* ,

return();

*/

 

return x+1;

}

 

---------------------------------------------------------------------------

 

int func(...

 

func

( , ).

 

int , .

---------------------------------------------------------------------------

 

...(int x)...

 

( ) .

---------------------------------------------------------------------------

 

...){

...

}

 

-

.

---------------------------------------------------------------------------

 

return ;

 

.

---------------------------------------------------------------------------

 

:

 

int y;

...

y = func(5); /* a */

...... /* b */

 

:

 

y = func(5);

 

1) " ",

- , -

.

 

2) func.

 

int func(int x){...

 

func(5).

, x 5.

 

( )

 

int x;

 

x = 5;

return x+1;

 

3) x+1 6.

 

return.

 

:

 

" " - func,

.

 

y = func(5);

 

func(5) ,

return;

 

y = 6;

 

4) .

 

---------------------------------------------------------------------------

 

int y, z, w;

 

y = func(5);

z = func(6);

w = func(7) + func(8) + 1;

 

 

y = 6;

z = 7;

w = 8 + 9 + 1;

 

"" func(),

x

.

 

 

.

main(),

 

main .

 

(

; ).

 

main() - .

 

---------------------------------------------------------------------------

 

:

 

#include /* */

 

/* ( ) */

int a = 7;

int b; /* 0 */

 

/* */

f1(){....}

f2(){....}

 

/* () */

void main(){

...

}

---------------------------------------------------------------------------

 

:

 

#include

 

int f1(int x, int y){

return (x + y*2);

}

 

int f2(int x){

int z;

 

z = x+7;

return 2*z;

}

 

void main(){

/* */

int a, b, c;

 

/* */

a = 5; b = 6;

 

c = f1(a, b+3);

b = f1(1, 2);

a = f2(c);

 

printf("A %d B %d C %d\n", a, b, c);

}

 

:

 

A 60 B 5 C 23

 

 

int i;

 

for(i=0; i < 4; i++){

if(i == 0) func0();

else if(i == 1) func1();

else if(i == 2) func2();

else if(i == 3) func3();

}

 

.

, , :

 

func0();

func1();

func2();

func3();

 

,

, , func(i).

i.

 

, :

 

int i;

 

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

if(i==0) func0();

else if(i == 1) func1();

else if(i == 2) func2();

else funcN(i);

}

 

funcN(i) " ".

, :

 

int i;

 

func0();

func1();

func2();

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

funcN(i);

 

, 3.

 

- , :

 

int i;

 

for(i=0; i < 100; i++){

if((i % 2) == 0) even(); /* */

else odd(); /* */

}

 

i.

03.c

 

/* */

 

#include

 

/* putchar('c') - c */

/* \n - */

/* nstars - */

 

/* */

void drawOneLine(int nstars){

int i; /* , */

 

for(i=0; i < nstars; i++) /* nstars */

putchar('*');

putchar('\n'); /* */

}

 

void main(){

/* */

int nline; /* */

 

/* () */

for(nline=1; nline <= 25; nline++)

drawOneLine(nline);

/* ? , */

}

04.c

 

/* */

/* , */

 

#include

 

void main(){

/* */

int nline; /* */

int i; /* , */

 

/* () */

for(nline=1; nline <= 25; nline++){

/* ? , */

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

putchar('*');

putchar('\n');

}

}

05.c

 

/* */

/* */

 

#include

 

/* nstars - */

/* nspaces - */

 

void drawOneLine(int nspaces, int nstars){

int i; /* , */

/* - */

 

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

putchar(' ');

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

putchar('*');

putchar('\n');

}

 

/*

n ( )

...* 1

..*** 2

.***** 3

******* 4

 

: LINES

n- : n*2 - 1

( ): LINES - n

 

...

 

drawOneLine __,

. ,

-

,

.

 

, ,

.

 

*/

void main(){

/* */

int LINES = 25; /* .

*/

int nline; /* */

 

/* () */

for(nline=1; nline <= LINES; nline++)

drawOneLine(LINES - nline, /* --> nspaces */

nline*2 - 1 /* --> nstars */

);

}

06.c

 

/* */

/* */

 

#include

 

void drawOneLine(int nspaces, int nstars){

int i; /* , */

/* - */

 

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

putchar(' ');

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

putchar('*');

putchar('\n');

}

 

void main(){

/* */

int LINES = 25; /* . */

int nline; /* */

 

/* 1.

- .

for(nline=1; nline <= LINES; nline++)

for(nline=0; nline < LINES; nline++)

 

25 , -

nline 1 .

.

 

n ( )

...* 0

..*** 1

.***** 2

******* 3

 

: LINES

n- : n*2 + 1

( ): LINES - n - 1

 

*/

 

/* () */

for(nline=0; nline < LINES; nline++)

drawOneLine(LINES - nline - 1, nline*2 + 1);

}

07.c

 

/*

 

char

 

( character).

 

'a' 'b' '+'.

 

:

char letter;

 

letter = 'a';

putchar(letter);

letter = 'b';

putchar(letter);

letter = '\n';

putchar(letter);

 

'\n' " " -

, new line.

, - .

 

.

\

'\\'

 

putchar('\');

printf ("\"); .

 

: putchar('\\'); printf("\\");

 

, \ ,

,

.

*/

 

/*

n, n 0,

 

(x % n) == 0

 

, /,

x%2.

 

x n

0 1 2 ... n-1.

2

0 x

1 x

 

*/

 

/* :

*--------------------------------------------------------*

 

: ,

drawOneLine - symbol -

.

 

.

 

*/

 

#include

 

void drawOneLine(int nspaces, int nsymbols, char symbol){

int i; /* */

 

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

putchar(' ');

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

putchar(symbol);

putchar('\n');

}

 

/* ,

"", .

*/

int LINES = 25; /* . */

 

void main(){

/* */

int nline; /* */

 

/* () */

for(nline=0; nline < LINES; nline++){

 

if((nline % 2) == 0) /* ? */

drawOneLine(LINES - nline - 1, nline*2 + 1, '+');

else drawOneLine(LINES - nline - 1, nline*2 + 1, '*');

}

}

08.c

 

/* , .

*/

 

#include

 

/* -

.

*/

 

int LINES = 25; /* . */

 

/* , -

. drawLineNumber.

drawOneLine() !

- - .

 

if(x) .....;

( ):

( int).

, x != 0,

, x == 0.

 

- .

*/

void drawOneLine(int nspaces,

int nsymbols,

char symbol,

/* */

int drawLineNumber,

int linenum

){

int i; /* */

 

if(drawLineNumber)

printf("%d\t", linenum); /* */

 

/*

 

if(drawLineNumber != 0)

 

.

*/

 

/* \t - .

( )

8 .

:

| | | | | | | | | ...

.

| | | | | | | | | ...

^

 

| | | | | | | | | ...

^

 

*/

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

putchar(' ');

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

putchar(symbol);

putchar('\n');

}

 

void main(){

/* */

int nline; /* */

 

/* () */

for(nline=0; nline < LINES; nline++){

 

if((nline % 2) == 0) /* ? */

drawOneLine(LINES - nline - 1, nline*2 + 1, '+', 1, nline);

else drawOneLine(LINES - nline - 1, nline*2 + 1, '*', 9, nline);

}

 

/* 1 9 ?

* , 0.

* 3, 333, 666, -13445,

*

* : , 0 ?

*/

}

09.c

 

/* ,

:

*+*+*+*.....*+*

 

.

*/

 

#include

 

int LINES = 25; /* . */

 

void drawOneLine(int nspaces, int nsymbols){

int i;

 

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

putchar(' ');

 

/*

.

*/

for(i=0; i < nsymbols; i++){

if((i % 2) == 0)

putchar('*');

else putchar('+');

}

putchar('\n');

}

 

void main(){

int nline; /* */

 

for(nline=0; nline < LINES; nline++) {

drawOneLine(LINES - nline - 1, nline*2 + 1);

}

}

10.c

 

/* :

*

***

*****

***

*

*/

 

#include

 

int LINES = 10; /* . */

 

void drawOneLine(int nspaces, int nsymbols){

int i;

 

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

putchar(' ');

 

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

putchar('+');

putchar('\n');

}

 

void main(){

int nline; /* */

 

for(nline=0; nline < LINES; nline++)

drawOneLine(LINES - nline - 1, nline*2 + 1);

 

/* .

.

.

:

LINES-2 0

*/

 

for(nline=LINES-2; nline >= 0; nline--)

drawOneLine(LINES - nline - 1, nline*2 + 1);

}

11.c

 

/* , . */

 

#include

 

void draw(int nspaces, int nstars, char symbol){

int i;

 

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

putchar(' ');

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

putchar(symbol);

putchar('\n');

}

 

void main(){

int LINES = 21;

int MIDDLELINE = LINES/2 + 1; /* */

int nline;

 

for(nline=0; nline < MIDDLELINE; nline++)

draw(MIDDLELINE - nline -1, nline*2+1, 'A');

 

/* for()

.

nline ,

, ,

MIDDLELINE.

*/

 

for( ; nline < LINES; nline++)

draw(nline - MIDDLELINE + 1, (LINES - 1 - nline) * 2 + 1, 'V');

}

* 12_ARRAYS.txt *

 

 

- ,

.

.

 

N ,

- var.

-

var[0]

var[1]

...

var[N-1]

 

.

 

--------

/ var /

/ /

------------------------------------------- ------------------

| | | | | |

| | | | .... ... | |

| | | | | |

------------------------------------------- ------------------

/ var[0] / / var[1] / / var[2] / / var[N-1] /

--------- --------- --------- -----------

 

:

 

int var[N];

 

N - , .

 

N int

var[0] ... var[N-1];

 

n- ( 0 <= n < N)

 

var[n]

 

n - ( ,

), " ".

[] " ".

- N .

var - (N )

n - (), 0..N-1

var[n] - . .

n - - "" .

 

:

 

int var[5]; /* 1 */

 

var[0] = 2; /* 2 */

var[1] = 3 + var[0]; /* 3 */

var[2] = var[0] * var[1]; /* 4 */

var[3] = (var[0] + 4) * var[1]; /* 5 */

 

printf("var %d\n", var[3]);

 

:

 

var[0] var[1] var[2] var[3] var[4]

------------------------------------------------

/* 1 */

/* 2 */ 2

/* 3 */ 2 5

/* 4 */ 2 5 10

/* 5 */ 2 5 10 30

 

, .

 

- , ,

var0, var1, var2, ...

:

var[0], var[1], var[2], ...

 

- .

 

-

1)

2) - ,

- , .

---------------------------------------------------------------------------

 

,

,

(, ).

 

printf("var4 %d\n", var[4]);

 

.

 

( ).

 

,

.

 

, .

---------------------------------------------------------------------------

 

, .

 

int a[5];

int b[5];

 

a = b; /* */

 

() :

 

a = 0; /* */

 

, , .

:

 

int i;

 

for(i=0; i < 5; i++) /* i a[i] = 0; */

a[i] = 0;

 

---------------------------------------------------------------------------

 

=======================

( )

, ( ) .

 

int i;

 

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

a[i] = b[i];

 

.

 

.

 

:

 

int index, array[5];

 

for(index=0; index < 5; index++)

array[index] = index * 2 + 1;

 

 

int index, array[5];

 

index = 0;

while(index < 5){

array[index] = index * 2 + 1;

index++;

}

 

/* : { 1, 3, 5, 7, 9 } */

 

-

"/" .

 

-

, .

.

 

:

;

:

 

int a[N], i;

 

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

...a[i]...

---------------------------------------------------------------------------

 

:

 

int a[5];

 

a[0] = 17;

a[0] += 4;

a[0]++;

---------------------------------------------------------------------------

 

: .

:

 

f[1] = 1

f[2] = 1

f[n+2] = f[n+1] + f[n]

 

:

---------------------------------------------------------------------------

 

#include /* */

#define N 20 /* */

 

void main(){

int fibs[N], index;

 

fibs[0] = 1; /* !!! */

fibs[1] = 1;

 

/* , */

 

for(index=2; index < N; index++)

fibs[index] = fibs[index-1] + fibs[index-2];

 

/* */

for(index = N-1; index >= 0; index--)

printf("%d- %d\n",

index+1, fibs[index]);

}

 

#define

N 20,

 

const int N = 20;

 

,

, #define - .

 

 

- char,

\0

 

char string[20];

 

string[0] = '';

string[1] = '';

string[2] = '';

string[3] = '';

string[4] = '';

string[5] = '';

string[6] = '\0';

 

printf("%s\n", string);

 

%s - .

.

 

char string[20];

 

string[0] = '';

string[1] = '';

string[2] = '';

string[3] = '';

string[4] = '';

string[5] = '';

string[6] = '\n'; /* - */

string[7] = '\0';

 

printf("%s", string);

 

 

printf(string);

 

""

 

char string[20] = "\n";

 

string[8] string[19]

.

 

" "?

=================================================

- .

,

( ).

, - (

). :

(1) .

( ).

 

char str[32]; /* */

int slen; /* slen */

...

func(str, &slen); /* */

...

 

,

: . .

 

(2) str[0],

- str[1] ... .

, str[0] 0 255,

- .

 

(3) , - .

 

func(str); /* - */

 

,

, :

 

int strlen(char s[]){ /* */

int counter = 0; /* */

 

while(s[counter] != '\0') /* */

counter++; /* */

return counter; /* , '\0' */

}

 

.

, .

strlen(s)

( , ).

---------------------------------------------------------------------------

 

=================================

, - ,

:

 

int array[5] = { 12, 23, 34, 45, 56 };

 

char string[7] = { '', '', '', '', '', '', '\0' };

 

, ,

( int) '\0' char.

 

int array[5] = { 12, 23, 34 };

 

, -

.

 

int a[5] = { 177, 255, 133 };

 

a[] :

 

n a[n]

---------------------------------------------------------------------------

 

-1 (: " ")

0 177

1 255

2 133

3 0

4 0

5 ()

* 13_FUNCS.txt *

 

============================

 

, .

 

/* func(). */

/* func - . . */

 

int func(int a, int b, int c){

int x, y;

 

...

x = a + 7;

...

b = b + 4;

...

 

return(_);

}

 

a, b, c - ()

x, y -

 

- -

, main()

 

main(){

 

int zz, var;

...

var = 17;

zz = func(33, 77, var + 3) + 44;

...

}

 

 

zz = func(33, 77, var + 3) + 44;

 

1) func()

 

(a) .

 

(b) a, b, c, x, y;

 

(c) - ,

.

( ) ():

 

func(1, 2, 3)

 

1-, 2- 3- () :

 

int func(a, b, c){ /* a = 1, b = 2, c = 3 */

 

:

 

a = 33;

 

:

 

b = 77;

 

:

 

c = var + 3;

 

, ,

 

c = 20;

 

x y ,

( ,

- ).

 

2) , , { ... }

. :

 

x = a + 7;

 

, - ,

.

 

b = b + 4;

 

.

( ).

 

3) .

 

...

return(_);

}

 

,

 

...

return(a + 2 * x);

}

 

, :

 

zz = func(33, 77, var + 3) + 44;

 

(1) func(.....)

 

zz = XXXXXXX + 44;

 

(2) "_" return,

.

128.

 

(3) func(.....)

 

zz = 128 + 44;

 

(4) :

 

a -

b -

c -

x -

y -

 

( ) .

 

(5) , .

 

(6) :

 

zz = 128 + 44;

 

 

zz = 172; /* */

 

---------------------------------------------------------------------------

 

int func1(int x){

printf("func1: x=%d\n", x); /* 1 */

x = 77;

printf("func1: x=%d\n", x); /* 2 */

return x;

}

 

void main(){

int var, y;

 

var = 111;

y = func1(var); /* @ */

 

printf("main: var=%d\n", var); /* 3 */

}

 

@ func1()

var, 111.

, x

111

 

x = 111;

 

printf() 111.

 

x 77.

x, var !!!

( ) var x,

var - ( - ).

 

printf() 77.

var 111,

printf,

111.

 

---------------------------------------------------------------------------

 

=============================

 

int func1(int x){ /* f.1 */

printf("func1: x=%d\n", x); /* f.2 */

x = 77; /* f.3 */

printf("func1: x=%d\n", x); /* f.4 */

return x; /* f.5 */

}

 

void main(){

int x, y; /* 1 */

 

x = 111; /* 2 */

y = func1(x); /* 3 */

 

printf("main: x=%d y=%d\n", x, y); /* 4 */

}

 

main(),

func1() . ?

 

, .

 

func1()

x, ()

().

 

,

:

 

main::x

 

 

func1::x

 

( ++, ).

 

:

 

|/* 1 */ main::x main::y ;

|/* 2 */ main::x = 111;

|/* 3 */ func1(111);

|

+-------+

. |/* f.1 */ func1::x 111;

. |/* f.2 */ 111 func1::x;

. |

. |/* f.3 */ func1::x = 77; ( main::x, ,

. | func1.

. | main::x -

. | ""

. | .

. | ).

. |

. |/* f.4 */ 77 func1::x;

. |/* f.5 */ func1::x , 77.

. | func1::x .

. |

. | main(),

. | x main::x

 

+-------+

|

|/* 3 */ y = 77;

|/* 4 */ main::x main::y,

| 111 77.

 

main() func1()

, ,

. -

, .

- x, i.

 

---------------------------------------------------------------------------

 

,

.

 

int func1(int arg){ /* - func1::arg */

int x; /* func1::x */

 

x = arg;

printf("func1: x=%d\n", x);

x = 77;

printf("func1: x=%d\n", x);

return x;

}

 

void main(){

int x, y; /* main::x main::y */

 

x = 111;

y = func1(x);

 

printf("main: x=%d y=%d\n", x, y);

}

 

x.

,

:

,

 

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

1 2 3

 

.

 

 

:

 

int f(int 1, int 2, int 3){

int 1, 2;

...

/* */

}

 

:

 

.... f(1, 2, 3) ...

 

( ):

 

1 = 1;

2 = 2;

3 = 3;

 

1 = ;

2 = ;

 

...

/* */

 

---------------------------------------------------------------------------

 

=====================

 

, , ,

( , , ).

 

. - .

 

int x = 12; /* ::x - */

int globvar; /* ::globvar */

 

int f1(){

int x; /* f1::x */

 

x = 77;

printf("x=%d\n", x); /* 4 */

return x;

}

 

int f2(){

printf("x=%d\n", x); /* 5 */

return 0;

}

 

void main(){

int x, y; /* main::x */

 

x = 111; /* 1 */

printf("x=%d\n", x); /* 2 */

printf("glob=%d\n", globvar); /* 3 */

 

y = f1();

y = f2();

}

 

:

- - . .

- - "x".

 

?

 

/* 1 */ main::x = 111;

x, .

x 12.

 

/* 2 */ main::x, 111.

main ::x

x.

main

x, ++ ::x

 

globvar .

 

/* 3 */ ::globvar. , 0.

,

,

0.

 

, .

 

/* 4 */ f1()

f1::x

main::x

::x

 

77,

::x main::x x = 77.

f1::x

 

/* 5 */ f2() .

x.

-

::x

main::x ?

 

: ::x

12.

 

,

.

 

.

( "?" )

 

,

 

funca(){

int vara;

...

...funcb();... /* */

...

}

 

funcb() vara.

 

funcb(){

int z;

 

z = vara + 1; /* ,

vara funcb() */

}

 

, , funcb() funcc(),

funcc() vara .

 

.

-

,

,

.

, ,

.

.

 

(a).

 

(a) .

~~~~~~~~~~

:

 

(5) .

 

,

,

.

 

---------------------------------------------------------------------------

 

=========

, .

void ("").

.

 

void func(){

printf("!\n");

return; /* */

}

 

" ",

( )

.

 

int glob;

 

void func(int a){

glob += a;

}

 

return ,

}

 

:

 

main(){

int z;

 

z = func(7); /* , ??? */

}

 

:

 

main(){

func(7);

}

 

.

 

?

 

!

 

int res1, res2;

...

 

res1 = func(12 * x * x + 177, 865, 'x');

res2 = func(432 * y + x, 123 * y - 12, 'z');

 

, ,

;

, ?

 

,

,

().

,

.

 

:

,

"" .

 

- , .

- .

---------------------------------------------------------------------------

 

return ,

.

.

 

int f(int x){

int y;

 

y = x + 4;

if(y > 10) return (x - 1);

y *= 2;

return (x + y);

}

 

.

 

, .

 

int factorial(int arg){

if(arg == 1)

return 1; /* a */

else

return arg * factorial(arg - 1); /* b */

}

 

factorial(n)

 

n * (n-1) * ... * 3 * 2 * 1

 

" n".

arg ( ) .

 

factorial::arg :

 

factorial::arg[_]

 

.

factorial(4)

 

+----------------------------------------+

| | factorial::arg[ 0__ ] 4 |

| | factorial::arg[ 1__ ] 3 |

| | factorial::arg[ 2__ ] 2 |

| | factorial::arg[ 3__ ] 1 |

V --------+ +---------

 

:

 

+----------------------------------------+

A | /* b */ return 4 * 6 = 24 |

| | /* b */ return 3 * 2 = 6 |

| | /* b */ return 2 * 1 = 2 |

| | /* a */ return 1; |

| --------+ +---------

 

(stack).

 

--------+ +------------

| |

+---------------+

 

a

|

--------+ | +------------

| V |

+---------------+

| a | <---

+---------------+

 

b

 

--------+ +------------

| |

+---------------+

| b | <---

+---------------+

| a |

+---------------+

 

c

 

--------+ +------------

| |

+---------------+

| c | <---

+---------------+

| b |

+---------------+

| a |

+---------------+

 

, " " : c, b, a.

 

()

, .

 

factorial::arg

( - arg) -

, .

, ,

() .

 

- .

,

:

 

int stack[10];

int in_stack = 0; /* */

 

/* */

void push(int x){

stack[in_stack] = x;

in_stack++;

}

 

/* */

int pop(){

if(in_stack == 0){

printf(" , .\n");

return (-1);

}

in_stack--;

return stack[in_stack];

}

 

, ( )

, .

,

.

 

void main(){

push(1);

push(2);

push(3);

 

while(in_stack > 0){

printf("top=%d\n", pop());

}

}

 

 

" ",

.

 

return .

 

:

 

int x = 7; /* */

int v = 333; /* */

 

int factorial(int n){

int w; /* , */

 

w = n;

 

if(n == 1) return 1; /* #a */

else return n * factorial(n-1); /* #b */

}

void func(){

int x; /* func::x */

 

x = 777; /* #c */

printf(" func()\n");

}

void main(){

int y = 12; /* main::y */

int z;

 

/* A */

z = factorial(3); /* B */

printf("z=%d\n", z);

 

func(); /* C */

}

---------------------------------------------------------------------------

 

main().

/* A */

 

| |

V V

 

--------+ +--------

|=======================|

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

,

a) ( ) .

b) .

"" :

 

main::z, main::y, ::x, ::v

 

---------------------------------------------------------------------------

 

/* B */ factorial(3).

 

--------+ +--------

|=======================|

| w = |

| n = 3 |

|factorial(3) |

|=======================|

| +-|---> z = factorial(3);

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

:

factorial(3)::w, factorial(3)::n, ::x, ::v

 

:

main::z, main::y

 

" ..." ,

, .

---------------------------------------------------------------------------

 

factorial(3)

/* #b */ return 3 * factorial(2).

factorial(2).

 

--------+ +--------

|=======================|

| w = |

| n = 2 |

|factorial(2) |

|=======================|

| +-|---> return 3 * factorial(2);

| w = 3 |

| n = 3 |

|factorial(3) |

|=======================|

| +-|---> z = factorial(3);

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

---------------------------------------------------------------------------

 

factorial(2)

/* #b */ return 2 * factorial(1).

factorial(1).

 

--------+ +--------

|=======================|

| w = |

| n = 1 |

|factorial(1) |

|=======================|

| +-|---> return 2 * factorial(1);

| w = 2 |

| n = 2 |

|factorial(2) |

|=======================|

| +-|---> return 3 * factorial(2);

| w = 3 |

| n = 3 |

|factorial(3) |

|=======================|

| +-|---> z = factorial(3);

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

---------------------------------------------------------------------------

 

factorial(1) /* #a */

return 1.

 

return ,

" " ,

.

, factorial(m) .

: return,

return. .

 

--------+ +--------

|=======================|

| +-|---> return 1;

| w = 1 |

| n = 1 |

|factorial(1) |

|=======================|

| +-|---> return 2 * factorial(1);

| w = 2 |

| n = 2 |

|factorial(2) |

|=======================|

| +-|---> return 3 * factorial(2);

| w = 3 |

| n = 3 |

|factorial(3) |

|=======================|

| +-|---> z = factorial(3);

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

---------------------------------------------------------------------------

 

return;

 

--------+ +--------

|=======================|

| +-|---> return 2 * 1;

| w = 2 |

| n = 2 |

|factorial(2) |

|=======================|

| +-|---> return 3 * factorial(2);

| w = 3 |

| n = 3 |

|factorial(3) |

|=======================|

| +-|---> z = factorial(3);

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

--------+ +--------

|=======================|

| +-|---> return 3 * 2;

| w = 3 |

| n = 3 |

|factorial(3) |

|=======================|

| +-|---> z = factorial(3);

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

--------+ +--------

|=======================|

| +-|---> z = 6;

| z = |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

--------+ +--------

|=======================|

| z = 6 |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

---------------------------------------------------------------------------

 

, /* C */ func().

/* #c */ .

 

--------+ +--------

|=======================|

| x = 777; |

|func(); |

|=======================|

| +-|---> func();

| z = 6 |

| y = 12 | +------+---------+

|main() | |x = 7 | v = 333 |

+-----------------------+-----------+------+---------+-----

 

- ?

:

func::x = 777

::v = 333

 

.

::x .

main::y, main::z ,

---------------------------------------------------------------------------

 

.

,

( ). - .

 

int fibonacci(int n){

if(n==1 || n==2) return 1;

 

/* else */

 

return fibonacci(n-1) + fibonacci(n-2);

}

void main(){

printf("20 %d\n", fibonacci(20));

}

 

,

.

-

. - ,

fibonacci(1) ?

 

int called = 0;

 

int fibonacci(int n){

if(n==1){

called++;

return 1;

 

} else if(n==2)

return 1;

 

return fibonacci(n-1) + fibonacci(n-2);

}

void main(){

printf("20 %d\n", fibonacci(20));

printf("fibonacci(1) %d \n", called);

}

 

... 2584 !

14.c

 

/* */

#include

 

const int LINES = 15;

 

void draw(int nspaces, int nstars, char symbol){

int i;

 

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

putchar(' ');

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

putchar(symbol);

}

 

void main(){

int nline, nsym;

char symbols[3]; /* */

 

symbols[0] = '\\';

symbols[1] = 'o';

symbols[2] = '*';

 

for(nline=0; nline < LINES; nline++){

for(nsym = 0; nsym < 3; nsym++)

draw(nline, nline, symbols[nsym]);

 

/*

*/

putchar('\n');

}

}

15.c

 

/* :

 

...

 

, , , , ...

6 .

*/

 

#include /* */

 

/* .

- .

*/

 

int TOMCATS = 3; /* */

int CATS = 2; /* */

int ANIMALS_PER_LINE = 6; /* */

int LINES = 25; /* */

 

/* - .

, ...

- .

- main().

*/

int ANIMALS; /* */

 

int nth_in_line = 0; /* */

/* ,

* . ,

* . - .

*/

 

/* ,

,

().

*/

void checkIfWeHaveToBreakLine(){

nth_in_line++; /* ( ) */

 

if(nth_in_line == ANIMALS_PER_LINE){

/* ... */

putchar('\n'); /* */

nth_in_line = 0; /* */

} else {

putchar('\t'); /* */

}

}

 

void main(){

int nanimal; /* */

int i; /* */

 

ANIMALS = ANIMALS_PER_LINE * LINES;

nanimal = 0;

 

while(nanimal < ANIMALS){

 

for(i=0; i < TOMCATS; i++){

/* printf() .

( putchar().

*/

printf("");

nanimal++; /* */

 

/* - ? */

checkIfWeHaveToBreakLine();

}

for(i=0; i < CATS; i++){

printf("");

nanimal++; /* */

 

/* - ? */

checkIfWeHaveToBreakLine();

}

}

/* putchar('\n'); */

}

16.c

 

/*

, .

.

*/

 

#include /* */

 

int TOMCATS = 3; /* */

int CATS = 2; /* */

int ANIMALS_PER_LINE = 6; /* */

int LINES = 5; /* */

int ANIMALS; /* */

 

int nth_in_line = 0; /* */

 

void checkIfWeHaveToBreakLine(){

nth_in_line++;

 

if(nth_in_line == ANIMALS_PER_LINE){

putchar('\n');

nth_in_line = 0;

} else

printf("\t\t"); /* @ */

 

/* {...} */

}

 

void main(){

int nanimal;

int i;

 

ANIMALS = ANIMALS_PER_LINE * LINES;

nanimal = 0;

 

while(nanimal < ANIMALS){

 

for(i=0; i < TOMCATS; i++){

/* %d int

.

( - ).

().

 

--

checkIfWeHaveToBreakLine()

@.

 

-

putchar('a');

-

printf("abcdef");

 

- .

- .

*/

 

printf("%d", nanimal);

nanimal++;

 

checkIfWeHaveToBreakLine();

}

for(i=0; i < CATS; i++){

printf("%d", nanimal);

nanimal++;

 

checkIfWeHaveToBreakLine();

}

}

}

17.c

 

/* : 1 100.

 

:

- .

, ( ).

, - int.

- char.

- double.

( float, ).

 

.

 

q = x;

 

q[0] := x;

q[n+1] := 1/2 * ( q[n] + x/q[n] );

 

, q[n]

. ,

.

, , q.

*/

 

#include

 

/* - const. .

, .

, - epsilon = ... ;

.

*/

const double epsilon = 0.0000001; /* */

 

/* */

double doubleabs(double x){

if(x < 0) return -x;

else return x;

}

 

/* */

double sqrt(double x){

 

double sq = x;

 

/* :

double sq;

sq = x;

" ".

*/

 

while(doubleabs(sq*sq - x) >= epsilon){

sq = 0.5 * (sq + x/sq);

}

return sq;

}

 

void main() {

int n;

 

for(n=1; n <= 100; n++)

printf("sqrt(%d)=%lf\n",

n, sqrt((double) n)

);

 

}

 

/*

printf() .

------ --------

%d -- n

%lf -- sqrt((double) n)

 

%d int.

%c char.

%lf ( %g) double.

 

" sqrt(xxx)" ?

:

- sqrt() , xxx;

- ;

- %lf,

.

 

,

, .

 

, x = 12 + 34;

12 34 ,

 

printf("%d\n", 12);

 

12, .

 

,

 

double z;

 

z = sqrt(12) + sqrt(23);

 

, ,

(

,

).

 

z = sqrt( sqrt(81));

 

( 81 --> 3)

 

, (double) n ?

sqrt() double.

 

int n;

 

-,

 

12 12.0 -.

 

, .

(double) x

" double".

 

,

.

 

, , int double

int double,

double.

 

int var1;

double var2, var3;

 

var1 = 2;

var2 = 2.0;

var3 = var1 + var2;

 

 

var3 = (double) var1 + var2;

 

var3 4.0

 

, char -

0...255. 0 255.

 

*/

* 18_POINTERS.txt *

 

=========

void f(int x){

x = 7;

}

 

main(){

int y = 17;

f(y);

printf("y=%d\n", y); /* : y=17 */

}

 

x ? y,

x=7; .

,

?

 

:

 

- y

( - ),

 

- y=f(y);

( ?

return, , ).

 

: .

---------------------------------------------------------------------------

 

(@)

 

void f(int *ptr){ /* #2 */

*ptr = 7; /* #3 */

}

 

main (){

int y=17;

 

f(&y); /* #1 */

printf("y=%d\n", y); /* : y=7 */

}

 

, ?

---------------------------------------------------------------------------

 

:

 

&y " y"

" y"

 

*ptr " ptr"

( - )

 

int *ptr; ptr,

,

int-.

 

, .

 

int var1, var2, z; /* */

int *pointer; /* */

 

var1 = 12;

var2 = 43;

pointer = &var1;

 

;

.

________

/pointer/

_/_______/_

| | ,

| | ( )

| |

| &var1 |

| |

|_______|_|

|

|&var1 - var1 -

| ", var1".

V &var1

________

/ var1 /

_/_______/_

| |

| |

| 12 |

|_________|

 

, - ", -".

( ) - .

 

, int,

, , int *

 

char, - char *

 

( ) ,

.

 

int x;

int arr[5];

 

&x "x"

& arr[3] "arr[3]"

 

&(2+2) "",

,

.

 

 

.

, :

 

*pointer

 

---------------------------------------------------------------------------

 

===================================

*pointer

" ( ),

,

pointer".

 

- 12.

 

*pointer " ".

 

printf("%d\n", *pointer);

 

12;

 

z = *pointer; /* z = 12; */

z = *pointer + 66; /* z = 12 + 66; */

 

( , " ")

 

pointer = &var2;

 

________

/pointer/

_/_______/_

| |

| &var2 |

| |

|_______|_|

|

|&var2

|

V

________

/ var2 /

_/_______/_

| |

| 43 |

| |

|_________|

 

z = *pointer;

z = 43;

 

---------------------------------------------------------------------------

 

,

 

z = *pointer;

 

 

z = *(&var2);

 

 

z = var2;

 

* & .

 

...

 

*pointer = 123;

 

" (.. 123)

(), ,

pointer".

 

.

 

*pointer

,

.

 

________

/pointer/

_/_______/_

| |

| &var2 |

| |

|_______|_|

|

| 123

|

V

________

/ var2 /

_/_______/_

| |

| 123 |

| |

|_________|

 

pointer = &var2;

*pointer = 123;

 

 

*(&var2) = 123;

 

 

var2 = 123;

 

* & .

 

---------------------------------------------------------------------------

 

? :

 

*pointer = *pointer + 66;

 

 

*pointer += 66;

 

---------------------------------------------------------------------------

 

(@). ?

 

/* #1 */

f(),

y (" y").

 

/* #2 */

ptr,

-

y.

 

/* #3 */

*ptr = 7;

 

 

*(&y) = 7; *(&main::y)=7;

 

 

y = 7; main::y=7;

 

.

 

, "y"

f() !

 

---------------------------------------------------------------------------

 

: .

 

void main(){

int x, y;

int temporary; /* */

 

x=1; y=2;

 

temporary=x; x=y; y=temporary;

printf("x=%d y=%d\n", x, y); /* x=2 y=1 */

}

---------------------------------------------------------------------------

 

:

 

void swap(int *a, int *b){

int tmp;

 

tmp = *a; *a = *b; *b = tmp;

}

 

void main(){

int x, y;

 

x = 1; y = 2;

swap(&x, &y);

printf("x=%d y=%d\n", x, y);

}

---------------------------------------------------------------------------

 

? :

 

int x;

int *ptr1, *ptr2;

 

ptr1 = &x; ptr2 = &x;

*ptr1 = 77;

printf("%d\n", *ptr2); /* 77 */

 

.

---------------------------------------------------------------------------

 

? :

int x;

int *ptr1; /* */

 

x = *ptr1;

 

ptr1 , .

" " ( ).

? .

 

: , .

 

 

.

"a"

 

int a[5];

 

.

 

()

 

a[0] a[1] ... a[4].

 

a &a[0]

 

a

|

|

|

V

a[0] a[1] a[2] a[3] a[4]

_________________________________________

| | | | | |

| | | | | |

| | | | | |

-----------------------------------------

 

 

int a[5];

 

/* ? , */

 

void f(int *a){ /* f(int a[]), */

printf("%d\n", a[1]);

a[2] = 7;

}

 

main (){

a[1] = 777;

f(a); /* - */

printf("%d\n", a[2]);

}

 

f(a); .

:

 

_1:

( ),

0- .

 

_2:

.

, *pointer,

: pointer[n].

 

.

 

int a[5]; /* */

int *ptr; /* */

 

ptr = a; /* , ptr = &a[0]; */

 

 

ptr[0] = 3; /* a[0] = 3; */

ptr[1] = 5; /* a[1] = 5; */

 

.

 

ptr = &a[2];

 

a[0] a[1] a[2] a[3] a[4]

_________________________________________

| | | | | |

a: | | | | | |

| | | | | |

----------------------------------------------

| | | | ...

ptr: | | | |

-----------------------------

-2 -1 ptr[0] ptr[1] ptr[2]

 

"" a[] ptr[].

 

 

ptr[0] a[2]

ptr[1] a[3]

ptr[2] a[4]

ptr[3] a[],

 

, !

 

ptr[-1] a[1]

ptr[-2] a[0]

ptr[-3] a[],

 

: .

 

, a[]

*a , a[0].

 

.

19.c

 

/* :

char.

A B C D ---> D C B A

.

*/

 

/* ( ) */

#include

 

/* - . */

 

/* .

, - char,

'\0'.

\0 .

*/

int strlen(char s[]){ /* */

int counter = 0; /* */

 

while(s[counter] != '\0') /* */

counter++; /* */

return counter; /* , '\0' */

}

 

/* .

putchar(c).

, '\0' - .

,

, .

 

.

*/

int putstr(char s[]){

int i = 0; /* */

 

while(s[i] != '\0'){

putchar(s[i]);

i++;

}

putchar('\n');

return i;

}

 

/* .

:

1 .

 

: A B C D E F

<----------

: B C D E F F

-

.

 

n - .

:

 

: A B C D E F n=6

i=1 B B C D E F

i=2 B C C D E F

i=3 B C D D E F

i=4 B C D E E F

i=5 B C D E F F

i=6 ==> .

 

*/

void shiftLeft(char s[], int n){

int i;

 

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

s[i-1] = s[i];

}

 

/* .

:

- 1, ,

1 0 .

- > 1,

a) .

A B C D E F

|

|

V

tmp

 

b)

B C D E F F

 

c) .

tmp

|

V

B C D E F A

 

d) n-1.

{B C D E F}A

 

:

F E D C B A

 

.

 

s[] - ,

n - .

*/

void reverse(char s[], int n){

char tmp;

 

if(n <= 1) /* */

return;

 

tmp = s[0]; /* */

shiftLeft(s, n); /* */

s[n-1] = tmp; /* */

 

reverse(s, n-1); /* */

}

 

/* . .

, ,

length.

*/

void reverse1(char s[], int n){

char tmp;

int length;

 

for(length=n; length > 1; --length){

tmp = s[0];

shiftLeft(s, length);

s[length-1] = tmp;

}

}

 

char testString[] = "abcdefghijklmnopqrstuvwxyz";

 

/* , .

"..."

'\0' .

27.

*/

 

void main(){

int len;

 

len = strlen(testString);

/* : 26 ('\0' ) */

 

printf(" : \"%s\", %d\n",

testString, len);

/* :

- ( char) %s

- "..." "

\"

 

putchar ' putchar('\'');

*/

 

/* */

reverse(testString, len);

putstr(" :");

putstr(testString);

 

/* - */

reverse1(testString, len);

putstr(" :");

putstr(testString);

}

19_1.c

 

/* :

.

 

A B C D E F G H I J

J B C D E F G H I A

| |

J B C D E F G H I A

J I C D E F G H B A

| |

J I C D E F G H B A

J I H D E F G C B A

| |

----> <-----

 

J I H D E F G C B A

J I H G E F D C B A

| |

J I H G E F D C B A

| |

J I H G F E D C B A

 

.

*/

 

#include

 

/* char */

void swap(char *s1, char *s2){

char c;

 

c = *s1; *s1 = *s2; *s2 = c;

}

 

void reverse(char s[], int n){

int first, last;

 

first = 0; /* */

last = n-1; /* */

 

while(first < last){ /* first last */

swap(&s[first], &s[last]);

first++; /* */

last--; /* */

}

}

 

char testString[] = "abcdefghijklmnopqrstuvwxyz.";

 

void main(){

int len;

 

len = strlen(testString); /* */

reverse(testString, len);

printf(" : %s\n", testString);

}

19_2.c

 

/* :

,

.

*/

 

#include

 

char testString[] = "abcdefghijklmnopqrstuvwxyz.";

 

/* sizeof()/sizeof([0])

, .

( )

, .

*/

 

char tempString[ sizeof(testString) / sizeof(testString[0]) ];

 

void reverse(char s[], int n){

int i;

 

/* , tempString[] */

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

tempString[n-1-i] = s[i];

 

tempString[n] = '\0'; /* */

 

/* */

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

s[i] = tempString[i];

 

s[n] = '\0'; /* */

}

 

void main(){

int len;

 

len = strlen(testString); /* */

reverse(testString, len);

printf(" : %s\n", testString);

}

19_3.c

 

/* */

 

#include

 

int arr[] = {1, 5, 10, 15, 20, 25, 30};

int arrLen = sizeof(arr) / sizeof(arr[0]); /* */

 

/* */

void printit(int row[], int n){

int i;

 

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

printf("%d", row[i]);

 

if(i == n-1) putchar('\n');

else putchar(' ');

}

}

/* . */

void printShift(int n){

n = arrLen - n;

while(n > 0){

printf(" ");

n--;

}

}

/* */

void shiftleft(int row[], int n){

int i;

 

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

row[i-1] = row[i];

}

/* */

void reverse(int row[], int n){

int pocket;

 

printShift(n); /* */

printf("CALLED reverse(row, %d)\n", n); /* */

 

if(n <= 1){

printShift(n); /* */

printf("return from reverse(row, %d);\n", n); /* */

return;

}

 

pocket = row[0];

shiftleft(row, n);

row[n-1] = pocket;

 

printShift(n); /* */

printit(arr, arrLen); /* */

 

reverse(row, n-1);

 

printShift(n); /* */

printf("all done; return from reverse(row, %d);\n", n); /* */

}

void main(){

reverse(arr, arrLen);

printit(arr, arrLen);

}

 

20.c

 

/* :

columns .

:

 

0 4 8

1 5 9

2 6 10

3 7

*/

 

/* n .

n < columns, (n=2, columns=4)

 

0 1

 

 

if(n < columns) columns = n;

 

, columns lines

columns*lines . :

 

columns*lines >= n

 

.

,

 

lines >= n/columns

 

 

lines = (n + (columns - 1)) / columns;

 

.

 

(y) (x).

 

index(x, y) = (x * lines + y);

index >= n,

*/

 

#include

 

int array[100];

 

void printArray(int a[], int n, int columns){

int lines; /* */

 

int x, y; /* , - */

int index; /* */

 

if(n < columns) columns = n;

lines = (n + (columns-1)) / columns;

 

/* : , - */

for(y=0; y < lines; y++){

for(x=0; x < columns; x++){

index = x * lines + y;

if(index >= n) /* */

break; /* */

 

/* break

( ) */

 

/* */

if(x != 0) putchar('\t');

 

printf("%02d|%d", index, a[index]);

/* %02d

,

,

.

*/

}

putchar('\n'); /* */

}

}

void main(){

int i, cols;

 

/* */

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

array[i] = i + 1;

 

for(cols=4; cols <= 13; cols++){

printf("\t\t* * * %d * * *\n", cols);

printArray(array, 77, cols);

putchar('\n');

}

}

20_1.c

 

#include

 

main(){

int x, y;

int COLUMNS = 11;

int LINES = 10;

 

int value;

 

/* */

for(y=0; y < LINES; y++){

 

/* */

for(x=0; x < COLUMNS; x++){

 

/* */

value = LINES * x + y;

 

/* ,

*/

 

if(x > 0) putchar('\t');

 

/* ... */

printf("%d", value);

 

}

putchar('\n'); /* */

}

}

20_2.c

 

/*

elem(x, y) = LINES * x + y;

 

 

elem(0, y+1) - elem(COLUMNS-1, y) = 1 + LINES - COLUMNS*LINES;

elem(x+1, y) - elem(x, y) = LINES;

*/

 

#include

 

int A = 150; /* */

int COLUMNS = 7; /* */

int LINES; /* */

int value; /* */

 

int OFFSET_NEXT_COLUMN;

int OFFSET_NEXT_LINE;

 

/* */

void line(){

int col; /* */

 

for(col=0; col < COLUMNS; col++){

if(value >= A) /* */

printf("* ");

else printf("%03d ", value);

 

/* */

value += OFFSET_NEXT_COLUMN; /* 1 */

}

/* */

putchar('\n');

 

/* .

, value OFFSET_NEXT_COLUMN 1,

OFFSET_NEXT_COLUMN + OFFSET_NEXT_LINE

1 - LINES*COLUMNS + LINES,

.

*/

value += OFFSET_NEXT_LINE; /* 2 */

}

 

int main(){

int nline; /* */

 

LINES = (A + (COLUMNS - 1)) / COLUMNS;

 

OFFSET_NEXT_COLUMN = LINES;

OFFSET_NEXT_LINE = 1 - LINES*COLUMNS;

 

for(nline=0; nline < LINES; nline++)

line();

 

/* 0 main() " " */

return 0;

}

21.c

 

/* */

 

/*

.

 

:

 

int array[LINES][COLUMNS];

 

:

 

array[y][x]

 

0 <= y <= LINES - 1

0 <= x <= COLUMNS - 1

 

+-------------+-------------+-------------+------> x

| array[0][0] | array[0][1] | array[0][2] | ...

+-------------+-------------+-------------+

| array[1][0] | array[1][1] | array[1][2] | ...

+-------------+-------------+-------------+

| array[2][0] | array[2][1] | array[2][2] | ...

+-------------+-------------+-------------+

| ... ... ...

V

y

 

, ,

.

*/

 

/* , ,

,

.

 

,

. .

*/

 

#define LINES 31 /* */

#define COLUMNS 79 /* */

 

char field[LINES][COLUMNS];

 

/* ,

.

, .

*/

void line(int x1, int y1, int x2, int y2, char sym){

int dx, dy, i1, i2, i, kx, ky;

int d; /* "" */

int x, y;

int flag;

 

dy = y2 - y1;

dx = x2 - x1;

if (dx == 0 && dy == 0){

field[y1][x1] = sym; /* */

return;

}

kx = 1; /* x */

ky = 1; /* y */

 

/* */

if( dx < 0 ){ dx = -dx; kx = -1; } /* Y */

else if(dx == 0) kx = 0; /* X */

 

if(dy < 0) { dy = -dy; ky = -1; }

 

if(dx < dy){ flag = 0; d = dx; dx = dy; dy = d; }

else flag = 1;

 

i1 = dy + dy; d = i1 - dx; i2 = d - dx;

x = x1; y = y1;

 

for(i=0; i < dx; i++){

field[y][x] = sym; /* */

 

if(flag) x += kx; /* . */

else y += ky;

 

if( d < 0 ) /* */

d += i1;

else{ /* */

d += i2;

if(flag) y += ky; /* */

else x += kx;

}

}

field[y][x] = sym; /* */

}

 

int main(){

int x, y;

 

/* */

for(y=0; y < LINES; y++)

for(x=0; x < COLUMNS; x++)

field[y][x] = ' ';

 

/* */

line(0,0, 0, LINES-1, '*');

line(0,0, COLUMNS-1, 0, '*');

line(COLUMNS-1, 0, COLUMNS-1, LINES-1, '*');

line(0, LINES-1, COLUMNS-1, LINES-1, '*');

 

line(0,0, COLUMNS-1, LINES-1, '\\');

line(COLUMNS-1,0, 0,LINES-1, '/');

 

/* */

for(y=0; y < LINES; y++){

for(x=0; x < COLUMNS; x++)

putchar(field[y][x]);

putchar('\n');

}

return 0;

}

 

 



<<