Стили и методы программирования
4cab9ef0

Вторая программа, чуть дальше от


ENTRY Go{=<Open 'r' 1 'input.txt'><Open 'w' 2 'output.txt'><Init<Get 1>>}; Letters {='abcdefghijklmnopqrstuvwxyz';}; Init{=; e.1=<St1 e.1>;}; St1 { s.1 e.3,<Letters>: e.A s.1 e.B =<St2 e.3 (s.1)>; = <Init<Get 1>>; s.1 e.3 =<St1 e.3>; }; St2 {(e.2)= <Outstr e.2> <Init<Get 1>>; s.1 e.3 (e.2),<Letters>: e.A s.1 e.B =<St2 e.3 (e.2 s.1)>; s.1 e.3 (e.2)=<Outstr e.2><St1 e.3 >; }; * St3 не нужно Outstr { e.2, <Lenw e.2>: {s.1 e.2 = <Putout 2 e.2 " - " <Symb s.1>>;}; }; * * Вторая программа, чуть дальше от непосредственной автоматной модели * $ENTRY Go{=<Open 'r' 1 'input.txt'><Open 'w' 2 'output.txt'><Init <Get 1>>}; Letters {='abcdefghijklmnopqrstuvwxyz';}; Init {=; e.1=<Parse e.1()>;}; Parse { s.1 e.3 (e.2),<Letters>: e.A s.1 e.B =<Parse e.3 (e.2 s.1)>; (e.2)= <Outstr e.2> <Init<Get 1>>; s.1 e.3 (e.2)=<Outstr e.2><Parse e.3 ()>; }; Outstr { = ; e.2, <Lenw e.2>: {s.1 e.2 = <Putout 2 e.2 " - " <Symb s.1>>;}; };
Листинг 10.2.1. Длины слов: рефал
Закрыть окно




// Реализация автомата с табл. 9.1 #include <stdlib.h> #include <stdio.h> #include <time.h>
char symbol; int cnt;
enum States { St1, St2, St3 } State; void main( void ) {fstream a,b; a.open("input.txt",ios::in); b.open("output.txt",ios::out); State = St1; while (true ) {symbol=a.get(); switch ( State ) { case St1: if ('a'<=symbol && symbol <= 'z') { b<<symbol; cnt = 1; State = St2; } else if (symbol != '\n') {State = St1;} else if (symbol == '\n') State = St3; break; case St2: if ('a'<=symbol && symbol <= 'z') {b<< symbol; cnt++; // State = St2; } else if (symbol != '\n') { b<<" - "<<cnt<<endl; State = St1; } else if (symbol == '\n') { b<<" - "<<cnt<<endl; State=St3; }; break; case St3: if ('a'<=symbol && symbol <= 'z') { b<< symbol; cnt = 1; State = St2; } else if (symbol != '\n') {State = St1;} else if (symbol == '\n') { a.close(); b.close(); return;}; } } }
Листинг 10.2.2. Длины слов: явный цикл обработки потока
Закрыть окно




#include <stdlib.h> #include <stdio.h> #include <time.h>
char symbol; int cnt; enum States { St1, St2, St3, Exit } State;


inline States f_St1 () { if ('a'<=symbol && symbol <= 'z') printf ("%c", symbol); cnt = 1; symbol = getchar (); return St2; } else if (symbol != '\n') { symbol = getchar (); return } else {symbol = getchar (); return }
inline States f_St2 () { if ('a'<=symbol && symbol <= 'z') printf ("%c", symbol); cnt++; symbol = getchar (); return St2; } else if (symbol != '\n') { printf (" -%i\n", cnt); symbol = getchar (); return St1; } else { printf (" - %i\n", cnt); symbol = getchar (); return St3; } }
inline States f_St3 () { if ('a'<=symbol && symbol <= 'z') { printf ("%c", symbol); cnt = 1; symbol = getchar (); return St2; } else if (symbol != '\n') { symbol = getchar (); return St1; } else return Exit; }
void main( void ) { symbol = getchar (); State = St1; for (;;) { switch ( State ) { case St1: State = f_St1 (); break; case St2: State = f_St2 (); break; case St3: State = f_St3 (); break; default: return; } } }
Листинг 10.2.3. Длины слов: использование процедур для описания состояний.
Закрыть окно




program funcgoto; {$APPTYPE CONSOLE} {$T+} uses SysUtils;
type P=procedure; type Pp= ^P; const maxstate = 7; const maxcond = 3; type states = 1.. maxstate; conds = 1.. maxcond; type table = array [states, conds] of states; type act = array [states] of P; const gotos: table = ((2,2,2),(3,2,4),(3,5,6),(3,2,7),(3,2,4),(3,2,7),(1,1,1)); var Symbol: char; var Cnt: integer; var Inf, Outf: text; var state: states;
procedure Start; begin Cnt:=0; AssignFile(Inf, 'input.txt'); Reset(Inf); AssignFile(Outf, 'output.txt'); Rewrite(Outf); end;
procedure Finish; begin Closefile(Inf); Closefile(Outf); Abort; end;
procedure St1; begin {No actions} end;
procedure St2; begin write(outf,Symbol); Inc(Cnt); end;
procedure St4; begin writeln(outf,' - ',Cnt); Cnt:=0; end;
const actions: act = (Start, St1,St2,St1,St4,St4,Finish);
begin state:=1; while true do begin actions[state]; if (state <>1) and (state<>7) then begin read(inf,Symbol); if Ord(Symbol)=10 then read(inf,Symbol) end; if Symbol in ['a'..'z'] then state:= gotos[state,1]; if not(Symbol in ['a'..'z']) and (Ord(Symbol)<>13) then state:=gotos[state,2]; if Ord(Symbol)=13 then state:=gotos[state,3]; end; end.
Листинг 10.2.4. Длины слов: массив функций и переходов
Закрыть окно




#include <stdlib.h> #include <stdio.h> #include <time.h>
char symbol; int cnt; void main( void ) { symbol = getchar (); St1: if ('a'<=symbol && symbol <= 'z') { printf ("%c", symbol); cnt = 1; symbol = getchar (); goto St2; } else if (symbol != '\n') { symbol = getchar (); goto St1; } else /* (symbol == '\n') */ {symbol = getchar (); goto St3;}; St2: if ('a'<=symbol && symbol <= 'z') { printf ("%c", symbol); cnt++; symbol = getchar (); goto St2; } else if (symbol != '\n') { printf (" -%i\n", cnt); symbol = getchar (); goto St1; } else { printf (" -%i\n", cnt); symbol = getchar (); goto St3; }; St3: if ('a'<=symbol && symbol <= 'z') { printf ("%c", symbol); cnt = 1; symbol = getchar (); goto St2; } else if (symbol != '\n') { symbol = getchar (); goto St1; } else /* (symbol == '\n') */ return; }
Листинг 10.2.5. Длины слов: состояния - метки в программе.
Закрыть окно



Содержание раздела