Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169081
  • 博文数量: 86
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 885
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-05 18:46
文章存档

2011年(4)

2010年(51)

2009年(10)

2008年(21)

我的朋友

分类: C/C++

2010-09-21 06:50:23

发表于:2006-1-30 10:08:43
输入两个5元素的集合,取并集 [c++]

#include
#include
#include
class set
{
 char a[5];
public:
 void seta(char ele, int n)
 { 
  a[n]=ele;
 }
 void disp();
 friend void Union(set c, set b);
};
void set::disp()
{
 int n;
 for(n=0;n<=4;n++)
 cout< cout<}

void Union(set b,set c)

 int n;
 int m;
 int o;
 char a[11]={'0','0','0','0','0','0','0','0','0','0','0'};
 strcpy(a,b.a);        //将数组1复制给a
 o=5;                   //取并集时,o从5开始计数
 for(m=0;m<=4;m++)     //取数组2种的第m个元素与数组1中个元素比较
 {
  for(n=0;n  {
   if(a[n]==c.a[m])      //如果有相同元素,则跳出循环
   {    
    break;
   }   
   if(n==o-1)      //当n与已复制的所有元素作了比较后……   
   {
    a[o]=c.a[m]; 
    o++;
    continue;
   } 
  }
 }
 cout<<"Union the two sets together are:"< for(n=0;n<=o-1;n++)
 {
  cout< }
 
};

 

void main()
{
 set b;
 set d;
 int elenum;
 int n;
 char x[5];
 cout<<"Please input the 5 element of Set A:"< for(n=0;n<=4;n++)
    {
loop1:
  cin>>x[n];
  if(n>=1)
  { 
   for(elenum=0;elenum<=n-1;elenum++)
   {
    if(x[elenum]==x[n])
    {
     cout<<"wrong!!";
     goto loop1;
    }
   }
  }
  b.seta(x[n],n);
 }
 b.disp();
 cout< for(n=0;n<=4;n++)
 {
loop2:
  cin>>x[n];
  if(n>=1)
  {
   for(elenum=0;elenum<=n-1;elenum++)
   {
    if(x[elenum]==x[n])
    {
     cout<<"wrong!!";
     goto loop2;
    }
   }
  }
  d.seta(x[n],n);
 }
 cout< b.disp();
 d.disp();
 set u(set b, set d);
 Union(b,d);
 cout<}

阅读(1119) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-09-21 16:26:25

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com