c语言rr指令,c语言离散数学集合复合运算的代码,(RR)的代码实现如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的关系

");

hdR = CreateLink();

PrintLink(hdR);

printf("

请输入第二个集合S的关系

");

hdS = CreateLink();

PrintLink(hdS);

rhd = Operation(hdR,hdS);

if (rhd->next == NULL)

{

printf("

R。

S结果为空集

");

}

else

{

printf("

R。

S结果为:

");

PrintLink(rhd);

}

return 0;

}

struct Node *CreateLink()

{

struct Node *head, *p;

printf("*************输入格式如:a b, '#,#' 退出***************

");

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("

");

while (p != NULL)

{

printf(" ",p->left,p->right);

p = p->next;

}

printf("

");

}。

全部

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值