找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 577|回复: 0

一个c++编程问题

[复制链接]
发表于 2009-11-1 08:06:28 | 显示全部楼层 |阅读模式 IP:江苏扬州
定义如下集合类的成员函数,并用数据进行测试。
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();
}
请给这个实验程序添加异常处理,主要考虑资源的回收。
本人实在是不会,恳请哪位高手帮忙解决一下,急需!!!!!!!!非常非常感谢!!!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 19:26 , Processed in 0.236206 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表