Advertisement
MadCortez

Untitled

Nov 10th, 2021
1,800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.27 KB | None | 0 0
  1. program StackPolish;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8.   System.SysUtils;
  9.  
  10.  
  11. Type
  12.    TElement = String;
  13.    TStackPointer = ^TStack;
  14.    TStack = record
  15.       Data: TElement;
  16.       Next: TStackPointer;
  17. end;
  18.  
  19. TMyStack = Class(TObject)
  20.    private
  21.       fHead: TStackPointer;
  22.    public
  23.       constructor Create;
  24.       destructor Destroy;
  25.       procedure Push(Item: TElement);
  26.       procedure Pop();
  27.       function IsEmpty: Boolean;
  28.       function GetData(): TElement;
  29.       procedure GoNext();
  30. End;
  31.  
  32. constructor TMyStack.Create;
  33. begin
  34.    fHead := nil;
  35. end;
  36.  
  37. destructor TMyStack.Destroy;
  38. begin
  39.    while not isEmpty do
  40.       Pop;
  41.    fHead := nil;
  42. end;
  43.  
  44. procedure TMyStack.Push(Item: TElement);
  45. var
  46.    Temp: TStackPointer;
  47. begin
  48.    New(Temp);
  49.    Temp.Data := Item;
  50.    Temp.Next := nil;
  51.    if IsEmpty then
  52.       fHead := Temp
  53.    else
  54.    begin
  55.       Temp.Next := fHead;
  56.       fHead := Temp;
  57.    end;
  58. end;
  59.  
  60. function TMyStack.GetData(): TElement;
  61. begin
  62.    if not isEmpty then
  63.       Result := fHead.Data;
  64. end;
  65.  
  66. procedure TMyStack.GoNext();
  67. begin
  68.    fHead := fHead.Next;
  69. end;
  70.  
  71. procedure TMyStack.Pop();
  72. var
  73.    Temp: TStackPointer;
  74. begin
  75.    if not IsEmpty then
  76.    begin
  77.       Temp := fHead;
  78.       fHead := fHead.Next;
  79.       Dispose(Temp);
  80.    end;
  81. end;
  82.  
  83. function TMyStack.IsEmpty: Boolean;
  84. begin
  85.    Result := fHead = nil;
  86. end;
  87.  
  88.  
  89. const
  90.    symb = ['a'..'z', 'A'..'Z', '0'..'9', '.'];
  91.    num = ['0'..'9'];
  92.  
  93. var
  94.    InputString, S, Ans, Temp: String;
  95.    i, j, ToPush, ToSkip: Integer;
  96.    MainStack, TempStack: TMyStack;
  97.    err: Longint;
  98.  
  99. function GetPriority(c: Char): Byte;
  100. begin
  101.    Result := 0;
  102.    case c of
  103.       '(': Result := 1;
  104.       '+', '-': Result := 2;
  105.       '*', '/': Result := 3;
  106.       '^', '!': Result := 4;
  107.       'c', 's', 'l', 'e': Result := 5;
  108.       'n': Result := 6;
  109.    end;
  110. end;
  111.  
  112. function DeleteSpaces(S: String): String;
  113. begin
  114.    S := S + ' ';
  115.    i := 1;
  116.    while (i < Length(S)) do
  117.    begin
  118.       if (S[i] = ' ') then
  119.          Delete(S, i, 1)
  120.       else
  121.          Inc(i);
  122.    end;
  123.    Delete(S, Length(S), 1);
  124.    Result := S;
  125. end;
  126.  
  127. function Reverse(S: String): String;
  128. var
  129.    i: Integer;
  130.    Temp: Char;
  131. begin
  132.    for i := 1 to (Length(s) div 2) do
  133.    begin
  134.       Temp := s[i];
  135.       s[i] := s[Length(s) - i + 1];
  136.       s[Length(s) - i + 1] := Temp;
  137.    end;
  138.    Result := S;
  139. end;
  140.  
  141. begin
  142.    Writeln('Enter expression: ');
  143.    Readln(InputString);
  144.    S := DeleteSpaces(InputString);
  145.    MainStack := TMyStack.Create;
  146.    TempStack := TMyStack.Create;
  147.    for i := 1 to Length(s) do
  148.    begin
  149.       if ToSkip > 0 then
  150.       begin
  151.          Dec(ToSkip);
  152.          continue;
  153.       end;
  154.       if (s[i] in symb) then
  155.       begin
  156.          j := i;
  157.          Temp := '';
  158.          while s[j] in symb do
  159.          begin
  160.             Temp := Temp + s[j];
  161.             Inc(ToSkip);
  162.             Inc(j);
  163.             if j > Length(s) then
  164.                break;
  165.          end;
  166.          MainStack.Push(Reverse(Temp));
  167.          Dec(ToSkip);
  168.          continue;
  169.       end
  170.       else
  171.       begin
  172.          if s[i] = ')' then
  173.          begin
  174.             while TempStack.GetData <> '(' do
  175.             begin
  176.                MainStack.Push(TempStack.GetData);
  177.                TempStack.Pop();
  178.             end;
  179.             TempStack.Pop();
  180.             continue;
  181.          end;
  182.          if (TempStack.IsEmpty) or (GetPriority(s[i]) > GetPriority(TempStack.GetData[1])) or (s[i] = '(')  then
  183.             TempStack.Push(s[i])
  184.          else
  185.          begin
  186.             while GetPriority(TempStack.GetData[1]) >= GetPriority(s[i]) do
  187.             begin
  188.                MainStack.Push(TempStack.GetData);
  189.                TempStack.Pop;
  190.                if TempStack.IsEmpty then
  191.                   break;
  192.             end;
  193.             if (s[i] <> '(') and (s[i] <> ')') then
  194.                TempStack.Push(s[i]);
  195.          end;
  196.       end;
  197.    end;
  198.    while not TempStack.IsEmpty do
  199.    begin
  200.       MainStack.Push(TempStack.GetData);
  201.       TempStack.GoNext;
  202.    end;
  203.    while not MainStack.IsEmpty do
  204.    begin
  205.       Ans := Ans + MainStack.GetData + ' ';
  206.       MainStack.GoNext;
  207.    end;
  208.    Writeln('Result: ');
  209.    for i := Length(Ans) downto 1 do
  210.       Write(Ans[i]);
  211.    MainStack.Destroy;
  212.    TempStack.Destroy;
  213.    Readln;
  214. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement