【发布时间】: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