如何写一个c++写的集合类运算程序

Python017

如何写一个c++写的集合类运算程序,第1张

#include<iostream>

using namespace std

//================================================================

//辅助类,用来重载第二层[]操作符

class Matrixrow

{

friend class Cmatrix//定义友元类

public:

//构造函数

Matrixrow()

{

row=0

col=0

p=new int[row*col*sizeof(int)]//为指针所指向的数组分配内存

}

//构造函数

Matrixrow(int r,int c):row(r),col(c)

{

p=new int[r*c*sizeof(int)]//分配内存

}

//析构函数

~Matrixrow()

{

delete p//释放内存

}

//重载第二层[]操作符

int &operator[](int c)

{

return p[currentrow*col+c]//返回由第二层[]根据当前行取得的值作为[][]取得的值

}

private:

int *p//指针

int row//行

int col//列

int currentrow//当前行

}

//================================================================

//矩阵运算类

class Cmatrix

{

private:

int **matrix //二维指针指向存矩阵元素的区域

int height //矩阵的行数

int width //矩阵的列数

Matrixrow mr//声明一个mr对象,以传入第一层[]获取的值作为其当前行,然后进行第二层[]元素确定

public:

//构造函数

Cmatrix()

{

height=0

width=0

size()//分配内存

for (int i=0i<heighti++)

{

for (int j=0j<widthj++)

{

matrix[i][j]=0

}

}

}

//构造函数

Cmatrix(int h,int w): mr(h, w)

{

height=h

width=w

size()//分配内存

}

//拷贝构造函数

Cmatrix::Cmatrix(const Cmatrix &m1):height(m1.height),width(m1.width)

{

size()

for(int i=0i<m1.heighti++)

{

for (int j=0j<m1.widthj++)

{

matrix[i][j]=m1.matrix[i][j]

}

}

}

//分配内存的函数

void size()

{

//为动态二维数组分配内存

matrix=new int * [height]

for(int i = 0i <heighti++)

{

matrix[i] = new int[width]

}

}

//初始化函数,用来输入矩阵元素

void Initialize()

{

cout<<"请输入矩阵的 "<<height<<"(行)*"<<width<<"(列) 个元素:"<<endl

//输入元素

for (int i=0i<heighti++)

{

for (int j=0j<widthj++)

{

cin>>matrix[i][j]

}

}

//将输入的矩阵元素值传入mr对象

for (int i=0i<heighti++)

{

for (int j=0j<widthj++)

{

*(mr.p+i*width+j)=matrix[i][j]

}

}

}

//显示矩阵的函数

void display()

{

cout<<endl<<"得到的矩阵是:"<<endl<<endl

for (int i=0i<heighti++)

{

for (int j=0j<widthj++)

{

cout<<matrix[i][j]<<" "

}

cout<<endl

}

cout<<endl

}

//析构函数

virtual ~Cmatrix()

{

{

for (int i=0i<heighti++)

{

if(matrix!=NULL)

{

delete[] matrix[i]//释放为存储矩阵元素而分配的内存

}

}

}

}

//重载+操作符

friend const Cmatrix operator +(Cmatrix m1,const Cmatrix &m2)

{

//检验相加的条件

if ((m1.width==m2.width)&&(m1.height==m2.height))

{

cout<<"两个矩阵可以相加!"<<endl

//完成矩阵相加

for(int i=0i<m2.heighti++)

{

for(int j=0j<m2.widthj++)

m1.matrix[i][j]+=m2.matrix[i][j]

}

}

else

{

cout<<"两个矩阵不能相加!"<<endl

}

return m1

}

//重载-操作符

friend const Cmatrix operator -(Cmatrix m1,const Cmatrix &m2)

{

//检验相减的条件

if ((m1.width==m2.width)&&(m1.height==m2.height))

{

cout<<"两个矩阵可以相减!"<<endl

//完成矩阵相减

for(int i=0i<m2.heighti++)

{

for(int j=0j<m2.widthj++)

m1.matrix[i][j]-=m2.matrix[i][j]

}

}

else

{

cout<<"两个矩阵不能相减!"<<endl

}

return m1

}

//重载*操作符

friend const Cmatrix operator *(const Cmatrix &m1,const Cmatrix &m2)

{

Cmatrix mul(m1.height,m2.width)//定义一个Cmatrix对象临时存储乘积

//元素全部初始化为零

for(int m=0m<mul.heightm++)

{

for (int n=0n<mul.widthn++)

{

mul.matrix[m][n]=0

}

}

//检验相乘的条件

if (m1.width==m2.height)

{

cout<<"两个矩阵可以相乘!"<<endl

//完成矩阵元素相乘

for(int y=0y<m1.heighty++)

{

for (int x=0x<m2.widthx++)

{

for (int i=0i<m1.widthi++)

{

mul.matrix[y][x]+=m1.matrix[y][i]*m2.matrix[i][x]

}

}

}

}

else

cout<<"两个矩阵不能相乘!"<<endl

return mul//返回乘积

}

//重载=操作符

const Cmatrix&Cmatrix::operator=(const Cmatrix &m)

{

if(matrix!=NULL)

delete[] matrix

height = m.height

width = m.width

size()

for(int i=0i<m.heighti++)

{

for (int j=0j<m.widthj++)

{

matrix[i][j]=m.matrix[i][j]

}

}

return *this

}

//重载第一层[]操作符

Matrixrow &operator[](int row)

{

mr.currentrow = row//获取第一层[]取得的值作为当前行

return mr//返回

}

}

//================================================================

int main()

{

//矩阵相乘=========================================

Cmatrix c1(2,3)//2行3列的矩阵

c1.Initialize()

c1.display()

cout<<"第一行第二列是: "<<c1[0][1]<<endl

Cmatrix c2(3,2)//3行2列的矩阵

c2.Initialize()

c2.display()

Cmatrix c3

c3=c1*c2//矩阵相乘

c3.display()

//矩阵相加==========================================

Cmatrix c4(2,2),c5(2,2),c6

c4.Initialize()

c4.display()

c5.Initialize()

c5.display()

c6=c4+c5

c6.display()

//矩阵相减=========================================

c4.Initialize()

c4.display()

c5.Initialize()

c5.display()

c6=c4-c5

c6.display()

return 0

}

交:C={2,3,4,5} 就是既属于A的又属于B的那部分

并:C = {2,3,4,5,6,7,8,11,25} 两个集合的整合去掉重复的。A+B-AB(AB:公共部分)

差:C= {6,7,8}就是属于A但是不属于B的那部分

笛卡尔乘积:这个得出的集合就多了:举个例子。。假设集合A={a,b},集合B={c,d}则两个集合的笛卡尔积为{(a,c),(a,d),(b,c),(b,d)}

/*链表实现集合运算*/

#include<iostream.h>

typedef int ElemType

struct SNode {

ElemType data

SNode* next

}

void InitSet(SNode*&HT)

{

HT=NULL

}

void ClearSet(SNode*&HT)

{

SNode *p=HT, *q

while(p!=NULL)

{

q=p->next

delete p

p=q

}

HT=NULL

}

int LenthSet(SNode* HT)

{

int n=0

while(HT!=NULL)

{

n++

HT=HT->next

}

return n

}

bool EmptySet(SNode* HT)

{

return HT==NULL

}

bool Inset(SNode* HT, ElemType item)

{

while(HT!=NULL)

{

if(HT->data==item) return true

else HT=HT->next

}

return false

}

void OutputSet(SNode* HT)

{

while(HT!=NULL)

{

cout<<HT->data<<' '

HT=HT->next

}

cout<<endl

}

bool FindSet(SNode* HT, ElemType&item)

{

while(HT!=NULL)

{

if(HT->data==item) break

else HT=HT->next

}

if(HT!=NULL)

{

item=HT->data

return true

}

else return false

}

bool ModifySet(SNode* HT, ElemType item, ElemType temp)

{

while(HT!=NULL)

{

if(HT->data==item) break

else HT=HT->next

}

if(HT!=NULL)

{

HT->data=temp

return true

}

else return false

}

bool InsertSet(SNode*&HT, ElemType item)

{

//建立值为item的新结点

SNode* newptr= new SNode

newptr->data=item

//从单链表中顺序查找是否存在值为item的结点

SNode* p=HT

while(p!=NULL)

{

if(p->data==item) break

else p=p->next

}

// 若不存在则把新结点插入到表头并返回真,否则不返回假

if(p==NULL)

{

newptr->next=HT

HT=newptr

return true

}

else return false

}

bool DeleteSet(SNode*&HT, ElemType&item)

{

//从单链表中顺序查找是否存在值为item的结点

SNode *cp=HT, *ap=NULL

while(cp!=NULL)

{

if(cp->data==item) break

else

{

ap=cp

cp=cp->next

}

}

//若不存在则不返回假,表明删除失败

if(cp==NULL)

return false

//由item带回待删除结点cp的完整值,若不需要带回可设置item为值参

item=cp->data

if(ap==NULL) HT=cp->next

//从单链表中删除已经找到的cp结点并对ap是否为表头做不同处理

else ap->next = cp->next

//删除cp结点后返回真

delete cp

return true

}

//求两集合的并集

void UnionSet(SNode* HT1, SNode* HT2, SNode*&HT)

{

HT=NULL

//把HT1集合单链表元素复制到HT集合单链表中

SNode* p=HT1

while(p!=NULL)

{

//建立新结点并赋值为p->data

SNode* newptr=new SNode

newptr->data=p->data

//把新结点插入到HT集合单链表的表头并让P指向下一个结点

newptr->next=HT

HT=newptr

p=p->next

}

//把HT1集合单链表的每个元素插入到HT集合单链表中

p=HT2

while(p!=NULL)

{

InsertSet(HT, p->data)

p=p->next

}

}

//求集合的交集

void InterseSet(SNode* HT1, SNode* HT2, SNode*&HT)

{

HT=NULL

ElemType x

SNode* p=HT2

while(p!=NULL)

{

x=p->data

bool b=FindSet(HT1,x) //用x查找HT1集合

if(b) InsertSet(HT,x) //若查找成功则把x插入到HT集合中

p=p->next

}

if(HT==NULL)

cout<<"空集"<<endl

}

//求集合的差集

void DifferenceSet(SNode* HT1, SNode* HT2, SNode*&HT)

{

HT=NULL

ElemType x

SNode* p=HT1

while(p!=NULL)

{

x=p->data

bool b=FindSet(HT2,x) //用x查找HT2集合

if(b) InsertSet(HT,x) //若查找失败则把x插入到HT集合中

p=p->next

}

if(HT==NULL)

cout<<"空集"<<endl

}

int main()

{

SNode *a,*b,*c,*d

InitSet(a)

InitSet(b)

InitSet(c)

InitSet(d)

ElemType r[8] = {12,32,13,1,2,4,7,8}

ElemType r1[8] = {25,98,26,15,46,38,5,1}

ElemType r2[8] = {5,98,25,8,34,1,15,46}

int i

for(i=0i<8i++)

InsertSet(a,r[i])

for(i=0i<8i++)

InsertSet(b,r1[i])

for(i=0i<8i++)

InsertSet(c,r2[i])

ElemType x=1,y=25

DeleteSet(a,x)

DeleteSet(a,y)

cout<<"输出a集合的元素:"<<endl

OutputSet(a)

cout<<"---------------------------------------------------------"<<endl

if(ModifySet(a,13,30))

cout<<"把 a 集合的 13 更新为 30更新成功 !"<<endl

else

cout<<"集合 a 更新不成功 !"<<endl

if(EmptySet(a))

cout<<"集合 a 为空 !"<<endl

else

cout<<"集合 a 不为空 !"<<endl

cout<<"集合a的长度:"<<LenthSet(a)<<endl

cout<<"---------------------------------------------------------"<<endl

cout<<"输出b集合的元素:"<<endl

OutputSet(b)

cout<<endl

cout<<"输出c集合的元素:"<<endl

OutputSet(c)

cout<<endl

cout<<"b、c两个集合的并集是:"<<endl

UnionSet(b,c,d)

OutputSet(d)

cout<<endl

cout<<"b、c两个集合的交集是:"<<endl

InterseSet(b,c,d)

OutputSet(d)

cout<<endl

cout<<"b、c两个集合的差集(c-b)是:"<<endl

DifferenceSet(b,c,d)

OutputSet(d)

ClearSet(a)

ClearSet(b)

ClearSet(c)

ClearSet(d)

return 0

}