简体   繁体   English

在C中搜索数组类型struct的元素

[英]Searching an element of array type struct in c

I have an array of structures that store some basic informations about a pc within a company (code,model,connected to internet). 我有一系列结构,这些结构存储有关公司内部PC的一些基本信息(代码,模型,连接到Internet)。

typedef struct database{
    char code[5];
    char brand[20];
    char model[20];
    int lab;
    int connect;
}database;

Now I want to search through the array to find the pc coresponding to the code C01A 现在我想搜索数组以找到对应于代码C01A的pc C01A

here is a little example of info stored in the struct: 这是存储在结构中的信息的一个小例子:

C01A HP SJH1740 1 0
B02A HP SJ1290 3 1
A03B DELL PQ240 2 1
A02B DELL PQ240 3 1
C09H FUJITSU NP0001 1 0
A06D DELL PQ240 3 1
C00X FUJITSU LP1050 2 0
B89A HP SJ1290 3 1
A03F DELL PT1000 2 0
C12P HP AA0012 1 1
D01D DELL BB2300H 3 0

You can see that the first pc has the code C01A . 您可以看到第一台PC的代码为C01A here we see 11 pc's which I randomly picked from the file in which there can be millions of pc's (Imagine a big company, array is able to hold 10 million pc's just because I can). 在这里,我们看到我从文件中随机挑选的11台PC中可以有数百万台PC(想象一家大公司,因为我可以,阵列能够容纳1000万台PC)。

I could just search every single element in the array until I find the right one. 我可以只搜索数组中的每个元素,直到找到正确的元素。 but (using a quick calculation dunno if it's OK). 但是(如果可以,请使用快速计算方法dunno)。 this means I have to scroll trough 10.5MB of memory, which is quite a lot if the searching process is not optimalized. 这意味着我必须滚动通过10.5MB的内存,如果搜索过程没有得到最佳化,这将是一个很大的数目。

So I came up with this: 所以我想出了这个:

step 1: sort the array when the program is started. 步骤1:在程序启动时对数组进行排序。

step 2: start at the middle of the array and check that element 第2步:从数组的中间开始并检查该元素

step 3: if the element I want to search is smaller than the element checked, check the middle of the first half, else check the middle of the second half 步骤3:如果我要搜索的元素小于选中的元素,请检查上半部分的中间部分,否则检查下半部分的中间部分

step 4: repeat until the element is found or until the same element is checked twice, in which case the element does not exist. 步骤4:重复该操作,直到找到该元素或对同一元素进行两次检查为止,在这种情况下该元素不存在。

Here is a fast code that I tried: 这是我尝试过的快速代码:

database searchpc(database temp[],int n){
    int i=n/2;
    char code [5];
    printf("incerici codice da cercare: ");
    scanf("%s",code);
    getchar();
    while(strcmp(temp[i].code,code)!=0){
        if(strcmp(temp[i].code,code)>0)
            i=i/2;
        else
            i=i*2;
    }
    return temp[i];
}

this returns the element if found, else it gets stuck in a loop (This is work in process) 如果找到该元素,则返回该元素,否则将陷入循环(此过程正在进行中)

I tested it on the info above and I searched, just as in the example, for the C01A pc. 我在上面的信息中对其进行了测试,然后像示例中一样搜索了C01A pc。 but it got stuck as well. 但它也卡住了。

What am I doing wrong? 我究竟做错了什么? Can this be done even faster when I realy have to go trough a lot of pc's? 当我真的要花很多时间去做PC时,可以更快地完成此操作吗?

如果i为零,并且尝试将其减半,或者i * 2大于n ,则找不到该结构。

What you trying to implement is binary search. 您尝试实现的是二进制搜索。 Your implementation is not correct. 您的实现不正确。 It should be similar to this: 它应该类似于:

/*
* searches for a value in sorted array
*   arr is an array to search in
*   value is searched value    
*   left is an index of left boundary
*   right is an index of right boundary
* returns position of searched value, if it presents in the array
* or -1, if it is absent
*/

int binarySearch(int arr[], int value, int left, int right) {
      while (left <= right) {
            int middle = (left + right) / 2;
            if (arr[middle] == value)
                  return middle;
            else if (arr[middle] > value)
                  right = middle - 1;
            else
                  left = middle + 1;
      }
      return -1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM