再读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;
}
#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;
}
- 再读C++ Primer 写了个小例子——模板的操作(08-01-18)
- 再读C++ Primer 写了个小例子——模板类的操作
- 再读C++ Primer 写了个小例子——友员类的操作
- 再读C++ Primer 写了个小例子——练习多态虚函数的特性(08-01-25)
- 再读C++ Primer 写了个小例子——实现stack类的压栈弹栈功能(08-12-10)
- 再读C++ Primer 写了个小例子——运算符重载(08-01-17)
- 再读C++ Primer 写了个小例子(08-01-16)
- 再读C++ Primer 写了个小例子——实现Vector类的自动添加元素功能(08-12-10)
- 再读C++ Primer 写了个小例子——多线程
- 再读C++ Primer 写了个小例子——网络编程
- 再读C++ Primer 写了些小例子来熟悉C++基本语法
- 再读C++Primer
- 看了C++primer后,用书上的方法写个hw
- c写了个通用链表带了例子。
- 最近有人问NSInvocation怎么用,于是写了个NSInvocation的小例子
- 最近有人问NSInvocation怎么用,于是写了个NSInvocation的小例子
- 利用jdk1.8写个模板方法模式的例子
- 模板元编程的2个小例子
- Tomcat5配置Mysql JDBC数据库连接池
- 再读C++ Primer 写了个小例子——运算符重载(08-01-17)
- Windows系统中巧妙隐藏驱动器
- 改进JAVA字符串分解的方法
- 从例子看List的add()方法的工作机制
- 再读C++ Primer 写了个小例子——模板的操作(08-01-18)
- div 命名规范
- 开源软件的十大杰出人物
- 期待已久!InstallShield 2008 v14 完美破解安装版v2 发布!提供下载
- dwr学习笔记
- [英语阅读]荷兰造酒商花500万欧元为鼻子上保险
- 转,做单片机开发,必须跳出芯片型号!
- 再读C++ Primer 写了个小例子——实现Vector类的自动添加元素功能(08-12-10)
- [英语阅读]“电子菜单”进餐馆 节约成本刺激消费