帮我看看这个C++程序是什么错误。
#include<iostream>using namespace std;
class List{
public:
List(): listsize(N),length(0)
{
elem=new int[N];
cout<<"当前建立的是默认的线性表,大小默认为"<<N<<"。"<<endl;
}
List(int x): listsize(x),length(0)
{
elem=new int[x];
}
int ListSwap(int x,int y)
{
int temp=elem[x-1];
elem[x-1]=elem[y-1];
elem[y-1]=temp;
return 1;
}
int ClearList()
{
delete [] elem;
elem=new int[listsize];
return 1;
}
int ListSearch(int x)
{
int j=0;
for(int i=1;i<=length;i++)
if(elem[i-1]==x)
{
cout<<"第"<<i<<"个元素与所要查找的值相等!\n"<<flush;
j++;
}
return j;
}
int ListInsert(int i,int e)
{
if(i<1||i>length+1)
{
cerr<<"错误,插入位置异常!"<<endl;
return 0;
}
if(length>=listsize)
{
int *newbase=new int[listsize+INCREMENT];
listsize+=INCREMENT;
for(int i=0;i<length;i++)
newbase[i]=elem[i];
delete [] elem;
elem=newbase;
}
int *q=&(elem[i-1]);
for(int *p=&(elem[length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++length;
return 1;
}
int GetElem(int i)
{
return elem[i-1];
}
int ListLength()
{
return length;
}
~List()
{
delete [] elem;
}
private:
int *elem;
enum num
{
N=150,
INCREMENT=30
};
int length;
int listsize;
};
int seque(List&,string &);
int main()
{
cout<<"********************************************\n"
<<"* 计算机科学与技术0902班_常文涛_数据结构 *\n"
<<"* 实验一 线性表的顺序存储结构 *\n"
<<"********************************************\n"<<endl;
cout<<"\n接下来请完全按照提示输入,因为本人水平有限,\n如果有太多的非法输入那我就完蛋了!!\n"<<endl;
cout<<"你想建立初始多大的整形顺序表,请输入:";
int N;
cin>>N;
List L1(N);
cout<<"现在可以开始输入数据了\n"
<<"请输入数据:"<<endl;
while(true)
{
int ls;
cin>>ls;
L1.ListInsert(L1.ListLength()+1,ls);
cout<<"结束输入?Y/N"<<endl;
char s;cin>>s;
if(s=='Y'||s=='y')
break;
else
continue;
}
cout<<"\n已经成功建立一个线性顺序表!\n"
<<"下面演示顺序表的遍历:"<<endl;
for(int i=1;i<L1.ListLength()+1;i++)
cout<<"顺序表中的第"<<i<<"个整数为"<<L1.GetElem(i)<<"。"<<endl;
cout<<"顺序表的遍历演示完毕!!"<<endl;
cout<<"\n下面进行查找操作,请输入要查找的整数值:";
int zhi;cin>>zhi;
cout<<"已完成对值"<<zhi<<"的查找。\n"<<"共查找到"<<L1.ListSearch(zhi)<<"个。"<<endl;
cout<<"\n下面来看看表中哪些元素是对称的!"<<endl;
for(int i=1;i<=L1.ListLength()/2;i++)
{
if(L1.GetElem(i)==L1.GetElem(L1.ListLength()-i+1))
cout<<"表中的第"<<i<<"个元素"<<L1.GetElem(i)<<"存在对称元素!"<<endl;
else
cout<<"表中的第"<<i<<"个元素"<<L1.GetElem(i)<<"不存在对称元素!"<<endl;
}
if(L1.ListLength()%2==1)
cout<<"表中的第"<<L1.ListLength()/2+1<<"个元素与自己对称!"<<endl;
cout<<"\n现在我们要对表重新排序,奇数在前偶数在后。"<<endl;
int ss=1,cc=L1.ListLength();
while(ss<cc)
{
while(L1.GetElem(ss)%2==1)
ss++;
while(L1.GetElem(cc)%2==0)
cc--;
if(ss<cc)
L1.ListSwap(ss,cc);
}
cout<<"重排列完毕!"<<endl;
for(int i=1;i<L1.ListLength()+1;i++)
cout<<"顺序表中的第"<<i<<"个整数为"<<L1.GetElem(i)<<"。"<<endl;
cout<<"顺序表的遍历演示完毕!!!"<<endl;
cout<<"\n接下来建立一个新的顺序表,这次为有序表。"<<endl;
int W;
cout<<"你想建立初始多大的顺序表,请输入:";
cin>>W;
List L2(W);
cout<<"现在可以开始输入数据了\n"
<<"输入的各个整数之间以分号;分隔。\n"
<<"请输入数据:"<<endl;
string poly;
cin>>poly;
seque(L2,poly);
cout<<"已经成功建立一个线性顺序表!\n"<<endl;
for(int i=1;i<L2.ListLength()+1;i++)
cout<<"顺序表中的第"<<i<<"个整数为"<<L2.GetElem(i)<<"。"<<endl;
cout<<"顺序表遍历完毕!!!\n"<<endl;
cout<<"实现两个非递减顺序表的合并!\n"
<<"请开始输入第一个顺序表,分号分割元素!"<<endl;
List L3,L4;
cout<<"请输入:"<<endl;
string poly1;
cin>>poly1;
seque(L3,poly1);
cout<<"请开始输入第二个顺序表,分号分隔元素!\n"
<<"请输入:"<<endl;
string poly2;
cin>>poly2;
seque(L4,poly2);
cout<<"成功建立两个非递减有序表。\n"
<<"合并他们:"<<endl;
List L5(L3.ListLength()+L4.ListLength());
int k1=1,k2=1;
while(k1<=L3.ListLength()&&k2<=L4.ListLength())
{
if(L3.GetElem(k1)<=L4.GetElem(k2))
L5.ListInsert(L5.ListLength()+1,L3.GetElem(k1++));
else
L5.ListInsert(L5.ListLength()+1,L4.GetElem(k2++));
}
while(k1<=L3.ListLength())
L5.ListInsert(L5.ListLength()+1,L3.GetElem(k1++));
while(k2<=L4.ListLength())
L5.ListInsert(L5.ListLength()+1,L4.GetElem(k2++));
cout<<"合并完毕!!!"<<endl;
for(int i=1;i<L5.ListLength()+1;i++)
cout<<"顺序表中的第"<<i<<"个整数为"<<L2.GetElem(i)<<"。"<<endl;
cout<<"顺序表遍历完毕!!!"<<endl;
system("pause");
return 0;
}
int seque(List &le,string &len)
{
string::iterator iter=len.begin();
while(iter!=len.end())
{
string s;
while(iter!=len.end()&&(*iter=='.'||isdigit(*iter)))
s.push_back(*iter++);
if(iter!=len.end())
++iter;
if(!s.empty())
{
stringstream ss;
int number;
ss<<s;
if(!ss.good())
throw runtime_error("程序遇到错误。");
ss>>number;
while(true)
{
if(le.ListLength()==0)
{
le.ListInsert(1,number);
continue;
}
for(int i=1;i<=le.ListLength();i++)
{
if(number<le.GetElem(i))
{
le.ListInsert(i,number);
break;
}
}
le.ListInsert(le.ListLength()+1,number);
break;
}
}
}
return 1;
}