2014-03-05 55 views
0

我想循环遍历数组的元素,对于这些元素中的每一个,我必须调用一个指向的函数,并将元素地址与存储的地址一起传递在最后一个参数中。该函数返回函数指向的元素的数量返回true。这是我一直努力遵循的要求,但我不能让我的功能做的到底是什么要求做..第一个元素指向的数组移动通过元素的函数

------要求&功能------- -----

/* Write an enumeration function named sum() with the following parameters: 

    a generic pointer 
    an int that holds the number of elements in the array pointed to 
    an int that holds the size in bytes of a single element 
    a pointer to a function that has two generic pointer parameters and returns a bool 
    a generic pointer 

Your function moves through the array pointed to by the first parameter element by element. 
For each element, your function calls the function pointed to and passes the element's address along 
with the address stored in the last parameter. Your function returns the number of elements for 
which the function pointed to returned true. 

Since your first function parameter is a generic pointer and your function can handle any type, 
you will need to cast the address of the input array to the address of a chars in order to move 
from one element to the next. */ 

INT总和(无效* X,INT N,int类型,布尔(F)(空隙,无效*),无效* Z){

char *arr = static_cast<char*>(x); 

    int count = 0; 
    for (; s < n-2; s++){ 
     arr += s; 
     count += f(arr, z);  
    } 
    if (n/1 == n) 
     return count; 
    else if (n % 2 == 0) 
     return count; 
    else 
     return 0; 

}

我希望你们能告诉我,解释或者至少是我不是正确的方式做..我会很感激这是唯一的办法,我能学到.. :)

如果需要更多的解释,请让我知道..

** * ***加成* ** * ***

/* Write a callback function named isEven() with the following parameters: 

a generic pointer to an input value 
a generic pointer to an output value 

Your function works with ints and returns true if the input value is even, false otherwise. 
Moreover, if the value is even, your function adds the value to that pointed to by the second 
parameter. */ 

bool isEven(void* x, void* z){ 
    int a = *static_cast<int*>(x); 
    int b = *static_cast<int*>(z); 

    if (a % 2 == 0){ // finding even numbers 
     b += a; 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

/* Write another callback function named isPrime() with the following parameters: 

a generic pointer to an input value 
a generic pointer to an output value 

Your function works with ints and returns true if the input value is a prime number, false otherwise. 
Moreover, if the value is prime, your function adds the value to that pointed to by the second 
parameter. */ 

bool isPrime(void* x, void* z){ 
    int a = *static_cast<int*>(x); 
    int b = *static_cast<int*>(z); 

    if ((a/1 == a) && (a/a == 1)){ // finding prime numbers 
     b += a; 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

** * ** * ****预期输出* ** * ** * ** * *

5 evens found in {1,2,3,4,5,6,7,8,9,10,11} sum is 30 
5 primes found in {1,2,3,4,5,6,7,8,9,10,11} sum is 28 
+0

在C++世界中,不要使用void指针而不是函数指针。有更好的方法(模板/虚拟方法) –

+1

@EdHeal,但显然他必须 – Paranaix

+0

是的,我必须以这种方式使用它.. – NorthBlast

回答

2

如果我已经明白正确的功能将如下

int sum(const void* x, int n, int s, bool(*f)(const void*, const void*), const void* z) 
{ 
    const char *p = reinterpret_cast<const char *>(x); 

    int count = 0; 
    for (int i = 0; i < n; i++) 
    { 
     count += f(p, z); 
     p += s; 
    } 

    return count; 
} 

下面是使用功能

#include <iostream> 
#include <cstdlib> 
#include <ctime> 

int sum(const void* x, int n, int s, bool(*f)(const void*, const void*), const void* z) 
{ 
    const char *p = reinterpret_cast<const char *>(x); 

    int count = 0; 
    for (int i = 0; i < n; i++) 
    { 
     count += f(p, z); 
     p += s; 
    } 

    return count; 
} 

bool lt(const void *p1, const void *p2) 
{ 
    return (*reinterpret_cast<const int *>(p1) < 
      *reinterpret_cast<const int *>(p2)); 
} 

int main() 
{ 
    std::srand((unsigned int)std::time(0)); 

    const int N = 10; 
    int a[N]; 

    for (int &x : a) x = std::rand() % N; 

    for (int x : a) std::cout << x << ' '; 
    std::cout << std::endl; 

    int x = 5; 

    int n = sum(a, N, sizeof(int), lt, &x); 

    std::cout << "There are " << n << " elements less than " << x << std::endl; 

    return 0; 
} 

样品输出的例子

4 7 2 9 2 8 6 1 9 9 
There are 4 elements less than 5 
+0

感谢您抽出时间并回复我的问题......我只是添加了通过函数指针调用的另外两个函数,并且还添加了描述他们应该这样做..我用你的答案来指导自己,并修改了一些我的程序,但我没有得到元素的总和..我认为这发生在两个布尔功能..我希望你能看到我错过了什么.. – NorthBlast

-1
char* arr = (char*) x; 

for(; s < (n-1); s++) 
{ 
    f((void*) &arr[s], z); 
    // or 
    f((void*) (arr + s * sizeof(char)), z); 
} 
+0

我将如何使用它的迭代.. ?? – NorthBlast

+0

@NorthBlast查看我的编辑。但是请注意,这是非常糟糕的设计(正如Ed Heal所提到的) – Paranaix

相关问题