Advertisement
wojtas626

[PAS] Sprawdzanie nawiasowanie na stosie

Dec 15th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.78 KB | None | 0 0
  1. program Project1;
  2.  
  3. uses
  4.     crt, DOS;
  5. type
  6.   listPtr = ^list;
  7.  
  8.   list = record
  9.        next: listPtr;
  10.        previous: listPtr;
  11.        x: String;
  12.   end;
  13.  
  14. procedure push(var listStart: listPtr; var listEnd: listPtr; y: String);
  15. var
  16.    newElement: listPtr;
  17. begin
  18.      clrScr;
  19.      if listEnd = NIL then
  20.      begin
  21.           newElement := listStart;
  22.           new(newElement);
  23.  
  24.           newElement^.x := y;
  25.           newElement^.next := NIL;
  26.           newElement^.previous := NIL;
  27.           listEnd := newElement;
  28.           listStart := newElement;
  29.      end else
  30.      begin
  31.           newElement := listEnd;
  32.           New(newElement^.next);
  33.           newElement := newElement^.next;
  34.           newElement^.x := y;
  35.  
  36.           newElement^.next := NIL;
  37.           newElement^.previous := listEnd;
  38.           listEnd := newElement;
  39.      end;
  40. end;
  41.  
  42.  
  43. function pop(var listStart: listPtr; var listEnd: listPtr): String;
  44. var
  45.    pointer: listPtr;
  46. begin
  47.      if listStart^.next = NIL then
  48.      begin
  49.           pointer := listStart;
  50.           pop := pointer^.x;
  51.           dispose(pointer);
  52.           listStart := NIL;
  53.           listEnd := NIL;
  54.      end else
  55.      begin
  56.           pointer := listEnd;
  57.           listEnd := pointer^.previous;
  58.           listEnd^.next := NIL;
  59.           pop := pointer^.x;
  60.           dispose(pointer);
  61.      end;
  62. end;
  63.  
  64.  
  65. function isEmpty(var listStart: listPtr): Boolean;
  66. begin
  67.      clrScr;
  68.      if listStart = NIL then
  69.          isEmpty := True
  70.      else
  71.          isEmpty := False;
  72. end;
  73.  
  74.  
  75.  
  76. function readBracket(var listStart: listPtr; var listEnd: listPtr): Boolean;
  77. var
  78.    line: String;
  79. begin
  80.      readBracket := True;
  81.      writeLn('Wprowadz nawias lub 0, aby zakonczyc: ');
  82.      ReadLn(line);
  83.  
  84.      if (line = '{') or (line = '(') then
  85.      begin
  86.           push(listStart, listEnd, line);
  87.      end else if (line = ')') then
  88.      begin
  89.           if not isEmpty(listStart) then
  90.           begin
  91.                if pop(listStart, listEnd) = '(' then
  92.                begin
  93.                     writeLn('Nawias pasuje!');
  94.                     delay(500);
  95.                end else
  96.                begin
  97.                     readBracket := False;
  98.                     writeLn('Nawias NIE pasuje!');
  99.                     delay(500);
  100.                end;
  101.           end else
  102.           begin
  103.                writeLn('Brak nawiasow na stosie!');
  104.                delay(500);
  105.           end;
  106.      end else if (line = '}') then
  107.      begin
  108.           if not isEmpty(listStart) then
  109.           begin
  110.                if pop(listStart, listEnd) = '{' then
  111.                begin
  112.                     writeLn('Nawias pasuje!');
  113.                     delay(500);
  114.                end else
  115.                begin
  116.                     readBracket := False;
  117.                     writeLn('Nawias NIE pasuje!');
  118.                     delay(500);
  119.                end;
  120.           end else
  121.           begin
  122.                writeLn('Brak nawiasow na stosie!');
  123.                delay(500);
  124.           end;
  125.      end else if (line = '0') then
  126.      begin
  127.           readBracket := False;
  128.           if isEmpty(listStart) then
  129.               begin
  130.                    writeLn('Nawiasowanie poprawne!');
  131.               end else
  132.               begin
  133.                    writeLn('Nawiasowanie bledne!');
  134.               end;
  135.           delay(500);
  136.      end else
  137.      begin
  138.           writeLn('Bledne dane wejsciowe, wpisz jeszcze raz!');
  139.           delay(500);
  140.      end;
  141. end;
  142.  
  143. var
  144.    listStart, listEnd: listPtr;
  145.    go: Boolean;
  146. begin
  147.      listEnd:= NIL;
  148.      listStart := NIL;
  149.  
  150.      go := True;
  151.  
  152.      clrScr;
  153.      while go do
  154.      begin
  155.           clrScr;
  156.           go := readBracket(listStart, listEnd);
  157.      end;
  158.  
  159.      writeLn('Nacisnij dowolny klawisz aby wyjsc...');
  160.      ReadKey;
  161.  
  162. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement