广工离散数学Anyview参考答案.docx
- 文档编号:27480762
- 上传时间:2023-07-02
- 格式:DOCX
- 页数:73
- 大小:26.03KB
广工离散数学Anyview参考答案.docx
《广工离散数学Anyview参考答案.docx》由会员分享,可在线阅读,更多相关《广工离散数学Anyview参考答案.docx(73页珍藏版)》请在冰豆网上搜索。
广工离散数学Anyview参考答案
广东工业大学离散数学Anyview习题答案
——更新于2014年12月作者Seasand2014
1.00①试设计一算法,
判断元素与集合之间的关系。
实现下列函数:
/**
*判断元素与集合之间的关系。
元素和集合之间的关系只有两种。
*@paramelem:
元素
*@parampA:
集合
*@return:
如果elem∈pA,则返回TRUE,否则返回FALSE
*/
BooleanIsInSet(SetElemelem,pSetpA){
//Addyourcodehere
}
//1.00
BooleanIsInSet(SetElemelem,pSetpA)
{
//Addyourcodehere
SetElem*a=outToBuffer(pA);
for(;*a!
='\n';a++)
{
if(elem==*a)
{
returntrue;
}
}
returnfalse;
}
1.01③试设计一算法,
实现集合的并运算。
实现下列函数:
/**
*进行两个集合的并运算
*@parampA:
要进行并运算的集合
*@parampB:
要进行并运算的集合
*@return:
将pA和pB进行并运算后得到的集合
*/
pSetSetUnion(pSetpA,pSetpB){
//Addyourcodehere
}
//1.01
pSetSetUnion(pSetpA,pSetpB)
{
SetElem*a=outToBuffer(pA);
SetElem*b=outToBuffer(pB);
pSetpC=createNullSet();
inti=0;
for(;*b!
='\n';b++)
{
directInsertSetElem(pC,*b);
}
for(a=outToBuffer(pA);*a!
='\n';a++)
{
if(isInSet(pB,*a)!
=true)
{
directInsertSetElem(pC,*a);
}
}
returnpC;
}
1.02②试设计一算法,
实现集合的交运算。
实现下列函数:
/**
*进行两个集合的交运算
*@parampA:
要进行交运算的集合
*@parampB:
要进行交运算的集合
*@return:
将pA和pB进行交运算后得到的集合
*/
pSetSetIntersection(pSetpA,pSetpB){
//Addyourcodehere
}
//1.02
pSetSetIntersection(pSetpA,pSetpB)
{
SetElem*a=outToBuffer(pA);
SetElem*b=outToBuffer(pB);
pSetpC=createNullSet();
for(;*b!
='\n';b++)
{
if(isInSet(pA,*b)==true)
{
directInsertSetElem(pC,*b);
}
}
returnpC;
}
1.03②试设计一算法,
实现集合的差运算。
实现下列函数:
/**
*进行两个集合的差运算
*@parampA:
要进行差运算的集合,相当于A-B中的A
*@parampB:
要进行差运算的集合,相当于A-B中的B
*@return:
将pA和pB进行差运算后得到的集合
*/
pSetSetSubtraction(pSetpA,pSetpB){
//Addyourcodehere
}
//1.03
pSetSetSubtraction(pSetpA,pSetpB)
{
SetElem*a=outToBuffer(pA);
SetElem*b=outToBuffer(pB);
pSetpC=createNullSet();
for(;*a!
='\n';a++)
{
if(isInSet(pB,*a)==true)continue;
directInsertSetElem(pC,*a);
}
returnpC;
}
1.04②试设计一算法,
实现集合的求补集运算。
实现下列函数:
/**
*进行集合的求补集运算。
*@parampA:
要进行求补集运算的集合
*@parampI:
全集
*@return:
返回pA相对于pI的补集。
注意:
有可能存在pA不是PI的子集的情况,
*在这种情况下pA的补集不存在,应当返回NULL
*/
pSetSetComplement(pSetpA,pSetpI){
//Addyourcodehere
}
//1.04
pSetSetComplement(pSetpA,pSetpI)
{
SetElem*a=outToBuffer(pA);
SetElem*i=outToBuffer(pI);
pSetpC=createNullSet();
intb=0,k=0;
for(;*i!
='\n';i++)
{
if(isInSet(pA,*i)==true)
{
b++;
continue;
}
directInsertSetElem(pC,*i);
}
for(;*a!
='\n';a++)
{
if(isInSet(pI,*a)==true)
{
continue;
}
k++;
directInsertSetElem(pC,*i);
}
if((b==0&&isNullSet(pA)!
=true)||(b!
=0&&k!
=0))returnNULL;
if(isNullSet(pA)==true&&isNullSet(pI)!
=true)returnpI;
elsereturnpC;
}
1.05②试设计一算法,
实现集合的对称差运算。
实现下列函数:
/**
*进行集合的对称差运算。
*@parampA:
需要进行对称差运算的集合
*@parampB:
需要进行对称差运算的集合
*@return:
返回pA与pB进行对称差运算后得到的集合。
*/
pSetSetSysmmetricDifference(pSetpA,pSetpB){
//Addyourcodehere
}
//1.05
pSetSetSysmmetricDifference(pSetpA,pSetpB)
{
//Addyourcodehere
pSetpS;
pS=createNullSet();
SetElem*pAElems;
SetElem*pBElems;
pAElems=outToBuffer(pA);
pBElems=outToBuffer(pB);
while(*pAElems!
='\n')
{
if(!
isInSet(pB,*pAElems))
{
directInsertSetElem(pS,*pAElems);
}
pAElems++;
}
while(*pBElems!
='\n')
{
if(!
isInSet(pA,*pBElems))
{
directInsertSetElem(pS,*pBElems);
}
pBElems++;
}
returnpS;
}
1.06③试设计一算法,
判断两个集合之间的包含关系。
实现下列函数:
/**
*判断两个集合之间的包含关系。
规定集合之间的包含关系为:
*真包含,被真包含于,相等。
*@parampA:
需要进行包含关系判断的集合
*@parampB:
需要进行包含关系判断的集合
*@return:
如果pA真包含pB,返回REALINCLUDING;
*如果pA被真包含于pB,返回REALINCLUDED;
*如果pA等于pB,返回EQUAL;
*对于其它情况,返回NOT_INCLUSIVE。
*/
SetRelationshipStatusSetRelationship(pSetpA,pSetpB){
//Addyourcodehere
}
//1.06
SetRelationshipStatusSetRelationship(pSetpA,pSetpB)
{
//Addyourcodehere
pSetpS;
pS=createNullSet();
SetElem*pAElems;
SetElem*pBElems;
pAElems=outToBuffer(pA);
pBElems=outToBuffer(pB);
pS=pB;
if(isNullSet(pA)&&!
isNullSet(pB))returnREALINCLUDED;
if(!
isNullSet(pA)&&isNullSet(pB))returnREALINCLUDING;
if(isNullSet(pA)&&isNullSet(pB))returnEQUAL;
if(!
isNullSet(pA)&&!
isNullSet(pB))
{
while(*pAElems!
='\n')
{
if(isInSet(pS,*pAElems))pAElems++;
else
{
while(*pBElems!
='\n')
{
if(isInSet(pA,*pBElems))pBElems++;
elsereturnNOT_INCLUSIVE;
if(*pBElems=='\n')returnREALINCLUDING;
}
}
}
if(*pAElems=='\n')
{
while(*pBElems!
='\n')if(isInSet(pA,*pBElems))pBElems++;
elsereturnREALINCLUDED;
if(*pBElems=='\n')returnEQUAL;
}
}
}
1.07⑤试设计一个非递归算法,
实现集合的求幂集运算。
实现下列函数:
/**
*求一个集合的幂集。
*@parampA:
需要进行幂集的集合
*@return:
返回pA的幂集
*/
pSetFamilyPowerSet(pSetpA){
//Addyourcodehere
}
//1.07
pSetmiji(pSetpB,pSetpC)
{
SetElem*pBElems;
SetElem*pCElems;
pSetpS;
pS=createNullSet();
pBElems=outToBuffer(pB);
pCElems=outToBuffer(pC);
while(*pBElems!
='\n')
{
directInsertSetElem(pS,*pBElems);
pBElems++;
}
while(*pCElems!
='\n')
{
if(!
isInSet(pB,*pCElems))
{
directInsertSetElem(pS,*pCElems);
}
++pCElems;
}
returnpS;
}
pFamilyOfSetPowerSet(pSetpA)
{
//Addyourcodehere
inti;
pSetpI=createNullSet();
pFamilyOfSetpF=createNullFamilyOfSet();
SetElem*pBElems=outToBuffer(pA);
insertToFamilyOfSet(pF,pI);
if(isNullSet(pA)==FALSE)
while(*pBElems!
='\n')
{
pSetpK=createNullSet();
directInsertSetElem(pK,*pBElems);
pSet*pS=outFamilyOfSetToBuffer(pF);
for(i=0;*(pS+i)!
=NULL;i++)
insertToFamilyOfSet(pF,miji(pK,*(pS+i)));
pBElems++;
}
returnpF;
}
1.08④试设计一个递归算法,
实现集合的求幂集运算。
实现下列函数:
/**
*求一个集合的幂集。
*@parampA:
需要进行幂集的集合
*@return:
返回pA的幂集
*/
pFamilyOfSetPowerSet(pSetpA){
//Addyourcodehere
}
//1.08
pSetmiji(pSetpB,pSetpC)
{
SetElem*pBElems;
SetElem*pCElems;
pSetpS;
pS=createNullSet();
pBElems=outToBuffer(pB);
pCElems=outToBuffer(pC);
while(*pBElems!
='\n')
{
directInsertSetElem(pS,*pBElems);
pBElems++;
}
while(*pCElems!
='\n')
{
if(!
isInSet(pB,*pCElems))
{
directInsertSetElem(pS,*pCElems);
}
++pCElems;
}
returnpS;
}
pFamilyOfSetPowerSet(pSetpA)
{
//Addyourcodehere
inti;
pSetpI=createNullSet();
pFamilyOfSetpF=createNullFamilyOfSet();
SetElem*pBElems=outToBuffer(pA);
insertToFamilyOfSet(pF,pI);
if(isNullSet(pA)==FALSE)
while(*pBElems!
='\n')
{
pSetpK=createNullSet();
directInsertSetElem(pK,*pBElems);
pSet*pS=outFamilyOfSetToBuffer(pF);
for(i=0;*(pS+i)!
=NULL;i++)
insertToFamilyOfSet(pF,miji(pK,*(pS+i)));
pBElems++;
}
returnpF;
}
3.01⑤试设计一个非递归算法,
验证一个表达式是不是命题公式(statementformula)。
实现下列函数:
/**
*判断一个表达式是不是命题公式。
*@parampFormula:
要进行判断的表达式。
该表达式的最后一个元素为“#”,而且规定
*'#'仅用于指示该表达式的结尾,并不属于表达式的一部分。
*@return:
如果pFormula是命题公式,返回TRUE;否则返回FALSE。
*/
BooleanIsStatementFormula(pStatementFormulapFormula){
//Addyourcodehere
}
BooleanIsStatementFormula(pStatementFormulapFormula){//Addyourcodehere
StatementFormulaSymblespreS,currS,nextS;
intleftNum=0,rightNum=0;
preS=getCurrentSymbleType(pFormula);
switch(preS){
caseException:
returnfalse;
caseConjunction:
caseDisjunction:
caseImplication:
caseEquivalence:
caseLeftParenthesis:
leftNum++;break;
caseRightParenthesis:
caseEndOfFormula:
returntrue;}nextPos(pFormula);
currS=getCurrentSymbleType(pFormula);
switch(currS){
caseLeftParenthesis:
leftNum++;break;
caseRightParenthesis:
rightNum++;break;
casePropositionalVariable:
if(preS==PropositionalVariable)
returnfalse;}
while(nextS!
=EndOfFormula){
nextPos(pFormula);
nextS=getCurrentSymbleType(pFormula);
if(nextS==LeftParenthesis)leftNum++;
if(nextS==RightParenthesis)rightNum++;
if(preS==Exception||currS==Exception||nextS==Exception)
returnfalse;
if(currS==PropositionalVariable)
if(nextS==PropositionalVariable||preS==PropositionalVariable)
returnfalse;
if(currS==Conjunction||currS==Disjunction||currS==Implication||currS==Equivalence)
if((preS!
=PropositionalVariable&&preS!
=RightParenthesis)||(nextS!
=PropositionalVariable&&nextS!
=LeftParenthesis&&nextS!
=Negation))
returnfalse;
if(nextS==Negation)
if(currS==PropositionalVariable||currS==RightParenthesis)
returnfalse;
preS=currS;
currS=nextS;}
if(leftNum!
=rightNum)
returnfalse;
returntrue;}
3.02⑤试设计一个递归算法,
验证一个表达式是不是命题公式(statementformula)。
实现下列函数:
/**
*判断一个表达式是不是命题公式。
*@parampFormula:
要进行判断的表达式。
该表达式的最后一个元素为“#”,而且规定
*'#'仅用于指示该表达式的结尾,并不属于表达式的一部分。
*@return:
如果pFormula是命题公式,返回TRUE;否则返回FALSE。
*/
BooleanIsStatementFormula(pStatementFormulapFormula){
//Addyourcodehere
}
BooleanFunction(intleftNum,intrightNum,StatementFormulaSymblespreS,StatementFormulaSymblescurrS,StatementFormulaSymblesnextS,pStatementFormulapFormula)
{
nextPos(pFormula);//解释同上题所示
nextS=getCurrentSymbleType(pFormula);
if(nextS==LeftParenthesis)
leftNum++;
if(nextS==RightParenthesis)
rightNum++;
if(preS==Exception||currS==Exception||nextS==Exception)
returnfalse;
if(currS==PropositionalVariable)
if(nextS==PropositionalVariable||preS==PropositionalVariable)
returnfalse;
if(currS==Conjunction||currS==Disjunction||currS==Implication||currS==Equivalence)if((preS!
=PropositionalVariable&&preS!
=RightParenthesis)||(nextS!
=PropositionalVariable&&nextS!
=LeftParenth
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 离散数学 Anyview 参考答案