Don't like ads? PRO users don't see any ads ;-)

Prac_parentheses

By: t1nman on May 28th, 2012  |  syntax: Pascal  |  size: 1.31 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. uses UStack;
  2.  
  3. procedure Inp(var somestack: Stack);
  4. var
  5.     ifp: Text;
  6.     parenthesis: char;
  7. begin
  8.     assign(ifp,'input.txt');
  9.     reset(ifp);
  10.  
  11.     read(ifp,parenthesis);
  12.     while (parenthesis <> '.') do
  13.     begin
  14.         somestack.push(parenthesis);
  15.         read(ifp,parenthesis);
  16.     end;
  17.  
  18.     close(ifp);
  19. end;
  20.  
  21.  
  22. procedure Outp(somestack: Stack);
  23. var
  24.     ofp: Text;
  25.     tmp: Stack;
  26.     null: char;
  27.     flag: boolean;
  28. begin
  29.     tmp.Init;
  30.     flag:= TRUE;
  31.     while not (somestack.isEmpty) do
  32.     begin
  33.         if (somestack.top = ')') then
  34.             tmp.push(somestack.pop)
  35.         else
  36.         begin
  37.             null:= somestack.pop;
  38.             if (tmp.isEmpty) then
  39.             begin
  40.                 flag:= FALSE;
  41.                 break;
  42.             end
  43.             else
  44.                 null:= tmp.pop;
  45.         end;
  46.     end;
  47.  
  48.     assign(ofp,'output.txt');
  49.     rewrite(ofp);
  50.     if ((flag) and (tmp.isEmpty)) then
  51.         writeln(ofp, 'TRUE')
  52.     else
  53.         writeln(ofp, 'FALSE');
  54.     close(ofp);
  55. end;
  56.  
  57. var
  58.     mystack: Stack;
  59. begin
  60.     writeln;
  61.     mystack.Init;
  62.     writeln('start reading from file...');
  63.     Inp(mystack);
  64.     writeln('...finished'); writeln;
  65.    
  66.     writeln('start writing to file...');
  67.     Outp(mystack);
  68.     writeln('...finished'); writeln;
  69. end.