哪位大师帮我看看C++程序,应该错在了运算符重载但不会改,急急急

2025-01-07 04:46:20
推荐回答(2个)
回答1:

改完了虽然能够运行,但是我把基类的一个保护成员改成了公有成员,遗憾!!!

#include
templateclass dataList;
templateclass Node{
friend class dataList;
public:
Node(){}
Type getKey()const{return key;}
void setKey(Type k){key=k;}
private:
Type key;
};
templateclass dataList{
public:
dataList(int sz=10):ArraySize(sz)
{
Element=new Node[sz];
}
virtual ~dataList(){delete[] Element;}
void setCurrent(int lhs){CurrentSize=lhs}
Node* Element;
protected:

int ArraySize;
public:
int CurrentSize;
};
templateclass orderedList:public dataList{
public:
orderedList(int sz=10):dataList(sz){}
virtual ~orderedList(){}
friend ostream& operator<<(ostream& OutStream,const orderedList& OutList);
friend istream& operator>>(istream& InStream,orderedList& InList);
void Order(Node* &a,int i);
int Search(const Type &x)const;
};
templatevoid orderedList::Order(Node* &a,int i)
{
if(i!=0&&a[i].getKey()Type p=a[i].getKey();
a[i]=a[i-1];
a[i-1].setKey(a[i].getKey());
Order(a,i-1);
}
templateint orderedList::Search(const Type &x)const
{
int h=CurrentSize-1,l=0,mid;
while(l<=h)
{
mid=(l+h)/2;
if(Element[mid].getKey()l=mid+1;
else if(Element[mid].getKey()>x)
h=mid-1;
else return mid+1;
}
return -1;
}
templateostream& operator<<(ostream& OutStream,orderedList& OutList)
{
OutStream<<"Array Contents:\n";
for(int i=0;iOutStream<OutStream<OutStream<<"Array Current Size:"<return OutStream;
}
templateistream& operator>>(istream& InStream,orderedList& InList)
{
int x;
cout<<"Enter array Current Size:";
InStream>>InList.CurrentSize;
cout<<"Enter array elements:\n";
for(int i=0;i{
InStream>>x;
InList.Element[i].setKey(x);
InList.Order(InList.Element,i);
}
return InStream;
}
int main()
{
int i=0,k=0,j=0;

cout<<"Please input the length of the array£º\t";
cin>>k;
orderedList A(k);
cin>>A;
cout<cout<<"1¡¢Search 2¡¢Quit\t";
cin>>j;
while(j==1)
{
cout<<"Please input the number you search for£º\t";
cin>>i;
if(A.Search(i)==-1)
cout<<"Not found£¡\n";
else
cout<<"The location of the number is"<cout<<"1¡¢Search 2¡¢Quit\t";
cin>>j;
}
return 0;
}

回答2:

可以运行。。

#include
templateclass dataList;
templateclass Node{
friend class dataList;
public:
Node(){}
Type getKey()const{return key;}
void setKey(Type k){key=k;}
private:
Type key;
};
templateclass dataList{
public:
dataList(int sz=10):ArraySize(sz)
{
Element=new Node[sz];
}
virtual ~dataList(){delete[] Element;}
void setCurrent(int lhs){CurrentSize=lhs}
Node* Element;
protected:

int ArraySize;
public:
int CurrentSize;
};
templateclass orderedList:public dataList{
public:
orderedList(int sz=10):dataList(sz){}
virtual ~orderedList(){}
friend ostream& operator<<(ostream& OutStream,const orderedList& OutList);
friend istream& operator>>(istream& InStream,orderedList& InList);
void Order(Node* &a,int i);
int Search(const Type &x)const;
};
templatevoid orderedList::Order(Node* &a,int i)
{
if(i!=0&&a[i].getKey()Type p=a[i].getKey();
a[i]=a[i-1];
a[i-1].setKey(a[i].getKey());
Order(a,i-1);
}
templateint orderedList::Search(const Type &x)const
{
int h=CurrentSize-1,l=0,mid;
while(l<=h)
{
mid=(l+h)/2;
if(Element[mid].getKey()l=mid+1;
else if(Element[mid].getKey()>x)
h=mid-1;
else return mid+1;
}
return -1;
}
templateostream& operator<<(ostream& OutStream,orderedList& OutList)
{
OutStream<<"Array Contents:\n";
for(int i=0;iOutStream<OutStream<OutStream<<"Array Current Size:"<return OutStream;
}
templateistream& operator>>(istream& InStream,orderedList& InList)
{
int x;
cout<<"Enter array Current Size:";
InStream>>InList.CurrentSize;
cout<<"Enter array elements:\n";
for(int i=0;i{
InStream>>x;
InList.Element[i].setKey(x);
InList.Order(InList.Element,i);
}
return InStream;
}
int main()
{
int i=0,k=0,j=0;

cout<<"Please input the length of the array£º\t";
cin>>k;
orderedList A(k);
cin>>A;
cout<cout<<"1¡¢Search 2¡¢Quit\t";
cin>>j;
while(j==1)
{
cout<<"Please input the number you search for£º\t";
cin>>i;
if(A.Search(i)==-1)
cout<<"Not found£¡\n";
else
cout<<"The location of the number is"<cout<<"1¡¢Search 2¡¢Quit\t";
cin>>j;
}
return 0;
}