.


:




:

































 

 

 

 


4.11 - new delete




 

#include <stdio.h>

//

void AddVec (

//

int* pA,

//

int* pB,

//

int* pC,

//

int Dim){

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

pC [ i ] =pA [ i ] +pB [ i ];

// , -,

// *(pC+i)=*(pA+i)+*(pB+i);

}

void main (void) {

//

int Dim;

//

printf ("Input Dim: ");

//

scanf ("%d", &Dim);

//

int* pA=new int [ Dim ],

* pB=new int [ Dim ],

* pC=new int [ Dim ];

// new

if (!(pA&&pB&&pC)){

// new

printf ("Memory is not allocated! \n");

return;

}

//

printf (" Input A \n ");

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

scanf (" %d ", pA+i);

//

printf (" Input B \n ");

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

scanf (" %d ", pB+i);

//

AddVec (pA, pB, pC, Dim);

//

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

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

printf (" \n ");

//

delete [] pA;

delete [] pB;

delete [] pC;

}

 

, , .

 

4.12

 

#include <stdio.h>

//

double* AddVec (

//

double* pA,

//

double* pB,

//

int Dim){

double* pC=new double [ Dim ];

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

pC [ i ]= pA [ i ]+ pB [ i ];

return pC;

}

void main (void) {

//

int Dim =3;

double A []={1,1,1},

B []={2,2,2};

//

double* pC = AddVec (A, B, Dim);

//

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

printf ("% f ", pC [ i ]);

printf ("\ n ");

//

delete [] pC;

}

 

4.5

 

. .

 

, . () .

a_ 0 ,,a_m (m+ 1) . , . , , a (m+ 1)

 

Type* a []={ a_ 0 ,...,a_m- 1};

 

(m+ 2)

 

Type* a [ m+ 1];

a [0]= a_ 0;...; a [ m ]= a_m;

 

a , , . , a [ i ] , , () i - . - i - , a [ i ]. , a [ i ][ j ] a_i [ j ]. , , ( ) , . , Type , Type * - . h w .

4.13 -

 

#include <stdio.h>

double ItemSum (double** a, int h, int w) {

double S=0;

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

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

S += a [ i ][ j ];

return S;

}

void main () {

double a_ 0[]={1, 2}, a_ 1[]={4, 5}, a_ 2[]={7, 8};

double* a []={ a_ 0, a_ 1, a_ 2 };

double S=ItemSum (a, 3, 2);

printf (" Sum=%f \n ", S);

}

 

, :

 

Sum =27.000000

 

, . . . .

 

4.14 -

 

#include <stdio.h>

double ItemSum (double** a, int h) {

double S=0;

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

for (int j=1; j<=a [ i ][0]; j++)

S += a [ i ][ j ];

return S;

}

void main () {

double a_0 []={3,1,2,3}, a_1 []={2,4,5}, a_2 []={4,7,8,1,3};

double * a []={ a_0,a_ 1 ,a_ 2};

double S=ItemSum (a, 3);

printf ("Sum=%f\n", S);

}

 

, :

 

Sum =34.00000

 

 

4.6

 

. . . rand() . . .

 

- . A , : . , . . . - , h w . h*w

 

Type* pT=new Type [ h*w ];

 

:

 

Type* AllocMemoryTable(int h, int w){

Type* pT=new Type [ h*w ];

return pT;

}

, , pT.

 

a00,,a0w-1,a10,,ah-1,w-1,

 

. . , pT , . , i=k/w, j=k%w.

. , rand (), < stdlib.h >. unsigned int [0,232-1]. , , . . m , , rand () % (m+ 1) [0, m ]. PrintTable ().

 

4.15

 

# include <stdio.h>

#include <stdlib.h>

//

void PrintTable (int* T, int h,int w){

//

printf ("\ n ");

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

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

printf ("% d ", T [ i*w+j ]);

printf ("\ n ");

}

//

printf ("\ n ");

}

void main (){

int h,w;

int m;

printf (" input h =");

scanf ("% d ",& h);

printf (" input w =");

scanf ("% d ",& w);

m =2;

int n=h*w;

//

int* pTab=new int [ n ];

if (pTab ==0){

printf (" Memory is not allocated! \ n ");

return;

}

//

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

pTab [ j ]= rand ()% m;

//

PrintTable (pTab, h, w);

//

delete [] pT;

}

 

h w

 

input h =10

input w =20

 

 

1 1 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 0 1 0

1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0

1 1 0 1 1 0 1 1 1 0 1 0 0 1 1 1 1 1 1 0

0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0

1 1 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 1 1

1 1 1 0 0 0 1 0 1 0 1 1 0 0 0 1 1 1 1 0

0 0 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 1 1 1

1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 1 0 1

0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 1 0 0

1 0 1 1 0 0 0 0 1 1 0 1 0 1 1 1 0 1 1 0

 

w Type. h Type*. h . . , . AllocMemoryTable (), :

 

int ** AllocMemoryTable (int h, int w){

//

int** ppTable=new int* [ h ];

//

if (ppTable ==0){

//

printf ( Memory is not allocated! \n );

//

return (int**)0;

}

// h w

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

ppTable [ i ] =new int [ w ];

//

if (ppTable [ i ]==0){

//

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

delete [] ppTable [ k ];

delete [] ppTable;

//

printf ( Memory is not allocated! \n );

//

return (int**)0;

}

}

}

 

ppT , aij . , ppT [ i ] i - . ppT [ i ][ j ].

. :

 

void FreeTable (int** ppT, int Height){

for (int k=0; k<h; k ++)

delete [] ppT [ k ];

delete [] ppT;

}

 

 

4.7.

 

. -. . gets () puts (), strlen (), strcmp (), strcpy (), strcat (), getch () putchar ().

 

3.5 . n n . ,

 

abcd , , 56 &%\n

 

4, 19 6 . n n+ 1 . n . (n+ 1)- - \0. char, . :

 

char* pStr= _ ;

 

, , , pStr [1] = x . , , . , , .

 

4.16

 

//

const char* pString = ;

//

pString [2]= d;

// ( )

pString = ;

 

, -, .

 

4.17 -

 

// -

char* const pString= ;

// -

pString=;

 

, n n+ 1 char, - \0. - . char. ,

 

char String []={ a , b , c , d , \0 };

char String []= abcd ;

 

.

. , :

 

char Str []= char ;

char Str []= _

char ;

 

scanf () printf (), s. . stdio.h. ,

 

char* gets (char* str);

 

str , . gets () . str. Enter , - \0. str. (char*)0. .

, str,

 

int puts (char* str);

 

, -. . EOF, stdio.h - 1. .

, string.h. , str

 

size_t strlen (const char* str);

 

size_t string.h . -.

, . . str 1 str 2

 

int strcmp (const char* str 1, const char* str 2);

 

, str 1 str 2. . str 2 str 1, .

 

char* strcpy (char* str 1, const char* str 2);

 

str 2, -, str 1. , str 1 . str 1.

 

char* strcat (char* str 1, const char* str 2);

 

() str 1 str 2. - str 1 str 2. str 1. , str 1 .

TestPassword (), . , . . , . , < conio.h > < stdio.h > .

 

int getch ();

 

. ASCII- int. Enter ASCII- \r.

 

int putchar (int ch);

 

. ASCII-, int, . ASCII- , int.

, .

 

 

4.18 -

 

// 1/3

#include<stdio.h>

#include<string.h>

#include<conio.h>

//

int TestPassword (

//

char* Password,

//

int NumberRepetition);

extern int NumberRepetition;

extern char Password [7];

 

void main (){

//

int Res=TestPassword (Password, NumberRepetition);

//

if (Res! =0){

puts (" Error!");

return;

}

puts (" Ok ");

//

//

}

// 2/3

int TestPassword (char* Password, int NumberRepetition){

//

char pBuff [255];

//

int i =0;

//

int j;

//

int Res;

//

do {

//

i ++;

//

printf (" Enter password: ");

// puts (" Enter password: ");

j =0;

//

do {

pBuff [j]= getch ();

if (pBuff [ j ++]!=' \r ')

putchar ('*');

else {

pBuff [--j]='\0';

break;

}

} while (1);

//

Res = strcmp (Password, pBuff);

printf (" \n ");

} while ((Res!=0)&&(i<NumberRepetition));

//

return Res;

}

// 3/3

//

char Password []=" Secret ";

//

int NumberRepetition =2;

 





:


: 2016-03-27; !; : 340 |


:

:

. .
==> ...

1483 - | 1444 -


© 2015-2024 lektsii.org - -

: 0.17 .