再读C++ Primer 写了个小例子——模板的操作(08-01-18)

 再读C++ Primer 写了个小例子——模板的操作(08-01-18)
#pragma  once
#define  MAX 100

template 
< class  T >
class  QUeue
{
public:
    QUeue(
void)
    
{
        length  
= 0;
        crtdata 
= 0;
    }

    
void Insert(T& ele)
    
{
//    _ASSERT(length >= 100);
    elements[length] = ele;
    length 
+= 1;
    
if(length != 1)
    
{
        crtdata 
+= 1;
    }

    }

    
int Remove(int index)
    
{
//        ASSERT(0 < index && index >100); 
    if(index < length)
    
{
        
for(int i = index;i < length;i ++)
        
{
            
this->elements[i] = elements[i + 1];
        }

        
if(crtdata == index)
        
{
            crtdata 
= index;
        }

        length
--;
    }

    
else
    
{
        
return 0;
    }

    
    
return 1;
    }

    
int IsEmpty()
    
{
    
if(length == 0)
    
{
        
return 0;
    }

    
else
    
{
        
return 1;
    }

    }

    
int Lengh()
    
{
        
return length;
    }

    
void Front()
    
{
        crtdata 
= 0;
    }

    
virtual ~QUeue(void)
    
{}
public:
    T elements[MAX];
    
int length;
    
int crtdata;
}
;


#pragma  once
using   namespace  std;
class  Book
{
public:
    Book(
void)
    
{
        memset(szTitle,
0,sizeof(szTitle));
        memset(szAuthor,
0,sizeof(szAuthor));
        memset(szSSN,
0,sizeof(szSSN));
    }

    Book(
char* title,char* author,char* ssn)
    
{
        strcpy(
this->szAuthor,author);
        strcpy(
this->szSSN,ssn);
        strcpy(
this->szTitle,title);
    }

    
~Book(void)
    
{}
    
void operator = (Book& book)
    
{
        strcpy(
this->szAuthor,book.szAuthor);
        strcpy(
this->szSSN,book.szSSN);
        strcpy(
this->szTitle,book.szTitle);
    }

    
void show()
    
{
        cout
<<szTitle<<" "<<szAuthor<<" "<<szSSN<<endl;
    }

public:
    
char szTitle[64];
    
char szAuthor[32];
    
char szSSN[32];

}
;




#include 
" StdAfx.h "
#include 
" .QUeue.h "

/*template <class T>
QUeue<T>::QUeue(void)
{
    length  = 0;
    crtdata = 0;
}

QUeue<T>::~QUeue(void)
{
}
void QUeue<T>::Insert(T ele)
{
    _ASSERT(length >= 100);
    elements[crtdata] = ele;
    length += length;
    crtdata += crtdata;
}
int QUeue<T>::Remove(int index)
{
    _ASSERT(index < 0 && index >100); 
    if(index < length)
    {
        for(int i = index;i < length;i ++)
        {
            element[i] = element[i + 1];
        }
        if(crtdata == index)
        {
            crtdata = index;
        }
    }
    else
    {
        return 0;
    }
    
    return 1;
}
int QUeue<T>::IsEmpty(QUeue& que)
{
    if(que.length == 0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
int QUeue<T>::Lengh(const QUeue& que)
{
    return que.length;
}
void QUeue<T>::Front(QUeue& que)
{
    que.crtdata = 0;
}
*/




//  class08011801.cpp : Defines the entry point for the console application.
//

#include 
" stdafx.h "
using   namespace  std;
#include  
" Queue.h "
#include 
" Book.h "

int  _tmain( int  argc, _TCHAR *  argv[])
{
    QUeue
<int> qu;
    
int a = 5;
    
int b = 56;
    qu.Insert(a);
    qu.Insert(b);
    cout
<<"输入10个数据"<<endl;
    
for(int j=0;j<10;j++)
    
{
        cin
>>a;
        qu.Insert(a);
    }

    cout
<<"QUeue int是否为空 0为空 1不为空 "<<qu.IsEmpty()<<endl;
    cout
<<"输入要删除的数据index"<<endl;
    cin
>>a;
    
if(qu.Remove(a) == 1)
    
{
        cout
<<"删除成功"<<endl;
    }

    Book book(
"c++","zhangdali","0000012");
    Book book1(
"java","zhangdali","0000014");
    cout
<<endl;
    cout
<<"QUeue int中的数据 "<<endl;
    
for(int i=0;i<qu.length;i++)
    
{
        cout
<<qu.elements[i]<<" ";
    }

    cout
<<endl;
    QUeue
<Book> qubook;
    qubook.Insert(book);
    qubook.Insert(book1);
    cout
<<"QUeue book是否为空 "<<qubook.IsEmpty()<<endl;
    cout
<<"QUeue book的当前位置和长度  ";
    cout
<<qubook.crtdata<<" "<<qubook.length<<endl;
    cout
<<"QUeue book的当前位置的值  ";
    qubook.elements[qubook.crtdata].show();
    cout
<<endl;
    
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值