⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.pas

📁 能够计算复数 (Complex) 的单元 ( 1.0 版
💻 PAS
字号:
{
This is a test program gone wild!
I originally created it to test my complex-math unit, Complex.pas,
but it has since grown into this monster.
Things to do:
1. Warn user when accessing the ArcXXX functions if the input value
isn't <= |1| : generates an EInvalidFloatOperation (or whatever) at present.
2. Expand ( Geez, will you EVER be satisfied ?!!!) the input routine
to eventually make a truly useful complex calculator
3. Overall, more graceful errorhandling (warnings etc instead of allout exceprions)
}
unit Test;

interface

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

type
  TForm1 = class(TForm)
    ComplexIn1: TEdit;
    ComplexIn2: TEdit;
    Complex1: TLabel;
    Complex2: TLabel;
    ListBox1: TListBox;
    Calculate: TButton;
    OutPut: TEdit;
    Avail: TLabel;
    Label1: TLabel;
    Real1: TEdit;
    Real2: TEdit;
    Reals: TLabel;
    Label2: TLabel;
    StatusBar: TStatusBar;
    Equal: TButton;
    procedure ListBox1DblClick(Sender: TObject);
    procedure EqualClick(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ListBox1DblClick(Sender: TObject);
var Z,Z2:TComplex;R1,R2:Extended;b:boolean;
begin
Z := ToComplex(0,0);
Z2 := ToComplex(0,0);
R1 := 0;
R2 := 0;
Output.Text := '';
if ComplexIn1.Text <> '' then Z := StringToComplex(ComplexIn1.Text);
if ComplexIn2.Text <> '' then Z2 := StringToComplex(ComplexIn2.Text);
if Real1.Text <> '' then  R1 := StrToFloat(Real1.Text);
if Real2.Text <> '' then  R2 := StrToFloat(Real2.Text);

case ListBox1.ItemIndex of
0: R1 := Real(Z);
1: R1 := Imag(Z);
2: R1 := AbsZ(Z);
3: R1 := ModZ(Z);
4: R1 := ArgZ(Z);
5: R1 := NormZ(Z);
6:  Z := CosZ(Z);
7:  Z := CosHZ(Z);
8:  Z := ConjugateZ(Z);
9:  Z := SinZ(Z);
10: Z := ArcSinZ(Z);
11: Z := SinhZ(Z);
12: Z := ArcSinHZ(Z);
13: Z := TanZ(Z);
14: Z := ArcTanZ(Z);
15: Z := ArcCosZ(Z);
16: Z := TanhZ(Z);
17: Z := ArcTanHZ(Z);
18: Z := ExpZ(Z);
19: Z := LnZ(Z);
20: Z := Log10Z(Z);
21: Z := SqrtZ(Z);
22: Z := NegZ(Z);
23: Z := AddZR(Z,R1);
24: Z := SubZR(Z,R1);
25: Z := DivZR(Z,R1);
26: Z := PowZR1(Z,Trunc(R1));
27: Z := PowZR2(Z,R1);
28: Z := MulRZ(R1,Z);
29: Z := SubRZ(R1,Z);
30: Z := DivRZ(R1,Z);
31: Z := PowRZ(R1,Z);
32: Z := SubZZ(Z,Z2);
33: Z := MulZZ(Z,Z2);
34: Z := DivZZ(Z,Z2);
35: Z := PowZZ(Z,Z2);
36: Z := AddZZ(Z,Z2);
37:
begin
  Z := PolarZ(R1,R2);
  { save output in ComplexIn1.Text so we can check any rounding
    errors by running the RectangularZ function directly }
  ComplexIn1.Text := ComplexToString(Z);
  Exit;
end;

38: begin
     RectangularZ(Z,R1,R2);
     Real1.Text := FloatToStr(R1);
     Real2.Text := FloatToStr(R2);
     Exit;
    end;
39: begin
     EqualClick(Sender);
     Exit;
    end;
end;
if ListBox1.Itemindex < 6 then
   Output.Text := FloatToStr(R1)
else
   Output.Text := ComplexToString(Z);
{
ComplexIn1.Text := '';
ComplexIn2.Text := '';
Real1.Text := '';
Real2.Text := '';
}
ActiveControl := ListBox1;
end;

procedure TForm1.EqualClick(Sender: TObject);
begin
     if (ComplexIn1.Text = '') or (ComplexIn2.Text = '') then Exit;
     OutPut.Text := BoolToString((EqualZZ(StringToComplex(ComplexIn1.Text),StringToComplex(ComplexIn2.Text))));
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var i:integer;
begin
{ disable all }
for i := 0 to ComponentCount - 1 do
  if (Components[i] is TEdit) then
    (Components[i] as TEdit).Enabled := False;
{ enable all we allways need }

Calculate.Enabled := True;
ListBox1.Enabled := True;
Output.Enabled := True;
Equal.Enabled := False;

{ enable the ones we need right now }
case ListBox1.ItemIndex of
0..22,38:
begin
     ComplexIn1.Enabled := True;
end;
23..31:
begin
  ComplexIn1.Enabled := True;
  Real1.Enabled := True;
end;
32..36,39:
begin
  Equal.Enabled := True;
  ComplexIn1.Enabled := True;
  ComplexIn2.Enabled := True;
end;
37:
begin
  Real1.Enabled := True;
  Real2.Enabled := True;
end;
end;

{ set appropriate colors }
for i := 0 to ComponentCount - 1 do
begin
if (Components[i] is TEdit) then
   with Components[i] as Tedit do
   if Enabled then Color := clWindow
   else Color := clBtnFace;
end;
{ some help... }
with StatusBar do
case ListBox1.ItemIndex of
 0: SimpleText := 'Return the real part of Z ';
 1: SimpleText := 'Return the imaginary of Z ';
 2: SimpleText := 'Return the absolute value of Z ';
 3: SimpleText := 'Return the absolute value of Z ';
 4: SimpleText := 'Return the argument of Z ';
 5: SimpleText := 'Return the normalized value of Z ';
 6: SimpleText := 'Return the cosine of Z ';
 7: SimpleText := 'Return the hyperbolic cosine of Z ';
 8: SimpleText := 'Return the conjugate of Z';
 9: SimpleText := 'Return the sine of Z ';
10: SimpleText := 'Return the arcsine of Z ';
11: SimpleText := 'Return the hyperbolic sine of Z ';
12: SimpleText := 'Return the hyperbolic arcsine of Z';
13: SimpleText := 'Return the tangens of Z ';
14: SimpleText := 'Return the arctangens of Z ';
15: SimpleText := 'Return the arccosine of Z ';
16: SimpleText := 'Return the hyperbolic tangens of Z ';
17: SimpleText := 'Return the hyperbolic arctangens of Z ';
18: SimpleText := 'Return e raised to the power of Z ';
19: SimpleText := 'Return the natural logarithm of Z ';
20: SimpleText := 'Return the base 10 logarithm of Z ';
21: SimpleText := 'Return the square root of Z ';
22: SimpleText := 'Return the negative value of Z ';
23: SimpleText := 'Return the addition of Z and R1';
24: SimpleText := 'Return the subtraction of Z and R1';
25: SimpleText := 'Return the division of Z and R1';
26: SimpleText := 'Return Z raised to the power of R (integer)';
27: SimpleText := 'Return Z raised to the power of R (float)';
28: SimpleText := 'Return the multiplication of R1 and Z ';
29: SimpleText := 'Return the subtraction of R1 and Z ';
30: SimpleText := 'Return the division of R1 and Z ';
31: SimpleText := 'Return R raised to the power of Z ';
32: SimpleText := 'Return the subtraction of Z and Z2';
33: SimpleText := 'Return the multiplication of Z and Z2';
34: SimpleText := 'Return the division of Z and Z2';
35: SimpleText := 'Return Z raised to the power of Z2';
36: SimpleText := 'Return the  addition of Z and Z2 ';
37: SimpleText := 'Return the polar coordinates of R1 and R2 ';
38: SimpleText :='Return the rectangular coordinates of Z ';
39: SimpleText :='Return true if Z and Z2 is equal';
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     { update }
     ListBox1.ItemIndex := 0;
     ListBox1Click(Sender);
     Application.Title := 'ZCalc';
     Caption := Application.Title + ' - complex calculator';
end;

end.

Real(Z);
Imag(Z);
AbsZ(Z);
ModZ(Z);
ArgZ(Z);
NormZ(Z);
CosZ(Z);
CosHZ(Z);
ConjugateZ(Z);
SinZ(Z);
ArcSinZ(Z);
SinhZ(Z);
ArcSinHZ(Z);
TanZ(Z);
ArcTanZ(Z);
ArcCosZ(Z);
TanhZ(Z);
ArcTanHZ(Z);
ExpZ(Z);
LnZ(Z);
Log10Z(Z);
SqrtZ(Z);
NegZ(Z);
AddZR(Z,R1);
SubZR(Z,R1);
DivZR(Z,R1);
PowZR1(Z,Trunc(R1));
PowZR2(Z,R1);
MulRZ(R1,Z);
SubRZ(R1,Z);
DivRZ(R1,Z);
PowRZ(R1,Z);
SubZZ(Z,Z2);
MulZZ(Z,Z2);
PowZZ(Z,Z2);
AddZZ(Z,Z2);
PolarZ(R1,R2);
EqualZZ(Z,Z2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -