anyview:离散数学(第一章)

前言(用前说明)

以下所有答案皆为作者本人所做,自认为写的是一坨,仅供参考,切勿抄袭(懂得都懂),也希望各位可以写下评论给出更好的解法,谢谢。

1

#include "allinclude.h"
Boolean IsInSet(SetElem elem, pSet pA)
{  // Add your code here
    if(pA==NULL)
    {
        return false;
    }
    SetElem*p;
    pSet pSetA=pA;
    for(p=outToBuffer(pSetA);*p!='\0';p++)
    {
        if(elem==*p)
        {
            return true;
        }
    }
    return false;
}

2

#include "allinclude.h"
SetRelationshipStatus SetRelationship(pSet pA, pSet pB)
{  // Add your code here
   if(pA==NULL||pB==NULL)
   {
      return EXCEPTION;//有空集情况
   }
   int isAIncludedInB = 1;
   int isBIncludedInA = 1;
   SetElem*a,*b;
   // 判断pA是否被真包含于pB
   for (a=outToBuffer(pA);*a!='\0';a++)
   {
      int isIncluded = 0;
      for (b=outToBuffer(pB);*b!='\0';b++)
      {
         if (*a==*b)
         {
            isIncluded = 1;
            break;
         }
      }
      if (!isIncluded)
      {
         isAIncludedInB = 0;
         break;
      }
   }
   // 判断pB是否被真包含于pA
   for (b=outToBuffer(pB);*b!='\0';b++)
   {
      int isIncluded = 0;
      for (a=outToBuffer(pA);*a!='\0';a++)
      {
         if (*b==*a) {
            isIncluded = 1;
            break;
         }
      }
      if (!isIncluded) {
         isBIncludedInA = 0;
         break;
      }
   }
   if (isAIncludedInB && isBIncludedInA)//两个如果均为1,则两个相等
   {
      return EQUAL;
   }
   else if(isAIncludedInB)
   {
      return REAL_INCLUDED;
   }
   else if(isBIncludedInA)
   {
      return REAL_INCLUDING;
   }
   else
   {
      return NOT_INCLUSIVE;
   }
}

3

#include "allinclude.h"
pSet SetUnion(pSet pA, pSet pB)
{  // Add your code here
   if(pA==NULL&&pB==NULL)
   {
      return NULL;
   }
   if(pA==NULL)
   {
      return pB;
   }
   if(pB==NULL)
   {
      return pA;
   }
   SetElem * a , *b;
   for(a=outToBuffer(pA);*a!='\0';a++)
   {
      int flag=0;
      for(b=outToBuffer(pB);*b!='\0';b++)
      {
         if(*a==*b)
         {
            flag=1;
            break;
         }
      }
      if(flag==0)
      {
         directInsertSetElem(pB,*a);
      }
   }
   return pB;
}

4

#include "allinclude.h"
pSet SetIntersection(pSet pA, pSet pB)
{  // Add your code here
   if(pA==NULL||pB==NULL)
   {
      return NULL;
   }
   SetElem * a , *b;
   pSet p=createNullSet();
   for(a=outToBuffer(pA);*a!='\0';a++)
   {
      int flag=0;
      for(b=outToBuffer(pB);*b!='\0';b++)
      {
         if(*a==*b)
         {
            flag=1;
            break;
         }
      }
      if(flag==1)
      {
         directInsertSetElem(p,*a);
      }
   }
   return p;
}

5

#include "allinclude.h"
pSet SetComplement(pSet pA, pSet pI)
{  // Add your code here
   pSet bu=createNullSet();//创建空的集合
   if(isNullSet(pA)==TRUE)//判断pA是否为空集
   {
      return pI;
   }
   if(isNullSet(pI)==TRUE)//判断pI是否为空集
   {
      return NULL;
   }
   SetElem *p;
   for(p=outToBuffer(pA);*p!='\0';p++)
   {
      if(isInSet(pI,*p)==FALSE)
      {
         return NULL;//判断不是子集
      }
   }
   for(p=outToBuffer(pI);*p!='\0';p++)
   {
      if(FALSE==isInSet(pA,*p))
      {
         directInsertSetElem(bu,*p);//插入补集元素
      }
   }
   return bu; //返回补集
}

6

#include "allinclude.h"
pSet SetSubtraction(pSet pA, pSet pB)
{  // Add your code here
   if(isNullSet(pA)==TRUE)
   {
      return pA;
   }
   if(isNullSet(pB)==TRUE)
   {
      return pA;
   }
   pSet abu=createNullSet();
   pSet bbu=createNullSet();
   SetElem *p;
   for(p=outToBuffer(pA);*p!='\0';p++)
   {
      if(isInSet(pB,*p)==FALSE)
      {
         directInsertSetElem(abu,*p);
      }
   }
   return abu;
}

7

#include "allinclude.h"
pSet SetSysmmetricDifference(pSet pA, pSet pB)
{  // Add your code here
   if(isNullSet(pA)==TRUE)
   {
      return pB;
   }
   if(isNullSet(pB)==TRUE)
   {
      return pA;
   }
   pSet dp=createNullSet();
   SetElem *p;
   for(p=outToBuffer(pA);*p!='\0';p++)
   {
      if(isInSet(pB,*p)==FALSE)
      {
         directInsertSetElem(dp,*p);
      }
   }
   for(p=outToBuffer(pB);*p!='\0';p++)
   {
      if(isInSet(pA,*p)==FALSE)
      {
         directInsertSetElem(dp,*p);
      }
   }
   return dp;
}

8

#include "allinclude.h"
pSet myfunction(pSet pB,pSet pC)
{
   SetElem *pBElems;
   SetElem *pCElems;
   pSet pS;
   pS=createNullSet();
   for(pBElems=outToBuffer(pB);*pBElems!='\0';pBElems++)
   {
      directInsertSetElem(pS,*pBElems);
   }
   for(pCElems=outToBuffer(pC);*pCElems!='\0';pCElems++)
   {
      directInsertSetElem(pS,*pCElems);
   }
   return pS;
}
pFamilyOfSet PowerSet(pSet pA)
{  // Add your code here
   int i;
   pSet pI=createNullSet();//创建一个空集
   pFamilyOfSet pF=createNullFamilyOfSet();//创建一个空的集族
   SetElem *pAElems=outToBuffer(pA);//pA的元素拿出缓冲区
   insertToFamilyOfSet(pF,pI);//先将空集插入集族,空集必定为子集之一
   if(!isNullSet(pA))//pA不为空集
   {
      while(*pAElems!='\0')
      {
         pSet pK=createNullSet();//创建一个空集
         directInsertSetElem(pK,*pAElems);//将pA元素插入pK空集
         pSet *pS=outFamilyOfSetToBuffer(pF);//从pF中拿出单个元素的集合
         for(i=0;*(pS+i)!=NULL;i++)
         {
            insertToFamilyOfSet(pF,myfunction(pK,*(pS+i)));
         }
         pAElems++;
      }
   }
   return pF; // This is temporary code. Modify it if necessary.
}

9

#include "allinclude.h"
void myFunction(int k,SetElem *pAElems,pFamilyOfSet pFA,pSet pB)
{
   long i=k,j=0;
   pB=createNullSet();//创建新的集合
   while(*(pAElems+j)!='\0')
   {
      if(i%2==1)//二进制的最后一位是1
      {
         directInsertSetElem(pB,*(pAElems+j));
      }
      j++;
      i>>=1;//i二进制右移一位
   }
   insertToFamilyOfSet(pFA,pB);//把集合加到集族中
   k--;//子集个数减1
   if(k>0)//剩余子集个数作为终止条件
   {
      myFunction(k,pAElems,pFA,pB);//递归调用
   }
}
pFamilyOfSet PowerSet(pSet pA)
{  // Add your code here
   pFamilyOfSet pFA=createNullFamilyOfSet();//创建新的集族
   long i=0,k=1;//循环变量
   SetElem *pAElems=outToBuffer(pA);//将pA集合的头指针取得
   pSet pB=createNullSet();//创建新的空集合
   while(*(pAElems+i)!='\0')
   {
      i++;//计数得出pA集合中的元素个数
   }
   k=pow(2,i);
   myFunction(k,pAElems,pFA,pB);//递归函数调用
   return pFA;//返回集族
}

  • 50
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sɪʟᴇɴᴛ໊ོ5329

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值