📄 shujujiegou1.cpp
字号:
#include<iostream.h>
template<class type> class OrderList;
template<class type> class Node
{
friend class OrderList<type>;
type key;
public:
Node():key(-1){}
Node(const type &value):key(value){}
type getkey()const{return key;}
void setkey(type k){key=k;}
};
template<class type>class OrderList
{
protected:
Node<type> *element;
int arraySize,currentSize;
public:
OrderList(int sz=10):arraySize(sz)
{element=new Node<type>[sz];}
~OrderList(){delete[]element;}
friend ostream&operator<<(ostream &Out,const OrderList<type> &OutList);
friend istream&operator>>(istream &In,OrderList<type> &InList);
int Search(const type &x)const;
void Order(Node<type> *&a,int i);
};
template<class type>int OrderList<type>::Search(const type &x)const
{
int h=currentSize-1,l=0,mid;
while(l<=h)
{
mid=(l+h)/2;
if(element[mid].getkey()<x) l=mid+1;
else if(element[mid].getkey()>x) h=mid-1;
else return mid+1;
}
return -1;
};
template<class type> ostream &operator <<(ostream &Out,const OrderList<type> &OutList)
{
Out<<"您输入的数据为: ";
for(int i=0;i<OutList.currentSize;i++)
Out<<OutList.element[i].getkey()<<' ';
Out<<"\n数据个数为:\t"<<OutList.currentSize<<endl;
return Out;
};
template<class type> istream &operator >>(istream &In,OrderList<type> &InList)
{
type x;
cout<<"输入要输入的数的个数:\t";
In>>InList.currentSize;
cout<<"请依次输入数据:\t";
for(int i=0;i<InList.currentSize;i++)
{
In>>x;
InList.element[i].setkey(x);
InList.Order(InList.element,i);
}
return In;
};
template<class type>void OrderList<type>::Order( Node<type> *&a,int i)
{
if(i!=0&&a[i].getkey()<a[i-1].getkey())
{
type p=a[i].getkey();
a[i]=a[i-1];
a[i-1].setkey(p);
Order(a,i-1);
}
};
void main()
{
char u;
cout<<"choose type :c(char),i(int),d(double) ";
cin>>u;cout<<endl;
switch(u)
{
case'c':{int i,k;
cout<<"建立足够大数组长度: ";
cin>>k;
OrderList<char> OL(k);
cin>>OL;
cout<<OL;
cout<<"进行搜索操作----";
cout<<"请输入您要搜索的数: ";
cout<<"(退出请输入-1)";
cin>>i;
while(i==-1)
if(OL.Search(i)==-1)
cout<<"ERROR!没有您所查找的数!\n";
else
cout<<"您所查找的数是数组中第"<<OL.Search(i)<<"个数。"<<endl;
cout<<"程序结束。\n";
}
case'i':{int i,k;
cout<<"建立足够大数组长度: ";
cin>>k;
OrderList<int> OL(k);
cin>>OL;
cout<<OL;
cout<<"进行搜索操作----";
cout<<"请输入您要搜索的数: ";
cout<<"(退出请输入-1)";
cin>>i;
while(i==-1)
if(OL.Search(i)==-1)
cout<<"ERROR!没有您所查找的数!\n";
else
cout<<"您所查找的数是数组中第"<<OL.Search(i)<<"个数。"<<endl;
cout<<"程序结束。\n";
}
case'd':{int i,k;
cout<<"建立足够大数组长度: ";
cin>>k;
OrderList<double> OL(k);
cin>>OL;
cout<<OL;
cout<<"进行搜索操作----";
cout<<"请输入您要搜索的数: ";
cout<<"(退出请输入-1)";
cin>>i;
while(i==-1)
if(OL.Search(i)==-1)
cout<<"ERROR!没有您所查找的数!\n";
else
cout<<"您所查找的数是数组中第"<<OL.Search(i)<<"个数。"<<endl;
cout<<"程序结束。\n";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -