忽略Pascal中的注释

时间:2015-06-02 19:42:50

标签: pascal freepascal

我做了以下计划:

program test;

uses crt;

var
  Input: text;
  Line: string;
  CharCount: Integer;

procedure FindIdentifiers(Line: string);
var
  Word: string;
  WordFound: Boolean;
  NumberFound: Boolean;

begin
  Word := '';
  WordFound := false;
  NumberFound := false;
  for CharCount := 1 to Length(Line) do
  begin
    if Line[CharCount] in [' ', '.', ',', '+', '-', '''', '*', '=', '!', '?',
      ':', ';', '(', ')', '/', '\', '[', ']', '^', '>', '<'] then
    begin
      if WordFound then
      begin
        if not NumberFound then
        begin   
          writeln(Word);    
        end;
        WordFound := false;
        NumberFound := false;
        Word := '';
      end;
    end
    else
    begin
      if not WordFound then
      begin
        if Line[CharCount] in ['0' .. '9'] then
          NumberFound := true
        else
          WordFound := true;
      end;
      Word := Word + Line[CharCount];
    end;
  end;
end;

begin
  assign(Input, 't.pas');
  reset(Input);

  while not eof(Input) do
  begin
    readln(Input, Line);

    FindIdentifiers(Line);
  end;

  repeat
  until keypressed;    
end.

该程序的输入是:

program p;
var i:integer;
{
this is comment
}
begin
end.

它必须忽略该注释但不删除只是忽略.. 输出必须是这样的:

program
p
var
i
integer
begin
end

实际输出是:

program
p
var
i
integer
this
is
end

0 个答案:

没有答案