我不知道为什么我不能把这个做好,我花了一些时间在这上面,但我没有得到我想要的。现在我很沮丧。我有一个所需项目的列表 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/