Страница 1 из 1

Кодирование и декодирование текстовой информации

Добавлено: 18 окт 2006, 22:37
multiblock
Народ, помогите! Вот объединил две программы (кодировщик и декодировщик) а паскаль ругается на конец программы
(...end;
read(s);
end. ): error 85 ";" expected. Понятно что пропустил знак точка с запятой, но вопрос: какая в конце программы точка с запятой??? Там должен быть and. а он говорит надо ; Ни чего не могу понять... Вот код программы:

Код: Выделить всё

program Coding_and_decoding_of_the_text_information_by_method_Gronsfelds;
uses crt,dos;

{Procedures and function}

function getnum(c: char): integer;

var
  n: integer;

{Show title}

procedure title;
begin
window(1,1,1,80);
textbackground(2);
clrscr;
gotoxy(27,1);
textbackground(5);
textcolor(133); write('CODETIMEG');
end;

{Main change}

const
  N1 = 5; { The maximum quantity of lines in a file }
  N2 = 3; { Quantity of alphabets }
var
  f,f1: text;
  s,ne,s1: string;
  i,j,k,l,m: integer;
  ce: integer; { The counter of a position in a line of a code }
  w : array [1..N1] of string; { Lines }
  w1 : array [1..N1] of string; { Lines }
  abc : array [1..N2] of string; { Alphabets }
  sign : string;

label menugame,coding,decoding,help;

{Main part}

begin
clrscr;

{Menu programm}

menugame:
while 1>0 do
begin
  title;
  gotoxy(30,18);
  textcolor(1);
  write('Press C for coding');
  gotoxy(30,20);
  textcolor(1);
  write('Press D for decoding');
  gotoxy(30,22);
  textcolor(1);
  write('Press H for help');
  gotoxy(30,24);
  textcolor(1);
  write('Press E for exit');
  case readkey of
   #99 : goto coding;
   #100 : goto decoding;
   #104 : goto help;
   #101 : exit;
  end;
end;

{Coding}

coding:
begin
  case c of
    '0': n := 0;
    '1': n := 1;
    '2': n := 2;
    '3': n := 3;
    '4': n := 4;
    '5': n := 5;
    '6': n := 6;
    '7': n := 7;
    '8': n := 8;
    '9': n := 9;
  else
    n := -1;
  end;
  getnum := n;
end;

begin
  abc[1] := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  abc[2] := 'abcdefghijklmnopqrstuvwxyz';
  abc[3] := '0123456789';
  writeln(' *** An encryption method Gronsfeld ***');
  write('Enter a full way to a file from which it is readable: '); readln(s);
  write('Enter a full way to a file in which we write: '); readln(s1);
  write('Enter a digital key (4 figures or less): '); readln(n);
  assign(f,s); assign(f1,s1);
  reset(f); rewrite(f1);
  i := 1;

  { Reads a file }

  while not EOF(f) and (i<=N1) do begin
    readln(f,w[i]); w1[i]:=w[i];
    inc(i);
  end;

  { We code }
    { 1-st cycle - in the lines from a file }
    { 2-nd cycle - on elements in line from a file }
    { 3-rd cycle - under alphabets }
    { 4-th cycle - on elements of the alphabet }

  ce := 1;
  for i := 1 to N1 do if ord(w[i][0])<>0 then
  for j := 1 to (ord(w[i][0])+1) do
  for k := 1 to N2 do
  for l := 1 to (ord(abc[k][0])+1) do begin
    if abc[k][l]=w[i][j] then begin
      m := l+getnum(ne[ce]); inc(ce); if ce>ord(ne[0]) then ce := 1;
      if m>ord(abc[k][0]) then m := m-ord(abc[k][0]);
      w1[i][j] := abc[k][m];
    end;
  end;
  for i:=1 to N1 do begin
    writeln(w[i]);
    writeln(w1[i]);
  end;
  for i := 1 to N1 do begin
    for j := 1 to ord(w1[i][0]) do write(f1,w1[i][j]);
   writeln(f1,s1,'');
end;
begin
  title;
  gotoxy(30,18);
  textcolor(1);
  write('Press C for coding');
  gotoxy(30,20);
  textcolor(1);
  write('Press D for decoding');
  gotoxy(30,22);
  textcolor(1);
  write('Press H for help');
  gotoxy(30,24);
  textcolor(1);
  write('Press E for exit');
  case readkey of
   #99 : goto coding;
   #100 : goto decoding;
   #104 : goto help;
   #101 : exit;
  end;
end;
read(s);
end;

{Decoding}

decoding:
begin
  case c of
    '0': n := 0;
    '1': n := 1;
    '2': n := 2;
    '3': n := 3;
    '4': n := 4;
    '5': n := 5;
    '6': n := 6;
    '7': n := 7;
    '8': n := 8;
    '9': n := 9;
  else
    n := -1;
  end;
  getnum := n;
end;

begin
  abc[1] := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  abc[2] := 'abcdefghijklmnopqrstuvwxyz';
  abc[3] := '0123456789';
  writeln(' *** An encryption method Gronsfeld ***');
  write('Enter a full way to a file from which it is readable: '); readln(s);
  write('Enter a full way to a file in which we write: '); readln(s1);
  write('Enter a digital key (4 figures or less): '); readln(n);
  assign(f,s); assign(f1,s1);
  reset(f); rewrite(f1);
  i := 1;

  { Reads a file }

  while not EOF(f) and (i<=N1) do begin
    readln(f,w[i]); w1[i]:=w[i];
    inc(i);
  end;

  { We code }
    { 1-st cycle - in the lines from a file }
    { 2-nd cycle - on elements in line from a file }
    { 3-rd cycle - under alphabets }
    { 4-th cycle - on elements of the alphabet }

  ce := 1;
  for i := 1 to N1 do if ord(w[i][0])<>0 then
  for j := 1 to (ord(w[i][0])+1) do
  for k := 1 to N2 do
  for l := 1 to (ord(abc[k][0])+1) do begin
    if abc[k][l]=w[i][j] then begin
      m := l-getnum(ne[ce]); inc(ce); if ce>ord(ne[0]) then ce := 1;
      if m<1 then m := m+ord(abc[k][0]);
      w1[i][j] := abc[k][m];
    end;
  end;
  for i:=1 to N1 do begin
    writeln(w[i]);
    writeln(w1[i]);
  end;
  for i := 1 to N1 do begin
    for j := 1 to ord(w[i][0]) do write(f1,w1[i][j]);
    writeln(f1,s1,'');
   end;
begin
  gotoxy(30,18);
  textcolor(1);
  write('Press C for coding');
  gotoxy(30,20);
  textcolor(1);
  write('Press D for decoding');
  gotoxy(30,22);
  textcolor(1);
  write('Press H for help');
  gotoxy(30,24);
  textcolor(1);
  write('Press E for exit');
  case readkey of
   #99 : goto coding;
   #100 : goto decoding;
   #104 : goto help;
   #101 : exit;
  end;
end;
read(s);
end;

{Help}

help:
begin
  write('This is help');
end;
begin
  gotoxy(30,18);
  textcolor(1);
  write('Press C for coding');
  gotoxy(30,20);
  textcolor(1);
  write('Press D for decoding');
  gotoxy(30,22);
  textcolor(1);
  write('Press H for help');
  gotoxy(30,24);
  textcolor(1);
  write('Press E for exit');
  case readkey of
   #99 : goto coding;
   #100 : goto decoding;
   #104 : goto help;
   #101 : exit;
  end;
end.

Добавлено: 19 окт 2006, 09:43
Лелик_1044
Статистика
begin - 21
end - 26
case - 6
Значит не хватает 1 end.
А смотрит она в конце потому что так логично
Разбей на функции и процедуры ее легче читать будет ии ошибку быстрее найдешь

Добавлено: 19 окт 2006, 09:50
somewhere
for i := 1 to N1 do begin
for j := 1 to ord(w1[0]) do write(f1,w1[j]);
writeln(f1,s1,'');
end;
begin


Вот здесь смотри

Re: Кодирование и декодирование текстовой информации

Добавлено: 10 дек 2006, 19:06
multiblock
Большое спасибо! Все заработало! Можно еще один вопрос задать такой: есть где нибудь исходники на Дельфи по данной теме. Мне нужен алгоритм. Просто перешел на дельфи, программу написал, а вот алгоритм ни как не получается... Надеюсь поможите. Если нужно скину исходники программы. Заранее спасибо!
P.s. пытался данный код под дельфи подогнать - итог полное фиаско

Re: Кодирование и декодирование текстовой информации

Добавлено: 11 дек 2006, 11:15
somewhere
Кидай, переделаем...

Re: Кодирование и декодирование текстовой информации

Добавлено: 13 май 2007, 12:37
Mihanik
Добрый день! Есть программа по методу Гронсфельда (не моя правда). Проблема в том, что не могу понять прицип работы т.е. нужен алгоритм этой программы!

Код: Выделить всё

function GronsfeldEncipher(toCode, K: string): string;
var i, T, _T: integer;
begin
  for i := 1 to length(toCode) do begin
    _T := ord(toCode[ i ]);

    T := (Ord(toCode[ i ])

      +
      (Ord(K[(pred(i) mod length(K)) + 1]) - Ord('0'))

         );

    if T >= 256 then dec(T, 256);
    toCode[ i ] := Chr(T);
  end;
  GronsfeldEncipher := toCode;
end;

function GronsfeldDecipher(toDecode, K: string): string;
var i, T: integer;
begin
  for i := 1 to length(toDecode) do begin
    T := (Ord(toDecode[i])

      -
      (Ord(K[(pred(i) mod length(K)) + 1]) - Ord('0'))

         );
    if T < 0 then Inc(T, 256);
    toDecode[ i ] := Chr(T);
  end;
  GronsfeldDecipher := toDecode;
end;

var
  s: string;

  f_in, f_out: text;


begin
  if (paramcount < 3) or (

    (paramstr(1) <> '/e') and (paramstr(1) <> '/d')

  ) then exit;

  assign(f_in, paramstr(2));
  reset(f_in);
  assign(f_out, paramstr(3));
  rewrite(f_out);

  while not eof(f_in) do begin

    readln(f_in, s);
    if paramstr(1) = '/e' then
      s := GronsfeldEncipher(s, '2178')
    else
      s := GronsfeldDecipher(s, '2178');

    writeln(f_out, s);
  end;

  close(f_out);
  close(f_in);
end.