#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[n]; 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[n]; 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; }