Description
- Create new arrays quicker (
NewIntegerArray(count), NewRealArray(count), ...)
- Create arrays from ranges and random ones (
IntegerArrayFromRange(from, to_), IntegerArrayFromRandom(from, to_, count))
- Read arrays item-by-item from keyboard (
ReadIntegerArray(count), ReadRealArray(count), ...)
- Print arrays (
Write(array), WriteArrayln(array))
Code
unit ArraysNDE;
/*
Simplifies array creation by providing wrappers.
*/
interface
type
IntegerArrayT = array of integer;
RealArrayT = array of real;
StringArrayT = array of string;
function NewIntegerArray(capacity: integer): IntegerArrayT;
function NewRealArray(capacity: integer): RealArrayT;
function NewStringArray(capacity: integer): StringArrayT;
function IntegerArrayFromRange(from, to_: integer): IntegerArrayT;
function IntegerArrayFromRandom(from, to_, count: integer): IntegerArrayT;
function ReadIntegerArray(count: integer): IntegerArrayT;
function ReadRealArray(count: integer): RealArrayT;
function ReadStringArray(count: integer): StringArrayT;
procedure WriteArray(source: IntegerArrayT);
procedure WriteArray(source: RealArrayT);
procedure WriteArray(source: StringArrayT);
procedure WriteArrayln(source: IntegerArrayT);
procedure WriteArrayln(source: RealArrayT);
procedure WriteArrayln(source: StringArrayT);
implementation
const
Prompt = '> ';
Prefix = '[';
Delimiter = ' ';
Suffix = ']';
function NewIntegerArray(capacity: integer): IntegerArrayT;
var
result: IntegerArrayT;
begin
SetLength(result, capacity);
NewIntegerArray := result;
end;
function NewRealArray(capacity: integer): RealArrayT;
var
result: RealArrayT;
begin
SetLength(result, capacity);
NewRealArray := result;
end;
function NewStringArray(capacity: integer): StringArrayT;
var
result: StringArrayT;
begin
SetLength(result, capacity);
NewStringArray := result;
end;
function IntegerArrayFromRange(from, to_: integer): IntegerArrayT;
var
result: IntegerArrayT;
index: integer;
current: integer;
begin
result := NewIntegerArray(to_ - from + 1);
current := from;
index := 0;
while current <= to_ do
begin
result[index] := current;
Inc(current);
Inc(index);
end;
IntegerArrayFromRange := result;
end;
function IntegerArrayFromRandom(from, to_, count: integer): IntegerArrayT;
var
result: IntegerArrayT;
index: integer;
begin
result := NewIntegerArray(count);
index := 0;
while index <= count do
begin
result[index] := Round(from + Random() * (to_ - from));
Inc(index);
end;
IntegerArrayFromRandom := result;
end;
function ReadIntegerArray(count: integer): IntegerArrayT;
var
result: IntegerArrayT;
index: integer;
current: integer;
begin
result := NewIntegerArray(count);
index := 0;
while index < count do
begin
Write(index + 1, Prompt);
Readln(current);
result[index] := current;
Inc(index);
end;
ReadIntegerArray := result;
end;
function ReadRealArray(count: integer): RealArrayT;
var
result: RealArrayT;
index: integer;
current: real;
begin
result := NewRealArray(count);
index := 0;
while index < count do
begin
Write(index + 1, Prompt);
Readln(current);
result[index] := current;
Inc(index);
end;
ReadRealArray := result;
end;
function ReadStringArray(count: integer): StringArrayT;
var
result: StringArrayT;
index: integer;
current: string;
begin
result := NewStringArray(count);
index := 0;
while index < count do
begin
Write(index + 1, Prompt);
Readln(current);
result[index] := current;
Inc(index);
end;
ReadStringArray := result;
end;
procedure WriteArray(source: IntegerArrayT);
var
index: integer;
begin
write(Prefix);
if Length(source) > 0 then
write(source[0]);
index := 1;
while index < Length(source) do
begin
Write(Delimiter, source[index]);
Inc(index);
end;
write(Suffix);
end;
procedure WriteArray(source: RealArrayT);
var
index: integer;
begin
write(Prefix);
if Length(source) > 0 then
write(source[0]);
index := 1;
while index < Length(source) do
begin
Write(Delimiter, source[index]);
Inc(index);
end;
write(Suffix);
end;
procedure WriteArray(source: StringArrayT);
var
index: integer;
begin
write(Prefix);
if Length(source) > 0 then
write(source[0]);
index := 1;
while index < Length(source) do
begin
Write(Delimiter, source[index]);
Inc(index);
end;
write(Suffix);
end;
procedure WriteArrayln(source: IntegerArrayT);
begin
WriteArray(source);
Writeln();
end;
procedure WriteArrayln(source: RealArrayT);
begin
WriteArray(source);
Writeln();
end;
procedure WriteArrayln(source: StringArrayT);
begin
WriteArray(source);
Writeln();
end;
end.
Description
NewIntegerArray(count),NewRealArray(count), ...)IntegerArrayFromRange(from, to_),IntegerArrayFromRandom(from, to_, count))ReadIntegerArray(count),ReadRealArray(count), ...)Write(array),WriteArrayln(array))Code