1. 用c語言寫兩個程序,1,集合的交並差運算。2,計算關系的閉包(3種)不要求功能非常完備,能實現最基
第一個程序
集合的交並差運算
#include<stdio.h>
#include<stdlib.h>
typedef struct pointer{
char dat;
struct pointer *link;
} pointer;
void readdata(pointer *head){ //讀集合
pointer *p;
char tmp;
printf("input data ('0' for end):");
scanf("%c",&tmp);
while(tmp!='0')
{
if((tmp<'a')||(tmp>'z'))
{
printf("輸入錯誤!必須為小寫字母!\n");
return;
}
p=(pointer *)malloc(sizeof(struct pointer));
p->dat=tmp;
p->link=head->link;
head->link=p;
scanf("%c",&tmp);
}
}
void disp(pointer *head){ //顯示集合數據
pointer *p;
p=head->link;
while(p!=NULL)
{
printf("%c ",p->dat);
p=p->link;
}
printf("\n");
}
void bing(pointer *head1,pointer *head2, pointer *head3){ //計算集合1與集合2的並
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
p1=p1->link;
}
p2=head2->link;
while(p2!=NULL)
{
p1=head1->link;
while((p1!=NULL)&&(p1->dat!=p2->dat))
p1=p1->link;
if(p1==NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p2->dat;
p3->link=head3->link;
head3->link=p3;
}
p2=p2->link;
}
}
void jiao(pointer *head1,pointer *head2, pointer *head3){ //計算集合1與集合2的交
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p2=head2->link;
while((p2!=NULL)&&(p2->dat!=p1->dat))
p2=p2->link;
if((p2!=NULL)&&(p2->dat=p1->dat))
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
}
p1=p1->link;
}
}
void cha(pointer *head1,pointer *head2, pointer *head3){ //計算集合1與集合2的差
pointer *p1,*p2,*p3;
p1=head1->link;
while(p1!=NULL)
{
p2=head2->link;
while((p2!=NULL)&&(p2->dat!=p1->dat))
p2=p2->link;
if(p2==NULL)
{
p3=(pointer *)malloc(sizeof(struct pointer));
p3->dat=p1->dat;
p3->link=head3->link;
head3->link=p3;
}
p1=p1->link;
}
}
main(){
pointer *head1,*head2,*head3;
head1=(pointer *)malloc(sizeof(struct pointer));
head1->link=NULL;
head2=(pointer *)malloc(sizeof(struct pointer));
head2->link=NULL;
head3=(pointer *)malloc(sizeof(struct pointer));
head3->link=NULL;
printf("輸入集合1:\n");
readdata(head1);
printf("輸入集合2:\n");
readdata(head2);
printf("集合1為:\n");
disp(head1);
printf("集合2為:\n");
disp(head2);
printf("集合1與集合2的並為:\n");
bing(head1,head2,head3);
disp(head3);
head3->link=NULL;
printf("集合1與集合2的交為:\n");
jiao(head1,head2,head3);
disp(head3);
head3->link=NULL;
printf("集合1與集合2的差為:\n");
cha(head1,head2,head3);
disp(head3);
}
測試用例為(0表示集合輸入結束):
fdsa0
savc0
第二個程序
計算關系的閉包(3種)
#include<stdio.h>
void output(int s[][100]);
void zifan(int s2[][100]);
void ichen(int s2[][100]);
void chuandi2(int s2[][100]);
void chuandi1(int s2[][100]);
void aa();
int s[100][100],z;
int d,n ,i,j;
int main(){aa();return 0;}
void aa()
{
printf("請輸入矩陣的行數(必須小於10)\n ");
scanf("%d",&n);
printf("請輸入矩陣的列數(必須小於10)\n ");
scanf("%d",&d);
printf("請輸入關系矩陣\n");
for(i=0;i<n;i++)
{ printf("\n");
printf("請輸入矩陣的第%d行元素",i);
for(j=0;j<d;j++)
scanf("%d",&s[i][j]);
}
printf("輸入對應序號選擇演算法\n1:自反閉包\n2:傳遞閉包1\n3:傳遞閉包(Warhall演算法)2\n4:對稱閉包\n");
scanf("%d",&z);
switch(z)
{
case 1:zifan(s); break;
case 2:chuandi1(s);break;
case 3:chuandi2(s);break;
case 4:ichen(s); break;
}
}
void output(int s[][100])
{printf("所求關系矩陣為\n");
for(i=0;i<n;i++)
{for(j=0;j<d;j++)
printf("%d",s[i][j]);
printf("\n");
}
}
void zifan(int s2[][100])
{
for(i=0;i<n;i++)
s2[i][i]=1;
output(s2);aa();
}
void ichen(int s2[][100])
{int s1[100][100];
for(i=0;i<n;i++)
for(j=0;j<d;j++)
s1[j][i]=s2[i][j];
for(i=0;i<n;i++)
for(j=0;j<d;j++)
{s2[i][j]=s2[i][j]+s1[i][j];
if(s2[i][j]>1)
s2[i][j]=1;
}
output(s2);
aa();
}
void chuandi1(int s2[][100])
{int m[100][100],a[100][100],k,h;
int t[100][100];
for(i=0;i<n;i++)
for(j=0;j<d;j++)
{ a[i][j]=0;
t[i][j]=s2[i][j];
m[i][j]=s2[i][j];}
for(h=0;h<n;h++)
{for(i=0;i<n;i++)
for(j=0;j<d;j++)
if(m[i][j]==1)
{for(k=0;k<n;k++)
if(s2[j][k]==1)
a[i][k]=1;
}
for(i=0;i<n;i++)
for(j=0;j<d;j++)
{ m[i][j]=a[i][j];
t[i][j]+=a[i][j];
a[i][j]=0;
if(t[i][j]>1)
t[i][j]=1;
}
}
output(t);aa();
}
void chuandi2(int s2[][100])
{int k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(s2[j][i]==1)
for(k=0;k<n;k++)
s2[j][k]+=s2[i][k];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(s2[i][j]>1)
s2[i][j]=1;
output(s2);aa();
}