pascal中如何实现标准函数作为参数.(50分)

G

gzxie

Unregistered / Unconfirmed
GUEST, unregistred user!
pascal中如何实现标准函数作为参数.
我已经实现自定义函数作为参数,但pascal中说标准函数也可以作为参数,
我试了一下,即使在两个函数的参数和返回类型完全一样时,标准函数作为
参数也要出错,这是为什么?
 
你说的是这个意思吗?用过程类型实现?

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type
myfunc=function(mypara:Integer):string;

var
Form1: TForm1;
a:myfunc;

function aaa(aa:myfunc):string;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
a:=IntToStr;
Caption:=aaa(a);
end;

function aaa(aa:myfunc):String
begin
Result:=aa(10);
end;

end.
 
程序如下:我想用integ过程调用标准的sin函数就不行,自己定义的就可以哟!
program integral(input,output);

type func=function(x:real):real;

function y(x:real):real;
begin

y:=1/(1+x);
end;

function sin1(x:real):real;
begin
sin1:=sin(x);
end;


procedure integ(operation:func
a,b:real;var inte:real);
const n=100;
var
s,w:real;
i:integer;

begin
w:=(b-a)/n;
s:=(operation(a)+operation(b))/2;
for i:=1 to n-1 do
s:=s+operation(a+i*w);
inte:=w*s;
end;

var a0,b0,inte0:real;

begin
writeln('Please input a and b:');
readln(a0,b0);
writeln('The following counts the integal of y=1/(1+x) from ');
writeln(a0:10:2,' to ',b0:10:2,':');
integ(y,a0,b0,inte0);
writeln('Throught method we get inte0=',inte0:10:2);
writeln('The following counts the integal of y=sin(x) from ',a0:10:2,' to ',b0:10:2,':');
{注意下面哟!}
integ(sin1,a0,b0,inte0);{此处将sin1改为sin就出错}

writeln('Throught method we get inte0=',inte0:10:2);

end.

 
你忘了uses XXXX了。
 
恩,问题基本就是这样了,没什么好说的了,实际上就是使用函数指针,不过
一般要事先知道函数的参数。
 
uses math;
 
不会吧,上面用了sin,不会是没use。是否是只有自定义的function地址才可传递
呢?这一点应该是可以理解的。
 
uses math;
试试
integ(sin,a0,b0,inte0);
不行就试试:
integ(@sin,a0,b0,inte0);
两个肯定有一个可行.
 
我试了一下,好象还是不行啊!包括加@符!
 
老兄,
math中根本就没有sin,
只有sincos,tan等,
当然不行,
不信你用tan试试!
 
amo说法错误!pascal中有标准函数sin,无须用use math之类什么的!
 
pascal中是有标准函数sin,
在Delphi中,Sin是定义在system中的。
Math中确实没有sin.
 
hehe, 没错,
Sin returns the sine of the angle in radians.

Unit

System
加上: use system再试试:)
 
我都快发疯了,如此凭空建议,为什么不自己试通了再建议呢,难道如此在乎这点分?!
 
第一次回答时我没试,后几次是试过了才答的。
 
没用, 在delphi中你无法找到sin, cos, trunc, round, frac等等函数的定义.
因为这些函数原型在IDE中, 是依靠编译器的魔力实现的(也就是说@sin不
存在).
可以试试integ(@_SIN,a0,b0,inte0)
(_SIN是sin函数的汇编原型, 在system
中定义, 类似函数还有一大堆).
如果不行, 只能用自定义函数了.
 
出什么错误了,能说一下吗?
我总的感觉象这种标准函数地址是无法传递的,sin是否是属于vcl的一部分呢?
 
呵呵,用sincos 吧
 
顶部