.. , ..
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)
.
;
.
,
.
,
.
CLU, Alphard,
.
.
1)
(),
.
2) ,
, .
3)
.
4) (2) (3), ,
(3) (2).
, .
(1)
(3). .
,
.
9
,
.
:
1)
;
2) ,
.
C++ ,
),
, ,
.
- ,
C++
2.
([1]).
,
.
1.
( stack)
: "
10
" (Last In - First Out)
:
;
;
, ;
, ;
, ,
;
, , ..
, , .
2.
(Query).
"
3.
(vector).
n
:
;
;
;
11
;
.
4. (polynom).
y = a0 xn + a1
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;
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(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).
.
3.7. -
(=), ([ ]), () >.
(+)
vector .
:
friend
25
(+, -, *, /) .
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 &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.
28
class QUE
{ private:
struct link
{ T inf;
link *next;
};
link *start,*end;
public:
QUE ();
~QUE ();
void add(T elem);
T get();
Int empty();
};
.
29
, ,
.
C++ ,
30
.
.
: ,
.
QUE. :
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
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 .
35
poli(poli &y);
int operator()(int );
friend poli
operator+ (poli
poli & operator= (poli bb);
void prosm();
};
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
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
1. .., .., ..
. . . 2001.
41
2. . C++. . .
.: . 1991, 1993 ( ).
3. .. .
C++. . 2000.