Задание: Дан текстовый файл F, удалить из файла слова длиннее 5 символов, результат записать в фал G. Вот пример моего кода, сделайте пожалуйста код не таким большим, и подругому. program ShortWordsFiler; type FSMState = (Reading,Skiping); FSMEvent = ( (*Reading events*) NoWordFound,WordGoesOn,ShortWordFinished,LongWordDetected, (*Skiping events*) LongWordGoesOn,LongWordFinished ); var Input: file of char; Output: Text; iBuf :Char; oBuf :String; Evt : FSMEvent; State: FSMState; const WordChar: set of Char = ['a'..'z','A'..'Z']; begin State := Reading; assign(Input,'c:\tmp\F.txt'); assign(Output,'c:\tmp\G.txt'); reset(Input); rewrite(Output);
while not eof(input) do begin Read(Input,iBuf); if length(oBuf) < SizeOf(String) then oBuf:=oBuf+iBuf; case State of Reading: begin if iBuf in WordChar then begin case length(oBuf) of 1..5: Evt:=WordGoesOn; else Evt:=LongWordDetected; end; end else begin case length(oBuf) of 1 : Evt:=NoWordFound; else Evt:=ShortWordFinished; end; end; end; Skiping: begin if iBuf in WordChar then Evt:= LongWordGoesOn else Evt:= LongWordFinished; end; end; case Evt of WordGoesOn, LongWordGoesOn: begin end; LongWordDetected: begin State:=Skiping; end; ShortWordFinished: begin oBuf[length(oBuf)]:=' '; Write(Output,oBuf); oBuf:=''; State:=Reading; if iBuf in [#13,#10] then Write(Output,iBuf); end; LongWordFinished: begin oBuf:=''; State:=Reading; if iBuf in [#13,#10] then Write(Output,iBuf); end; NoWordFound:begin oBuf:=''; if iBuf in [#13,#10] then Write(Output,iBuf); end; end; end; close(Input); close(Output); end.
|
|
|