| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 627 人关注过本帖
标题:一个c++编程问题
只看楼主 加入收藏
yilunduyue
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-6-12
收藏
 问题点数:0 回复次数:0 
一个c++编程问题

定义如下集合类的成员函数,并用数据进行测试。
Class Set
{
int *elem; //存放集合元素的指针
int count; //存放集合中的元素个数
public:
Set(); //默认构造函数
Set(int s[],int n); //以数组s的元素和元素个数为s构造一个集合
int find(int x) const; //判断x是否为集合元素
Set operator+(const Set &); //集合的并集
Set operator-(const Set &);//集合的差集
Set operator*(const Set &);//集合的交集
void disp(); //输出集合元素
};
程序清单为:
#include<iostream>
using namespace std;
int p[15];
int r;
class Set{
int *elem;
int count;
public:
Set()
{elem=0;
count=0;
}

Set(int s[],int n)
{elem=s;
count=n;
}

void find(int x)
{int i;
for(i=0;i<=4;i++)
{if(elem[i]==x)
{cout<<x<<"is number."<<endl;break;}
else
if(elem[i]!=x&&(i==4))
{cout<<x<<"is not number."<<endl;}
}
}


Set operator+(const Set&c)
{int i=0,j,k=0;
Set temp;
for(i=0;i<=5;i++)
{p[k++]=c.elem[i];}
for(i=0;i<=4;i++)

{if(elem[i]!=c.elem[0]&&elem[i]!=c.elem[1]&&elem[i]!=c.elem[2]&&elem[i]!=c.elem[3]&&elem[i]!=c.elem[4]&&elem[i]!=c.elem[5])
{p[k++]=elem[i];}
}
{ r=k;temp.elem=p;return temp;}
}

Set operator-(const Set&c)
{Set temp;int i,k=0;
for(i=0;i<=4;i++)

{if(elem[i]!=c.elem[0]&&elem[i]!=c.elem[1]&&elem[i]!=c.elem[2]&&elem[i]!=c.elem[3]&&elem[i]!=c.elem[4]&&elem[i]!=c.elem[5])
{p[k++]=elem[i];}
}
{r=k;temp.elem=p;return temp;}

}
Set operator*(const Set&c)
{Set temp;int i,k=0;
for(i=0;i<=4;i++)

{if(elem[i]==c.elem[0]||elem[i]==c.elem[1]||elem[i]==c.elem[2]||elem[i]==c.elem[3]||elem[i]==c.elem[4]||elem[i]==c.elem[5])
{p[k++]=elem[i];}
}
{r=k;temp.elem=p;return temp;}

}

void dis()
{int i;
for(i=0;i<r;i++)
{cout<<elem[i]<<" ";}
cout<<endl;
}

};

void main()
{int n[]={22,4,6,64,7};
int m[]={33,55,6,4};
int i;
Set s(n,4);
Set t(m,4);
Set w;
cout<<"please input one number:"<<endl;
cin>>i;
s.find(i);
w=s+t;
cout<<"s+t=";
w.dis();
w=s-t;
cout<<"s-t=";
w.dis();
w=s*t;
cout<<"s*t=";
w.dis();
}

请给这个实验程序添加异常处理,主要考虑资源的回收。
本人实在是不会,恳请哪位高手帮忙解决一下,急需!!!!!!!!非常非常感谢!!!!!!

搜索更多相关主题的帖子: Set int operator 元素 const 
2007-06-23 20:56
快速回复:一个c++编程问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017019 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved