code-examples/2015/2015_03/ada/nauka/SPOJ_PA05_POT/exp.adb

133 lines
3.1 KiB
Ada

with Ada.Text_IO;
with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Integer_Text_IO;
with Ada.IO_Exceptions;
with Ada.Task_Identification; use Ada.Task_Identification;
procedure exp is
package T_IO renames Ada.Text_IO;
package CL renames Ada.Command_Line;
package S renames Ada.Strings;
package SF renames Ada.Strings.Fixed;
package I_IO renames Ada.Integer_Text_IO;
type LargeInt is range 0 .. 1000000000;
function GetLine (length: Integer := 1000) return String is
Line: String(1 .. length);
Last: Integer;
begin
T_IO.Get_Line(Line, Last);
return Line(1 .. Last);
end GetLine;
PartsLength : constant := 100;
X : String (1..1000);
Pos, Length2, Length : Integer;
Part1, Part2: String (1 .. PartsLength);
base: Integer;
exponent : LargeInt;
function strlen(s : String) return Integer is
pos : Integer := s'First;
begin
SearchNull:
while pos < s'Last and s(pos) /= Character'Val(0) loop
pos := pos + 1;
end loop SearchNull;
return pos-s'First;
end strlen;
procedure ReadLine(str : in out String) is
begin
SF.Move(
GetLine,
str,
S.Right,
S.Left,
Character'Val(0));
end ReadLine;
type LastDigit is range 0 .. 9;
function GetLastDigit (base : Integer; exponent : LargeInt) return LastDigit is
temp : Integer := 1;
digit: LastDigit := LastDigit(base mod 10);
type Matrix is array (LastDigit, Integer range 0 .. 3) of LastDigit;
tab : Matrix :=
(
0 => (0, 0, 0, 0),
1 => (1, 1, 1, 1),
2 => (2, 4, 8, 6),
3 => (3, 9, 7, 1),
4 => (4, 6, 4, 6),
5 => (5, 5, 5, 5),
6 => (6, 6, 6, 6),
7 => (7, 9, 3, 1),
8 => (8, 4, 2, 6),
9 => (9, 1, 9, 1)
);
begin
if exponent <= 0 then
return 1;
end if;
return tab(digit,Integer((exponent-1) mod 4) );
end GetLastDigit;
digit : LastDigit;
lines: Integer;
begin
lines := Integer'Value(GetLine);
ReadingLines:
for i in Integer range 1 .. lines loop
exit ReadingLines when T_IO.End_Of_File;
ReadLine(X);
Length := strlen(X);
Pos := SF.Index(X," ");
Length2 := Length - Pos ;
SF.Move(
X(1..Pos-1),
Part1,
S.Right,
S.Left,
Character'Val(0));
SF.Move(
X(Pos+1 .. Pos+Length2),
Part2,
S.Right,
S.Left,
Character'Val(0));
base := Integer'Value(Part1(1..Pos-1));
exponent := LargeInt'Value(Part2(1..Length2));
digit := GetLastDigit(base,exponent);
I_IO.Put(Integer(digit),0);
T_IO.New_Line;
end loop ReadingLines;
exception
when Ada.IO_Exceptions.End_Error =>
null;
when Error : others =>
T_IO.Put_Line("Improper data format");
end exp;