Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program StackPolish;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- Type
- TElement = String;
- TStackPointer = ^TStack;
- TStack = record
- Data: TElement;
- Next: TStackPointer;
- end;
- TMyStack = Class(TObject)
- private
- fHead: TStackPointer;
- public
- constructor Create;
- destructor Destroy;
- procedure Push(Item: TElement);
- procedure Pop();
- function IsEmpty: Boolean;
- function GetData(): TElement;
- procedure GoNext();
- End;
- constructor TMyStack.Create;
- begin
- fHead := nil;
- end;
- destructor TMyStack.Destroy;
- begin
- while not isEmpty do
- Pop;
- fHead := nil;
- end;
- procedure TMyStack.Push(Item: TElement);
- var
- Temp: TStackPointer;
- begin
- New(Temp);
- Temp.Data := Item;
- Temp.Next := nil;
- if IsEmpty then
- fHead := Temp
- else
- begin
- Temp.Next := fHead;
- fHead := Temp;
- end;
- end;
- function TMyStack.GetData(): TElement;
- begin
- if not isEmpty then
- Result := fHead.Data;
- end;
- procedure TMyStack.GoNext();
- begin
- fHead := fHead.Next;
- end;
- procedure TMyStack.Pop();
- var
- Temp: TStackPointer;
- begin
- if not IsEmpty then
- begin
- Temp := fHead;
- fHead := fHead.Next;
- Dispose(Temp);
- end;
- end;
- function TMyStack.IsEmpty: Boolean;
- begin
- Result := fHead = nil;
- end;
- const
- symb = ['a'..'z', 'A'..'Z', '0'..'9', '.'];
- num = ['0'..'9'];
- var
- InputString, S, Ans, Temp: String;
- i, j, ToPush, ToSkip: Integer;
- MainStack, TempStack: TMyStack;
- err: Longint;
- function GetPriority(c: Char): Byte;
- begin
- Result := 0;
- case c of
- '(': Result := 1;
- '+', '-': Result := 2;
- '*', '/': Result := 3;
- '^', '!': Result := 4;
- 'c', 's', 'l', 'e': Result := 5;
- 'n': Result := 6;
- end;
- end;
- function DeleteSpaces(S: String): String;
- begin
- S := S + ' ';
- i := 1;
- while (i < Length(S)) do
- begin
- if (S[i] = ' ') then
- Delete(S, i, 1)
- else
- Inc(i);
- end;
- Delete(S, Length(S), 1);
- Result := S;
- end;
- function Reverse(S: String): String;
- var
- i: Integer;
- Temp: Char;
- begin
- for i := 1 to (Length(s) div 2) do
- begin
- Temp := s[i];
- s[i] := s[Length(s) - i + 1];
- s[Length(s) - i + 1] := Temp;
- end;
- Result := S;
- end;
- begin
- Writeln('Enter expression: ');
- Readln(InputString);
- S := DeleteSpaces(InputString);
- MainStack := TMyStack.Create;
- TempStack := TMyStack.Create;
- for i := 1 to Length(s) do
- begin
- if ToSkip > 0 then
- begin
- Dec(ToSkip);
- continue;
- end;
- if (s[i] in symb) then
- begin
- j := i;
- Temp := '';
- while s[j] in symb do
- begin
- Temp := Temp + s[j];
- Inc(ToSkip);
- Inc(j);
- if j > Length(s) then
- break;
- end;
- MainStack.Push(Reverse(Temp));
- Dec(ToSkip);
- continue;
- end
- else
- begin
- if s[i] = ')' then
- begin
- while TempStack.GetData <> '(' do
- begin
- MainStack.Push(TempStack.GetData);
- TempStack.Pop();
- end;
- TempStack.Pop();
- continue;
- end;
- if (TempStack.IsEmpty) or (GetPriority(s[i]) > GetPriority(TempStack.GetData[1])) or (s[i] = '(') then
- TempStack.Push(s[i])
- else
- begin
- while GetPriority(TempStack.GetData[1]) >= GetPriority(s[i]) do
- begin
- MainStack.Push(TempStack.GetData);
- TempStack.Pop;
- if TempStack.IsEmpty then
- break;
- end;
- if (s[i] <> '(') and (s[i] <> ')') then
- TempStack.Push(s[i]);
- end;
- end;
- end;
- while not TempStack.IsEmpty do
- begin
- MainStack.Push(TempStack.GetData);
- TempStack.GoNext;
- end;
- while not MainStack.IsEmpty do
- begin
- Ans := Ans + MainStack.GetData + ' ';
- MainStack.GoNext;
- end;
- Writeln('Result: ');
- for i := Length(Ans) downto 1 do
- Write(Ans[i]);
- MainStack.Destroy;
- TempStack.Destroy;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement