.


:




:

































 

 

 

 


(-)




++ inline. inline , " ", "-". , :

inline float module(float x = 0, float = 0) {

return sqrt(x * x + * );

}

module () float, "" (,), . sqrt() . , . , "" . inline , , . , .. inline , , . . , . ++, , , , .

, . inline . , - .

, inline , , . inline.

, inline ( ):

, ;

;

;

;

, .

- , .

 

 

( - ), . .

, , , :

( , );

;

, , , return;

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

len(), z , char [] ( ). z f , . len(f) z f, .. z " ++", main(). , len() z ( ), , . .

, , , , , sizeof(z) . ( ), . , .. char[], '\0', . , ' \0', - ( ). len() ( ), z, ' \0':

// 6.5

#include "stdafx.h"

#include <iostream>

int len(char z[]){

int m = 0;

while (z[m++]);

return m-1;

}

void main() {

char f[] = "Language C++";

std::cout<<"\nThe length of string \"Language C++\" equal "<<len(f);

}

- , , , . . , -:

// 6.6

#include "stdafx.h"

#include <iostream>

#include <math.h>

float scalar(int n, float x[], float y[]){

float a = 0;

for(int i = 0; i < n; i++) a += x[i]*y[i];

return a;

}

void main(){

float E[] = { 1, 1, 1, 1, 1, 1, 1};

float G[] = {-1, -1, -1, -1, -1, -1, -1};

std::cout <<"\n (E, G) = "<< scalar (7, E, G);

getchar();

}

: (E, G) = -7

, , , , , . , max_vect() z, - ( ):

// 6.7

#include "stdafx.h"

#include <iostream>

void max_vect(int n, int *x, int *y, int* z){

for(int i =0;i<n; i++) z[i] = x[i] > y[i]? x[i]: y[i];

}

void main(){

int a[] = { 1, 2, 3, 4, 5, 6, 7};

int b[] = { 7, 6, 5, 4, 3, 2, 1};

int c[7];

max_vect(7,a,b,c);

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

std:: cout <<"\t"<< c[i];

getchar();

}

: 7654567

scalar(), int n -.

, . , , fusion(), . .

// 6.8

#include "stdafx.h"

#include <iostream>

int *fusion(int n, int* a, int m, int* b){

int* f = new int[n + m]; /* */

int ia = 0, ib = 0, ix = 0, i;

for (i = 0; i < n; i++) f[i] = a[i];

for (; i < n+m; i++) f[i] = b[i-n];

return f;

}

void main(void) {

int c[] = { 1, 3, 5, 7, 9 };

int d[] = { 0, 2, 4, 5 };

int *h; //

int kc=sizeof(c)/sizeof(c[0]);/* []*/

int kd = sizeof(d)/sizeof(d[0]);/* d[]*/

h = fusion(kc, c, kd, d);

std::cout<< "\nresult of integration of arrays:\n";

for (int i = 0; i < kc + kd; i++) std::cout << " " << h[i];

delete [] h; //

getchar();

}

:

:

1 3 5 7 9 0 2 4 5

fusion() , , .

fusion() new () (n+m)*sizeof(int) . f. f[], . return fusion() int*, f ( ), ( ) fusion() (n, a, m, b, f, ia, ib, ix, i) . int* ( ) h.

, fusion() main(). h[i] . , , delete [] h.

, , .. int* f = new int[n + m] int f[100]. fusion() main() c h[i] . , .

: . , , , , , .

, ++ ( ) - , ( ) . . , - , , , . , , . , ++. , . , , :

double prim[6][4][2];

, prim, , double[4] [2]. , double [2]. , , double.

. , , ( ) . . .

, - :

void transponir(double x[][], int n).

n - ; double [ ][ ] - . :

Error...: Size of type is unknown or zero

- ( -) ( ) . - , . [][] , .

:

// 6.9

#include "stdafx.h"

#include <iostream>

void transp(int n, float d[][3]){

float r;

int i, j;

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

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

r = d[i][j];

d[i] [j] = d[j][i];

d[j][i] = r;

}

}

void main(){

float x[3][3] = { 0, 1, 1, 2, 0, 1, 2, 2, 0 };

int n = 3;

transp(3,x);

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

std::cout <<"\n C " <<(i+1) <<":";

for (int j = 0; j < n; j++) std::cout<< "\t" << x[i][j];

}

getchar();

}

:

1: 0 2 2

2: 1 0 2

3: 1 1 0

, transp () - , .. 33.

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

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

, . trans() , . : int n - ; double *p[] - double. . p[i] - ( double), p[i][j] - . :

// 6.10

#include "stdafx.h"

#include <iostream>

void trans(int n, double *p[]){

double x;

for (int i = 0; i < n - 1; i++)

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

x = p[i][j];

p[i][j] = p[j][i];

p[j][i] = x;

}

}

void main(){

double A[4][4]={ 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44 };

double *ptr[] = { (double *)&A[0], (double *)&A[1],

(double *)&A[2], (double *)&A[3] };

int n = 4;

trans(n, ptr);

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

std::cout <<"\n row " << (i+1) <<":";

for (int j =0; j < n; j++) std::cout<< "\t" <<A[i][j];

}

getchar();

}

:

1: 11 21 31 41

2: 12 22 32 42

3: 13 23 33 43

4: 14 24 34 44

double A[4][4]. double *[], double *ptr [ ]. , .. &[0], &A[l], &[2], &[3], double*. .

-. int*, int **pi. (n==3) . fill() "": [0][0] = 0, [0][1] =1... .., .. a[i] [j] = (i * n) + j, n - . mat : int** mat. fill() int n . :

// 6.11

#include "stdafx.h"

#include <iostream>

void fill(int n, int** mat){

int k = 0;

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

for (int j = 0; j < n; j++) mat[i][j] = k++;

}

void main() {

int **pi;

int m = 3, i;

pi = new int* [m];

for (i = 0; i < m; i++) pi[i] = new int [m];

fill(m, pi);

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

std::cout<< "\n "<< (i+1)<<":";

for (int j = 0; j < m; j++) std::cout<< "\t"<< pi[i][j];

}

for (i = 0; i < m; i++) delete pi[i];

delete [] pi;

getchar();

}

 

:

1: 0 1 2

2: 3 4 5

3: 6 7 8

, delete .

, , . . single_matr() , int **. int . , int n. n. , - , .. . single_matr() int** "" . (int n), . :

// 6.12

#include "stdafx.h"

#include <iostream>

int **single_matr(int n) { // n -

int** p; // :

p = new int* [n];/* - :*/

if (p == NULL){

std::cout <<"He !";

exit(1);

}

for (int i = 0; i < n; i++){ /* int: */

p[i] = new int [n];

if (p[i] == NULL){

std::cout <<"He !";

exit(1);

}

for (int j = 0; j < n; j++)

if (j!= i) p[i][j] = 0;

else p[i][j] = 1;

}

return p;

}

void main(){

int n; //

std::cout <<"\n Input order of matrix: ";

std::cin >>n;

int **matr, i; //

matr = single_matr(n);// :

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

std::cout << "\n row " << (i + 1) <<":"; /* : */

for (int j = 0; j < n; j++)

std::cout <<"\t" <<matr[i][j];

}// :

for (i = 0; i < n; i++) delete matr[i];

delete[] matr;

getchar();

}

 

:

: 4_ <Enter>

1: 1 0 0 0

2: 0 1 0 0

3: 0 0 1 0

4: 0 0 0 1

, , , . .

 

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

_ (*_)(__);

:

int (* funclPtr) (char); - funclPtr char, int.

, .. int *fun (char); fun char, int

:

char * (* func 2 Ptr) (char *, int); - func2Ptr char int, char.

(, ) , . :

// 6.13

#include "stdafx.h"

#include <iostream>

void f1(void) { // f1

std::cout<< "\nExecute f1()";

}

void f2(void) { // f2

std::cout<<"\nExecute f2()";

}

void main(){

void (*ptr)(void); // ptr -

ptr = f2; // f2()

(*ptr)(); // f2()

ptr = f1; // f1()

(*ptr) (); // f1() no

ptr(); // (*ptr)();

}

:

f2 () f1() f1()

ptr , f2 f1. :

(*_)(__);

_ , * . *ptr(). , () , *. , ptr(). , .

. , .

, . , .. , , . , :

char f1(char) {... } //

char f2(int) (...) //

void f3(float) (...) //

int* f4(char *){...} //

char (*ptl)(int); //

char (*pt2)(int); //

void (*ptr3)(float) = f3; //

void main(){

ptl = f1; // -

pt2 = f3; // -

// ( )

ptl = f4; // -

ptl = f2; //

pt2 = ptl; //

char = (*ptl)(44); //

= (*pt2)('\t'); // - -

}

. , float (* ptrArray)(char)[4]; - ptrArray , char float. , , , :

float = (*ptrArray[2])('f');

, 0, 2.

ptrArray . . , , , . typedef :

typedef unsigned int UINT;

UINT a = 3;

typedef float (*PTF)(float);

typedef char *(*PTC)(char);

typedef void (*PTFONC)(PTF, int, float);

ptf - " float, float". ptc - " , char, char. ptfunc - " , ( void)". : ptf - float (float), int float. ( ptfunc ptf.)

, , :

PTF ptfloat 1, ptfloat 2[5]; //

// float (float)

PTC ptchar; // char *(char)

PTFUNC ptfunc [8]; //

++ , , , . , [6,11]:

void qsort(void *base, size_t nelem, size_t width,

int (*fcmp)(const void *pl, const void *p2));

, UNIX ANSI . stdlib.h. , . qsort() , , . fcmp, . , qsort() , . qsort ():

base - ( 0- );

nelem - ( , );

width - ( , );

fcmp - , pi, 2 :

*pl < *2, femp < 0;

*pl = *p2, fcmp 0;

*pl > *p2, fcmp > 0.

" " (<) , *pl *2, .. *pi , *2. ( ) " " (>).

qsort() . , . . .

// 6.14

#include "stdafx.h"

#include <iostream>

#include <stdlib.h> // qsort()

#include <string.h> // -.strcmpO

// :

int sravni(const void *a, const void *b) {

unsigned long *pa = (unsigned long *)a,

*pb = (unsigned long *)b;

return strcmp((char *)*pa, (char *)*pb);

}

void main() {

char *pc[] = {"Ivanov", "Cidorov", "Petrov", "Antonov"};

int i, n = sizeof(pc)/sizeof(pc[0]);

std::cout << "\n before sorter:\n";

for(int i = 0; i < n; i++) std::cout<<"\t"<< pc[i];

qsort((void *)pc, // "

n, //

sizeof(pc[0]), //

sravni); // ()

std::cout << "\n After sorter:\n";

for(i = 0; i < n; i++) std::cout<<"\t"<< pc[i];

getchar();

}

 

:

:

:

 

( [ ]) sravni() strcmp(), string. h

int strcmp(const char *sl, const char *s2);

strcmp() , s1 s2. . , , .

strcmp() , (const char *). sravni() (const void *), qsort(). . sravni() (unsigned long *), ( [ ]) ). , strcmp() . , char* [ ], , pc[i]. qsort() [] . [] , . pc[i] - , cout .

, b, sravni() :

return strcmp((char *)(*(unsigned long *)a), (char *)(*(unsigned long *)b));

(void *) (unsigned long *). "" pc[i]. (char *) , strcmp().

 

1. 6.1, , print vilue.

2. 6.2, , .

3. 6.3, 6.3 6.2

4. 6.4, ,

5. ++

6. 6.5, .

7. 6.6 6.7, , .

8. , ( ) , .

9. .

10. , ( ) , .

11. , ( ) , .

12. , ( ) , .

13. , ( ) , .

14. , ( ) ,

 

1. ++

a)

b)

2.

a)

b)

3.

a)

b)

4. ++

a)

b) , , ,

5.

a)

b)

6. .

a)

b)

c) , , .

7.

a)

b)

c)

8. .

a)

b)

9. , ptr int long.

a) Int (ptr*)(long);

b) Long ptr (int)

c) Int* ptr(long);

 

1 b 6 c
2 a 7 c
3 b 8 b
4 b 9 a
5 a 10  

 

. , , .

85 100% - ;

70 84% - ;

55 69% - ;

55% - .

 

 

4

: - .

 





:


: 2018-10-15; !; : 173 |


:

:

, .
==> ...

1721 - | 1537 -


© 2015-2024 lektsii.org - -

: 0.275 .