Вы находитесь на странице: 1из 41

1

.. , ..

C++
(- )

1 2
( )
-

2
--
2005 .

-
30 2004 , 4 .

, .

- .
C++, .

- ,

.
: .., ..

3
@ .., ..

_________________________________________________4
1. ____________________6
2. ___________________9
3.
C++________________________________________________12
3.2. ______________________________________14
3.3. (inline) ____________________________________15
3.4. vector___________________________________________________18
3.5. ___________________________________________21
3.6. this_________________________________________________22
3.7. -______________________________23
3.8. ___________________________________________________24
3.9. (QUE). ____________________________26
3.10. . ____________________________28
3.11. . ___30
4. _________________________________33
4.1. ________________________________________________33
4.2. _________________________________________________35

4
4.3. __________________________________________________37
_______________________________________________________39

C++

. ,

(),

).
,
.

.
. ,
.

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

C++

().

.
Smaltalk Simula 67.
-

5
. .
-

.
( private)
, , ( protected),
, ( public)
,
. ,

.
.

- .

, .

C++,

()

. Vector
.

.

6

, , ,
, .

1.

:
(integer, real, character,

boolean);

( array,

record, union);

;
.
([1])
. ,

, ( )

:
,


(,

, ,

).

. ( )

7
1) ,
;
2)
;
3)

,
,

;
4)
;
5)
;
6)

.
;
.

,
.

,

.

Simula 67, C++,


CLU, Alphard,

Modula, Ada, Russel ,


Pascal.

.

.
1)

(),
.
2) ,

, .
3)
.
4) (2) (3), ,
(3) (2).
, .
(1)
(3). .
,
.

"", ", ", - ,


([1]).

9

,

.
:
1)

;
2) ,

.
C++ ,

),

, ,
.

- ,

C++

2.

([1]).
,
.
1.

( stack)

: "

10
" (Last In - First Out)
:
;
;
, ;
, ;
, ,
;
, , ..
, , .
2.

(Query).

"

" (First In - First Out)


:
;
;
, ;
, ;
.

3.

(vector).
n

:
;
;
;

11
;
.
4. (polynom).

y = a0 xn + a1

xn-1+ ... +an-1x + an

n+1

:
;
;
;
;
;
;
.

5.

:
;
;
;
.
6.

12
. ,
,
.
( Tree).

()

:
( );
;
;
.

3.
C++
C++

[3],

, , .
,

() .

.
(
).

13
C++
.
3.1.

,
, .

(private), ,
(public), .

, .
.
.
(stack) ( 1 2).
.
,
.

lass STACK
{ private:
struct sp
{ int inf;
struct sp *next;
};
sp *s;
public:
CREATE();
//
int top();
//
push(int x);
//
int pop();
//
int empty();
// ,
int full();
// ,
};

14
,
:

STACK

st;

st -
CREATE(),

CREATE() :

st.CREATE() ;
C++ , ,

. .
3.2.

, .

CREATE()
STACK().

STACK.
,
.

, .

.
, ,
(~).

STACK ~STACK() .

15

, -.
1.

class STACK
{ private:
struct sp
{
int inf;
struct sp *next;
};
sp *s;
public:
STACK() { s=0;}
~ STACK()
{ while (s!=0) delete s;}
int top()
{ return s->inf;
}
push(int x)
{ sp *p;
p=new sp;
s=p;
}

p->inf=x; p->next=s;

int pop()
{ sp *p;
p=s;
int r=p->inf; s=p->next;
delete p;
return r;
}
int empty()
{ return s==0;
}
int full()
{ sp *p;
p=new sp;
if (p!=0)
{ delete p; return 1; }

16

else return 0;
}
};
3.3. (inline)
-,

(inline).

, ..
.
.
- .
inline .
, inline
(, ).
,
:: .
:: .
2.

class STACK
{ private:
struct sp
{ int inf;
struct sp *next;
};
sp *s;
public:
STACK() { s=0;}
~STACK()
{ if (s!=0) delete s;}
int top();
push(int x);

17

int pop();
int empty();
int full();
};
inline int STACK :: top()
{ return s->inf;
}
push(int x)
{ sp *p;
p=new sp;
s=p;
}

p->inf=x; p->next=s;

inline int STACK :: pop()


{ sp *p;
p=s;
int r=p->inf;s=p->next;
delete p;
return r;
}
inline int STACK :: empty()
{ return s == 0;
};
inline int STACK :: full()
{ sp *p;
p=new sp;
if (p!=0)
{ delete p; return 1; }
else return 0;
};
.

18
,
.
.
,
:
.
:
, ;
,

, ,

;

, ,
.

STACK

stack.h.
1 2 ,
.
:

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include stack.h
void main()
{ STACK a;
char x;
int n,i,B;
char *s;
cout <<"\n vvod str";

19

cin >>s;
n=strlen(s);
i=0; B=1;
while (B && (i<n))
{ if (s[i]=='(') a.push(s[i]);
if (s[i]==')')
if (a.empty())
B=0; else x=a.pop();
i++;
}
if ((B) && (i=n) && (a.empty()) )
cout <<"\n yes";
else
cout <<"\n no";
getch();
}
3.4. vector

vector

. STACK

,
(copy ). vector

copy .

class vector

20

{ private:
int *v;
int s;
public:
vector(int s1)
{ s=s1;
v=new int[s];
vector(int _n,int *n);
vector(const vector &y);
~vector()
{ delete [ ]v; };
void
vv_vect();
void show();
};
vector::vector(int s1,int *n)
{ s=s1;
v=new int[s];
for(int i=0;i<s;i++)
v[i]=n[i];
};
vector::vector(const vector &y)
{ s=y.s;
v=new int[s];
for(int i=0;i<s;i++)
v[i]=y.v[i];
}
void vector::vv_vect()
{ cout<<"\nVvod vectora\n";
for (int i=0;i<s;i++)
cin>>v[i];
cout<<"\nVector vveden\n";
};
void vector::show()
{ for (int i=0;i<s;i++)
cout<<v[i]<<" ";
};

21
vector.
: vector a(n);

vector(int s1)
a n .
: vector d(m,y);

vector(int s1,int *n)


d m
n y .
: vector c(a);
copy

vector(vector &y)
c
a.
vector.

int n,m;
int y[]={6,3,6,5,9};
cout <<\n n,m <<
cin >> n >> m;
vector a(n);
a.vv_vect();
a.show();
vector d(m,y);
d.show();
vector c(a);
a.show();

22

3.5.

vector

( +), ( -)
( =).

class vector
{ private:
int *v;int s;
public:
vector(int s1)
{ s=s1;
v=new int[s];
}
vector(int _n,int *n);
vector( vector &y);
~vector()
{ delete []v; };
void vv_vect();
void show();
vector operator+(vector n);
vector operator-(vector n);
vector operator=(vector n);
};
.

vector vector::operator+(vector n)
{
vector t(s);
for (int i=0;i<s;i++) t.v[i]=v[i]+n.v[i];
return t;
};

23

vector vector::operator-(vector n)
{
vector t(s);
for (int i=0;i<s;i++) t.v[i]=v[i]-n.v[i];
return t;
};
vector vector::operator=(vector n)
{
if (n.s<s) s=n.s;
for (int i=0;i<s;i++) v[i]=n.v[i];
return *this;
};
,
.

a=a-d;
cout<<"\n vect a=a-d
a=a+d;
cout<<"\n vect a=a+d

"<<" " ;

a.show();

"<<" " ;

a.show();

vector

, ,
a.minus(d), a.add(d).
3.6. this
,
.
this.

this.
this .
( vector)

vector operator=(vector n)

24
this.

this,

t(s).

vector vector :: operator+(vector n)


{ if (n.s<s) s=n.s;
for (int i=0;i<s;i++) v[i]=v[i]+n.v[i];
return *this;
};
vector vector :: operator-(vector n)
{
if (n.s<s) s=n.s;
for (int i=0;i<s;i++) v[i]=v[i]-n.v[i];
return *this;
};
this

.
3.7. -


(=), ([ ]), () >.

(+)

vector .
:

friend

vector operator + (vector v1, vector v2);

25

vector operator + (vector v1, vector v2)


{ int s;
if (v1.s<v2.s)
s=v1.s ;
else s=v2.s;
vector t(s);
for (int i=0;i<s;i++) t.v[i]=v1.v[i]+v2.v[i];
return t;
}
3.8.

(+, -, *, /) .

class Drob
{
private:
int a,b;
int nod();
void sokr();
public:
Drob(int _a=1,int _b=1)
{ a=_a; b=_b;};
void show();
Drob operator+(const Drob
Drob operator-(const Drob
Drob operator*(const Drob
Drob operator/(const Drob
int operator==(const Drob
int operator< (const Drob
};
int Drob::nod()
{ int x,y;
x=a; y=b;
while(x!=y)
{ if (x>y)
else y=y-x;
}

x=x-y ;

&y);
&y);
&y);
&y);
&y);
&y);

26

return x;
};
void Drob :: sokr()
{ if (a==b)
a=b=1;
else
{ int n=nod();
a=a/n; b=b/n;
}
};
void Drob :: show()
{ printf("\n%d / %d",a,b);
};
Drob Drob :: operator *(const Drob &y)
{ Drob z(0,0);
z.a=a*y.a;
z.b=b*y.b;
if (z.a==0)
z.b=1;
else z.sokr();
return z;
};
Drob Drob :: operator + (const Drob &y)
{ Drob z(0,0);
z.a=a*y.b+b*y.a;
z.b=b*y.b;
if (z.a==0)
z.b=1;
else z.sokr();
return z;
};
Drob Drob :: operator / (const
{ Drob z(0,0);
z.a=a*y.b;
z.b=b*y.a;
if (z.a==0)
z.b=1;
else z.sokr();
return z;
};

Drob &y)

Drob Drob :: operator - (const


{ Drob z(0,0);

Drob &y)

27

z.a=a*y.b-b*y.a;
z.b=b*y.b;
if (z.a==0)
z.b=1;
else z.sokr();
return z;
};
int Drob:: operator==(const Drob &y)
{ sokr(); y.sokr();
if ((b== y.b)&&(a==y.a)) return 1;
return 0;
}
, Drob.

Drob a(1,5),b(3,10),d;
a.show(); b.show();
d=a+b;
d.show();
d=b-a;
d.show();
d=a*b;
d.show();
d=a/b;
d.show();
if (d==a) cout<<" " ;
else
cout<<"\n \n";
3.9. (QUE).

C++

[3].

( function

templates)

.

: template <class T>.

, T.

template <class T>

28

class QUE
{ private:
struct link
{ T inf;
link *next;
};
link *start,*end;
public:
QUE ();
~QUE ();
void add(T elem);
T get();
Int empty();
};
.

template <class T>


QUE <T>:: QUE ()
{ start=end=0;
};
template <class T>
void QUE <T> :: add(T elem)
{ link *p;
p=new link;
p->inf=elem;
p->next=0;
if (start==0 && end==0) start=p;
else end->next=p;
end=p;
};
template <class T>
T QUE <T> :: get()
{ link *p;
T buf;
p=start;
buf=p->inf;
start=start->next;
delete p;
return buf;
};

29

template <class T>


QUE <T>::~ QUE ()
{ link *p;
while (start!=0)
{ p=start;
start=start->next;
delete p;
};
};

QUE <int> ocher;


int x;
scanf("%d",&x);
ocher.add(x);
printf("%d\n",ocher.get());

QUE <float> ocher1;


int m;
float y;
scanf("%f",&y);
ocher1.add(y);
printf("%f\n",ocher1.get());

.
3.10. .

, ,
.
C++ ,

30

.
.

: ,
.
QUE. :

void add(T elem);


T get();

QUERY

class QUERY
{ private:
struct link
{ int inf;
link *next;
};
link *start,*end;
public:
QUERY ();
~QUERY ();
void add(int elem);
int get();
int empty();
};

,

QUERY

QUE_0.

class
QUE_0 : public
{
public:

QUERY

31

int def_0 ();


};
:

int QUE_0 ::def_0 ()


{ link *p=start;
while (p!= end)
p=p->next;

and

if (p->inf == 0 )
else

(p->inf !=0 )
return 1;

return 0;

}
, ,
private ,
.
QUE_0
QUERY, protected.
QUERY :

class QUERY
{ protected:
struct link
{ int inf;
link *next;
};
link *start,*end;
public:
QUERY();
~QUERY();
void add(int elem);
int get();
int empty();
};

32

.
.
3.11. .

( matrica)

vector.

const c=5;
class vector
{ protected: int *v;int s;
public:
vector(){s=0;}
vector(int s1)
{ s=s1;
v=new int[s];
}
vector(int _n,int *n);
vector::vector(vector &y);
~vector();
void vv_vect();
vector operator+(vector n);
vector operator-(vector n);
vector operator=(vector n);
void show();
};
class matrica:public vector
{ vector *m[c];int a;int b;
public:
matrica(int a1,int b1); //
//
~matrica();

33

void vv_matr();
void show();
};
matrica::matrica(int a1,int b1)
{
a=a1; ;b=b1;
m=new vector *[b];
for (int i=0;i<a;i++)
m[i] = new vector (b); //
}
matrica::~matrica()
{
for (int i=0;i<a;i++)
m[i]->~ vector ();
}

void matrica::vv_matr()
{
cout<<"\n \n";
for (int i=0;i<a;i++)
m[i]->vv_vect();
//
cout<<"\n \n";
};
void matrica::show()
{ cout<<"\n----- -----\n";
for (int i=0;i<a;i++)
{ m[i]->show();
//
cout<<"\n";
};
}
.

.

34

int n,m;
cout<<"\n vvod n m "<<;
cin >> n >> m;
vector a(m);
a.vv_vect();
a.show();
if (n>c) cout<<"\n
";
else
{ matrica b(n,m);
b.vv_matr();
b.show();
}

4.
4.1.

class stp
{ protected:
int n;
stp(){};
stp(int st)
{n=st;}
};

//

poli .

class poli: public stp


{ public:
int *a;
poli(){};
poli(int s){ n=s;};
~poli()
{ delete []a;};
void cr_poli();

35

poli(poli &y);
int operator()(int );
friend poli
operator+ (poli
poli & operator= (poli bb);
void prosm();
};

bb, poli cc);

poli & poli:: operator= (poli bb)


{ n=bb.n;
for (int i=0;i<=n;i=i+1) a[i]=bb.a[i];
return *this;
};
poli::poli(poli &y)
{ n=y.n;
a=new int[n+1];
for(int i=0;i<=n;i++)
a[i]=y.a[i];
};
void poli::cr_poli()
{ a=new int[n+1];
cout<<" - "<<n;
for (int i=0;i<=n;i++)
cin>>a[i];
}
int poli:: operator()(int x)
{ int k=n;
float r;
r=a[0];
for (int i=1;i<=k;i=i+1)
r=r*x+a[i];
return r;
}
poli operator+ (poli bb,poli cc)
{ int i,k,l;
k=bb.n; l=cc.n;
if (k>=l)

36

{ poli r(k);
for (i=0;i<=k;i++) r.a[i]=bb.a[i];
for (i=0;i<=l;i++)
r.a[k-l+i]=r.a[k-l+i]+cc.a[i];
return r;
};
poli t(l);
for (i=0;i<=l;i++) t.a[i]=cc.a[i];
for (i=0;i<=k;i++)
t.a[l-k+i]=t.a[l-k+i]+bb.a[i];
return t;
}
void poli:: prosm()
{ int i;
for (i=0;i<=n;i++)
cout <<a[i]<<" ";
cout<<"\n";
} ;
4.2.

class zap
//
{ protected:
char fio[31];
char pr[10];
int a1;
public:
zap(){};
zap(zap &z)
{ strcpy(fio,z.fio);
strcpy(pr,z.pr);
a1=z.a1;
};
int get(){ return a1;};
char* get_f()
{ char *s; s=strdup(fio);
return s;
};
void input();
void output();

37

};
class table : public zap
{ public:
zap *a; int n;
table(){};
table(int s){ n=s;};
~table()
{ delete []a;};
void cr_table();
void sort(int t);
void sort(char t);
void show();
};
void zap:: input()
{ cout<<"\nVvod fio\n";
cout<<"\nVvod pred\n";
cout<<"\nVvod ball\n";
};

cin>>fio;
cin>>pr;
cin>>a1;

void zap::output()
{ cout<< fio <<" "<<pr<<" "<<a1<<"
};
void table :: cr_table()
{ a=new zap [n];
for(int i=0;i<n;i++)
a[i].input();
cout <<"\n \n";
};
void table :: show()
{ int i;
for(i=0;i<n;i++)
{ a[i].output();
cout <<"\n ";
};
};
int SRAVN(int a,int b)
{ if(a>b) return 1;
else return 0;
};

";

38

int SRAVN(char a[],char b[])


{ char *u,*v;
u=strdup(a);v=strdup(b);
if (strcmp(u,v)>0) return 1;
else return 0;
};
void table :: sort(char t)
{ zap s; int i,j;
for (i=n-1;i>0;i--)
{ int k=0;
for (j=1;j<i;j++)
if (SRAVN(a[j].get_f(),a[k].get_f()))
k=j;
s=a[k];a[k]=a[i];a[i]=s;
}
};
void table :: sort(int t)
{ zap s;
int i,j;
for (i=n-1;i>0;i--)
{ int k=0;
for (j=1;j<i;j++)
if (SRAVN(a[j].get(),a[k].get()))
k=j;
s=a[k];a[k]=a[i];a[i]=s;
}
}
4.3.
(Tree)
( ).

template <class Tip>


class Node
// ,
{ public:
Tip inf;
Node<Tip> *left, *right;
Node(Tip dat = 0)
{inf =dat; left = 0; right = 0;}

39

~Node();
void Add(Tip i);
int find(Tip i);
void print();
};
template <class Tip>
class Tree :public Node<Data>
{
Node<Tip> *root;
public:
Tree()
{root = 0;}
~Tree()
{if (root != 0) delete root;}
void Add(Tip x)
{
if (root != 0) root->Add(x);
else root = new Node<Tip>(x);
}
int find(Tip x)
{ if (root != 0) return root->find(x);
else return 0;
}
void print()
{ if (root != 0) root->print();
else printf("Tree is empty\n");
}
};
template <class Tip>
Node <Tip>::~Node()
{ if (left != 0) {delete left;}
if (right != 0) {delete right;}
}
template <class Tip>

40

void Node <Tip>::Add(Tip i)


{
if (i<inf)
{ if (left != 0)
left->Add(i);
else
left = new Node<Tip>(i);
}
else
{ if (right != 0)
right->Add(i);
else
right = new Node<Tip>(i);
}
}
template <class Tip>
int Node <Tip>::find(Tip i)
{
if (i == inf) return 1;
if (i<inf)
{ if (left != 0)
return left->find(i);
else
return 0;
}
else
{ if (right != 0) return right->find(i);
else
return 0;
}
}
template <class Tip>
void Node <Tip>::print()
{
if (left != 0) left->print();
cout << inf << endl;
if (right != 0) right->print();
}

1. .., .., ..
. . . 2001.

41
2. . C++. . .
.: . 1991, 1993 ( ).
3. .. .
C++. . 2000.

Оценить