这个贴发在C版一天了还是无人问津,一阵郁闷!
如果大家赏脸的话,帮忙解答一下吧,也顺便去http://expert.csdn.net/Expert/topic/2111/2111282.xml?temp=.5758325
蹭分吧!唉!
/*
定积分求解。
程序如下。调试信息见最后注释。
1.调试信息中的“__cdecl”是什么意思?看不懂?
2.warning 中的'double' to 'float'是怎么回事?我没有把double转成float啊?!
3.程序还应该如何修改才能正常运行?我的程序错在什么地方?*/#include <stdio.h>
#include <math.h>float integral(float (*p),float a,float b,int n);
float fsin(float);/*声明fsin函数*/
float fcos(float);/*声明fcos函数*/
float fexp(float);/*声明fexp函数*/void main()
{
float a1,b1,a2,b2,a3,b3,c,(*p)(float);
int n=20;
printf("Input a1,b1:"); /*输入求 sin x 定积分的下限和上限*/
scanf("%f,%f",&a1,&b1);
printf("Input a2,b2:");/*输入求 cos x 定积分的下限和上限*/
scanf("%f,%f",&a2,&b2);
printf("Input a2,b2:");/*输入求exp(x)定积分的下限和上限*/
scanf("%f,%f",&a3,&b3);
p=fsin;
c=integral(p,a1,b1,n); /*求出sinx的定积分*/
printf("The integral of sin(x) is :%f\n",c);
p=fcos;
c=integral(p,a2,b2,n);/*求出cosx的定积分*/
printf("The integral of cos(x) is :%f\n",c);
p=fexp;
c=integral(p,a3,b3,n);/*求出exp(x)的定积分*/
printf("The integral of exp(x) is :%f\n",c);
}float integral(float (*p)(float),float a,float b,int n)/*用矩形法求定积分的通用函数*/
{ int i;
float x,h,s;
h=(b-a)/n;
x=a;
s=0;
for(i=1;i<=n;i++)
{ x=x+h;
s=s+(*p)(x)*h;
}
return s;
}float fsin(float x)
{ return sin(x);
}float fcos(float x)
{ return cos(x);
}float fexp(float x)
{ return exp(x);
}/*===========================*/
/*
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
D:\Documents and Settings\Administrator\My Documents\test\test\test.cpp(30) : error C2664: 'integral' : cannot convert parameter 1 from 'float (__cdecl *)(float)' to 'float *'
        There is no context in which this conversion is possible
D:\Documents and Settings\Administrator\My Documents\test\test\test.cpp(33) : error C2664: 'integral' : cannot convert parameter 1 from 'float (__cdecl *)(float)' to 'float *'
        There is no context in which this conversion is possible
D:\Documents and Settings\Administrator\My Documents\test\test\test.cpp(36) : error C2664: 'integral' : cannot convert parameter 1 from 'float (__cdecl *)(float)' to 'float *'
        There is no context in which this conversion is possible
D:\Documents and Settings\Administrator\My Documents\test\test\test.cpp(54) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
D:\Documents and Settings\Administrator\My Documents\test\test\test.cpp(58) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
D:\Documents and Settings\Administrator\My Documents\test\test\test.cpp(62) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
Error executing cl.exe.test.obj - 3 error(s), 3 warning(s)
*/

解决方案 »

  1.   

    //float integral(float (*p),float a,float b,int n);
    float integral(float (*p)(float),float a,float b,int n);float fsin(float);/*声明fsin函数*/
    float fcos(float);/*声明fcos函数*/
    float fexp(float);/*声明fexp函数*/
      

  2.   

    float integral(float (*p),float a,float b,int n);请问,你的第一个参数究竟是什么类型的?
      

  3.   

    1、其实我估计第一个float integral(float (*p),float a,float b,int n);
    实际上是float integral(float (*p)(),float a,float b,int n);的错误。因为float (*p)实际上是一个float指针不是函数指针,float (*p)()才是函数指针。但是也要改成
    float integral(float (*p)(float),float a,float b,int n)
    这是因为 C 与 C++ 在解释函数声明中的空白括号方面存在着根本的不同。C 中的函数声明方式如下: int (*funcptr)();
    它声明一个接受未知数目的参数的函数。在 C++ 中,这一声明却表示一个不接受任何参数的函数。换言之,该语句在 C++ 中等同于: int (*funcptr)(void);
    2、要把
    return sin(x);改成return sinf(x);return cos(x);改成return cosf(x);
    return exp(x);改成return expf(x);
      

  4.   

    调试中的“__cdecl”是什么意思谁能跟我解释一下?
    我看不懂那个意思啊
      

  5.   

    “__cdecl”是函数调用的方式,论坛上有很多相关的帖子,自己查询一下吧
      

  6.   

    哈,论坛的top不是有刷新|搜索
    点搜索啊
      

  7.   

    Microsoft SpecificThis is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions. The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code. The following list shows the implementation of this calling convention.Element Implementation 
    1.Argument-passing <-->order Right to left 2.Stack-maintenance responsibility <-->Calling function pops the arguments from the stack 3.Name-decoration convention <-->Underscore character (_) is prefixed to names 4.Case-translation convention <-->No case translation performed Note   For related information, see Decorated Names. 
    Place the __cdecl modifier before a variable or a function name. Because the C naming and calling conventions are the default, the only time you need to use __cdecl is when you have specified the /Gz (stdcall) or /Gr (fastcall) compiler option. The /Gd compiler option forces the __cdecl calling convention.Example
    In the following example, the compiler is instructed to use C naming and calling conventions for the system function:// Example of the __cdecl keyword on function
    _CRTIMP int __cdecl system(const char *);
    // Example of the __cdecl keyword on function pointer
    typedef BOOL (__cdecl *funcname_ptr)(void * arg1, const char * arg2, DWORD flags, ...);
    END Microsoft SpecificSee Also
    Argument Passing and Naming Conventions | C++ Keywords