主题:函数指针的问题
题目要求 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
#include <iostream.h>
double oqiuhe(double);
double qqiuhe(double);
double qiuhe(double(*p)(),double n){
double s;
s=double(*p)(n);
return s;
}
double oqihe(double n){
double i;
double sum1=0;
for(i=2;i<=n;i+=2){
sum1+=1/i;
}
return sum1;
}
double qqiuhe(double n){
double i;
double sum2=0;
for(i=1;i<=n;i+=2){
sum2+=1/i;
}
return sum2;
}
void main(){
double n;
double su=0;
cout<<"Please give the Zahl n:"<<endl;
cin>>n;
if(n%2==0) su=qiuhe(oqiuhe,n);
else su=qiuhe(qqiuhe,n);
cout<<"The sum is "<<su<<endl;
}
C:\Dokumente und Einstellungen\CONG\uebung.cpp(407) : error C2440: 'type cast' : cannot convert from 'double (__cdecl *)(void)' to 'double'
There is no context in which this conversion is possible
C:\Dokumente und Einstellungen\CONG\uebung.cpp(435) : error C2296: '%' : illegal, left operand has type 'double'
C:\Dokumente und Einstellungen\CONG\uebung.cpp(435) : error C2664: 'qiuhe' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(void)'
None of the functions with this name in scope match the target type
C:\Dokumente und Einstellungen\CONG\uebung.cpp(436) : error C2664: 'qiuhe' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(void)'
None of the functions with this name in scope match the target type
不知道错误出在哪里,还请指教
#include <iostream.h>
double oqiuhe(double);
double qqiuhe(double);
double qiuhe(double(*p)(),double n){
double s;
s=double(*p)(n);
return s;
}
double oqihe(double n){
double i;
double sum1=0;
for(i=2;i<=n;i+=2){
sum1+=1/i;
}
return sum1;
}
double qqiuhe(double n){
double i;
double sum2=0;
for(i=1;i<=n;i+=2){
sum2+=1/i;
}
return sum2;
}
void main(){
double n;
double su=0;
cout<<"Please give the Zahl n:"<<endl;
cin>>n;
if(n%2==0) su=qiuhe(oqiuhe,n);
else su=qiuhe(qqiuhe,n);
cout<<"The sum is "<<su<<endl;
}
C:\Dokumente und Einstellungen\CONG\uebung.cpp(407) : error C2440: 'type cast' : cannot convert from 'double (__cdecl *)(void)' to 'double'
There is no context in which this conversion is possible
C:\Dokumente und Einstellungen\CONG\uebung.cpp(435) : error C2296: '%' : illegal, left operand has type 'double'
C:\Dokumente und Einstellungen\CONG\uebung.cpp(435) : error C2664: 'qiuhe' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(void)'
None of the functions with this name in scope match the target type
C:\Dokumente und Einstellungen\CONG\uebung.cpp(436) : error C2664: 'qiuhe' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(void)'
None of the functions with this name in scope match the target type
不知道错误出在哪里,还请指教