Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Mad-Libs/Ada/mad-libs-1.ada
Normal file
32
Task/Mad-Libs/Ada/mad-libs-1.ada
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
with Ada.Text_IO, Ada.Command_Line, String_Helper;
|
||||
|
||||
procedure Madlib is
|
||||
|
||||
use String_Helper;
|
||||
|
||||
Text: Vector := Get_Vector(Ada.Command_Line.Argument(1));
|
||||
M, N: Natural;
|
||||
|
||||
begin
|
||||
-- search for templates and modify the text accordingly
|
||||
for I in Text.First_Index .. Text.Last_Index loop
|
||||
loop
|
||||
Search_Brackets(Text.Element(I), "<", ">", M, N);
|
||||
exit when M=0; -- "M=0" means "not found"
|
||||
Ada.Text_IO.Put_Line("Replacement for " & Text.Element(I)(M .. N) & "?");
|
||||
declare
|
||||
Old: String := Text.Element(I)(M .. N);
|
||||
New_Word: String := Ada.Text_IO.Get_Line;
|
||||
begin
|
||||
for J in I .. Text.Last_Index loop
|
||||
Text.Replace_Element(J, Replace(Text.Element(J), Old, New_Word));
|
||||
end loop;
|
||||
end;
|
||||
end loop;
|
||||
end loop;
|
||||
|
||||
-- write the text
|
||||
for I in Text.First_Index .. Text.Last_Index loop
|
||||
Ada.Text_IO.Put_Line(Text.Element(I));
|
||||
end loop;
|
||||
end Madlib;
|
||||
25
Task/Mad-Libs/Ada/mad-libs-2.ada
Normal file
25
Task/Mad-Libs/Ada/mad-libs-2.ada
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
with Ada.Containers.Indefinite_Vectors;
|
||||
|
||||
package String_Helper is
|
||||
|
||||
function Index(Source: String; Pattern: String) return Natural;
|
||||
|
||||
procedure Search_Brackets(Source: String;
|
||||
Left_Bracket: String;
|
||||
Right_Bracket: String;
|
||||
First, Last: out Natural);
|
||||
-- returns indices of first pair of brackets in source
|
||||
-- indices are zero if no such brackets are found
|
||||
|
||||
function Replace(Source: String; Old_Word: String; New_Word: String)
|
||||
return String;
|
||||
|
||||
package String_Vec is new Ada.Containers.Indefinite_Vectors
|
||||
(Index_Type => Positive,
|
||||
Element_Type => String);
|
||||
|
||||
type Vector is new String_Vec.Vector with null record;
|
||||
|
||||
function Get_Vector(Filename: String) return Vector;
|
||||
|
||||
end String_Helper;
|
||||
51
Task/Mad-Libs/Ada/mad-libs-3.ada
Normal file
51
Task/Mad-Libs/Ada/mad-libs-3.ada
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
with Ada.Strings.Fixed, Ada.Text_IO;
|
||||
|
||||
package body String_Helper is
|
||||
|
||||
function Index(Source: String; Pattern: String) return Natural is
|
||||
begin
|
||||
return Ada.Strings.Fixed.Index(Source => Source, Pattern => Pattern);
|
||||
end Index;
|
||||
|
||||
procedure Search_Brackets(Source: String;
|
||||
Left_Bracket: String;
|
||||
Right_Bracket: String;
|
||||
First, Last: out Natural) is
|
||||
begin
|
||||
First := Index(Source, Left_Bracket);
|
||||
if First = 0 then
|
||||
Last := 0; -- not found
|
||||
else
|
||||
Last := Index(Source(First+1 .. Source'Last), Right_Bracket);
|
||||
if Last = 0 then
|
||||
First := 0; -- not found;
|
||||
end if;
|
||||
end if;
|
||||
end Search_Brackets;
|
||||
|
||||
function Replace(Source: String; Old_Word: String; New_Word: String)
|
||||
return String is
|
||||
L: Positive := Old_Word'Length;
|
||||
I: Natural := Index(Source, Old_Word);
|
||||
begin
|
||||
if I = 0 then
|
||||
return Source;
|
||||
else
|
||||
return Source(Source'First .. I-1) & New_Word
|
||||
& Replace(Source(I+L .. Source'Last), Old_Word, New_Word);
|
||||
end if;
|
||||
end Replace;
|
||||
|
||||
function Get_Vector(Filename: String) return Vector is
|
||||
F: Ada.Text_IO.File_Type;
|
||||
T: Vector;
|
||||
begin
|
||||
Ada.Text_IO.Open(F, Ada.Text_IO.In_File, Filename);
|
||||
while not Ada.Text_IO.End_Of_File(F) loop
|
||||
T.Append(Ada.Text_IO.Get_Line(F));
|
||||
end loop;
|
||||
Ada.Text_IO.Close(F);
|
||||
return T;
|
||||
end Get_Vector;
|
||||
|
||||
end String_Helper;
|
||||
Loading…
Add table
Add a link
Reference in a new issue