Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-1.ada
Normal file
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-1.ada
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package CSV is
|
||||
|
||||
type Row(<>) is tagged private;
|
||||
|
||||
function Line(S: String; Separator: Character := ',') return Row;
|
||||
function Next(R: in out Row) return Boolean;
|
||||
-- if there is still an item in R, Next advances to it and returns True
|
||||
function Item(R: Row) return String;
|
||||
-- after calling R.Next i times, this returns the i'th item (if any)
|
||||
|
||||
private
|
||||
type Row(Length: Natural) is tagged record
|
||||
Str: String(1 .. Length);
|
||||
Fst: Positive;
|
||||
Lst: Natural;
|
||||
Nxt: Positive;
|
||||
Sep: Character;
|
||||
end record;
|
||||
end CSV;
|
||||
24
Task/CSV-data-manipulation/Ada/csv-data-manipulation-2.ada
Normal file
24
Task/CSV-data-manipulation/Ada/csv-data-manipulation-2.ada
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package body CSV is
|
||||
|
||||
function Line(S: String; Separator: Character := ',')
|
||||
return Row is
|
||||
(Length => S'Length, Str => S,
|
||||
Fst => S'First, Lst => S'Last, Nxt => S'First, Sep => Separator);
|
||||
|
||||
function Item(R: Row) return String is
|
||||
(R.Str(R.Fst .. R.Lst));
|
||||
|
||||
function Next(R: in out Row) return Boolean is
|
||||
Last: Natural := R.Nxt;
|
||||
begin
|
||||
R.Fst := R.Nxt;
|
||||
while Last <= R.Str'Last and then R.Str(Last) /= R.Sep loop
|
||||
-- find Separator
|
||||
Last := Last + 1;
|
||||
end loop;
|
||||
R.Lst := Last - 1;
|
||||
R.Nxt := Last + 1;
|
||||
return (R.Fst <= R.Str'Last);
|
||||
end Next;
|
||||
|
||||
end CSV;
|
||||
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-3.ada
Normal file
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-3.ada
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
with CSV, Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure CSV_Data_Manipulation is
|
||||
Header: String := Get_Line;
|
||||
begin
|
||||
Put_Line(Header & ", SUM");
|
||||
while not End_Of_File loop
|
||||
declare
|
||||
R: CSV.Row := CSV.Line(Get_Line);
|
||||
Sum: Integer := 0;
|
||||
begin
|
||||
while R.Next loop
|
||||
Sum := Sum + Integer'Value(R.Item);
|
||||
Put(R.Item & ",");
|
||||
end loop;
|
||||
Put_Line(Integer'Image(Sum));
|
||||
end;
|
||||
end loop;
|
||||
end CSV_Data_Manipulation;
|
||||
Loading…
Add table
Add a link
Reference in a new issue