【问题标题】:Interpolation Search with Strings使用字符串进行插值搜索
【发布时间】:2020-05-08 09:48:39
【问题描述】:

您好,我正在尝试使用字符串进行插值搜索。我的字符串洞察数组就像这些 2014-02-13T23:50:00。

void SearchInterpolation(char *data[],float data2[],char *data3[],int data4[], int n, char searchKey[]){

    long long int position, low, high,ResultKey,ResultLow,ResultHigh;
    low = 0;
    high = n-1;
    ResultKey = SearchKey(searchKey); // ResultKey = searchKey but integer
    do{

        ResultLow = SearchKey(data[low]); // ResultLow = searchKey but integer
        ResultHigh =SearchKey(data[high]); // ResultHigh = searchKey but integer
        position =low + (ResultKey - ResultLow) * (high-low+1) / (ResultHigh - ResultLow);
        if(strcmp(data[position], searchKey) == 0)
           break;
        else if(strcmp(data[position], searchKey) > 0)
         high = position - 1;
        else
         low = position + 1;
    } while ((strcmp(searchKey, data[low])>0) && strcmp(searchKey, data[high]) < 0);
    if ((strcmp(searchKey,data[position]))==0)              //if string is found
          {
          printf("The temp you are looking is: %.2f \n",data2[position]);
          } 


    low = 0;
    high = n-1;
    do{

        ResultLow = SearchKey(data3[low]); // ResultLow = searchKey but integer
        ResultHigh =SearchKey(data3[high]); // ResultHigh = searchKey but integer
        position = (ResultKey - ResultLow) * (high-low+1) / (ResultHigh - ResultLow) + low;   
        if(strcmp(data3[position], searchKey) == 0)
         break;
        else if(strcmp(data3[position], searchKey) > 0)
         high = position - 1;
        else
         low = position + 1;
    } while ((strcmp(searchKey, data3[low])>0) && strcmp(searchKey, data3[high]) < 0);

    if ((strcmp(searchKey,data3[position]))==0)              //if string is found
          {
          printf("The hum you are looking is: %d \n",data4[position]);
          } 
    else 
          printf("SEARCH FAILED \n");
}

函数 SearchKey 获取文本并将其转换为 long long int 例如 2014-02-13T23:50:00 --> 20140213235000。

我的问题是:

a)不适用于数组中已包含的每个时间戳

b)当我给一些随机的东西,例如像 dasd123 这样的文本时,程序不会说 SearchFailed

【问题讨论】:

    标签: c


    【解决方案1】:

    你好,几个小时后我再次找到了解决方案。如果有人对此感兴趣,这里就是解决方案

    void SearchInterpolation(char *data[],float data2[],char *data3[],int data4[], int n, char searchKey[]){
    
        long long int ResultKey,ResultLow,ResultHigh;
        int position,low,high;
        low = 0;
        high = n-1;
        ResultLow = SearchKey(data[low]); // ResultLow = searchKey but integer
        ResultHigh =SearchKey(data[high]); // ResultHigh = searchKey but integer
        ResultKey = SearchKey(searchKey); // ResultKey = searchKey but integer
        if (ResultKey>=ResultLow && ResultKey<=ResultHigh){
        do{
    
            ResultLow = SearchKey(data[low]); // ResultLow = searchKey but integer
            ResultHigh =SearchKey(data[high]); // ResultHigh = searchKey but integer
            position =low + (ResultKey - ResultLow) * (high-low) / (ResultHigh - ResultLow);
            if(strcmp(data[position], searchKey) > 0)
             high = position - 1;
            else if(strcmp(data[position], searchKey) < 0)
             low = position + 1;
            else
                low = position;
        } while ((strcmp(searchKey, data[position])>0) && (strcmp(searchKey, data[high]) < 0) && (strcmp(data[position], searchKey) != 0));
        }
        if ((strcmp(searchKey,data[position]))==0)              //if string is found
              {
              printf("The temp you are looking is: %.2f \n",data2[position]);
              } 
        low = 0;
        high = n-1;
        ResultLow = SearchKey(data3[low]); // ResultLow = searchKey but integer
        ResultHigh =SearchKey(data3[high]); // ResultHigh = searchKey but integer
        if (ResultKey>=ResultLow && ResultKey<=ResultHigh){
        do{
            ResultLow = SearchKey(data3[low]); // ResultLow = searchKey but integer
            ResultHigh =SearchKey(data3[high]); // ResultHigh = searchKey but integer
            position = (ResultKey - ResultLow) * (high-low) / (ResultHigh - ResultLow) + low;   
            if(strcmp(data3[position], searchKey) > 0)
             high = position - 1;
            else if(strcmp(data3[position], searchKey) < 0)
             low = position + 1;
            else
                low=position;
        } while ((strcmp(searchKey, data3[position])>0) && (strcmp(searchKey, data3[high]) < 0) && (strcmp(data3[position], searchKey)!=0));
    
        if ((strcmp(searchKey,data3[position]))==0)              //if string is found
              {
              printf("The hum you are looking is: %d \n",data4[position]);
              } 
       }
       else 
              printf("SEARCH FAILED \n");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-09
      • 2013-08-09
      • 2015-12-12
      • 2014-03-06
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多