【问题标题】:checking if two arrays of integers have the same elements regardless of their order检查两个整数数组是否具有相同的元素,而不管它们的顺序如何
【发布时间】:2018-12-23 16:43:54
【问题描述】:

此代码将卡片的值存储在arr.value 中。 然后我尝试检查 arr.value 中的元素是否与数组cmpvalues 具有相同的元素。 如果一个元素与数组cmpvalues 中的元素不匹配,它应该打印出“错误”并返回0。 但是,即使输入正确,它仍然会打印出错误。 一切都编译得很好,我只是找不到错误。

#include <stdio.h>

#define MAXCOLR   14
#define MAXLINE  100
#define MAXCHR  1024   
#define _GNU_SOURCE

typedef struct {
    char color[MAXCOLR];
    int value;
} colorval_t;

int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}


int main (int argc, char **argv) {

size_t n;
int cmpvalues [] = {
   65,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
   10,
   74,
   81,
   75,
   65,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
   10,
   74,
   81,
   75
};


    size_t ndx = 0;
    char buf[MAXCHR];
    colorval_t arr[MAXLINE] = {{ .color = "" }};

    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) { 
        perror ("file open failed");
        return 1;
    }

    while (ndx < MAXLINE && fgets (buf, MAXCHR, fp)) {
        char c;
        if (sscanf (buf, "%13s %d", arr[ndx].color, &arr[ndx].value) == 2)
            ndx++;
        else if (sscanf (buf, "%13s %c", arr[ndx].color, &c) == 2) {
            arr[ndx].value = c;
            ndx++;
        }
    }
    if (fp != stdin) fclose (fp);   

    for (size_t i = 0; i < ndx; i++)
        printf ("arr[%2zu] : %s %d\n", i, arr[i].color, arr[i].value);




 qsort(arr, 26, sizeof(arr[26]), cmpfunc);
 for( n = 0 ; n < 26; n++ ) {   
      printf("%d ", arr[n].value);
   }



    return 0;



}

输入:

RED A
RED 2
RED 3
RED 4
RED 5
RED 6
RED 7
RED 8
RED 9
RED 10
RED J
RED Q
RED K
BLACK A
BLACK 2
BLACK 3
BLACK 4
BLACK 5
BLACK 6
BLACK 7
BLACK 8
BLACK 9
BLACK 10
BLACK J
BLACK Q
BLACK K

【问题讨论】:

  • 很难理解你的代码应该如何工作。你希望这会做什么:例如match = match ;? (提示:这是一个 noop)
  • 测试两个列表是否包含完全相同的元素的常用方法是对两个列表进行排序,然后逐项检查。
  • @Ctx 排序会花费很多时间,我不知道如何对数组元素进行排序
  • 看看qsort(),这是您拥有的最快的选择。
  • @Ctx 我使用 qsort() 对数组进行排序,但我不断收到警告说 qsort 是隐式声明的,但一切正常。现在比较两个数组的最佳方法是什么?

标签: c arrays struct integer compare


【解决方案1】:

正确缩进代码表明该函数什么都不做并且总是返回0(除非t1size == 0)。

int all_match( int table1, int table2 , size_t t1size, size_t t2size)
{
    for(size_t t1index = 0; t1index < t1size; t1index++)
    {
        int match = 0;
        for(size_t t2index = 0; t2index < t2size; t2index++)
        {
            match = match ;
            if(match)
            {
                break;          // never happens
            }
        }
        if(!match){
            printf("error");
            return 0;           // always happens
        }
    }
    return 1;
}

请注意,参数 tabletable2 将被忽略。

【讨论】:

  • 它们应该是数组
【解决方案2】:

以下匹配函数可以解决问题:

int all_match( colorval_t *table1, colorval_t *table2 , size_t t1size, size_t t2size)
{
    for(size_t t1index = 0; t1index < t1size; t1index++)
    {
        for(size_t t2index = 0; t2index < t2size; t2index++)
        {
            if (table1[t1index].value==table2[t2index].value
            &&  strcmp(table1[t1index].color, table2[t2index].color)==0)
                break;
        }
        if (t2index >= t2size) {
            printf("error");
            return 0;
         }
    }
    return 1;
}

【讨论】:

  • 当我将它添加到我的代码并尝试编译它时,我得到了很多错误。像 colorval 一样没有名为 colour 的成员
  • 如果列表中允许重复,此方法可能会失败。排序和比较方法更快、更准确。
  • @momonosuke,有没有听说过为自己着想,比如看到colour 应该是color 或者修复一个流氓支架?
  • @Ctx,原卡组不应该排序。目前的命令可能有意义。但原则上你是正确的。规范并没有说输入是一副牌。结构中的附加字段,例如checked可以解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
  • 2022-12-12
  • 2020-12-27
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
相关资源
最近更新 更多