.


:




:

































 

 

 

 


Cin.getline ( mainstr, m);




cout << " " << endl;

Cin.getline (substr, m);

cout << " : " << mainstr << endl;

cout << "ϳ: " << substr << endl;

pt = strstr (mainstr, substr);

cout << " " << endl;

While (pt)

{

k++;

n = pt - mainstr;

cout << k << " n = " << n << endl;

pt = strstr(++pt, substr);

cout << k << "." << *pt << endl;

}

if (k == 0) cout <<"ϳ " << endl;

}

 

3.8 .

 

//P3_8.CPP

#include <string.h>

#include < iostream>

Using namespace std;

Main()

{

const int m = 25;

char sim, *pt, str[m];

int n, k = 0;

cout << " " << endl;

Cin.get (str, m);

cout << " " << endl;

cin >> sim;

pt = strchr (str, sim);

cout << " " << endl;

While (pt)

{

k++;

n = pt - str;

cout << k << " n = " << n << endl;

pt = strchr(++pt, sim);

}

cout << "- ="<<k<<endl;

if(k==0) cout << " " << endl;

Return 0;

}

 

3.9 "" " " ""

 

//P3_9.CPP ,

// "" "" ""

#include <string.h>

#include < iostream>

Using namespace std;

Void main(void)

{

const int n = 5;

char gr [ 5 ] [15], s2[2] = {"k"};

int ;

cout << " " << endl;

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

cin >> gr[i];

cout << " " << endl;

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

cout << gr[i] << endl;

Strrev (s2);

cout << " " << endl;

for ( = 0; < n; ++)

if (((gr [] [0] == '') || (gr [] [0] == ''))

&& (strncmp (strrev (gr[i]), s2,2) == 0))

cout << strrev(gr[i]) << endl;

}

 

 

3.2

 

1. ?

2. ?

3. ?

4. ?

5. ?

6. ?

7. ?

8. ?

9. ++ ?

 

3.4

 

, , .

 

1. . ϳ , Do d.

2. , ().

3. .

4. , ().

5. , .

6. . .

7. : , , .

8. 10 .

9. 10 ..

10. , .

11. ³ , .

 

12. ,

13. , .

14. .

15. 10 , .

16. ³ , .

17. , .

18. ϳ ( ).

19. , pro.

20. ++. ϳ cut.

21. :

!

!

.

³ .

22. , .

23. :

,

,

' ,

, !

, .

24. (. 23) , .

25. ++ for.

 


4 в

4.1 ֳ

4.2

.

, ' ( ), '. .

- , . : ( ) .

:

 

struct < ' >

{ < 1 > ' 1;

< 2 > ' 2........;

} [ 1, 2....];

 

struct ;

< ' > ' ;

< 1>, < 2 > ;

' 1, ' 2, ;

1, 2....; .

 

4.1. : , , , .

:

Struct stud

{

char fam [25]; //

int mat, fiz, prg; //

float sb; //

} st1,st2;

st1 st2 , :

stud st1 st2;

, . :

Struct stud

{ char fam [25];

Int mat,fiz, prg;

Float sb;

}

st1 = { " ..", 4, 5, 5},

st2 = { " ..", 3, 4, 5};

, ' , ' , ( ). , , st1 ' ,

stud st1 = {" ..",4,5,5); , :

// 4_1. PP

//

#include < string.h >

#include < stdio.h >

#include < iostream>

Using namespace std;

Struct stud

{ char fam [20];

Int mat, fiz, prg;

Float sb;

} st1, st2;

Main ()

{

strcpy (st1. fam, " ..");

st1. mat = 4;

st1. fiz = 5;

st1. prg = 5;

st1. sb = (st1. fiz + st1. mat + st1. prg) / 3;

st2 = st1;

puts (st2. fam); // st2;

cout << st2. mat << st2. fiz << st2. prg << st2. sb << endl;

Return 0;

}

st1 . , st1.fam strcpy (st1.fam, " ..");. st2 , st1, st2 = st1;. , . , :

 

Struct

{ char fam [25];

Int mat, fiz, prg;

Float sb;

} st1, st2;

 

"" ( ) , ' , "". . , , : , (, , ) , , : , , , .

:

Struct stud

{ char fio [25];

Int den,god;

char mes [10];

Char grup;

Float sb;

}

 

Struct prep

{ char fio [25];

Int den, god;

char mes [10];

Char kaf, dolg;

}

. :

:

Struct spd

{ char fio [25];

Int den,god;

char mes[10];

};

:

Struct stud

{ spd dr;

Char grup;

Float sb

} st1,st2;

:

Struct prep

{ spd dr;

char kaf [10];

char dolg [15];

} pr1,pr2;

stud prep , spd. fio, den, god, mes , :

St1. dr. fio

, gets (st1. dr. fio); pr1. dr. fio.

ϳ , , :

Struct stud

{ char fam [25];

Int mat, fiz, prg;

Float sb;

} st1, *pst;

 

:

(*), ,

gets ((*pst). fam); (*pst). fiz = 5;

"->", ,

gets (pst -> fam); pst -> fiz = 5; ..

st1 , , .

' , , :

 

Struct stud

{ char fam [25];

Int mat, fiz, prg;

Float sb;

} spis[15], *sp = &spis[0];

 

, , :

stud spis [15];.

, ' :

 

strcpy(spis [1]. fam, " ");

spis [1]. fiz = 5;

 

strcpy ((sp +1) -> fam, " ");

(sp + 1) -> fiz = 5;

:

 

strcpy ((* (spis +1)). fam, " ");

(*(spis+1)). fiz = 5;

 

, (.) " " , (*).

.

 

4.2. ' 25 , : , , :

, ;

;

.

:

// P4_2. CPP ¾

//

#include < string.h >

#include < stdio.h >

#include < iomanip >

#include < iostream >

Using namespace std;

Struct stud

{ char fam[25];

Int mat, fiz, prg;

Float sb;

};

Void main(void)

{

const k = 5;

int n = 1, i;

Float sm, sf, sp;

stud ved[k];

sm = sf = sp = 0;

cout << " mat, fiz, prg \n ";

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

{

gets (ved[i]. fam);

cin >> ved[i]. mat >> ved[i]. fiz >> ved[i]. prg;

ved[i]. sb = (ved[i]. mat + ved[i]. fiz + ved[i]. prg) / 3;

sm += ved[i]. mat;

sf += ved[i]. fiz;

sp += ved[]. prg;

}

cout << "\t ³ \n\n";

cout << "\t";

for (i = 0; i < 47; i++) cout << "-"; //

cout << "\n";

cout << "\t \t \t . \n";

for (i = 0; i < 47; i++) cout << "-"; //

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

{

cout << "\t" << i + 1 << " " << setw(17)

<< setiosflags (ios:: left) << ved[i]. fam;

cout << ved[i]. mat << " " << ved[i]. fiz <<

" " << ved[i]. prg << " " << ved[i]. sb << "\n";

}

cout << "\t";

for (i = 0; i < 47; i++) cout << "-"; //

cout << "\n\t" << setw(16) << setiosflags (ios:: right)

<< ". ";

cout << " " << sm / k << " " << sf / k << " " << sp / k << "\n\n";

cout << "\t ³ : \n\n";

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

if (ved[i]. prg == 5)

{

cout << "\t" << n << " " << ved[i]. fam << "\n";

n++;

}

}

:

 

³

_______________________________________________

.

_______________________________________________

人 .. 3 4 4 3.7

.. 5 5 4 4.6

.. 4 5 5 4.6

_______________________________________________

. 3.8 4.6 4.8

 

³ :

 

..

 

 

, , stud ' . :

 

Struct stud1

{ char fam [25];

int pr [3];

Float sb

} st1 [10], *pst = &st1 [0];,

:

 

((*pst). fam) // gets ((*pst).fam);

(pst -> pr [0]) // cin >> pst -> pr[0] >> pst -> pr[1];

// cin >> pst -> * (pr +1).

< stdlib.h > . , :

 

#include < stdlib.h >

void qsort (void *base, n, width,

int (* fcmp) (const void *elem 1, const void -> elem2)),

base ;

n ;

width - .

 

qsort () .

:

 

int (*fcmp) (const void *elem1, const void *elem2).

 

, fcmp, , elem1 elem2.

:

 

< 0, *elem1 < *elem2;

= 0, *elem1 = *elem2;

> 0, *elem1 > *elem2;

 

*elem1 > *elem2, , elem1 ; "" .

. ³ , , , , ; , .

< stdlib.h > .

 

4.3

1. ?

2. ?

3. ?

4. ?

4.4.

1. , -?

2. - ( 5) ?

3. , , 180 ?

4. .

5. . : , , .

6. ³ - .

7. ' . ?

8. ?

9. , : - 1- , 2- 3- .

10. dz : , .

11. , , - .

12. ?

13. ' . ' .

14. 7 .

15. , . , .

16. , , . , .

17. ?

18. - "" ""?

19. - ?

20. , 4 5 ?

21. , ' .

22. , .

23. , "", .

24. , .

25. .

 

 


5 ֲ ֲ

 

 

5.1 ֳ

.

 

5.2

. , ++ () .

¾ , .

++ , .. ' main, - . . - , :

 

[ ] ' ([ ])

{

//

;

;

[return] [];

}

[ ], , ;

¾ - , (' ), , (int); (void), (). (*) ¾ ', ' (&) ( return);

' ¾ - ', , "_" (), "_", ' main, ' (), , , ¾ . , ' ¾ , ;

¾ , , . , , (), (void). - .

:

 

([const] 1 [ 1], [const] 2 [ 2],...)

, , . const , ( ' ). , , , , ﳿ. ֳ ﳿ , , , :

Void fun (int p)

{

++ p;

cout << " p=" << p << endl;

}

Void main ()

{

int x = 10;

fun (x); //

cout << "x=" << x << endl;

}

: =11, = 10, fun(x) 10, p 1, (++) , = 11, . , , -. , - , :

// -

void fun (int &)

{ ++p;

cout << "p = " << << endl;

}

Void main ()

{

int x = 10;

Fun(x);

cout << "x=" << x << endl;

}

 

: =11 =11.

 

ϳ - :

 

// -

void fun2 (int *p)

{

++*;

cout << "*p = " << *p << endl;

}

Void main ()

{

int x = 10;

fun2 (&x);

cout << "x = " << x << endl;

}

, = 11 = 11.

ϳ , , ( ). , - , -, const.

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

, , . . , '. , , .

, , static, :

 

static int x, y; static float p = 3.25;

 

, , .

, :

¾ , , ;

¾ : (&); (*), &, (&); ;

¾ () , , , ;

¾ , , static, :

 

static char st[ ] = " ..";

 

¾ ( ) , ; , ' , , :

 

int minmas (int mas [ 50]); //

int minmas (int mas [ ], int n); //

int minmas (int *mas, int n); //

 

¾ ¾ , ;

¾ , , :

double sgr (double); // sqr

.........

Int main ()

{

cout << "5**2 = " << sgr (5) << endl; //

}

 

, ";", .

¾ ++ ' . , . : - , , ( ), . , : , , , .

ϳ , , ' , , , , . , , , ( ) , , , :

 

void funct1 (float x, int y, int z =80)

{

cout << "x = " << x << " y= " << y << "z=" << z << endl;

}

void funct2 (float x, int y = 25, int z = 100)

{

cout << "x=" << x << "y=" << y << "z=" << z << endl;

}

void funct3 (float x=3.5, int y= 40, int z =200)

{

cout << "x = " << x << "y = " << y << "z = " << z << endl;

}

Main ()

{

funct1 (5.1, 10); // ¾ z

funct2 (10.2); // ¾ y,z

funct3 (); //

}

: x =5.1 y = 10 z = 80

x =10.2 y = 25 z = 100

x =3.5 y = 40 z = 200

, ¾ , , . , , , . , , funct (13.5, 75); :

 

x = 13.5 y =75 z = 80, z - .

5.2.1

 

++ . ' - ¾ , , . - :

 

type (*name) ( );,

 

type ¾ , ,;

name ¾ ' - .

 

:

¾ ;

¾ () (), .

5.1

//P5_1.CPP

// ¾ difference() sum().

#include < iostream >

Using namespace std;

int difference (int, int); //

Int sum (int, int);

Void main ()

{

int (*fun) (int, int);

int x = 20, y = 5, z;

fun = difference; // -

z = fun (x, y);

cout << "z = " << z << ndl;

fun = sum; //

z = fun (x, y);

cout << "z = " << z << endl;

}

int difference (int a, int b) //

{ return (a - b);

}

Int sum (int a, int b)

{ return (a + b);}

, ' , , , :

 

int god (const void*, const void *);

int chena (const void*, const void *);

int nazv (const void*, const void *);

int avtor (const void*, const void *);,

int (*fcmp[4]) () {god, chena, nazv, avtor};.

 

, , :

 

int i =0;

fcmp [i] (pt1,pt2); // god (pt1, pt2);

, , ..

, "*" "&".

"*" , '. ' , :

 

type * fname ( ).

 

(), :

 

har* dayweek (int data)

{

static char *weekday[ ] = {"Sunday", "Monday", "Tuesday",

"Weduesday", "Thursday", "Friday", "Saturday"}

return weekday [data % 7];

}

dayweek data, ,

, char* weekday ¾ char, char *.

ϳ , "&". :

type *funame ( )

{

Static type x;

//

return &x;

}

, ' , , . , "&", :

type& funame ( ).

5.2.2 -

() , . , ( ). , :

 

vid sort (int mas [ 30 ]);.

, , ' , , :

 

void sort (int mas [ ], int n);.

( ), , .

5.2 , .

 

//P5_2.CPP

#include < string.h >

#include < iostream >

Using namespace std;

void fun1 (char st [15]);

Int main ()

{

char p[15] =" ";

Fun1 (p);

cout << "p=" << p << endl; // p=" "

Return 0;

}

void fun1 (char st[15])

{

cout <<"p=" << st << endl; //

strcpy (st, " ");

}

 

:

=

=

, . , .

, :

 

void fun1 (int mat [7][10]); // mat(7,10)

, , , :

 

void fun2 (int mat [ ] [10], int rows, int cols);

 

5.3 , .

 

// P5_3.CPP

//

#include < iostream >

Using namespace std;

const int mincol = 1;

const int maxcol = 20;

const int minrow = 2;

const int maxrow = 30;

// getnum ()

int getnum (const char *elemtype, int low, int high)

{

Int n;

Do

{

cout << " " << elemtype

<< " N[ " << low << " ][ " << high << " ]: ";

cin >> n;

}

while ((n<low) || (n>high));

Return n;

}

// inmatr ()

void inmatr (float matr [ ] [maxcol], int rows, int cols);

{

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

{

cout << " " << i << " " << endl;

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

cin >> matr [i][j];

}

cout << endl;

}

 

// srcols ()

void srcols (float matr [ ] [maxcol], int rows, int cols)

{

Float sum, sr;

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

{

sum =0.0;

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

sum += matr [i][j];

sr = sum / rows;

cout << " " << j << " = " << sr << endl;

}

}

 

 

//

Int main ()

{

float matr [maxrow] [maxcol];

Int rows, cols;

 

//

rows = getnum ("rows", minrow, maxrow);

cols = getnum ("colnums", mincol, maxcol);

//





:


: 2015-11-05; !; : 1041 |


:

:

.
==> ...

1709 - | 1659 -


© 2015-2024 lektsii.org - -

: 0.488 .