[…] a simple program to solve Quadratic equations […][…]
Raiz:=sqrt((b*b)-4*(a*c));
[…]
Uhm, when you first heard about the quadratic formula, you probably heard about “el determinante” and its properties, too, didn't you? So, why don't you use it?
Just to mention three tricks:
- you can declare multiple variables of the same type by separating identifiers with commas
- an array[boolean] of string[1] for the sign
- readLn can read multiple arguments (you don't have to call readLn three times separately)
program quadraticFormula(input, output, stderr);
uses
// sysutils for format function
sysutils,
// gettext for internationalization
gettext;
// resourcestrings {{{
resourcestring
prompt = 'Enter the integers a, b, c as in ax²+bx+c:';
equationResult = 'The equation %0:dx²%1:s%2:dx%3:s%4:d';
oneRealRootOnly = ' has the root x₀ ⩰ %0:.3f.';
twoRealRoots = ' has roots at x₀ ⩰ %0:.3f and x₁ ⩰ %1:.3f.';
complexRootsOnly = ' has no real roots.';
// }}}
var
a, b, c: integer;
determinant, vertex_x, root_offset: extended;
sign: array[boolean] of string[1];
begin
translateResourceStrings('./' + applicationName() + '.%s.mo');
writeLn(prompt);
readLn(a, b, c);
determinant := sqr(b) - 4 * a * c;
// '-' (minus) is generated by format function
sign[false] := '';
sign[true] := '+';
write(format(equationResult, [a, sign[b >= 0], b, sign[c >= 0], c]));
if determinant >= 0 then
begin
vertex_x := -1 * (b / (2 * a));
if determinant > 0 then
begin
root_offset := sqrt(determinant / (4 * sqr(a)));
writeLn(format(twoRealRoots,
[vertex_x - root_offset, vertex_x + root_offset]));
end
else
begin
writeLn(format(oneRealRootOnly, [vertex_x]));
end;
end
else
begin
writeLn(complexRootsOnly);
end;
end.
Have fun learning Pascal, Rubén.