使用指针算术删除子阵列
我需要在C中使用指针算术来删除子阵列的函数C。功能应返回删除元素的数量。不允许辅助阵列。
#include <stdio.h>
int remove_subarray(int * first_start, int * first_end,const int * second_start,const int * second_end) {
int size_of_second = second_end-second_start;
int *subarray_start, *last = first_end - 1;
const int *pok = second_start,*second_start_copy = second_start;
int number_of_the_same = 0;
while (first_start != first_end) {
if ( * first_start == * second_start) {
if (number_of_the_same == 0)
subarray_start = first_start;
first_start++;
second_start++;
number_of_the_same++;
if (number_of_the_same == size_of_second) {
first_start = subarray_start;
while (1) {
if ( *first_start == *last)
break;
subarray_start = first_start;
subarray_start += size_of_second;
*first_start = *subarray_start;
first_start++;
}
break;
}
} else {
number_of_the_same = 0;
first_start++;
second_start = second_start_copy;
}
}
return size_of_second;
}
int main() {
// This gives correct result
int niz1[14] = {1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, -1},i;
int niz2[4] = {2, 3, 4, 5};
int k1 = remove_subarray(niz1, niz1 + 14, niz2, niz2 + 4);
for (i = 0; i < 14 - k1; ++i)
printf("%i ", niz1[i]);
printf("\n");
// This gives wrong result
int niz3[10] = {1, 1, 2, 3, 5, 6, 1, 2, 4, 10};
int niz4[3] = {1, 2, 3};
int k2 = remove_subarray(niz3, niz3 + 10, niz4, niz4 + 3);
for (i = 0; i < 10 - k2; i++) printf("%d ", niz3[i]);
return 0;
}
我的算法如下:
- 如果元素匹配,则将位置保存到指针 start
- 如果number_of_the_same(elements)等于第二个数组中的元素数量(n)(n)中的元素数量,则意味着
- 如果找到子阵列,则发现子阵列,i将所有元素设置为等于
在我尝试使用两组数组( niz1和niz2 )的主函数中向它们转发它们的元素,并且对于第一组,它的工作正常。但是,对于第二组数组( niz3和niz4 ),它不正确。
您能帮我修复我的代码吗?
I need to make a function for removing subarray using pointer arithmetic in C. Function should return number of removed elements. Auxiliary arrays are not allowed.
#include <stdio.h>
int remove_subarray(int * first_start, int * first_end,const int * second_start,const int * second_end) {
int size_of_second = second_end-second_start;
int *subarray_start, *last = first_end - 1;
const int *pok = second_start,*second_start_copy = second_start;
int number_of_the_same = 0;
while (first_start != first_end) {
if ( * first_start == * second_start) {
if (number_of_the_same == 0)
subarray_start = first_start;
first_start++;
second_start++;
number_of_the_same++;
if (number_of_the_same == size_of_second) {
first_start = subarray_start;
while (1) {
if ( *first_start == *last)
break;
subarray_start = first_start;
subarray_start += size_of_second;
*first_start = *subarray_start;
first_start++;
}
break;
}
} else {
number_of_the_same = 0;
first_start++;
second_start = second_start_copy;
}
}
return size_of_second;
}
int main() {
// This gives correct result
int niz1[14] = {1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, -1},i;
int niz2[4] = {2, 3, 4, 5};
int k1 = remove_subarray(niz1, niz1 + 14, niz2, niz2 + 4);
for (i = 0; i < 14 - k1; ++i)
printf("%i ", niz1[i]);
printf("\n");
// This gives wrong result
int niz3[10] = {1, 1, 2, 3, 5, 6, 1, 2, 4, 10};
int niz4[3] = {1, 2, 3};
int k2 = remove_subarray(niz3, niz3 + 10, niz4, niz4 + 3);
for (i = 0; i < 10 - k2; i++) printf("%d ", niz3[i]);
return 0;
}
My algorithm is the following:
- if elements match, save position to pointer start
- if number_of_the_same (elements) is equal to number of elements in second array (n) that means subarray is found
- if subarray is found, I set all elements to be equal to the elements that are n positions forward them
In the main function I tried with two set of arrays (niz1 and niz2) and for the first set it worked correct. However it didn't work correct for second set of arrays (niz3 and niz4).
Could you help me to fix my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
提供的代码很难阅读,因此也很难测试。至少对我来说。可能是作者可以使用更有意义的名称。
我认为原始代码中的错误是,在找到sub_array的第一个数字之后,如果搜索失败,则程序 不得推进数组的指针,因为当前值指向成为序列的真实开始,而前一个只是假阳性。在第二个提供的集合中,请参见Pair
1,1
,我将在一个示例中提供2个可能会有所帮助的选项。
该方法在示例中的
想法是
c ++ stl
中的迭代器:第一个参数指向第一个元素,第二个参数点超过数组的末尾,remove_subarray()
将整个sub_array移至数组末尾mark_subarray()
int_max
替换所有sub_array值
使事物更容易:很少有助手函数
int show_array(const int*,const int*,const char*);
此功能具有5行:只需写下带有可选标题的数组,例如此处
或此处的
样本输出:
或
<代码> int*find_sub_array(const int*,const int*,const int*,const int*);
返回
null
如果在数组中找不到sub_array或sub_array。这种类型的事物很容易由状态机A fsm 表达。在这里,我们需要一组2种国家的简约集:
可能的实现
remove> remove_subarray()
使用这些功能,可以写
remove_subarray()
以紧凑的方式测试多个版本
以上版本更改sub_array值。示例代码中的另一个将sub_array移至末尾。无论如何,这只是一个例子。
这个接受了应用于参数的函数的名称,例如 c ++ 或 java in_each() 。它在这样的示例中使用:
示例代码
示例代码
tl; dr
第二个示例仅适用于原始测试用例的代码,
find_array()
示例2
输出
The provided code is very hard to read and so is also hard to test. At least for me. May be the author could have used more meaningful names.
I think that the bug in the original code is that, after finding the first number of the sub_array, if the search fails the program must not advance the pointer of the array, because the current value pointed to can be the real start of the sequence and the previous just a false positive. See the pair
1,1
in the second supplied setI will let an example with 2 options that may help.
The method in the example
The idea is that
C++ STL
: first argument points to first element, second argument points past the end of the arrayremove_subarray()
that moves the entire sub_array to the end of the arraymark_subarray()
that replaces all sub_array values byINT_MAX
Making things easier: a few helper functions
int show_array(const int*, const int*, const char*);
This function has 5 lines: just write down the array with an optional title like here
or here
sample output:
or
int* find_sub_array(const int*, const int*, const int*, const int*);
returns
NULL
if the sub_array is not found in the array, or the address of the sub_array.This type of things are easily expressed by a FSM, a state machine. Here we need a minimalist set of 2 states:
A possible implementation
remove_subarray()
Using these functions one can write
remove_subarray()
in a compact wayTesting multiple versions
The version above changes sub_array values. Another one in the example code moves the sub_array to the end. It is just an example anyway.
This one accepts the name of the function to apply to the parameters, like a
for_each()
in C++ or java. It is used in the example like this:output for the example code
Example code
TL;DR
The second example has just code for the original test cases and
find_array()
Example 2
Output
您的算法有一个小错误,它仅比较子阵列的第一项与数组,如果匹配,则假定这是子阵列的开始,而无需看到以下项目。
标题文件
删除子数组的功能
主函数
注意:指针算术也计数
** (arr + i)
There is a little mistake in your algorithm, It only compares the first item of the subarray to the array and if it matches it assumes that this is the starting of the subarray without seeing the following items.
header files
function to remove sub array
main function
Note: Pointer Arithmetic also counts
*(arr + i)
您可能需要利用内存功能(memcmp,memcpy),该功能可以加快代码并创建更优雅的解决方案。我不确定“使用指针算术”是否暗示不允许arr_start [i]。如果是这种情况,则应用 *(x+i)代替x [i]的每个引用,从而有效地用等效的指针算术代替索引。
免责声明:我没有编译/测试。可能有错别字。
You might want to take advantage of memory function (memcmp, memcpy), which can speed up the code, and create more elegant solution. I'm not sure if "using pointer arithmetic" implied that arr_start[i] is not allowed. If this is the case, then every reference to X[i] should be replaced with *(X+i), effectively replacing indexing with the equivalent pointer arithmetic.
Disclaimer: I did not compile/test. Possible that there are typos.