Переделать код азбуки морзе в более простую и доступную форму - Pascal

Узнай цену своей работы

Формулировка задачи:

type PItem = ^Item;
Item = record
next : array['-'..'.'] of PItem;
value : string[3];
end;
 
ParsedLine = record
letter : string;
combination : string;
end;

function CreateItem() : PItem;
begin
(CreateItem);
CreateItem^.value := '';
CreateItem^.next['.'] := NIL;
CreateItem^.next['-'] := NIL;
end;

function Decode(rep : PItem; comb : string) : string;
var p : PItem;
i : integer;
begin
p := rep;
for i := 1 to length(comb) do
begin
p := p^.next[comb[i]];
 
if p = NIL then
begin
Decode := ''; exit;
end;
end;
 
Decode := p^.value;
 
for i := 1 to length(Decode) do
if Decode[i] in ['A'..'Z'] then Decode[i] := chr(ord(Decode[i]) - ord('A') + ord('a'));
end;

procedure AddToRepresentation(rep : PItem; parsed : ParsedLine);
var i : integer;
p : PItem;
prev : PItem;
begin
p := rep;
for i := 1 to length(parsed.combination) do
begin
if p^.next[parsed.combination[i]] = NIL then
p^.next[parsed.combination[i]] := CreateItem();
 
p := p^.next[parsed.combination[i]];
end;
p^.value := parsed.letter;
end;

function ParseLineRepresentation(line : string) : ParsedLine;
var i : integer = 1;
begin
ParseLineRepresentation.letter := '';
ParseLineRepresentation.combination := '';
 
while line[i] in [' '] do inc(i);
while line[i] in ['.','-'] do
begin
ParseLineRepresentation.combination := ParseLineRepresentation.combination + line[i];
inc(i);
end;
while line[i] in [' '] do inc(i);
while not (line[i] in [' ']) and (i <= length(line)) do
begin
ParseLineRepresentation.letter := ParseLineRepresentation.letter + line[i];
inc(i);
end;
 
end;

function ParseRepresentation(filename : string) : PItem;
var f : Text;
line : String;
parsed : ParsedLine;
begin
ParseRepresentation := CreateItem();
 
Assign(f, filename);
Reset(f);
 
while not EOF(f) do
begin
ReadLn(f, line);
parsed := ParseLineRepresentation(line);
AddToRepresentation(ParseRepresentation, parsed);
end;
 
Close(f);
end;

procedure DecodeAndPrint(filename, output : string; representation : PItem);
var f : Text;
o : Text;
c : char;
prev : char;
token : string;
begin
Assign(f, filename);
Reset(f);
 
Assign(o, output);
Rewrite(o);
 
token := '';
prev := '*';
while not EoF(f) do
begin
while not EoLn(f) do
begin
prev := c;
Read(f, c);
if c in ['.','-'] then token := token + c
else { / }
begin
if length(token) > 0 then
begin
Write(o, Decode(representation, token));
token := '';
end;
 
if prev in ['/'] then Write(o, ' ');
end;
end;
 
Read(f, c); 
WriteLn(o);
end;
 
Close(f);
Close(o);
end;

begin
DecodeAndPrint('vstup.txt', 'vystup.txt', ParseRepresentation('morse.txt'));
end.

Решение задачи: «Переделать код азбуки морзе в более простую и доступную форму»

textual
Листинг программы
const
  Blank = [#9,' '];
  Morse = ['.','-'];
  maxn = 99;
 
type
  TChars = set of Char;
  TMorse = String[8];
  TMChar = String[3];
 
function GetWord(const s: String; const Delim: TChars; var i: Integer): String;
var j: Integer;
begin
  while (i<=Length(s)) and (s[i] in Delim) do Inc(i); j:=i; 
  while (i<=Length(s)) and not (s[i] in Delim) do Inc(i);
  GetWord:=Copy(s,j,i-j);
end;
 
var
  m: array [0..maxn] of TMorse;
  a: array [0..maxn] of TMChar;
  n: Integer;
 
procedure ReadMorse;
var
  f: Text;
  i, j: Integer;
  s: String;
begin
  Assign(f,'morse.txt'); Reset(f);
  n:=0;
  while not EoF(f) do begin
    ReadLn(f,s);
    i:=1; m[n]:=GetWord(s,Blank,i); a[n]:=GetWord(s,Blank,i);
    if (Length(m[n])>0) and (m[n][1] in Morse) then Inc(n);
  end;
  Close(f); Dec(n);
  i:=n;
  repeat
    Dec(i); s:='';
    for j:=0 to i do
      if m[j]>m[j+1] then begin
        s:=a[j+1]; a[j+1]:=a[j]; a[j]:=s;
        s:=m[j+1]; m[j+1]:=m[j]; m[j]:=s;
      end;
  until s='';
end;
 
function GetChar(const s: TMorse): TMChar;
var l, r, c: Integer;
begin
  GetChar:=s;
  l:=0; r:=n;
  while l<=r do begin
    c:=l+(r-l) div 2;
    if m[c]>=s then r:=c-1 else l:=c+1;
  end;
  if m[l]=s then GetChar:=a[l];
end;
 
function Decode(const s: String): String;
var
  i: Integer;
  r: String;
begin
  r:=''; i:=1;
  while i<=Length(s) do begin
    if Copy(s,i,2)='//' then begin
      r:=r+' '; Inc(i,2);
    end else r:=r+GetChar(GetWord(s,['/'],i));
  end;
  Decode:=r;
end;
var
  fi, fo: Text;
  s: String;
begin
  ReadMorse;
  Assign(fi,'in.txt'); Assign(fo,'out.txt');
  Reset(fi); Rewrite(fo);
  while not EOF(fi) do begin
    ReadLn(fi,s); WriteLn(fo,Decode(s));
  end;
  Close(fi); Close(fo);
end.

Объяснение кода листинга программы

const Blank = [#9, ' ']; Morse = ['.', '-']; maxn = 99; type TChars = set of Char; TMorse = String[8]; TMChar = String[3]; function GetWord(const s: String; const Delim: TChars; var i: Integer): String; var j: Integer; begin while (i<=Length(s)) and (s[i] in Delim) do Inc(i); j:=i; while (i<=Length(s)) and not (s[i] in Delim) do Inc(i); GetWord:=Copy(s,j,i-j); end; var m: array [0..maxn] of TMorse; a: array [0..maxn] of TMChar; n: Integer; procedure ReadMorse; var f: Text; i, j: Integer; s: String; begin Assign(f,'morse.txt'); Reset(f); n:=0; while not EoF(f) do begin ReadLn(f,s); i:=1; m[n]:=GetWord(s,Blank,i); a[n]:=GetWord(s,Blank,i); if (Length(m[n])>0) and (m[n][1] in Morse) then Inc(n); end; Close(f); Dec(n); i:=n; repeat Dec(i); s:=''; for j:=0 to i do begin if m[j]>m[j+1] then begin s:=a[j+1]; a[j+1]:=a[j]; a[j]:=s; s:=m[j+1]; m[j+1]:=m[j]; m[j]:=s; end; end; until s=''; end; function GetChar(const s: TMorse): TMChar; var l, r, c: Integer; begin GetChar:=s; l:=0; r:=n; while l<=r do begin c:=l+(r-l) div 2; if m[c]>=s then r:=c-1 else l:=c+1; end; if m[l]=s then GetChar:=a[l]; end; function Decode(const s: String): String; var i: Integer; r: String; begin r:=''; i:=1; while i<=Length(s) do begin if Copy(s,i,2)='//' then begin r:=r+' '; Inc(i,2); end else r:=r+GetChar(GetWord(s,['/'],i)); end; Decode:=r; end; var fi, fo: Text; s: String; begin ReadMorse; Assign(fi,'in.txt'); Assign(fo,'out.txt'); Reset(fi); Rewrite(fo); while not EOF(fi) do begin ReadLn(fi,s); WriteLn(fo,Decode(s)); end; Close(fi); Close(fo); end.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4 из 5
Похожие ответы