.


:




:

































 

 

 

 


( )

: .

 

1.1. 1. - iTakeOut.

1.2. . , .

 

2.1. 1 ( ).

#include "stdafx.h"

#include <stdlib.h>

# include <string.h>

# include <stdio.h>

# include <malloc.h>

# include <conio.h>

 

 

# define STAFF struct sStaffType

STAFF // -

{

int iYearsOfService; // ()

float fHourlyWage; //

};

# define STUDENT struct sStudentType

STUDENT

{

float fGradePtAverage; //

int iLevel; //

};

# define PROFESSOR struct sProfType

PROFESSOR

{

int iDepartmentNumber; //

float fAnnualSalary; //

};

# define NODE_TYPE enum eNodeType

typedef NODE_TYPE {student, professor, staff};

# define TREE struct sTree

TREE

{

char sLastName[15]; //

char sFirstName[15]; //

int iAge; //

TREE *Left, *Right; // ()

NODE_TYPE tag; // -

union

{

STUDENT student;

PROFESSOR professor;

STAFF staff;

} uNodeTag; // ,

}; //

extern void Insert(TREE **root, TREE *item); // item

extern void Display(TREE *root); //

extern int iIsPresent(TREE *root, TREE *item); // item ?

extern int iTakeOut(TREE **root, TREE *item); // item

extern void Destroy(TREE *root); //

static TREE* CreateNode(TREE* item) // item

{

TREE* node;

node = (TREE*) malloc(sizeof(TREE));

*node = *item;

return node;

}

void Destroy(TREE* root) //

{

if (root) //

{

Destroy(root->Left);

Destroy(root->Right);

free(root); // ,

}

root = 0;

}

int iTakeOut(TREE** root, TREE* item) // item

{

TREE *previous = 0, //

*present = *root, //

*replace, // ,

*s, //

*parent; //

int iFound = 0;

while (present &&!iFound) // item

{

if(strcmp(item->sLastName, present->sLastName) == 0)

iFound = 1; //

else

{

previous = present;

// ASCII item ASCII

// (present),

// () present, -

if(strcmp(item->sLastName, present->sLastName) < 0)

present = present->Left;

else

present = present->Right;

}

}

if (iFound) // item

{

if (present->Left == 0) //

replace = present->Right;

else

{

if (present->Right == 0) //

replace = present->Left;

else // ()

{

parent = present;

replace = present->Right;

s = replace->Left;

// ,

// (present)

while (s!= 0) //

{ //

parent = replace;

replace = s;

s = replace->Left;

}

if (parent!= present) //

{

parent->Left = replace->Right; //

replace->Right = present->Right; //

}

replace->Left = present->Left; //

}

}

if (previous == 0) //

*root = replace;

else

if(present == previous->Left) //

previous->Left = replace;

else //

previous->Right = replace;

free (present); //

}

return iFound; // 1 - , 0 -

}

void Insert(TREE **root, TREE *item) // item

{

TREE *parent = 0,

// current () ()

*current = *root;

TREE *new_node; //

int iFound = 0;

while (current &&!iFound) // item

{

if (strcmp(item->sLastName, current->sLastName) == 0) iFound = 1;

else

{

parent = current;

if (strcmp(item->sLastName, current->sLastName) < 0)

current = current->Left; //

else

current = current->Right; //

}

}

if (iFound == 0)

{

if (parent == 0) // -

{

*root = CreateNode(item); //

(*root)->Left = (*root)->Right = 0;

}

else //

{

new_node = CreateNode(item);

new_node->Left = new_node->Right = 0;

if (strcmp(item->sLastName, parent->sLastName) < 0)

parent->Left = new_node;

else

parent->Right = new_node;

}

}

}

void Display(TREE *root) //

{

if(root) //

{

Display(root->Left);// ()

printf("\n%s, %s", root->sLastName, root->sFirstName);

printf("\n Old - %d", root->iAge);

switch(root->tag) //

{ // switch

case student: // (enum)

printf("\nReyting: %.2f",

root->uNodeTag.student.fGradePtAverage);

printf("\nKurs: %d\n", root->uNodeTag.student.iLevel);

break;

case professor:

printf("\nNumber of kafedra: %d",

root->uNodeTag.professor.iDepartmentNumber);

printf("\nYear selary: %.2f\n",

root->uNodeTag.professor.fAnnualSalary);

break;

case staff:

printf("\n Time of work(year): %d",

root->uNodeTag.staff.iYearsOfService);

printf("\nSelary of oure: %.2f\n",

root->uNodeTag.staff.fHourlyWage);

}

Display(root->Right); //

}

}

int iIsPresent(TREE *root, TREE *item)

{

TREE *current = root; // ()

int iFound = 0;

while (current &&!iFound) // item

{

if (strcmp(item->sLastName, current->sLastName) == 0) iFound = 1;

else

{ // ASCII item ASCII (current)

if (strcmp(item->sLastName, current->sLastName) < 0)

current = current->Left; //

else

current = current->Right; //

}

}

return iFound; // - 0, - 1

}

TREE* sMyTree;

void main()

{

//

TREE* item1 = (TREE*) malloc(sizeof(TREE));

TREE* item2 = (TREE*) malloc(sizeof(TREE));

TREE* item3 = (TREE*) malloc(sizeof(TREE));

//

strcpy(item1->sLastName, "Fyfikov");

strcpy(item1->sFirstName, "Ziberman");

item1->iAge = 32;

item1->tag = staff;

item1->uNodeTag.staff.iYearsOfService = 3;

item1->uNodeTag.staff.fHourlyWage = 5.25;

//

Insert(&sMyTree, item1);

strcpy(item2->sLastName, "Vibigalo");

strcpy(item2->sFirstName, "Ivanov");

item2->iAge = 56;

item2->tag = professor;

item2->uNodeTag.professor.iDepartmentNumber = 7;

item2->uNodeTag.professor.fAnnualSalary = 15321.0;

Insert(&sMyTree, item2);

strcpy(item3->sLastName, "Sidorov");

strcpy(item3->sFirstName, "Antonov");

item3->iAge = 18;

item3->tag = student;

item3->uNodeTag.student.iLevel = 1;

item3->uNodeTag.student.fGradePtAverage = 0.75;

Insert(&sMyTree, item3);

Display(sMyTree); //

getchar();

if(iIsPresent(sMyTree, item2))

printf("\n 2- element out of tree\n");

else

printf("\n element out of tree\n");

getchar();

iTakeOut(&sMyTree, item1);

Display(sMyTree);

getchar();

iTakeOut(&sMyTree, item2);

Display(sMyTree);

getchar();

iTakeOut(&sMyTree, item3);

Display(sMyTree);

getchar();

printf("\n");

}

 

2.3. .

2.4. , .

2.5 , .

2.6 sTree , set STL. .

 

  1. , , , , , .
  2. .
  3. , 2.4, 2.5 2.6 .

 

 

3.1. STL, .

3.2. STL, .

3.3. STL, .

3.4. STL, .

3.5. STL, .

3.6. , .

3.7. , .

3.8. .

3.9. , .

3.10. , (, ).

3.11. , , .

 

N 10

++

: ++

 

1.1. 1. .

1.2 , , ++. .

 

2.1. 1.

// Figura.cpp:

#include "stdafx.h"

#include "afxwin.h"

#include "iostream"

using namespace std;

 

class Figure{

static HWND hwnd;

protected:

static HDC hdc;

public:

Figure(){/*cout<<"\n Figure()";*/}

void show(){}

void hide(){}

void move(int x, int y){}

static void InitGraphic(){hwnd=FindWindow(_T("ConsoleWindowClass"),_T("C:\\Windows\\system32\\cmd.exe"));hdc=GetWindowDC(hwnd);}

static void CloseGraphic(){ReleaseDC(hwnd, hdc); CloseHandle(hwnd);}

~Figure(){/*cout<<"\t ~Figure()";*/}

};

HWND Figure::hwnd = 0;

HDC Figure::hdc = 0;

 

class Square: public Figure {

POINT pt[5];

public:

Square(POINT* p){

for(int i =0; i <5; i++){pt[i].x = p[i].x;pt[i].y = p[i].y;}

}

void show(){

CPen pen(PS_SOLID,2,RGB(255,0,0));

SelectObject(hdc,pen);

Polyline(hdc,pt,5);

}

void hide(){

CPen pen(PS_SOLID,2,RGB(0,0,0));

SelectObject(hdc,pen);

Polyline(hdc,pt,5);

}

void move(int x, int y){for(int i = 0; i<5;i++){ pt[i].x+=x;pt[i].y+=y;} }

~Square(){/*cout<<"\t ~Square()";*/}

};

 

class ClsEllipse: public Figure {

public:

CPoint pt1,pt2;

ClsEllipse(){/*cout<<"\t ClsEllipse()";*/

pt1.x=100; pt1.y=100;

pt2.x=200; pt2.y=200;

}

void show(){

CPen pen(PS_SOLID,2,RGB(0,255,0));

SelectObject(hdc,pen);

Arc(hdc,pt1.x,pt1.y,pt2.x,pt2.y,100,200,0,100);

}

void hide(){

CPen pen(PS_SOLID,2,RGB(0,0,0));

SelectObject(hdc,pen);

Arc(hdc,pt1.x,pt1.y,pt2.x,pt2.y,100,200,0,100);

}

void move(int x, int y){ pt1.x+=x,pt1.y+=y,pt2.x+=x,pt2.y+=y; }

~ClsEllipse(){/*cout<<"\t ~ClsEllipse()";*/}

};

//

class MyObject{

Square sq1, sq2; // ( )

ClsEllipse& elp; // ( )

public:

MyObject(const Square& p1,const Square& p2,ClsEllipse& el):sq1(p1),sq2(p2), elp(el){/*cout<<"\t MyObject()";*/}

void show(){sq1.show(); sq2.show();elp.show();}

void move(int x, int y){sq1.move(x,y); sq2.move(x,y);elp.move(x,y);}

void hide(){sq1.hide(); sq2.hide(); elp.hide();}

~MyObject(){/*cout<<"\n ~MyObject()";*/}

};

//

class Heir: Square, ClsEllipse{

public:

Heir(POINT *p):Square(p),ClsEllipse(){/*cout<<"\t Heir()";*/ }

void show(){Square::show(); ClsEllipse::show();}

void move(int x, int y){Square::move(x,y); ClsEllipse::move(x,y);}

void hide(){Square::hide(); ClsEllipse::hide();}

~Heir(){/*cout<<"\n ~Heir()";*/}

};

 

void ShowMyObject(MyObject obj){

for(int i = 0; i <100; i++){obj.show(); Sleep(24); obj.hide(); obj.move(4,0);}

}

void main(){

POINT pt1[5];

pt1[0].x = 40;pt1[0].y=40;

pt1[1].x = 40;pt1[1].y=140;

pt1[2].x = 140;pt1[2].y=140;

pt1[3].x = 140;pt1[3].y=40;

pt1[4].x = 40;pt1[4].y=40;

Figure::InitGraphic();

{

Square sq1(pt1); ClsEllipse elp;

for(int i = 0; i <100; i++){ sq1.show();elp.show(); Sleep(24); sq1.hide(); elp.hide(); sq1.move(1,1); elp.move(2,2);}

}

ClsEllipse elp;

Square sq2(pt1);

sq2.move(20,20);

MyObject obj(pt1, sq2, elp);

getchar();

ShowMyObject(obj);

{

Heir hr(pt1);

getchar();

for(int i = 0; i <100; i++){hr.show(); Sleep(24); hr.hide(); hr.move(0,3);}

}

Figure::CloseGraphic();

}

 

2.2. , , .

2.3. , , , .

2.4 .

 

3.1 , , , , , .

3.2 .

3.3 , 2.2, 2.3 2.4 .

 

 

4.1. .

4.2. .

4.3. .

4.4. . .

4.5. Square, .

4.6. ClsEllipse, .

4.7. MyObject, .

4.8. . .

4.9. , .

4.10. , .

 

N 11

++

: ++

 

1.1. 1. .

1.2 : , , , , , ++. .

 

2.1. 1.

 

// C/++ -> ; /MTd

// ->-> ; uafxcwd.lib

#include "stdafx.h"

#include "afxwin.h"

#include "iostream"

using namespace std;

 

HWND hwnd = 0;

HDC hdc = 0;

void InitGraphic(){

system("mode con cols=168 lines=55"); system("pause >> void");

hwnd=FindWindow(_T("ConsoleWindowClass"),_T("C:\\Windows\\system32\\cmd.exe"));hdc=GetWindowDC(hwnd);

}

void CloseGraphic(){ReleaseDC(hwnd, hdc); CloseHandle(hwnd);}

 

//-------------------------------------------------------------------------- IFigure

class IFigure{ //

protected:

int fMove; //0 - ; 1 - ; 2 - ;

int fClr; //0 - ; 1 -

public:

IFigure(): fMove(0), fClr(0){/*cout<<"\n IFigure()";*/}

virtual void show()=0;

virtual void hide()=0;

virtual void move(int x, int y)=0;

};

 

//-------------------------------------------------------------------------Square

class Square:virtual public IFigure {

POINT pt[5];

COLORREF color;

public:

Square(POINT* p): color(RGB(255,0,0)){ for(int i =0; i <5; i++) pt[i] = p[i]; }

void SetColor(COLORREF cl){color = cl;}

void show(){

CPen pen(PS_SOLID,2,color);

SelectObject(hdc,pen);

Polyline(hdc,pt,5);

}

void hide(){

CPen pen;

pen.CreatePen(PS_SOLID,2,RGB(0,0,0));

SelectObject(hdc,pen);

Polyline(hdc,pt,5);

}

void move(int x, int y){for(int i = 0; i<5;i++){ pt[i].x+=x;pt[i].y+=y;} }

virtual ~Square(){/*cout<<"\t ~Square()";*/}

};

//---------------------------------------------------------------------------ClsEllipse

class ClsEllipse: virtual public IFigure {

CPoint pt1,pt2;

public:

ClsEllipse():pt1(100,100),pt2(200,200) {}

virtual void show() {

CPen pen(PS_SOLID,2,RGB(0,255,0));

SelectObject(hdc,pen);

Arc(hdc,pt1.x,pt1.y,pt2.x,pt2.y,100,200,0,100);

}

virtual void hide() {

CPen pen(PS_SOLID,2,RGB(0,0,0));

SelectObject(hdc,pen);

Arc(hdc,pt1.x,pt1.y,pt2.x,pt2.y,100,200,0,100);

}

virtual void move(int x, int y) { pt1.x+=x,pt1.y+=y,pt2.x+=x,pt2.y+=y; }

virtual ~ClsEllipse(){/*cout<<"\t ~ClsEllipse()";*/}

};

//-------------------------------------------------------------------------Rectan

class Rectan: public IFigure {

Square* pSq;

public:

// operator=

virtual void show(){pSq->show();}//

virtual void move(int x, int y){pSq->move(x,y);}//

virtual void hide(){pSq->hide();}//

void SetColor(COLORREF cl){pSq->SetColor(cl);}

Rectan (Square& p){pSq = new Square(p);}

virtual ~Rectan(){delete pSq;}

};

//-------------------------------------------------------------------------DrowTxt

class DrowTxt{

CString str;

public:

DrowTxt(CString s):str(s){}

void show(){

CDC* pCDC = CDC::FromHandle(hdc);

pCDC->SetTextColor(RGB(255,0,0));

pCDC->SetBkColor(RGB(0,0,0));

pCDC->TextOutW(300,100,str); pCDC->TextOutW(0,0," ");

}

};

 

//-------------------------------------------------------------------------Heir

class Heir: public Square, public ClsEllipse{ //

public: //( )

Heir(POINT *p):Square(p),ClsEllipse(){/*cout<<"\t Heir()";*/ }

void show(){Square::show(); ClsEllipse::show();}

void move(int x, int y){Square::move(x,y); ClsEllipse::move(x,y);}

void hide(){Square::hide(); ClsEllipse::hide();}

virtual ~Heir(){/*cout<<"\n ~Heir()";*/}

};

//------------------------------------------------------------------------RecordPlayer

class RecordPlayer{ // , IFigure

IFigure**pFig;// IFigure*

int n; //

int N; //

public:

void Insert(IFigure* pF){if (n<N) pFig[n++] =pF; }

RecordPlayer(int Nfig): N(Nfig), n(0) { pFig = new IFigure*[N]; }

virtual void show(){ for(int i = 0; i < n; i++) pFig[i]->show(); }//

virtual void hide(){ for(int i = 0; i < n; i++) pFig[i]->hide(); }//

virtual void move(int x, int y){ for(int i = 0; i < n; i++) pFig[i]->move(x,y); }//

void PlayMyObject(int x, int y){ for(int i = 0; i <150; i++){show();Sleep(24);hide(); move(x,y);} show();}

virtual ~RecordPlayer(){delete []pFig;}

};

 

void main(){

POINT pt1[5];

pt1[0].x = 40;pt1[0].y=40;

pt1[1].x = 40;pt1[1].y=140;

pt1[2].x = 140;pt1[2].y=140;

pt1[3].x = 140;pt1[3].y=40;

pt1[4].x = 40;pt1[4].y=40;

 

InitGraphic();

 

DrowTxt dtxt("");

dtxt.show();

getchar();

 

Heir hr(pt1);

for(int i = 0; i <100; i++){hr.show(); Sleep(24); hr.hide(); hr.move(0,3);}

getchar();

 

ClsEllipse elp;

Square sq1(pt1), sq2(pt1), sq3(pt1);

sq1.SetColor(RGB(255,255,0)); sq2.SetColor(RGB(0,255,0));

sq3.SetColor(RGB(0,0,255)); hr.SetColor(RGB(0,255,255));

sq2.move(20,20); sq3.move(40,30); hr.move(0,-150);

Rectan rec(sq3);

RecordPlayer RPlayer(5);

RPlayer.Insert(&elp);

RPlayer.Insert(&sq1);

RPlayer.Insert(&sq2);

RPlayer.Insert(&rec);

RPlayer.Insert(&hr);

RPlayer.PlayMyObject(3,0);

 

getchar();

CloseGraphic();

}

2.2 Rectan.

2.3 ClsEllipse , , .

2.4 DrowTxt , RecordPlayer .

2.5 , RecordPlayer .

2.6 , RecordPlayer , , , .

2.7 , RecordPlayer.

 

3.1 , , , , , .

3.2 .

3.3 , 2.2 2.7 .

 

4.1. .

4.2. , ?

4.3. .

4.4. . .

4.5. , , RecordPlayer.

4.6. ClsEllipse, .

4.7. Square, .

4.8. , . 2-3 , . .

4.9. , . 2-3 , . .

N 12

STL

: STL

 

1.1. 1. , CountedPtr.

1.2 STL.

 

2.1. 1.

 

 

#include "stdafx.h"

#include <iostream>

#include <list>

#include <deque>

# include <set>

#include <algorithm>

using namespace std;

 

/* , .

* , ,

* CountedPtr

* .

*/

template <class T>

class CountedPtr {

private:

T* ptr; //

long* count; // ( )

public:

//

// - p new

explicit CountedPtr(T* p = 0)

: ptr(p), count(new long(1)) {

}

 

// ( )

CountedPtr(const CountedPtr<T>& p) throw()

: ptr(p.ptr), count(p.count) {

++*count;

}

// ( , )

~CountedPtr() throw() {

dispose();

}

// ( )

CountedPtr<T>& operator= (const CountedPtr<T>& p) throw() {

if (this!= &p) {

dispose();

ptr = p.ptr;

count = p.count;

++*count;

}

return *this;

}

// ,

T& operator*() const throw() {

return *ptr;

}

T* operator->() const throw() {

return ptr;

}

private:

void dispose() {

if (--*count == 0) {

delete count;

delete ptr;

}

}

};

 

 

struct comp {

int* Re, *Im;

comp() { Re = new int; Im = new int;;*Re = 0; *Im = 0; }

comp(int r, int i) { Re = new int; Im = new int;*Re = r; *Im = i; }

~comp() { delete Re; delete Im; }

const comp& operator=(const comp& T) { // =

*Re = *T.Re; *Im = *T.Im; return *this;

}

const comp& operator-() { // -

*Re = -*Re; *Im = -*Im; return *this;

}

comp(comp& T) { Re = new int; Im = new int; *Re = *T.Re; *Im = *T.Im;}

const comp operator*(const comp& T) { // *

comp Rez(0,0);

*Rez.Re =*Re * *T.Re - *Im * *T.Im;

*Rez.Im = *Re * *T.Im + *Im * *T.Re;

return Rez;

}

double modComp()const {return sqrt(*Re**Re + *Im**Im); }

bool operator<(const comp T) { // <

if(modComp() < T.modComp()) return 0;//<

return 1;

}

void comp::display() const

{

cout << "\n Re = " << *Re << "\t Im = " << *Im;

}

 

};

 

bool operator< (const CountedPtr<comp> p1, const CountedPtr<comp> p2) {

if (*p1 < *p2) return 0;

return 1;

}

 

void printCountedPtr(CountedPtr<comp> elem)

{

(*elem).display();

}

 

int main()

{

//

typedef CountedPtr<comp> IntPtr;

deque<IntPtr> coll1;

list<IntPtr> coll2;

set<IntPtr> coll3;

 

/* */

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

IntPtr ptr(new comp(i,i));

coll1.push_back(ptr); //coll1.push_back(comp(i,i));

coll2.push_front(ptr);

coll3.insert(ptr);

}

 

//

cout<<"\n deque";

for_each(coll1.begin(),coll1.end(), printCountedPtr);

cout << "\n list";

for_each(coll2.begin(), coll2.end(), printCountedPtr);

cout << "\n set";

for_each(coll3.begin(), coll3.end(), printCountedPtr);

cout << endl << endl;

/*

* - coll1

* - coll2

*/

*coll1[1] = *coll1[1] * *coll1[1];

// *coll2.front() = - (*coll2.front());

 

//

cout << "\n\n deque";

for_each(coll1.begin(), coll1.end(), printCountedPtr);

cout << "\n list";

for_each(coll2.begin(), coll2.end(), printCountedPtr);

cout << "\n set";

for_each(coll3.begin(), coll3.end(), printCountedPtr);

cout << endl;

}

 

 

2.2. . ( CountedPtr ). , , .

2.3. (: , , , , ) .

 

3.1 , , , , , .

3.2 .

3.3 , 2.2 2.3 .

 

4.1. ?

4.2. . ?

4.3. ? ?

4.4. .

4.5. , .

4.6. , .

4.7. , .

4.8. , ClsEllipse Square.

4.9. , ClsEllipse DrowTxt.

4.10. , . 2-3 , . .

4.11. , . 2-3 , . .

 

 

1. . . ++ []: . . . - .: , 2008. - 687 .: .. - .: . 667-669 (31 .). - ISBN 978-5-279-03243-3

2. . . ++ []: : . . . .. . 1 / , , . . - : - , 2007. - 159 .. - .: . 159 (5 .)

3. STL



<== | ==>
, . | No. Don't like card games. Tried it once, and I didn't like it.
:


: 2018-11-11; !; : 136 |


:

:

, .
==> ...

848 - | 684 -


© 2015-2024 lektsii.org - -

: 0.508 .