错误:函数'ptr'的参数太少,需要函数的eval_time

问题描述:

我的程序有一个小问题。 我想从函数中评估时间,编译器给我一个错误。 我知道,我们的问题是,但我只是不知道如何解决它:/错误:函数'ptr'的参数太少,需要函数的eval_time

#include <stdio.h> 
#include <time.h> 

int isPerfect(int zahl){ 
int zaehler1, zaehler2, summe; 
for(zaehler1=1;zaehler1<=zahl;zaehler1++){ 
    summe=0; 
    for(zaehler2=1;zaehler2<=zaehler1/2;zaehler2++){ 
     if(zaehler1%zaehler2==0){ 
     summe=summe+zaehler2; 
     } 
    } 
} 
return summe; 
} 

double eval_time(int(*ptr)(int)){ 
    time_t begin,end; 
    begin=time(NULL); 
    (*ptr)(); //compiler shows error here! 
    end=time(NULL); 
    return difftime(end,begin); 
    } 

int main(void){ 
    int zahl; 
    for(zahl=1;zahl<=500;zahl++){ 
     if(isPerfect(zahl)==zahl){ 
     printf("%d ist eine perfekte Zahl!\n", zahl); 
     } 
    } 
    printf("Die Zeit die gebraucht wurde: %.2lf s\n",eval_time(isPerfect)); 
return 0; 
} 

所以我的问题是我需要所以evals从funtion“isPerfect”的时候改变有哪些? 对不起我的变量是在德国,我希望这不是一个问题;)

+3

你在'eval_time'中的函数指针需要'int'参数。你需要提供这个论点。就像错误在说。确切地说*你应该通过什么取决于你。 –

传递一个参数,也许是这样的:

double eval_time(int(*ptr)(int), int zahl){ 
    time_t begin,end; 
    begin=time(NULL); 
    (*ptr)(zahl); 
    -------^ 
    end=time(NULL); 
    return difftime(end,begin); 
} 

的另一个问题是,你可能需要调用在beginend之间运行很多次,实际上在time()值中得到了可测量的差异。

+2

也'ptr(zahl)'只会做。 – alk

+0

好的,我现在可以编译,但输出总是0.00秒,即使程序超过1分钟以获得低谷时,我将zahl slauko

+1

@slauko:如何运行代码在调试器内部逐行跟踪程序,检查相关变量的值,以了解真正发生的事情? – alk