type
TMathArray = array of integer;
type
TNumber = record
int, frac: TMathArray;
sign: boolean;
end;
procedure Str2Number(s: string; var n: TNumber);
var
i, j, l: integer;
begin
if s = '' then
begin
setlength(n.int, 0);
setlength(n.frac, 0);
exit;
end;
l := length(s);
if s[1] = '-' then
begin
s := copy(s, 2, l);
l := l - 1;
n.sign := false;
end
else
n.sign := true;
j := pos('.', s);
if j > 0 then
begin
setlength(n.int, j - 1);
for i := 1 to j - 1 do
n.int[i - 1] := strtoint(s[j - i]);
setlength(n.frac, l - j);
for i := 1 to l - j do
n.frac[i - 1] := strtoint(s[l - i + 1]);
end
else
begin
setlength(n.int, l);
for i := 1 to l do
n.int[i - 1] := strtoint(s[l - i + 1]);
setlength(n.frac, 0);
end;
end;
function Num2Array(var n: TNumber; var a: TMathArray): integer;
var
i: integer;
begin
result := length(n.frac);
setlength(a, length(n.int) + result);
for i := 0 to length(a) - 1 do
if i < result then
a[i] := n.frac[i]
else
a[i] := n.int[i - result];
end;
procedure Array2Num(var n: TNumber; var a: TMathArray; frac: integer; sign:
boolean);
var
i: integer;
begin
setlength(n.frac, frac);
setlength(n.int, length(a) - frac);
for i := 0 to length(a) - 1 do
begin
if i < frac then
n.frac[i] := a[i]
else
n.int[i - frac] := a[i];
end;
n.sign := sign;
end;
procedure DisposeNumber(var n: TNumber);
begin
setlength(n.int, 0);
setlength(n.frac, 0);
end;
function Number2Str(var n: TNumber): string;
var
i: integer;
s: string;
begin
result := '';
for i := 0 to high(n.int) do
result := inttostr(n.int[i]) + result;
if length(n.frac) <> 0 then
begin
for i := 0 to high(n.frac) do
s := inttostr(n.frac[i]) + s;
result := result + '.' + s;
end;
while (length(result) > 1) and (result[1] = '0') do
delete(result, 1, 1);
if pos('.', result) > 0 then
while (length(result) > 1) and (result[length(result)] = '0') do
delete(result, length(result), 1);
if not n.sign then
result := '-' + result;
setlength(n.int, 0);
setlength(n.frac, 0);
end;
procedure MultiplyArray(var a1, a2, a: TMathArray);
var
i, j: integer;
b: boolean;
begin
{checking for zero, 1}
for i := length(a2) - 1 downto 0 do
begin
for j := length(a1) - 1 downto 0 do
begin
a[j + i] := a[j + i] + (a2[i] * a1[j]);
end;
end;
repeat
b := true;
for i := 0 to length(a) - 1 do
if a[i] > 9 then
begin
b := false;
try
a[i + 1] := a[i + 1] + 1;
except
setlength(a, length(a) + 1);
a[i + 1] := a[i + 1] + 1;
end;
a[i] := a[i] - 10;
end;
until b;
end;
function MyPower(First, Second: string): string;
var
i, j, c: integer;
a, a1, a2: TMathArray;
var
n1: TNumber;
max: integer;
begin
j := strtoint(Second);
if j = 0 then
begin
result := '1';
exit;
end
else if j = 1 then
begin
result := First;
exit;
end;
max := j - 1;
Str2Number(First, n1);
c := Num2Array(n1, a1);
setlength(a, 0);
setlength(a2, 0);
a2 := a1;
for i := 1 to j - 1 do
begin
if Assigned(OnProgress) then
OnProgress((i / max) * 100);
setlength(a, 0);
setlength(a, length(a1) + length(a2) + 1);
MultiplyArray(a1, a2, a);
setlength(a2, 0);
a2 := a;
end;
setlength(a1, 0);
setlength(a2, 0);
c := c * j;
if n1.sign then
Array2Num(n1, a, c, true)
else if odd(j) then
Array2Num(n1, a, c, false)
else
Array2Num(n1, a, c, true);
setlength(a, 0);
result := Number2Str(n1);
DisposeNumber(n1);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Clear;
memo1.Lines.Add(MyPower('255','255'));
end;