program
Project3;
{$APPTYPE CONSOLE}
uses
windows,
SysUtils;
const
filename=
'key.txt'
;
var
alllines:
integer
;
maxsymbol:
integer
;
sameword:
array
[
0..100
]
of
string
;
tr:
boolean
;
wrd:
array
[
0..99
]
of
string
;
cnt:
byte
;
index:
integer
;
same:
integer
;
countwords,len:
integer
;
type
tbookword=
record
w:
string
;
wasfound:
boolean
;
symbolcount:
integer
;
end
;
tbookdata=
array
of
tbookword;
function
countelement(input:
string
):
integer
;
var
divs:
set
of
char
;
i:
integer
;
begin
divs:=[
' '
,
','
,
'?'
,
'!'
,
':'
,
';'
,
'-'
,
'.'
];
for
i:=
1
to
length(input)
do
if
input[i]
in
divs
then
inc(result);
end
;
function
loadbook:tbookdata;
var
f:text;
z:
string
;
i:
integer
;
begin
result:=
nil
;
alllines:=
0
;
assignfile(f,filename);
reset(f);
while
not
Eof(f)
do
begin
readln(f,z);
trim(z);
SetLength(result,Length(result)+
1
);
result[length(result)-
1
].w:=z;
result[length(result)-
1
].wasfound:=
false
;
result[length(result)-
1
].symbolcount:=countelement(z)+
1
;
inc(alllines);
end
;
closeFile(f);
end
;
procedure
DivStrToWrd(s:
string
);
var
i,b:
integer
;
divs:
set
of
char
;
w:
boolean
;
begin
divs:=[
' '
,
','
,
'.'
,
'!'
,
'?'
,
':'
,
';'
];
w:=
false
;
s:=s+
' '
;
cnt:=
0
;
for
i:=
1
to
length(s)
do
begin
if
w
then
begin
if
s[i]
in
divs
then
begin
inc(cnt);
wrd[cnt]:=copy(s,b,i-b);
w:=
false
;
end
;
end
else
begin
if
not
(s[i]
in
divs)
then
begin
w:=
true
;
b:=i;
end
;
end
;
end
;
end
;
Function
Encode(input:
string
;book:tbookdata):
string
;
var
posline,posofword,c,g:
integer
;
l1,l2,l3,l4:
integer
;
enc1,enc2:
string
;
c1,c2:
integer
;
i:
integer
;
index:
integer
;
m:
integer
;
countnul:
integer
;
mas:
array
[
1..100
]
of
string
;
poslines:
array
[
1..100
]
of
integer
;
poswords:
array
[
1..100
]
of
integer
;
massive:
array
[
1..1000
]
of
string
;
ind:
integer
;
k:
integer
;
num:
integer
;
mm:
integer
;
begin
index:=
1
;
ind:=
1
;
len:=len*
100
;
for
g:=
0
to
alllines-
1
do
if
pos(input,book[g].w)<>
0
then
begin
DivStrToWrd(book[g].w);
posline:=g;
for
c:=
1
to
cnt
do
if
input=wrd[c]
then
begin
posofword:=c;
poslines[index]:=posline;
poswords[index]:=c;
inc(index);
end
;
end
;
for
m:=
1
to
same
do
begin
posofword:=poswords[m];
posline:=poslines[m];
l1:=length(inttostr(alllines));
l2:=length(inttostr(posline));
l3:=length(inttostr(maxsymbol));
l4:=length(inttostr(posofword));
if
l1>l2
then
begin
c1:=l1-l2;
for
i:=
1
to
c1
do
enc1:=enc1+
'0'
;
end
;
if
l3>l4
then
begin
c2:=l3-l4;
for
i:=
1
to
c2
do
enc2:=enc2+
'0'
;
end
;
mas[ind]:=(enc1+inttostr(posline)+enc2+inttostr(posofword)+
' '
);
inc(ind);
enc1:=
''
;
enc2:=
''
;
if
posofword=
0
then
begin
inc(countnul);
dec(same);
end
;
end
;
i:=
1
;
k:=
1
;
while
i<=
100
do
begin
while
k<=same
do
begin
massive[i]:=mas[k];
inc(k);
inc(i);
end
;
k:=
1
;
inc(i);
end
;
if
countnul>same
then
for
i:=
1
to
same+countnul+same-
1
do
write
(massive[i])
else
for
i:=
1
to
same+countnul+
1
do
write
(massive[i])
end
;
Function
Decode(input:
string
;book:tbookdata):
string
;
var
l1,l2:
integer
;
st1,st2:
string
;
l3:
integer
;
i:
integer
;
st3:
string
;
yes:
integer
;
Begin
l1:=length(inttostr(alllines));
l3:=length(input);
for
i:=
1
to
l1
do
st1:=st1+input[i];
For
i:=l1+
1
to
l3
do
st2:=st2+input[i];
i:=
1
;
while
st1[i]=
'0'
do
if
st1[i]=
'0'
then
Delete(st1,i,
1
)
else
break;
while
st2[i]=
'0'
do
if
st2[i]=
'0'
then
Delete(st2,i,
1
)
else
break;
st3:=book[strtoint(st1)].w;
DivStrToWrd(st3);
write
(wrd[strtoint(st2)]+
' '
);
end
;
var
str:
string
;
i:
integer
;
w:
string
;
c:
integer
;
k:
integer
;
g:
integer
;
max:
integer
;
l:
integer
;
yes:
integer
;
wordsame:
string
;
book:tbookdata;
begin
SetConsoleCP(
1251
);
SetConsoleOutputCP(
1251
);
maxsymbol:=
0
;
index:=
0
;
book:=loadbook;
for
i:=
0
to
length(book)-
1
do
if
book[i].symbolcount>=maxsymbol
then
maxsymbol:=book[i].symbolcount;
writeln
(
'Кодируем-1,Декадируем-0: '
);
readln(yes);
writeln
(
'Введите текст'
);
readln(str);
i:=
1
;
while
i<=length(str)
do
begin
while
(i<=length(str))
and
(str[i]=
' '
)
do
i:=i+
1
;
w:=
''
;
while
(i<=length(str))
and
(str[i]<>
' '
)
do
begin
w:=w+str[i];
i:=i+
1
;
end
;
sameword[index]:=w;
inc(index);
end
;
same:=
1
;
for
i:=
0
to
index-
1
do
begin
if
wordsame= sameword[i]
then
begin
tr:=
true
; inc(same);
end
else
wordsame:=sameword[i];
end
;
countwords:=index;
len:=index*index;
if
yes=
1
then
begin
if
tr
then
Encode(sameword[i],book)
else
for
l:=
0
to
index-
1
do
begin
Encode(sameword[l],book);
end
;
end
else
begin
if
tr
then
Encode(sameword[i],book)
else
for
l:=
0
to
index-
1
do
begin
Decode(sameword[l],book);
end
;
end
;
end
.