与数组进行比较并返回丢失的项目?

标签 c

我不知道为什么我不能把这个做好,我花了一些时间在这上面,但我没有得到我想要的。现在我很沮丧。我有一个所需项目的列表 LL[] 和一个列表 sawLL[] ,其中可能包含 LL[] 中的项目,但顺序是任意的。我想将丢失的项目放入 unseen[] 中,然后打印 unseen。至少这是我的想法:(

unsigned int i=0, j=0, k=0;
unsigned int LL[] = {0xB173,0xB193,0xB14D,0xB14E};  //Desired items
unsigned int seenLL[5];  // Seen items
unsigned int unseen[5];  // For unseen items

printf("\n List of Logcodes seen in filtered view :");      
for(i=0;i<view->numFilters;i++){   // I can't explain this, as it's BIG
    if(flt->filterType == LOGCODE_TYPE ){ 
        printf(" 0x%04X,",flt->code);  // This will print the list of items in the view. Usually about 2 or 3 items
        seenLL[k]=flt->code;   // Copy them to seenLL[]
        k++;
     }
 } 
 k=0;
 // At this point though my seenLL[] has a few of my desired items, it also contains junk information, which I am unable to explain.

for(i=0;i<sizeof(LL)/sizeof(LL[0]);i++){
    for(j=0;j<sizeof(seenLL)/sizeof(seenLL[0]);j++){
        if(LL[i] != seenLL[j]){
            unseen[k]=LL[i];    // I was trying to copy to unseen[] items that are missing from the desired list LL[]
            k++;
        }
        //else{       // Dumb, but what if seenLL[] had items from LL[] but not in the same order as they are in the array LL[], it can occur in any order
         // if(sizeof(unseen)/sizeof(unseen[0])!=0)
        //      if(LL[i] == unseen[i]{ 
        //            k--;
        //            unseen[i]=0;
         //       }                     
    }
}
if(k>0) printf(" Missing Logs Pkts :");
if(k>0) for(i=0;i<sizeof(unseen)/sizeof(unseen[0]);i++)    //sigh! my pathetic attempt to print missing items.
     printf("%d,",unseen[i]);

最佳答案

您必须扫描 seenLL 数组以查找 LL 的每个元素,但正确检测该元素是否已被看到:

for (i = 0;i < sizeof(LL) / sizeof(LL[0]); i++) {
    for (j = 0; j < sizeof(seenLL) / sizeof(seenLL[0]); j++) {
        if (LL[i] == seenLL[j])
            break;
    }
    if (j >= sizeof(seenLL) / sizeof(seenLL[0])) {
        unseen[k++] = LL[i];    // scan went to the end, no match.
    }
}

我建议使用宏 countof 来阐明您的代码:

#define countof(a)  ((sizeof(a) / sizeof((a)[0]))

关于与数组进行比较并返回丢失的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28998112/

相关文章:

c - 读取 .bin 文件的某些部分(例如从 11 日到 23 日): hex into int, 字符串。 C

c - 如何在不使用数组的情况下检查整数中相同的数字?

C 错误 : lvalue required as unary '&' operand (using pointers)

python - C 数组基本操作和操作与 Python 的比较

c - 使用 MPI_Isend 发送和接收不同类型的数据

将字符串复制到 char 数组中

c - 我是 C 新手,但收到 "Segmentation Faults"(段错误/SIGSEGV)。为什么?

c++ - C 中的外部类型声明冲突

c - 简单的 C 程序从硬币总数中查找卢比总数。

python - 读取文本列的大型数据文件的最快方法是什么?