使用结构对象进行二进制搜索操作
Using struct object for a binary search operation
我正在尝试将结构对象用于二进制搜索计数函数,但我正在尝试编译代码,但不幸的是。
二分搜索计数函数正在做的是计算在达到限制之前存在的值的数量。我必须使用结构而不是 类,因为代码正在进入微控制器。
我尝试编译代码时遇到的错误是“无法将参数 1 从 'volatile float' 转换为 float[]”
#include <iostream>
using namespace std;
#define DLS_MAX_DATAPOINTS 1200;
typedef struct TEST_SAMPLE
{
float loadcell; // loadcell (N)
} TEST_SAMPLE;
// See documentation in DATALOG.C for details
typedef struct BRAKETEST
{
int dataflag; // flags indicating what data is stored
int dataPointsCount; // no of sample collected during test
TEST_SAMPLE dataPoints[]; // data collection of sampled points
} BRAKETEST;
volatile BRAKETEST braketest; // test object holding test data
int binarySearchCount(float arr[], float n, float key)
{
int left = 0, right = n;
int mid;
while (left < right)
{
mid = (right + left) >> 1;
// Check if key is present in array
if (arr[mid] == key)
{
// If duplicates are present it returns
// the position of last element
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
// If key is smaller, ignore right half
else if (arr[mid] > key)
right = mid;
// If key is greater, ignore left half
else
left = mid + 1;
}
// If key is not found in array then it will be
// before mid
while (mid > -1 && arr[mid] > key)
mid--;
// Return mid + 1 because of 0-based indexing
// of array
return (mid + 1);
}
int main()
{
int braketest_trig_level_pedal = 30;
int count = 0;
double n = sizeof(braketest.dataPoints[count].loadcell) / sizeof(braketest.dataPoints[0].loadcell);
float results = binarySearchCount(braketest.dataPoints[count].loadcell, n, braketest_trig_level_pedal); // braketest.dataPoints[count].loadcell
}
您必须更改类型:
int binarySearchCount(volatile TEST_SAMPLE arr[], float n, float key)
binarySearchCount(braketest.dataPoints, n, braketest_trig_level_pedal);
并将对 arr[mid]
的访问权限更改为 arr[mid].loadcell
,例如:
if (arr[mid].loadcell == key)
那你的尺码计算有误:
sizeof(braketest.dataPoints[count].loadcell) / sizeof(braketest.dataPoints[0].loadcell);
应该是:
double n = sizeof(braketest.dataPoints) / sizeof(TEST_SAMPLE);
而不是 double
,因为 binarySearchCount
期望 float
。
通过这些更改,它将 return 搜索到的元素 +1(因为 return (mid + 1)
如果它找到键或最后一个索引(再次 +1)。
我正在尝试将结构对象用于二进制搜索计数函数,但我正在尝试编译代码,但不幸的是。
二分搜索计数函数正在做的是计算在达到限制之前存在的值的数量。我必须使用结构而不是 类,因为代码正在进入微控制器。
我尝试编译代码时遇到的错误是“无法将参数 1 从 'volatile float' 转换为 float[]”
#include <iostream>
using namespace std;
#define DLS_MAX_DATAPOINTS 1200;
typedef struct TEST_SAMPLE
{
float loadcell; // loadcell (N)
} TEST_SAMPLE;
// See documentation in DATALOG.C for details
typedef struct BRAKETEST
{
int dataflag; // flags indicating what data is stored
int dataPointsCount; // no of sample collected during test
TEST_SAMPLE dataPoints[]; // data collection of sampled points
} BRAKETEST;
volatile BRAKETEST braketest; // test object holding test data
int binarySearchCount(float arr[], float n, float key)
{
int left = 0, right = n;
int mid;
while (left < right)
{
mid = (right + left) >> 1;
// Check if key is present in array
if (arr[mid] == key)
{
// If duplicates are present it returns
// the position of last element
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
// If key is smaller, ignore right half
else if (arr[mid] > key)
right = mid;
// If key is greater, ignore left half
else
left = mid + 1;
}
// If key is not found in array then it will be
// before mid
while (mid > -1 && arr[mid] > key)
mid--;
// Return mid + 1 because of 0-based indexing
// of array
return (mid + 1);
}
int main()
{
int braketest_trig_level_pedal = 30;
int count = 0;
double n = sizeof(braketest.dataPoints[count].loadcell) / sizeof(braketest.dataPoints[0].loadcell);
float results = binarySearchCount(braketest.dataPoints[count].loadcell, n, braketest_trig_level_pedal); // braketest.dataPoints[count].loadcell
}
您必须更改类型:
int binarySearchCount(volatile TEST_SAMPLE arr[], float n, float key)
binarySearchCount(braketest.dataPoints, n, braketest_trig_level_pedal);
并将对 arr[mid]
的访问权限更改为 arr[mid].loadcell
,例如:
if (arr[mid].loadcell == key)
那你的尺码计算有误:
sizeof(braketest.dataPoints[count].loadcell) / sizeof(braketest.dataPoints[0].loadcell);
应该是:
double n = sizeof(braketest.dataPoints) / sizeof(TEST_SAMPLE);
而不是 double
,因为 binarySearchCount
期望 float
。
通过这些更改,它将 return 搜索到的元素 +1(因为 return (mid + 1)
如果它找到键或最后一个索引(再次 +1)。