离散数学中集合的相关代码C++

来源:互联网 发布:评价算法 编辑:程序博客网 时间:2024/06/06 16:31

#include<iostream>
#include <cstring>
#include<cstdio>
using namespace std;
const char E[]="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
class Set
{
private:
 char str[200];
 int count;
public:
 Set()
 {
  count=0;
  str[0]='/0';
 }
 Set(const char *s)
 {
  for(int i=0;i<strlen(s);i++)
  {
   str[i]=s[i];
            count++;
  }
  str[count]='/0';
 }
 void order_output()//排序去重
 { 
  char p[200];
  int m=0;
  for(int j=0;E[j]!='/0';j++)
   for(int i=0;str[i]!='/0';i++)
    if(str[i]==E[j])
    {
     p[m]=str[i];
     m++;
     break;
    }
  count=m;
  p[m]='/0';
  for(int i=0;i<m;i++)
   str[i]=p[i];
  str[m]='/0';
 }
 Set operator & (const Set &x) const//交集操作符重载
 {
  Set p1;int m=0;
  for (int i=0;str[i]!='/0';i++)
   for(int j=0;x.str[j]!='/0';j++)
   {
    if (str[i]==x.str[j])
    {
     p1.str[m]=str[i];
     m++;
    }
   }
  p1.str[m]='/0';
  p1.order_output();
  p1.output ();
  return p1;
 }
 Set operator |(const Set &x) const//并集操作符重载
 {
  Set p2;
  strcpy(p2.str,str);
  strcat(p2.str,x.str);
  p2.order_output();
  p2.output ();
  return p2;
 }
    Set operator -(const Set &x) const//差集操作符重载
 {
  Set p3;
  int m=0;
  for(int i=0;str[i]!='/0';i++)
  {
   int j;
   for(j=0;x.str[j]!='/0';j++)
    if(str[i]==x.str[j]) break;
   if(x.str[j]=='/0')
   {
    p3.str[m]=str[i];
    m++;
   }
  }
  p3.str[m]='/0';
  p3.output ();
  return p3;
    }
 Set jueduibu() const//补集函数
 {
  Set p4;int m=0;
  for(int i=0;E[i]!='/0';i++)
  {
   int j;
   for(j=0;str[j]!='/0';j++)
    if(E[i]==str[j]) break;
   if(str[j]=='/0')
   {
    p4.str[m]=E[i];
    m++;
   }
  }
  p4.str[m]='/0';
  p4.output();
  return p4;
 }
 void output()
 {
  cout<<"{";
  if(strlen(str)>0)
  {
   for(int i=0;i<strlen(str)-1;i++)
    cout<<str[i]<<",";
      cout<<str[strlen(str)-1];
  }
  cout<<"}"<<endl;
 }

 friend istream& operator>>(istream& in, Set& x);
 };
 istream& operator>>(istream& in, Set& x)
 {
     in >>x.str;
  return in;
 }
int main()
{
 Set a,b;

    cin>>a;
 a.order_output();
 a.output ();

 cin>>b;
    b.order_output ();
 b.output();
   
 a&b;
 a|b;
 a-b;
 a.jueduibu (); 
 return 0;
}

原创粉丝点击