请求简单的TComplexMath类示例(源代码) [英] Request simple example of how to a TComplexMath class (source included)

查看:155
本文介绍了请求简单的TComplexMath类示例(源代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Borland Delphi7做一些Pascal编程。我已经下载了一个相当基础(和免费)的复杂数学函数的源代码库,但不幸的是没有任何使用示例。由于我对Pascal中的课程不是非常熟悉,我想我只需要一个简单的例子来说明它的用法,让我开始。



任何事情都会做,即使是将两个数字加在一起的例子将让我开始。这是我试过的(很跛脚我知道)。我想我的问题是我不知道如何使用类构造函数。

 在'complexmath.pas'中使用ComplexMath 

var z1,z2,z3:TComplexNumber;
begin
z1.R:= 1.0; z1.I:= 2.0;
z2.R:= 3.0; z2.I:= - 1.0;
z3:= TComplexMath.Add(z1,z2);
结束。

TComplexMath的完整源代码在这里可用: http://delphi.about.com/library/weekly/aa070103a.htm 。我还剪辑了下面的源代码部分列表(请注意,这段代码是完整的文件,除非我已明确表示已被删除)。



Partial TComplexMath源代码列表是:

  unit ComplexMath; 

接口

使用Windows,SysUtils,Classes,Controls,Math;

type
TComplexNumber = record
R:single;
I:单;
结束

TComplexMath = class(TComponent)
private
{私有声明}
protected
{受保护的声明}
public
{公开声明}
构造函数创建(AOwner:TComponent);覆盖

function Add(C1,C2:TComplexNumber):TComplexNumber;超载;
{返回C1和C2的复合和}

函数Add(C1,C2,C3:TComplexNumber):TComplexNumber;超载;
{返回C1和C2和C3的复合和}

...和一堆更像这样...

实现

程序注册;
begin
RegisterComponents('delphi.about.com',[TComplexMath]);
结束

构造函数TComplexMath.Create(AOwner:TComponent);
begin
继承Create(AOwner);
结束

函数TComplexMath.Add(C1,C2:TComplexNumber):TComplexNumber;
begin
Result.R:= C1.R + C2.R;
Result.I:= C1.I + C2.I;
结束

...和一堆更像这样...

结束。

经过一段时间的努力,我最终剥离了类的定义,只使用了自己的功能(如简单的函数库)。虽然这对我来说很有用,但我知道这个组件是不是要被使用。我真的很感激,如果有人可以向我展示一个非常简单的例子,就是按照这个方式使用这个类。

解决方案

预期的用法是这样的:

  var 
cm:TComplexMath;

z,w,sum:TComplexNumber;
begin

cm:= TComplexMath.Create(Self)//或nil,最有可能是任何
try
sum:= cm.Add(z, w); //现在sum是z和w
的总和
cm.Free;
结束

end;

您还可以创建一个 TComplexMath 在应用程序启动时(或单元初始化或某些事物),并在应用程序的使用期限内使用它:

  unit Unit1; 

界面

var
cm:TComplexMath;

...

实现

程序测试;
begin

sum:= cm.Add(z,w); //现在sum是z和w的总和

end;

...

初始化

cm:= TComplexMath.Create(nil);

finialization

cm.Free;

最后,由于它是一个组件,您可以在设计时将其放在表单上。该实例将被称为 ComplexMath1 ,您可以在表单类中使用它,例如

  procedure TForm1.Button1Click(Sender:TObject); 
var
z,w,sum:TComplexNumber;
begin
sum:= ComplexMath1.Add(z,w);
结束

我真的不喜欢这个类的设计。首先,您只需要一个实例,那么为什么不使函数类函数呢?真的:为什么要上课?最后,如果您使用现代版本的Delphi,您可以使用高级记录和操作符重载来使您的源代码中的 z + w 之类的东西与 z w 是简单的类型,如整数!


I'm doing some Pascal programming using Borland Delphi7. I've download a fairly basic (and free) source code library of complex maths functions, but unfortunately it didn't come with any usage examples. As I'm not very familiar with classes in Pascal, I think I just need just one simple example of it's usage to get me started.

Anything will do, even an example of adding two numbers together will get me started. Here's what I've tried (very lame I know). I think my problem is that I don't know how to use the class constructor.

uses ComplexMath in 'complexmath.pas'

var z1,z2,z3 : TComplexNumber;
begin
  z1.R:=1.0; z1.I:=2.0;
  z2.R:=3.0; z2.I:=-1.0;
  z3 := TComplexMath.Add(z1,z2);
end.

Full source code for TComplexMath is available here: http://delphi.about.com/library/weekly/aa070103a.htm. I've also cut and pasted a partial listing of the source code below (note that this code is the complete file except for where I've explicitly indicated it's been cut).

Partial TComplexMath source code listing is:

unit ComplexMath;

interface

uses Windows, SysUtils, Classes, Controls, Math;

type
  TComplexNumber = record
    R : single;
    I : single;
  end;

TComplexMath = class(TComponent)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner : TComponent); override;

    function Add(C1, C2 : TComplexNumber) : TComplexNumber; overload;
    { Returns the complex sum of C1 and C2 }

    function Add(C1, C2, C3 : TComplexNumber) : TComplexNumber; overload;
    { Returns the complex sum of C1 and C2 and C3 }

    ... and a bunch more like this ...

implementation

procedure Register;
  begin
    RegisterComponents('delphi.about.com', [TComplexMath]);
  end;

constructor TComplexMath.Create(AOwner : TComponent);
  begin
    inherited Create(AOwner);
  end;

 function TComplexMath.Add(C1, C2 : TComplexNumber) : TComplexNumber;
   begin
     Result.R := C1.R + C2.R;
     Result.I := C1.I + C2.I;
   end;

    ... and a bunch more like this ...

end.

After struggling for a while I eventually stripped out the class definition and used just the functions themselves (like a simple library of functions). And though this is working for me, I know it's not how this component was intended to be used. I'd really appreciate it if someone could show me a very simple example of using this class in the way it was intended.

解决方案

The intended usage is like this:

var
  cm: TComplexMath;

  z, w, sum: TComplexNumber;
begin

  cm := TComplexMath.Create(Self) // or nil, can most likely be anything
  try
    sum := cm.Add(z, w); // Now sum is the sum of z and w
  finally
    cm.Free;
  end;

end;

You could also create an instance of TComplexMath at application startup (or unit initialization, or something), and use that for the lifetime of your application:

unit Unit1;

interface

var
  cm: TComplexMath;

...

implementation

procedure Test;
begin

  sum := cm.Add(z, w); // Now sum is the sum of z and w

end;

...

initialization

  cm := TComplexMath.Create(nil);

finialization

  cm.Free;

Finally, since it is a component, you can drop it onto a form at design time. The instance will be called ComplexMath1, and you can use it in the form class, like

procedure TForm1.Button1Click(Sender: TObject);
var
  z, w, sum: TComplexNumber;    
begin
  sum := ComplexMath1.Add(z, w);
end;

I really dislike the design of this class. First, you'd ever only need one instance of it, so why not make the functions class functions instead? Really: why use a class at all? Finally, if you use a modern version of Delphi, you can use advanced records and operator overloading to make things like z + w work in your source code, just like z and w were simple types, like integers!

这篇关于请求简单的TComplexMath类示例(源代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆