动态数组模板与struct结构
2009-06-11 10:41
323 查看
#include<iostream> #include<cstring> using namespace std; struct teacher{ int num; char name[10]; }; struct student{ int num; char name[10]; }; template <class T> class people { private: T* t; int size; public: people(int sz=20); people(const people<T>& p); ~people(); people<T>& operator = (const people<T>& rhs); T& operator[] (int i); operator T* () const; int ListSize() const; void Resize(int sz); void write(); void put(); void SelectionSort(); char temp[10]; void BinSearch(int key); }; template <class T> people<T> ::people(int sz) { size=sz; t = new T[size]; } template <class T> people<T> ::~people() { delete [] t; } template <class T> people<T> ::people(const people<T>& px) { int n=px.size; size=n; t = new T ; T* srcptr = px.t; T* destptr = t; while(n--) *destptr++ = *srcptr++; } template <class T> people<T>& people<T> ::operator= (const people<T>& rhs) { int n=rhs.size; if(size!=n) { delete [] t; t = new T ; size = n; } T* destptr = t; T* srcptr = rhs.t; while(n--) *destptr++ = *srcptr++; return *this; } template <class T> T& people<T> ::operator[] (int i) { return t[i]; } template <class T> people<T> ::operator T* () const { return t; } template <class T> int people<T> ::ListSize() const { return size; } template <class T> void people<T> ::Resize(int sz) { if(sz==size) return; T* newt=new T[sz]; int n=(sz<=size)?sz:size; T* srcptr = t; T* destptr = newt; while(n--) *destptr++ = *srcptr++; delete[] t; t = newt; size = sz; } template <class T> void people<T> ::write() { int i; for(i=0;i<size;i++) { cout<<"请输入第"<<i+1<<"个"<<temp<<"的编号:"<<endl; cin>>t[i].num; cout<<"请输入第"<<i+1<<"个"<<temp<<"的名字:"<<endl; cin>>t[i].name; } } template <class T> void people<T> ::put() { int i; cout<<temp<<"信息如下:"<<endl; for(i=0;i<size;i++) { cout<<t[i].num<<" "<<t[i].name<<endl; } } template <class T> void people<T> ::SelectionSort() { int smallindex; int i,j; T temp; for(i=0;i<size-1;i++) { smallindex=i; for(j=i+1;j<size;j++) if(t[j].num<t[smallindex].num) smallindex=j; temp=t[i]; t[i]=t[smallindex]; t[smallindex]=temp; } } template <class T> void people<T> ::BinSearch(int key) { int mid,low,high,m; int midvalue; low=0; high=size-1; while(low<=high) { mid=(low+high)/2; midvalue=t[mid].num; if(key==midvalue) { m=mid; goto a; } else if(key<midvalue) high=mid-1; else low=mid+1; } a: cout<<"您查找的"<<temp<<"信息如下:"<<endl; cout<<t[m].num<<" "<<t[m].name<<endl; } int main() { people<struct teacher> p(10); people<struct student> ps(10); int n; cout<<"1.教师信息"<<endl; cout<<"2.学生信息"<<endl; cin>>n; switch(n){ case 1: int key,n; strcpy(p.temp,"教师"); cout<<"默认的人数为:"; cout<<p.ListSize()<<endl; cout<<"修改为:"; cin>>n; p.Resize(n); cout<<"修改后的人数为:"<<p.ListSize()<<endl; p.write(); cout<<"开始排序..."<<endl; p.SelectionSort(); cout<<"排序完毕"<<endl; p.put(); cout<<"按编号查找:"; cin>>key; p.BinSearch(key); main(); case 2: int keys,m; strcpy(ps.temp,"学生"); cout<<"默认的人数为:"; cout<<ps.ListSize()<<endl; cout<<"修改为:"; cin>>m; ps.Resize(m); cout<<"修改后的人数为:"<<ps.ListSize()<<endl; ps.write(); cout<<"开始排序..."<<endl; ps.SelectionSort(); cout<<"排序完毕"<<endl; ps.put(); cout<<"按编号查找:"; cin>>keys; ps.BinSearch(keys); main(); default: cout<<"输入有误,返回"<<endl; main(); } return 0; }
相关文章推荐
- 结构struct动态数组创建、操作、删除
- 结构struct动态数组创建与操作
- 数据结构之顺序栈(数组)C++(模板)
- 使用动态数组结构的一个好处
- 数据结构之动态数组 (C++类模板实现)
- 打造 C++ 最灵活动态数组结构 (一)
- 动态储存结构体数组
- C编写的具有模板功能的Vector - 基于动态顺序数组
- 数据结构之动态数组实现
- 使用动态数组结构的一个好处
- 打造 C++ 最灵活动态数组结构 (三)
- GNU C标准中,struct 空数组成员妙用,构建动态数组
- PB动态数组实现机理1 --- 利用本身的结构来实现动态二维数组
- 算法(第四版) 能够动态调整数组大小的队列数据结构
- 数据结构与算法之动态数组实现堆栈
- [struct]C编程数组与结构的学习
- 使用C++类模板实现动态数组
- delphi 动态结构数组例程
- matlab结构(struct)数组
- 数据结构之动态数组