- •Рекурсивные алгоритмы (продолжение)
- •I, j: integer;
- •I, j: integer;
- •I, j: integer;
- •Метод динамического программирования
- •I, j: integer;
- •I, j: Integer;
- •I, j: integer;
- •Тип «Денежный»
- •Стандартные процедуры и функции для работы с любыми файлами
- •Текстовый файл
- •Стандартные процедуры для работы с текстовыми файлами
- •Условная компиляция
- •I, j: Integer;
- •I, j: Integer;
- •I, j: Integer;
- •I, j: integer;
- •Тип «Указатель»
- •Динамические структуры данных Линейный однонаправленный список
- •Объектно-ориентированное программирование (ооп)
- •Объявление класса
- •Interface
- •Implementation
- •Interface
- •Implementation
Interface
type
pPersonType = ^PersonType;
PersonType = record
sName: string[40];
Salary: double;
pNext: pPersonType;
end;
MyList1Class = Class(TObject)
protected
pHead, pTail: pPersonType;
public
constructor Create;
destructor Destroy;
function AloneItem(sName: string; Salary: double;
var pNew: pPersonType): boolean;
procedure AppendItem(sName: string; Salary: double);
procedure AppendFromFile(sFileName: string);
procedure ShowList;
end;
Implementation
constructor MyList1Class.Create;
begin
pHead := Nil;
pTail := Nil;
end;
destructor MyList1Class.Destroy;
begin
// Код будет написан позже!
end;
function MyList1Class.AloneItem(sName: string; Salary: double;
var pNew: pPersonType): boolean;
begin
New(pNew);
if pNew = Nil then Halt; // Нет места в памяти – прекращаем работу.
pNew^.sName := sName;
pNew^.Salary := Salary;
pNew^.pNext := Nil;
if pHead = Nil then
begin
pHead := pNew; pTail := pNew;
AloneItem := True;
end
else
AloneItem := False;
end;
procedure MyList1Class.AppendItem(sName: string; Salary: double);
var
pNew: pPersonType;
begin
if not AloneItem(sName, Salary, pNew) then
begin
pTail^.pNext := pNew;
pTail := pNew;
end;
end;
procedure MyList1Class.AppendFromFile(sFileName: string);
var
F: Text;
sName: string[40];
Salary: double;
begin
Assign(F, sFileName);
{$I-}
Reset(F);
{$I+}
if IOResult <> 0 then
begin
WriteLN('File ', sFileName, ' not found');
Halt;
end;
while not EOF(F) do
begin
Readln(F, sName);
Readln(F, Salary);
AppendItem(sName, Salary);
end;
end;
procedure MyList1Class.ShowList;
begin
pCurr := pHead;
Writeln('Name':40, ' ', 'Salary');
Writeln('----':40, ' ', '------');
while pCurr <> Nil do
begin
Writeln(pCurr^.sName:40, ' ', pCurr^.Salary:8:2);
pCurr := pCurr^.pNext;
end;
end;
end.
Текст файла Unit2.pas:
unit Unit2;
Interface
uses Unit1;
type
MyList2Class = Class(MyList1Class)
public
procedure PrependItem(sName: string; Salary: double);
procedure PrependFromFile(sFileName: string);
end;
Implementation
procedure MyList2Class.PrependItem(sName: string; Salary: double);
var
pNew: pPersonType;
begin
if not AloneItem(sName, Salary, pNew) then
begin
pNew^.pNext := pHead;
pHead := pNew;
end;
end;
procedure MyList2Class.PrependFromFile(sFileName: string);
var
F: Text;
sName: string[40];
Salary: double;
begin
Assign(F, sFileName);
{$I-}
Reset(F);
{$I+}
if IOResult <> 0 then
begin
WriteLN('File ', sFileName, ' not found');
Halt;
end;
while not EOF(F) do
begin
Readln(F, sName);
Readln(F, Salary);
PrependItem(sName, Salary);
end;
end;
end.