.


:




:

































 

 

 

 





 

C++ , :

goto

break

continue

return

goto

goto :

goto ;

:

: ;

̶ , , . goto .

goto , , , .

goto , :

,

(, - ).

goto . goto , . , .

if, switch . , , , , :

int ;...

goto metka;...

{int a = 3, b = 4;

k = a + b;

metka: int m = k + 1;...

}

m .

break

break switch , , break.

: x eps :

sh = x + 3/3! + 5/5! + 7/7! +...

, , , .

#include <iostream.h>

#include <math.h>

int main() {

const int MaxIter = 500; //

double , eps;

cout << : ;

cin >> x >> eps;

bool flag = true; //

double = x, ch = x; //

for (int n = 0; fabs(ch) > eps; n++) {

ch *= x * x /(2 * n + 2)/(2 * n + 3); //

+= ch;

if (n > MaxIter){ cout << \n !; flag = false; break;}

}

if (flag) cout << \n : << ;

return 0;

}

continue

continue , , .

return

return . : return [ ];

. void, .

 

1.11

 

(, int i=10;) (int) (10). (i) , . ++ .

, .

: , void. .

, ( ). .

:

*;

, , , .

, :

,

int *a, b, *;

, b.

, , .. , .

:

( , , ),

.

:

(* ) (__);

,

int (*fun) (double, double);

fun , int double.

void , , , (, ).

, , - , , .

, - . . , . . .

. - , .

. . .

. . , .

:

1. :

■ &:

int a = 5; //

int * = &; //

int *p (&); //

■ :

int *r = ;

■ , :

int b[10]; //

int *t = b; //

void f (int a){ /*... */ } //

void (*pf)(int); //

pf = f; //

2. :

char *vp = (char *)0xB8000000;

0xB8000000 , (char *) ( char).

3. :

int *suxx = NULL;

int *rulez = 0;

NULL , . 0, .. int .

, , .. , .

4. :

■ new:

int *n = new int; // 1

int *m = new int (10); // 2

int *q = new int [10]: // 3

■ malloc:

int *u = (int *)malloc(sizeof(int)); // 4

1 int n. n, , , .

2, , 10.

3 10 int ( 10 ) q, . .

bad_alloc. 0.

4 , 1, malloc, . . (int *) , , . , 0. malloc, <malloc.h>.

new , malloc, .

new, delete, malloc free. new [], delete []. .

:

delete n; delete m; delete [] q; free (u);

- .

, , , . .

:

. .

delete delete []. , .

-. , , , , , , . - .

, :

int i; //

const int ci = 1; //

int *pi; //

const int *pci; //

int * const cp = &i; // -

const int * const cpc = &ci; // -

const, , , a const , .

, .

, , , . .

:

, ;

, ;

, , ;

.

,

int *(*[10])();

5 4 2 1 3 //

10 , int.

 

1.12

 

:

( )

() (*)

(&)

(++)

(--)

, . ( ):

char a; // char

char * = new char; //

// char

* = ޒ; = *; //

*_ , .. L- ( ). , . , ( ).

. .

:

#include <stdio.h>

int main(){

unsigned long int A = 0Xcc77ffaa;

unsigned short int *pint = (unsigned short int*) &A;

unsigned char *pchar = (unsigned char *) &A;

printf ( | %x | %x | %x |, A, *pint, *pchar);

return 0;

}

| cc77ffaa | ffaa | |. pint pchar , pchar , a pint .

&. , . , , .

. , .

, . , .

void* , .. . 0 .

bool (, ), true, false.

, , . -. , -, .

( , , ) , . , , , .

, . sizeof ().

, , , :

short *p = new short [5];

p++; // 2

long *q = new long [5];

q++; // q 4

̶ , ( , , 3).

.

. , *++ = 10;

, , , . , , , 10, , . :

* = 10; ++;

(*)++; , .

 

1.13

 

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

, , .

:

&=;

, , & - (, ).

int kol;

int &pal = kol; // pal - kol

const char &CR = '\n'; //

.

int ival = 1024;

int &refVal = ival; // : refVal - ival

int &refValue; // :

, , , . :

int ival = 1024;

int &refVal = &ival; // : refVal int, int*

int *pi = &ival;

int *&ptrVal2 = pi; // : ptrVal -

, ( ).

int min_val = 0;

refVal = min_val; // min_val ival

// refVal

refVal, ival , refVal.

. .

refVal += 2; // 2 ival ,

// refVal.

int ii = refVal; // ii ival,

int *pi = &refVal; // pi ival.

, & ( ).

int ival = 1024, ival2 = 2048; // int

int &rval = ival, rval2 = ival2; //

int inal3 = 1024, *pi = ival3, &ri = ival3; // , //

int &rval3 = ival3, &rval4 = ival2; //

( ), , . :

double dval = 3.14159;

const int &ir = 1024; //

const int &ir2 = dval;

const double &dr = dval + 1.0;

const, .

, ( ).

, .

, :

double dval = 1024;

const int &ri = dval;

:

int temp = dval;

const int &ri = temp;

ri, dval, temp. dval . , , const.

, .

const int ival = 1024; // :

int *&pi_ref = &ival;

 

const int ival = 1024; //

const int *&pi_ref = &ival;

, pi_ref int. , :

const int ival = 1024;

int *const &piref = &ival;

. -, . -, , , .

, :

int *pi = 0;

pi ( , ).

:

const int &ri = 0;

:

int temp = 0;

const int &ri = temp;

,

int ival = 1024, ival2 = 2048;

int *pi = &ival, *pi2 = &ival2;

pi = pi2;

ival, pi, , pi ival2. pi, pi2 ival2.

:

int &ri = ival, &ri2 = ival2;

ri = ri2;

ival , ri - ival.

, :

- , , , extern .

.

, .

, .

. ++ , .

, , , .

 

1.14

 

. , .

:

[ ] [const] [[]] []

. , ( ). , , .

float a [10]; // 10

. () . 0 n 1 (n ). , .

. . , , , , :

int b[5] = {3, 2, 1}; // b[0] =3, b[1] = 2, b[2] =1, b[3] = 0, b[4] = 0

, . , .

, . . .

: .

#include <iostream.h>

int main() {

const int n = 10;

int i, sum;

int marks[n] = {3, 4, 5, 4, 4};

for (i = 0, sum = 0; i < n; i++) sum += marks[i];

cout << : << sum;

return 0; }

, . .

. . , , , , , n-1 . .

#include <iostream.h>

int main() {

const int n = 20; //

int b[n]; //

int i;

for (i = 0; i < n; i++) cin >> b[i]; //

for (i = 0; i < n-l; i++){ // n-1

// :

int imin = i;

// :

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

// , :

if (b[j] < b[imin]) imin = j;

int a = b[i]; //

b[i] = b[imin]; //

b[imin] = a; // i imin

}

for (i =0; i < n; i++) cout << b[i] << ; // :

return 0;

}

i imin i- 1.7. .

1.7 -

. , b , &b[0], i- , *(b+i).

, .

: b.

int [100], b[100];

int * = ; // int *pa = &[0];

int *pb = b;

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

*pb++ = *pa++; // pb[i] = pa[i];

.

,

int matr [6][8];

6 8 .

, . .

, , matr[i][j]. matr , matr[i] - i- , : *(matr[i]+j) *(*(matr+i)+j

:

, . : int mass [][2] = { {1, 1}, {0, 2}, {1, 0} };

, :

int mass [3][2] = {1, 1, 0, 2, 1, 0};

: , , .

#include <stdio.h>

int main() {

const int nstr = 4, nstb = 5; //

int b[nstr][nstb]; //

int i, j;

for (i =0; i < nstr; i++) //

for (j = 0; j < nstb; j++) scanf(%d, &b[i][j]);

int istr = -1; //

MaxKol = 0; //

for (i = 0; i < nstr; i++) { //

int Kol = 0; //

for (j = 0; j < nstb; j++)

if (b[i][j] == 0) Kol++;

if (Kol > MaxKol){istr = i; MaxKol = Kol;}

}

printf( :\n);

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

for (j = 0; j <nstb; j++) printf(%d, b[i][j]);

printf ("\n");

}

if (istr == -1) printf( );

else printf( : %d, istr);

return 0;

}

, . Kol . .

, .

new, :

int n = 100;

float * = new float [n];

- float, , 100 float, .

mall :

int n = 100;

float *q = (float *) malloc(n * sizeof(float));

, malloc, , void*, float.

, .

, new [], delete [], , malloc free. :

delete [] p; free (q);

delete , .

, . , [5] *(+5).

new ( ), :

int nstr = 5;

int **m = (int **) new int [nstr][10];

, . , :

int nstr, nstb;

cout << : ;

cin >> nstr >> nstb;

int **a = new int *[nstr]; // 1

for (int i = 0; i < nstr; i++) // 2

a[i] = new int [nstb]; // 3

1 int ( nstr). 2 . 3 , . nstb int ( 1.8).

- delete []. .

 

1.8 -

: . * () [] (), , :

int *[10]; 10 int.

 

1.15

 

, - . .

- (, ) :

[ ] ([ ])[throw ()]

{ }

, :

[ ] ([ ])[throw ()];

, . , .

:

extern - ( );

static , .

. , , . , void. , ( int).

, . . . . ( ) void.

.

, .

.

, , . . :

return [];

return, . void main . .

.

:

int f1(){return 1;} //

void f2(){return 1;} // - f2

double f3 (){return 1;} // - 1 double

, , - ( ).

:

( );

:

( )

. , . , .

. , , .

: , :

#include <iostream.h>

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

int main() {

int a = 2, b = 3, c, d;

= sum(a, b); //

cin >> d;

cout << sum(c, d); //

return 0;

}

int sum (int , int b) { //

return ( + b);

}

. .

, , . , , , .

, . , static.

:

#include <iostream.h>

void f (int a){

int m = 0;

cout << "n m p\n";

while (a--) {

static int n = 0;

int p = 0;

cout << n++ << ' ' << m++ << ' ' << p++ < '\n';

}

}

int main(){ f(3); f(2); return 0;}

n , . m . .

:

n m

0 0 0

1 1 0

2 2 0

n m

3 0 0

4 1 0

, , , .

:

int* f () {

int = 5;

return &; // !

}

inline . . , ( , ). .

inline . , , inline- .

 

1.16

 

.

, . , . . .





:


: 2016-11-12; !; : 4750 |


:

:

, ,
==> ...

1510 - | 1491 -


© 2015-2024 lektsii.org - -

: 0.279 .