问题是:用顺序表和单链表分别实现集合的并,交,差以及对称差运算。
以下为顺序表部分,编译时出现了LZ贴上所述error,请各位高手不吝赐教!
-------------------ADT.h---------------------
const int maxlen=100; // error C2370: 'maxlen' : redefinition; different storage class
// see declaration of 'maxlen'class seqlist
{
private:
int data[maxlen];
int len;
public:
seqlist();
~seqlist();
int length();
int get(int i);
int locate(int i);
int insert(int n,int i);
int del(int i);
int empty();
void clear();
};
-------------------ADT.cpp---------------------
#include <iostream.h>
#include "ADT.h"
seqlist::seqlist():len(0){}
seqlist::~seqlist(){}
int seqlist::length()
{
return len;
}
int seqlist::get(int i)
{
if(i>=1 && i<=len) return data[i-1];
return NULL;
}
int seqlist::locate(int item)
{
for(int i=0;i<len;i++)
if(data[i]==item) break;
if(i>len) return 0;
return i+1;
}
int seqlist::insert(int n,int i)
{
if(len==maxlen) return 0;
if(i<1 || i>len+1) return 0;
for(int j=len;j>=i;j--)
data[j]=data[j-1];
data[i-1]=n;
len++;
return 1;
}
int seqlist::del(int i)
{
if(len==0) return NULL;
if(i<1 || i>len) return NULL;
int t=data[i-1];
for(int j=i;j<len;j++)
data[j-1]=data[j];
len--;
return t;
}
int seqlist::empty()
{
if(len==0) return 1;
return 0;
}
void seqlist::clear()
{
len=0;
}
--------------setoperation.cpp------------------ //集合运算
#include <iostream.h>
#include "ADT.h"
void Union(seqlist &A,seqlist &B)
{
int lenA=A.length(),lenB=B.length();
cout<<"A和B的并是:";
int i;
for(i=0;i<lenA;i++) cout<<A.get(i)<<" ";
for(i=0;i<lenB;i++)
{
if(!A.locate(B.get(i))) cout<<B.get(i)<<" ";
}
cout<<endl;
}
void Intersection(seqlist &A,seqlist &B)
{
int lenA=A.length();
cout<<"A和B的交是:";
for(int i=0;i<lenA;i++)
{
if(!B.locate(A.get(i))) cout<<A.get(i)<<" ";
}
cout<<endl;
}
void Difference(seqlist &A,seqlist &B)
{
int lenA=A.length();
cout<<"A和B的差是:";
for(int i=0;i<lenA;i++)
{
if(!B.locate(A.get(i))) cout<<A.get(i)<<" ";
}
cout<<endl;
}
void SymmetrixDifference(seqlist &A,seqlist &B)
{
int lenA=A.length(),lenB=B.length();
cout<<"A和B的对称差是:";
int i;
for(i=0;i<lenA;i++)
{
if(!B.locate(A.get(i))) cout<<A.get(i)<<" ";
}
for(i=0;i<lenB;i++)
{
if(!A.locate(B.get(i))) cout<<B.get(i)<<" ";
}
cout<<endl;
}
----------------main.cpp--------------------
#include <iostream.h>
#include "ADT.h"
#include "setoperation.h"
int main()
{
cout<<"请选择数据存储结构:1 顺序存储; 2 单链表;0 退出。"<<endl;
int choose,select;
cin>>choose;
while(choose)
{
if(choose==1)
{
seqlist A,B;
int i,j;
int Na,Nb;
int dataA,dataB;
cout<<"请输入集合A的元素个数:"<<endl;
cin>>Na;
cout<<"请输入集合A的元素:"<<endl;
for(i=0;i<Na;i++)
{
cin>>dataA;
A.insert(dataA,i+1);
}
cout<<"请输入集合B的元素个数:"<<endl;
cin>>Nb;
cout<<"请输入集合B的元素:"<<endl;
for(i=0;i<Nb;i++)
{
cin>>dataB;
B.insert(dataB,i+1);
}
cout<<"输入的集合A为:";
for(i=0;i<Na;i++) cout<<A.get(i)<<" ";
cout<<endl;
cout<<"输入的集合B为:";
for(i=0;i<Nb;i++) cout<<B.get(i)<<" ";
cout<<endl;
cout<<"请选择集合的运算:"<<endl
<<"1 并;2 交;3 差;4 对称差;0 退出"<<endl;
cin>>select;
while(select)
{
switch(select)
{
case 1: Union(A,B);
case 2: Intersection(A,B);
case 3: Difference(A,B);
case 4: SymmetrixDifference(A,B);
default:cout<<"select=1,2,3,4"<<endl;
}
cout<<"请选择集合的运算:"<<endl
<<"1 并;2 交;3 差;4 对称差;0 退出"<<endl;
cin>>select;
}
}
cout<<"请选择数据存储结构:1 顺序存储; 2 单链表;0 退出。"<<endl;
cin>>choose;
}
return 0;
}
[此贴子已经被作者于2006-12-5 10:52:38编辑过]