Pascal Complex Number Arithmetic Program
Classified in Computers
Written on in English with a size of 2.49 KB
NosComp Program;
uses crt;
Type
Record number =
Real: Real;
imag: Real;
End;
Var
n1, n2: number;
resp: char;
Procedure LeerDatos;
begin
writeln ('Real part of the first number');
readln (n1.real);
writeln ('Imaginary part of the first number');
readln (n1.imag);
writeln ('Real part of the second number');
readln (n2.real);
writeln ('Imaginary part of the second number');
readln (n2.imag);
end;
Procedure Sum;
Begin
write ('The result of adding them is:');
if (n1.imag + n2.imag) >= 0 then
writeln ( (n1.real + n2.real):2:2, '+', (n1.imag + n2.imag):2:2, 'i')
else
writeln ( (n1.real + n2.real):2:2, '-', abs (n1.imag + n2.imag):2:2, 'i')
end;
Procedure Subtraction;
begin
write ('The result of subtracting them is:');
if (n1.imag - n2.imag) >= 0 then
writeln ( (n1.real - n2.real):2:2, '+', (n1.imag - n2.imag):2:2, 'i')
else
writeln ( (n1.real - n2.real):2:2, '-', abs (n1.imag - n2.imag):2:2, 'i')
end;
Procedure Product;
Begin
write ('The product is:');
if ((n1.real * n2.imag) + (n1.imag * n2.real)) >= 0 then
begin
write (((n1.real * n2.real) - (n1.imag * n2.imag)):2:2);
write ('+');
writeln ((n1.real * n2.imag) + (n1.imag * n2.real):2:2, 'i')
end
else
begin
write (((n1.real * n2.real) - (n1.imag * n2.imag)):2:2);
write ('-');
writeln (abs (((n1.real * n2.imag) + (n1.imag * n2.real))):2:2, 'i')
end
end;
Procedure Operation;
var
oper: char;
begin
write ('Enter the operation you want (+,-,*):');
readln (oper);
writeln;
case oper of
'+': Sum;
'-': Subtraction;
'*': Product;
else
writeln ('You have entered an incorrect operator.');
end;
end;
begin
ClrScr;
repeat
writeln ('Please enter two complex numbers:');
writeln;
LeerDatos;
repeat
Operation;
write ('Do you want to perform another operation? (N/n to end):');
readln (resp);
writeln;
until (resp = 'N') or (resp = 'n');
write ('Do you want to enter other numbers? (N/n to end):');
readln (resp);
writeln;
until (resp = 'N') or (resp = 'n');
end.