95 lines
1.4 KiB
Plaintext
95 lines
1.4 KiB
Plaintext
module Numbers;
|
|
type
|
|
{public,immutable}
|
|
Complex = record
|
|
re,im: real;
|
|
end Complex;
|
|
|
|
operator {public} "+" (a,b: Complex): Complex;
|
|
var
|
|
r: Complex;
|
|
begin
|
|
r.re := a.re + b.re;
|
|
r.im := a.im + b.im;
|
|
return r
|
|
end "+";
|
|
|
|
operator {public} "-" (a,b: Complex): Complex;
|
|
var
|
|
r: Complex;
|
|
begin
|
|
r.re := a.re - b.re;
|
|
r.im := a.im - b.im;
|
|
return r
|
|
end "-";
|
|
|
|
operator {public} "*" (a,b: Complex): Complex;
|
|
var
|
|
r: Complex;
|
|
begin
|
|
r.re := a.re*b.re - a.im*b.im;
|
|
r.im := a.re*b.im + a.im*b.re;
|
|
return r
|
|
end "*";
|
|
|
|
operator {public} "/" (a,b: Complex): Complex;
|
|
var
|
|
r: Complex;
|
|
d: real;
|
|
begin
|
|
d := b.re * b.re + b.im * b.im;
|
|
r.re := (a.re * b.re + a.im * b.im)/d;
|
|
r.im := (a.im * b.re - a.re * b.im)/d;
|
|
return r
|
|
end "/";
|
|
|
|
operator {public} "-" (a: Complex): Complex;
|
|
begin
|
|
a.im := -1 * a.im;
|
|
return a
|
|
end "-";
|
|
|
|
operator {public} "~" (a: Complex): Complex;
|
|
var
|
|
d: real;
|
|
c: Complex;
|
|
begin
|
|
d := a.re * a.re + a.im * a.im;
|
|
c.re := a.re/d;
|
|
c.im := (-1.0 * a.im)/d;
|
|
return c
|
|
end "~";
|
|
|
|
end Numbers.
|
|
|
|
|
|
module Main;
|
|
import Numbers;
|
|
|
|
var
|
|
a,b,c: Numbers.Complex;
|
|
|
|
procedure Writeln(c: Numbers.Complex);
|
|
begin
|
|
writeln("(",c.re:4:2,";",c.im:4:2,"i)");
|
|
end Writeln;
|
|
|
|
procedure NewComplex(x,y: real): Numbers.Complex;
|
|
var
|
|
r: Numbers.Complex;
|
|
begin
|
|
r.re := x;r.im := y;
|
|
return r
|
|
end NewComplex;
|
|
|
|
begin
|
|
a := NewComplex(1.5,3.0);
|
|
b := NewComplex(1.0,1.0);
|
|
Writeln(a + b);
|
|
Writeln(a - b);
|
|
Writeln(a * b);
|
|
Writeln(a / b);
|
|
Writeln(-a);
|
|
Writeln(~b);
|
|
end Main.
|