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

来源:互联网 发布:java接口的修饰符 编辑:程序博客网 时间:2024/05/16 05:25
 再读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;
}


原创粉丝点击