Note: all the expressions which are monstrous like
round(exp((x)*ln(y))) are nothing but xy
Thats because Pascal does not support a function for x raised to y. Hence the
exponent of a log and then rounding it. Its simply xy
The exponential function Exp computes the value of e to the power of x. The natural logarith Ln is the logarithm with base e. Round is a function to round it off to a whole number.
program tristate;
var n,a,b:real;task:boolean;choice:char;
procedure alpha;
{ alpha and beta are where + get written, gamma, delta for - }
begin
write(' + ',round(exp((a-1)*ln(3))));
{// + (a-1)3 }
n:=round(n-(exp((a-1)*ln(3))));
{// n:=n-(a-1)3 ; the n gets reassigned }
end;
procedure beta;
begin
write(' + ',round(exp(a*ln(3))));
{// + (a)3 }
n:=round(n-exp(a*ln(3)));
{// n:=n-(a)3 }
end;
procedure gamma;
begin
write(' - ',round(exp((a-1)*ln(3))));
n:=round(n-(exp((a-1)*ln(3))));
end;
procedure delta;
begin
write(' - ',round(exp(a*ln(3))));
n:=round((n-exp(a*ln(3))));
end;
procedure positive;label 10;
begin
a:=0;
repeat
begin
if n=round(exp(a*ln(3))) then
{ if n is equal to a3 then }
begin
write(' + ',round(exp(a*ln(3))));
task:=true;
goto 10;
end;
a:=a+1;
end;
until N<round((exp(a*ln(3)))); { repeat until
you reach the a where n < a3 ; a radical larger than n}
b:=round(exp(a*ln(3))-n);
{ then take the b which is the balance between n and a3 }
if b>round(((exp(A*ln(3)))-1)/2) then alpha {if balance is > half of (a3
-1) then take alpha else beta}
else
if b<=round(((exp((a)*ln(3)))-1)/2) then beta;
10:;
end;
procedure negative;label 10;
begin
n:=-n;a:=0;
repeat
if n=round(exp(a*ln(3))) then
begin
write(' - ',round(exp(a*ln(3))));
task:=true;
goto 10;
end;
a:=a+1;
until N<round((exp(a*ln(3))));
b:=round(exp(a*ln(3))-n);
if b>round(((exp((a)*ln(3)))-1)/2) then gamma
else
if b<=round(((exp((a)*ln(3)))-1)/2) then delta;
n:=-n;
10:;
end;
procedure process;
begin
if n<0 then negative
else
if n=0 then begin
write(' 0');
Task:=true;
end
else
if n>0 then positive;
end;
{MAIN PROGRAMME}
begin
choice:='y';
writeln;writeln;
repeat
begin
a:=0;n:=0;b:=0;
task:=false;
writeln;writeln;
writeln(' Enter your number (negative also allowed)');
readln(n);
writeln;
write(round(n),' = ');
while task=false do
begin
process;
end;
writeln;writeln;writeln;
writeln('q to quit, c to continue crunching');
readln(choice);
end;
until choice='q';
writeln;writeln(' Copyright Abhijit Bhattacharjee 20 Sept 1996');
writeln(' 5C Nilanjana Apts, 43 DH Road, Calcutta 34 ,INDIA');
readln;
end.
There is one goto statement thats been used. I dont subscribe to goto statements, particularly in algorithms, but they serve great for getting out of a process without further delay, this is such a use.