本人碰见一道C语言难题,寻大神帮助,利用C语言实现:求任意两个集合的交集、并集、差集,

   更新日期:2024.06.01

以前写过一个纯C的, 用的是数组,模拟C++ STL里面的set_intersection,set_union和set_difference的实现。 稍作了修改,添加了些注释,希望能帮到你。注意:必须先对输入集合排序;输出结果和C++ STL的测试结果吻合。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int set_intersection (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes);
int set_union (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes);                     
int set_difference (const int Set1[], const unsigned int SizeofSet1,
                      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};
    unsigned int size1, size2, size_intxn, size_union, size_diff, retcode;
    int *pr_intxn, *pr_union, *pr_diff;
    /* Pre-requirement, set MUST be sorted. */
    size1 = sizeof(first) / sizeof(first[0]);
    size2 = sizeof(second) / sizeof(second[0]);
    qsort(first, size1, sizeof(int), compare);
    qsort(second, size2, sizeof(int), compare);
           
    /* Intersection */
    size_intxn = (size1 > size2) ? size1 : size2; /* estimate size of result */
       
    pr_intxn = (int *)malloc(sizeof(int) * size_intxn); /* pre-allocate result */
    if (NULL == pr_intxn) {
        printf("Intersection memory error.
");
        return -1;
    }
           
    printf("1) Set of Intersection:
");   
    retcode = set_intersection(first, size1, second, size2, pr_intxn, &size_intxn);
    if (retcode == 0)
        print_array(pr_intxn, size_intxn);
    else
       printf("Error in set_intersection, code %d
", retcode);
          
    free(pr_intxn);
       
    /* Union */
    size_union = size1 + size2; /* estimate size of result */
       
    pr_union = (int *)malloc(sizeof(int) * size_union); /* pre-allocate result */
    if (NULL == pr_union) {
        printf("Union memory error.
");
        return -1;
    }
           
    printf("2) Set of Union:
");   
    retcode = set_union(first, size1, second, size2, pr_union, &size_union);
    if (retcode == 0)
       print_array(pr_union, size_union);
    else
       printf("Error in set_union, code %d
", retcode);
    free(pr_union);
       
    /* Difference */
    size_diff = size1 + size2; /* estimate size of result */
       
    pr_diff = (int *)malloc(sizeof(int) * size_diff); /* pre-allocate result */
    if (NULL == pr_diff) {
        printf("Difference memory error.
");
        return -1;
    }
           
    printf("3) Set of Difference:
");   
    retcode = set_difference(first, size1, second, size2, pr_diff, &size_diff);
    if (retcode == 0)
       print_array(pr_diff, size_diff);
    else
       printf("Error in set_difference, code %d
", retcode);
           
    free(pr_diff);
       
       
    return 0;
}
/*
  Input:
    Set1 - First set.
    Set2 - Second set.
    SizeofSet1 - Set length of First set.
    SizeofSet2 - Set length of Second set.
  Input/Output: 
    Res - Set for storing results.
    pSizeofRes - Point to SizeofRes, length of result. If SizeofRes is less than
                 expected, a proper size will be returned to caller.
  Return:
    0 - If successfully.              
    1 - SizeofRes is smaller than expected.
    -1 - Internal memory error.
*/
int set_difference (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes)
{
    int i, j, k;
    unsigned int size_pre;   
    int *pr = 0;
       
    size_pre = SizeofSet1 + SizeofSet2;
    if ( *pSizeofRes < size_pre)
    {
        *pSizeofRes = size_pre;
        return 1;
    }
       
    pr = (int *)malloc(size_pre * sizeof(int));
    if ( pr == NULL )
       return -1;
          
    i = 0; j = 0; k = 0;
    while ( i < SizeofSet1 && j < SizeofSet2 )
    {
        if (Set1[i] < Set2[j]) pr[k++] = Set1[i++];       
        else if (Set2[j] < Set1[i]) ++j;
        else
        { i++; j++;}
    }
    memcpy(pr+k, Set1+i-1, sizeof(int)*(SizeofSet1-i+1));
    k += SizeofSet1-i;
               
    memcpy(Res, pr, k*sizeof(int));
    *pSizeofRes = k;       
    free(pr);
       
    return 0;       
}                     
int set_union (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes)
{
    int i, j, k;
    unsigned int size_pre;   
    int *pr = 0;
       
    size_pre = SizeofSet1 + SizeofSet2;
    if ( *pSizeofRes < size_pre)
    {
        *pSizeofRes = size_pre;
        return 1;
    }
       
    pr = (int *)malloc(size_pre * sizeof(int));
    if ( pr == NULL )
       return -1;
          
    i = 0; j = 0; k = 0;
    while ( 1 )
    {
        if (i > SizeofSet1 - 1)
        {
            memcpy(pr+k, Set2+j-1, sizeof(int)*(SizeofSet2-j+1));
            k += SizeofSet2 - j;
            break;
        }
           
        if (j > SizeofSet2 - 1)
        {
            memcpy(pr+k, Set1+i-1, sizeof(int)*(SizeofSet1-i+1));
            k += SizeofSet1 - i;
            break;
        }
        if (Set1[i] < Set2[j]) pr[k] = Set1[i++];       
        else if (Set2[j] < Set1[i]) pr[k] = Set2[j++];       
        else { pr[k] = Set1[i]; ++i; ++j; }
        ++k;
    }
    memcpy(Res, pr, k*sizeof(int));
    *pSizeofRes = k;       
    free(pr);
       
    return 0;       
}                     
                         
int set_intersection (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes)
{
    int i, j, k;
    unsigned int size_pre;   
    int *pr = 0;
       
    size_pre = (SizeofSet1 > SizeofSet2) ? SizeofSet1 : SizeofSet2;
    if ( *pSizeofRes < size_pre)
    {
        *pSizeofRes = size_pre;
        return 1;
    }
       
    pr = (int *)malloc(size_pre * sizeof(int));
    if ( pr == NULL )
       return -1;
          
    i = 0; j = 0; k = 0;
    while ( i < SizeofSet1 && j < SizeofSet2 )
    {
        if (Set1[i] < Set2[j]) ++i;
        else if (Set2[j] < Set1[i]) ++j;
        else
        {
            pr[k++] = Set1[i];
            i++; j++;
        }
    }
    memcpy(Res, pr, k*sizeof(int));
    *pSizeofRes = k;       
    free(pr);
       
    return 0;   
}
void print_array(const int arr[], const size_t len)
{
    int i;
       
    for (i = 0; i < len; i++)
        printf("%d ", arr[i]);
           
    printf("
");           
}
int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}


具体忘了,利用链表可以求解,参考严蔚敏 数据结构(C语言版)

  • 18688316697 :本人碰见一道C语言难题,寻大神帮助,利用C语言实现:求任意两个集合的交集...
    沈怪泳2587 :答:以前写过一个纯C的, 用的是数组,模拟C++ STL里面的set_intersection,set_union和set_difference的实现。 稍作了修改,添加了些注释,希望能帮到你。注意:必须先对输入集合排序;输出结果和C++ STL的测试结果吻合。include <stdio.h>#include <stdlib.h>#include <string.h>int set_intersection (...
  • 18688316697 :一道C语言难题,没有思路,请教大神怎么做?
    沈怪泳2587 :答:循环,初始化,可以用random 选择法排序,双重循环搞定,不会的话随便搜一下,一堆样例 输入新值 从后向前循环比较 如果当前位置值比新输入的大,那么后移一位 否则退出循环 退出位置的下一个位置就是插入点 大致就是 for(i=N-2;a[i]>v &&i>=0;i--)a[i+1]=a[i];a[i+1]=v;...
  • 18688316697 :一道c语言编程题,找不到错处,求大神
    沈怪泳2587 :答:for (j = 0; j <N - 1; j++)这个语句的问题,当字符串长度没有那么大时就挪到了最后去了。改成for (j = 0; j < strlen(w) - 1; j++)就可以了。实际上还有一种更简单的解决方法,使用memcpy直接拷贝内存。void fun(char* w, int m){int len = strlen(w);char* tmp = (char...
  • 18688316697 :C语言!本人新手自学遇到了一个难题!请大神们帮帮忙,祝你们新年快乐,羊 ...
    沈怪泳2587 :答:include <stdio.h>int main(void){ int i, j; printf("请输入一个整数:"); scanf("%d", &i); j=1; while(j*=2 < i) printf("%7d", j); return 0;} 附: 这个程序用do...while会发生错误.
  • 18688316697 :有一道c语言的题不会做 求大神解答
    沈怪泳2587 :答:如图
  • 18688316697 :C语言的一道题不会做了,求大神帮一下
    沈怪泳2587 :答:第一题: a=4,b=8, 所以 (b==a) 为假,假就是 0,c= (b==a); c 得 0。语句中 a,b 数值 未发生过变化,保持初始值 a=4,b=8。所以输出 a,b,c 印出: 4,8,0 第二题:输入58,a=58;a>50 的条件成立,输出a值,印58 a>40 的条件成立,输出a值,印58 a>...
  • 18688316697 :一道c语言指针题,求大神解答,感谢
    沈怪泳2587 :答:这题目输出的结果是 5,6,6解析:首先声明一个整型数组a,整形变量y,整型指针p。p指向数组a[]的第1个元素,也就是8。接下来,指针p先--,指向了数组a[]的第0个元素,也就是5。之后y取出p指向的内容,即5。之后p指针指向的内容又累加,即数组a[]的第0个元素从5变成了6。因此打印输出y的...
  • 18688316697 :求大神解答一道C语言题~萌新实在不会~跪求大神!
    沈怪泳2587 :答:int n){printf("%d\t%s\t",class1[n].no,class1.name); for(i=0;i<5;i++) printf("%.1f%c",class1[n].score[i],i<4?'\t','\n');}void printstudent(int n){float s=0; for(i=0;i<5;i++) s+=class1[n].score[i]; printf("%.1f\n",s/5);} ...
  • 18688316697 :一道c语言的问题,求大神解答
    沈怪泳2587 :答:第一个max有缺省参数c,这个参数可写可不写,因此max(3,4)无法判断调用哪一个max,出现二义性,错误(如果是C语言,那直接就不支持函数的重载,同名函数直接会出错)对于任何函数来说,参数的缺省只能是右边,要调用ferror,可以使ferror(),ferror(1),ferror(1,2),但绝不能缺省左边的参数 ...
  • 18688316697 :c语言难题,求助啊
    沈怪泳2587 :答:然后再加1。如此经过有限次运算后,总可以得到自然数1。人们把谷角静夫的 这一发现叫做“谷角猜想”。要求由键盘输入一个自然数n,把n经过有限次运算 后,最终变成自然数1的全过程打印出来。 */ include<stdio.h> void main(void){ int i,t,count=0;scanf("%d",&i);printf("Input number ...
  • 相关链接

    欢迎反馈与建议,请联系电邮
    2024 © 视觉网