c语言 离散数学集合复合运算的代码,(R。R)的代码实现
如R={<a,b><b,t,><t,d,><s,j,><j,i><c,a>},如何用代码实现R。R?真的纠结了很久,想了很多方法发现都有漏洞~~求大神帮忙了~~...
如R={<a,b><b,t,><t,d,><s,j,><j,i><c,a>},如何用代码实现R。R?
真的纠结了很久,想了很多方法发现都有漏洞~~求大神帮忙了~~ 展开
真的纠结了很久,想了很多方法发现都有漏洞~~求大神帮忙了~~ 展开
展开全部
//说明:输入的格式需要提示按输入,因为要获取正确的有序对才能进行复合运算
/*
*************输入格式如:a b, #,# 退出***************
输入:a b
输入:b t
输入:t d
输入:s j
输入:j i
输入:c a
*/
#include "stdlib.h"
typedef char Element;
struct Node
{
Element left;
Element right;
struct Node *next;
};
struct Node *CreateLink();
struct Node *Operation(struct Node *R,struct Node *S);
void PrintLink(struct Node *h);
int main()
{
struct Node *hdR,*hdS,*rhd;
printf("请输入第一个集合R的关系\n");
hdR = CreateLink();
PrintLink(hdR);
printf("\n请输入第二个集合S的关系\n");
hdS = CreateLink();
PrintLink(hdS);
rhd = Operation(hdR,hdS);
if (rhd->next == NULL)
{
printf("\nR。S结果为空集\n");
}
else
{
printf("\nR。S结果为:\n");
PrintLink(rhd);
}
return 0;
}
struct Node *CreateLink()
{
struct Node *head, *p;
printf("*************输入格式如:a b, \'#,#\' 退出***************\n");
Element a,b;
head = (struct Node *)malloc(sizeof(struct Node));
head->left = 0;
head->right = 0;
head->next = NULL;
printf("输入:");
scanf("%c %c",&a,&b);
getchar();
while (a != '#')
{
p = (struct Node *)malloc(sizeof(struct Node));
p->left = a;
p->right = b;
p->next = head->next;
head->next = p;
printf("输入:");
scanf("%c %c",&a,&b);
getchar();
}
return head;
}
struct Node *Operation(struct Node *R, struct Node *S)
{
struct Node *newHead,*newP,*newQ;
struct Node *pH, *pNext;
newHead = (struct Node *)malloc(sizeof(struct Node));
newHead->left = 0;
newHead->right = 0;
newHead->next = NULL;
newP = newHead;
if (R->next == NULL || S->next == NULL)
{
return newP;
}
char cLeft,cRight;
pH = R;
while (pH->next != NULL)
{
cLeft = pH->next->left;
cRight = pH->next->right;
pNext = S->next;
while(pNext != NULL)
{
//存在可以复合运算的
if (cRight == pNext->left)
{
//在复合运算结果集中插入数据,如果存在相同的二元关系,则不需要插入
newP = newHead;
while (newP->next != NULL)
{
if (cLeft == newP->left && cRight == newP->right)
{
break;
}
newP = newP->next;
}
if (newP->next == NULL)
{
newQ = (struct Node *)malloc(sizeof(struct Node));
newQ->left = cLeft;
newQ->right = pNext->right;
newQ->next = NULL;
newP->next = newQ;
// newQ->next = newP->next->next;
}
}
pNext = pNext->next;
}
pH = pH->next;
}
return newHead;
}
void PrintLink(struct Node *h)
{
struct Node *p=h->next;
printf("\n");
while (p != NULL)
{
printf("<%c,%c> ",p->left,p->right);
p = p->next;
}
printf("\n");
}
/*
*************输入格式如:a b, #,# 退出***************
输入:a b
输入:b t
输入:t d
输入:s j
输入:j i
输入:c a
*/
#include "stdlib.h"
typedef char Element;
struct Node
{
Element left;
Element right;
struct Node *next;
};
struct Node *CreateLink();
struct Node *Operation(struct Node *R,struct Node *S);
void PrintLink(struct Node *h);
int main()
{
struct Node *hdR,*hdS,*rhd;
printf("请输入第一个集合R的关系\n");
hdR = CreateLink();
PrintLink(hdR);
printf("\n请输入第二个集合S的关系\n");
hdS = CreateLink();
PrintLink(hdS);
rhd = Operation(hdR,hdS);
if (rhd->next == NULL)
{
printf("\nR。S结果为空集\n");
}
else
{
printf("\nR。S结果为:\n");
PrintLink(rhd);
}
return 0;
}
struct Node *CreateLink()
{
struct Node *head, *p;
printf("*************输入格式如:a b, \'#,#\' 退出***************\n");
Element a,b;
head = (struct Node *)malloc(sizeof(struct Node));
head->left = 0;
head->right = 0;
head->next = NULL;
printf("输入:");
scanf("%c %c",&a,&b);
getchar();
while (a != '#')
{
p = (struct Node *)malloc(sizeof(struct Node));
p->left = a;
p->right = b;
p->next = head->next;
head->next = p;
printf("输入:");
scanf("%c %c",&a,&b);
getchar();
}
return head;
}
struct Node *Operation(struct Node *R, struct Node *S)
{
struct Node *newHead,*newP,*newQ;
struct Node *pH, *pNext;
newHead = (struct Node *)malloc(sizeof(struct Node));
newHead->left = 0;
newHead->right = 0;
newHead->next = NULL;
newP = newHead;
if (R->next == NULL || S->next == NULL)
{
return newP;
}
char cLeft,cRight;
pH = R;
while (pH->next != NULL)
{
cLeft = pH->next->left;
cRight = pH->next->right;
pNext = S->next;
while(pNext != NULL)
{
//存在可以复合运算的
if (cRight == pNext->left)
{
//在复合运算结果集中插入数据,如果存在相同的二元关系,则不需要插入
newP = newHead;
while (newP->next != NULL)
{
if (cLeft == newP->left && cRight == newP->right)
{
break;
}
newP = newP->next;
}
if (newP->next == NULL)
{
newQ = (struct Node *)malloc(sizeof(struct Node));
newQ->left = cLeft;
newQ->right = pNext->right;
newQ->next = NULL;
newP->next = newQ;
// newQ->next = newP->next->next;
}
}
pNext = pNext->next;
}
pH = pH->next;
}
return newHead;
}
void PrintLink(struct Node *h)
{
struct Node *p=h->next;
printf("\n");
while (p != NULL)
{
printf("<%c,%c> ",p->left,p->right);
p = p->next;
}
printf("\n");
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询