. (generic) . , , .
template () :
template<class Ttype > _( )
{// }
Ttype , . . , .
, , , . . . , . :
template <class Ttype > class _ {
,
}
Ttype , . :
template<class Type1,class Type2> class m {Type1 a;Type2 b;}
. . , integer, float, double .
#include <conio.h>
#include <string.h>
#include <iostream.h>
#include"vip\menu.cpp"
void ListForInt();
void ListForFloat();
void ListForDouble();
char bufRus[256];
char*Rus(const char*text){
CharToOem(text,bufRus);
return bufRus;
}
//
template<class mytype>class List {
// ,
class Node{
public:
mytype d;
Node* next;
Node(mytype dat=0){d=dat; next=0;}
};
Node* pbeg; //
public:
List(){pbeg=0;} //
~List(); //
Node * Add(mytype d); //
Node * Find(mytype key); //
Node * Insert(mytype key,mytype d); // d
// key
bool Remove(mytype key); //
void Print(); //
};
//*********************~List() *************************
//.
template<class mytype> List<mytype>::~List(){
if(pbeg!=0){
Node* pv=pbeg;
while(pv){
pv=pv->next;
delete pbeg;
pbeg=pv;
}
}
}
//*************************** void Add(mytype d) **********
//
// .
template<class mytype> List<mytype>::Node*
List<mytype>::Add(mytype d){
Node* pv=new Node(d); //
if(pbeg==0)pbeg=pv; //
|
|
else {
Node* rab=pbeg;
while(rab!=0){
if((rab->next)==0){rab->next=pv;return pv;}
rab=rab->next;
}
}
}
//*************************** Node* Find(mytype key)
//
// 0
template<class mytype> List<mytype>::Node*
List<mytype>::Find(mytype key){
Node* pv=pbeg;
while(pv){
if((pv->d)==key)break;
pv=pv->next;
}
return pv;
}
//************* Node* Insert(mytype key,mytype d)
// key
// . ,
// 0
template<class mytype> List<mytype>::Node*
List<mytype>::Insert(mytype key,mytype d){
if(Node* pkey=Find(key)) // key
{
Node* pv=new Node(d);
//
pv->next=pkey->next;
//
pkey->next=pv; //
return pv;
}
return 0;
}
//******************* bool Remove(mytype key)
// true
// false,
template<class mytype> bool List<mytype>::Remove(mytype key){
if(Node* pkey=Find(key)){
if(pkey==pbeg)pbeg=pbeg->next; //
else{ // ,
Node*rab=pbeg; //
while(rab) // .
{ //rab- .
if((rab->next)==pkey)break;
rab=rab->next;
}
rab->next=pkey->next;
}
delete pkey;
return true;
}
return false;
}
//******************** void Print() -
template<class mytype> void List<mytype>::Print(){
Node*pv=pbeg;
cout<<Rus(" :");cout<<endl;
while(pv){
cout<<pv->d<<' ';
pv=pv->next;
}
cout<<endl;}
//--------------------------- MAIN ---------------------------------------------
int main(int argc, char* argv[]){
int k=0,max,kol;
char menu[][100]= {{" ListForInt "}, {" ListForFloat "},
{" ListForDouble "}, {" EXIT "}, };
kol=4; // .
// .
//---- ------------------
max=viravnivaniestrok(menu,kol);
//----------------- ---------------------------------------
textmode(C80);
while(1){
switch(mmm(kol,menu,max,k))
{ case 0: {
ListForInt();
k=0;break;
}
case 1: {
ListForFloat();
k=1;break;
}
case 2: {
ListForDouble();
k=2;break;
}
case 3:{
exit(0);
}
}
}
return 0;
}
//*************************** void ListForInt()
// .
void ListForInt(){
List<int>l1;
int k=0,max,kol;
char menu[][100]=
{ {" PrintList "}, {" Add "}, {" Find "}, {" Insert "},
{" Remove "}, {" EXIT "}, {" Back "} };
kol=7; // .
max=viravnivaniestrok(menu,kol);
//------------------------ -----------------------------
|
|
textmode(C80);
while(1){
switch(mmm(kol,menu,max,k))
{ case 0: {
l1.Print();
while(!kbhit())
k=0;break;}
case 1: {
cout<<Rus(" , :");
int t;cin>>t;
if((l1.Add(t)))cout<<Rus(" ");
else cout<<Rus(" ");
while(!kbhit());
k=1;break;}
case 2: {
cout<<Rus(" :");
int t;
cin>>t;
if(l1.Find(t))cout<<Rus(" .");
else cout<<Rus(" .");
while(!kbhit());
k=2;break;}
case 3: {
cout<<Rus(" , :");
int t;cin>>t;
cout<<Rus(" , :");
int key;cin>>key;
if((l1.Insert(key,t)))cout<<Rus(" ");
else cout<<Rus(" ");
while(!kbhit());
k=3;break;}
case 4: {
cout<<Rus(" , :");
int t;cin>>t;
if((l1.Remove(t)))cout<<Rus(" ");
else cout<<Rus(" .");
while(!kbhit());
k=4;break;}
case 5:{exit(0);}
} } }
, ListForInt(), ListForFloat() ListForDouble(), float double, .
2 3. - (, , ).
1. . 1 4 (. 3).
2. . , (, ).
3. Vector n (. 3, . 2).
4. Matrix n´n (. 4, . 2).
5. Polynom n (. 5, . 2) . 5 (. 3).
6. . 6 (. 3).
7. . 7 (. 3).
8. . , .
9. Set () n (. 9, . 2) . 9 (. 3).
10. . 10 (. 3).
11. , n´m (. 11, . 2) . 11 (. 3).
12. . 12 (. 3).
13. . 13 (. 3).
14. . 15 (. 3).
|
|
15. . 16 (. 3).
1. , :
1) , ;
2) , ;
*3) , ;
*4) :
2. ?
:
*1) ; 2) .
3. ?
:
*1) template<class t1,class t2>; 2) template <class t1,t2>; 3) template <class t,class t>;
4. ?
:
1) 1; 2) , ; *3) , .
5. :
1) template(class T)
void func(T* p1,T* p2){}
2) template <class T>;
void func(T* p1,T* p2){}
*3) template<class T>
void func(T* p1,T* p2){}
4) template<class T>
void func(T* p1,T* p2){}
6. - ?
:
*1) ; 2) .