错误:函数'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”的时候改变有哪些? 对不起我的变量是在德国,我希望这不是一个问题;)
答
您有传递一个参数,也许是这样的:
double eval_time(int(*ptr)(int), int zahl){
time_t begin,end;
begin=time(NULL);
(*ptr)(zahl);
-------^
end=time(NULL);
return difftime(end,begin);
}
的另一个问题是,你可能需要调用在begin
和end
之间运行很多次,实际上在time()
值中得到了可测量的差异。
你在'eval_time'中的函数指针需要'int'参数。你需要提供这个论点。就像错误在说。确切地说*你应该通过什么取决于你。 –