當前位置:成語大全網 - 新華字典 - 在C語言中遇到了壹個難題,求神幫忙,用C語言實現了:求任意兩個集合的交、並、差集,

在C語言中遇到了壹個難題,求神幫忙,用C語言實現了:求任意兩個集合的交、並、差集,

我之前寫過壹個純C,用數組模擬C++ STL中set_intersection,set_union,set_difference的實現。我稍微修改了壹下,補充了壹些評論,希望對妳有幫助。註意:必須首先對輸入集進行排序;輸出結果與C++ STL的測試結果壹致。

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltstring.h & gt

int set _ intersection(const int set 1[],const unsigned int size of set 1,

const int Set2[],const unsigned int SizeofSet2,

int Res[],unsigned int * pSizeofRes);

int set _ union(const int set 1[],const unsigned int size of set 1,

const int Set2[],const unsigned int SizeofSet2,

int Res[],unsigned int * pSizeofRes);?

int set _ difference(const int set 1[],const unsigned int size of set 1,

const int Set2[],const unsigned int SizeofSet2,

int Res[],unsigned int * pSizeofRes);?

int compare (const void * a,const void * b);

void print_array(const int arr[],const size _ t len);

int main(int argc,char** argv)

{

int first[] = {5,10,15,20,25 };

int second[] = {50,40,30,20,10 };

無符號int size1,size2,size_intxn,size_union,size_diff,retcode

int *pr_intxn,*pr_union,* pr _ diff

/*先決條件,集合必須排序。*/

size 1 = sizeof(first)/sizeof(first[0]);

size2 = sizeof(秒)/ sizeof(秒[0]);

qsort(first,size1,sizeof(int),compare);

qsort(second,size2,sizeof(int),compare);

/*交集*/

size _ intxn =(size 1 & gt;size2)?size 1:size 2;/*估計結果的大小*/

pr _ int xn =(int *)malloc(sizeof(int)* size _ int xn);/*預分配結果*/

if (NULL == pr_intxn) {

printf("交叉點存儲器錯誤。\ n ");

return-1;

}

printf("1)交集:\ n ");

retcode = set_intersection(first,size1,second,size2,pr_intxn,& ampsize _ intxn);

if (retcode == 0)

print_array(pr_intxn,size _ int xn);

其他

printf(" set _ intersection錯誤,代碼%d\n ",retcode);

免費(pr _ int xn);

/*聯合*/

size _ union = size 1+size 2;/*估計結果的大小*/

pr _ union =(int *)malloc(sizeof(int)* size _ union);/*預分配結果*/

if (NULL == pr_union) {

printf("聯合內存錯誤。\ n ");

return-1;

}

printf("2)集合的並集:\ n ");

retcode = set_union(first,size1,second,size2,pr_union,& ampsize _ union);

if (retcode == 0)

print_array(pr_union,size _ union);

其他

printf(" set _ union中的錯誤,代碼%d\n ",retcode);

免費(pr _ union);

/*差異*/

size _ diff = size 1+size 2;/*估計結果的大小*/

pr _ diff =(int *)malloc(sizeof(int)* size _ diff);/*預分配結果*/

if (NULL == pr_diff) {

printf("差異內存錯誤。\ n ");

return-1;

}

printf("3)套差:\ n ");

retcode = set_difference(first,size1,second,size2,pr_diff,& ampsize _ diff);

if (retcode == 0)

print_array(pr_diff,size _ diff);

其他

printf(" set _ difference錯誤,代碼%d\n ",retcode);

free(pr _ diff);

返回0;

}

/*

輸入:

Set1 -第壹組。

Set2 -第二組。

SizeofSet1 -設置第壹組的長度。

SizeofSet2 -設置第二組的長度。

輸入/輸出:?

用於存儲結果的Res - Set。

指向SizeofRes,結果的長度。如果SizeofRes小於

預期,將向調用者返回正確的大小。

返回:

0 -如果成功。?

1 - SizeofRes比預期的小。

-1 -內部內存錯誤。

*/

int set _ difference(const int set 1[],const unsigned int size of set 1,

const int Set2[],const unsigned int SizeofSet2,

int Res[],unsigned int* pSizeofRes)

{

int i,j,k;

無符號int size _ pre

int * pr = 0;

size _ pre = sizeof set 1+sizeof set 2;

if(* pSizeofRes & lt;size_pre)

{

* pSizeofRes = size _ pre

返回1;

}

pr =(int *)malloc(size _ pre * sizeof(int));

if ( pr == NULL)

return-1;

I = 0;j = 0;k = 0;

while(我& ltsizeof set 1 & amp;& ampj & ltSizeofSet2)

{

if(set 1[I]& lt;set 2[j])pr[k++]= set 1[i++];

else if(set 2[j]& lt;set 1[I])++ j;

其他

{ i++;j++;}

}

memcpy(pr+k,Set1+i-1,sizeof(int)*(sizeof set 1-I+1));

k+= sizeof set 1-I;

memcpy(Res,pr,k * sizeof(int));

* pSizeofRes = k;

免費(pr);

返回0;

}?

int set _ union(const int set 1[],const unsigned int size of set 1,

const int Set2[],const unsigned int SizeofSet2,

int Res[],unsigned int* pSizeofRes)

{

int i,j,k;

無符號int size _ pre

int * pr = 0;

size _ pre = sizeof set 1+sizeof set 2;

if(* pSizeofRes & lt;size_pre)

{

* pSizeofRes = size _ pre

返回1;

}

pr =(int *)malloc(size _ pre * sizeof(int));

if ( pr == NULL)

return-1;

I = 0;j = 0;k = 0;

while ( 1)

{

如果(i & gtSizeofSet1 - 1)

{

memcpy(pr+k,Set2+j-1,sizeof(int)*(sizeof set 2-j+1));

k+= sizeof set 2-j;

打破;

}

if(j & gt;SizeofSet2 - 1)

{

memcpy(pr+k,Set1+i-1,sizeof(int)*(sizeof set 1-I+1));

k+= sizeof set 1-I;

打破;

}

if(set 1[I]& lt;set 2[j])pr[k]= set 1[i++];

else if(set 2[j]& lt;set 1[I])pr[k]= set 2[j++];

else { pr[k]= set 1[I];++ I;++ j;}

++ k;

}

memcpy(Res,pr,k * sizeof(int));

* pSizeofRes = k;

免費(pr);

返回0;

}?

int set _ intersection(const int set 1[],const unsigned int size of set 1,

const int Set2[],const unsigned int SizeofSet2,

int Res[],unsigned int* pSizeofRes)

{

int i,j,k;

無符號int size _ pre

int * pr = 0;

size _ pre =(sizeof set 1 & gt;SizeofSet2)?sizeof set 1:sizeof set 2;

if(* pSizeofRes & lt;size_pre)

{

* pSizeofRes = size _ pre

返回1;

}

pr =(int *)malloc(size _ pre * sizeof(int));

if ( pr == NULL)

return-1;

I = 0;j = 0;k = 0;

while(我& ltsizeof set 1 & amp;& ampj & ltSizeofSet2)

{

if(set 1[I]& lt;set2[j])++ I;

else if(set 2[j]& lt;set 1[I])++ j;

其他

{

pr[k++]= set 1[I];

i++;j++;

}

}

memcpy(Res,pr,k * sizeof(int));

* pSizeofRes = k;

免費(pr);

返回0;

}

void print_array(常數int arr[],常數size_t len)

{

int I;

for(I = 0;我& ltleni++)

printf("%d ",arr[I]);

printf(" \ n ");

}

int compare (const void * a,const void * b)

{

return(*(int *)a-*(int *)b);

}