86 lines
2.0 KiB
Plaintext
86 lines
2.0 KiB
Plaintext
MODULE Complex;
|
|
IMPORT Files,Out;
|
|
TYPE
|
|
Complex* = POINTER TO ComplexDesc;
|
|
ComplexDesc = RECORD
|
|
r-,i-: REAL;
|
|
END;
|
|
|
|
PROCEDURE (CONST x: Complex) Add*(CONST y: Complex): Complex;
|
|
BEGIN
|
|
RETURN New(x.r + y.r,x.i + y.i)
|
|
END Add;
|
|
|
|
PROCEDURE (CONST x: Complex) Sub*(CONST y: Complex): Complex;
|
|
BEGIN
|
|
RETURN New(x.r - y.r,x.i - y.i)
|
|
END Sub;
|
|
|
|
PROCEDURE (CONST x: Complex) Mul*(CONST y: Complex): Complex;
|
|
BEGIN
|
|
RETURN New(x.r*y.r - x.i*y.i,x.r*y.i + x.i*y.r)
|
|
END Mul;
|
|
|
|
PROCEDURE (CONST x: Complex) Div*(CONST y: Complex): Complex;
|
|
VAR
|
|
d: REAL;
|
|
BEGIN
|
|
d := y.r * y.r + y.i * y.i;
|
|
RETURN New((x.r*y.r + x.i*y.i)/d,(x.i*y.r - x.r*y.i)/d)
|
|
END Div;
|
|
|
|
(* Reciprocal *)
|
|
PROCEDURE (CONST x: Complex) Rec*(): Complex;
|
|
VAR
|
|
d: REAL;
|
|
BEGIN
|
|
d := x.r * x.r + y.i * y.i;
|
|
RETURN New(x.r/d,(-1.0 * x.i)/d);
|
|
END Rec;
|
|
|
|
(* Conjugate *)
|
|
PROCEDURE (x: Complex) Con*(): Complex;
|
|
BEGIN
|
|
RETURN New(x.r, (-1.0) * x.i);
|
|
END Con;
|
|
|
|
PROCEDURE (x: Complex) Out(out : Files.File);
|
|
BEGIN
|
|
Files.WriteString(out,"(");
|
|
Files.WriteReal(out,x.r);
|
|
Files.WriteString(out,",");
|
|
Files.WriteReal(out,x.i);
|
|
Files.WriteString(out,"i)")
|
|
END Out;
|
|
|
|
PROCEDURE New(x,y: REAL): Complex;
|
|
VAR
|
|
r: Complex;
|
|
BEGIN
|
|
NEW(r);r.r := x;r.i := y;
|
|
RETURN r
|
|
END New;
|
|
|
|
VAR
|
|
r,x,y: Complex;
|
|
BEGIN
|
|
x := New(1.5,3);
|
|
y := New(1.0,1.0);
|
|
|
|
Out.String("x: ");x.Out(Files.stdout);Out.Ln;
|
|
Out.String("y: ");y.Out(Files.stdout);Out.Ln;
|
|
r := x.Add(y);
|
|
Out.String("x + y: ");r.Out(Files.stdout);Out.Ln;
|
|
r := x.Sub(y);
|
|
Out.String("x - y: ");r.Out(Files.stdout);Out.Ln;
|
|
r := x.Mul(y);
|
|
Out.String("x * y: ");r.Out(Files.stdout);Out.Ln;
|
|
r := x.Div(y);
|
|
Out.String("x / y: ");r.Out(Files.stdout);Out.Ln;
|
|
r := y.Rec();
|
|
Out.String("1 / y: ");r.Out(Files.stdout);Out.Ln;
|
|
r := x.Con();
|
|
Out.String("x': ");r.Out(Files.stdout);Out.Ln;
|
|
|
|
END Complex.
|