code-examples/2015/2015_03/ada/nauka/parse/cl4.adb

91 lines
2.1 KiB
Ada

with Ada.Text_IO;
with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Integer_Text_IO;
procedure cl4 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;
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);
Val1, Val2: Integer;
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;
begin
T_IO.Put_Line("Próba wczytania tekstu ;)");
ReadingLines:
loop
SF.Move(
GetLine,
X,
S.Right,
S.Left,
Character'Val(0));
Length := strlen(X);
T_IO.Put_Line ("Wczytano: '" & X & "', Length: " &
Integer'Image(Length));
Pos := SF.Index(X," ");
Length2 := Length - Pos ;
T_IO.Put_Line ("Przerwe znaleziono na miejscu: " & Integer'Image(Pos)
& ", Length2: " & Integer'Image(Length2));
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));
T_IO.Put_Line ("1. Czesc: '" & Part1 & "', 2. Czesc: '" & Part2 & "'");
Val1 := Integer'Value(Part1(1..Pos-1));
Val2 := Integer'Value(Part2(1..Length2));
T_IO.Put_Line("Val1: " & Integer'Image(Val1) & ", Val2: "
& Integer'Image(Val2));
exit ReadingLines when T_IO.End_Of_File;
end loop ReadingLines;
end cl4;