Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program poland;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- PStack =^ TStack;
- TStack = record
- Next : PStack;
- Data : Char;
- end;
- var
- Stack : PStack;
- procedure InitialiseStack ();
- begin
- Stack := nil;
- end;
- function Pop (): Char; // достать из стека
- Var
- Temp : PStack;
- begin
- Temp := Stack;
- Stack := Stack^.Next;
- Pop := Temp^.Data;
- Dispose(Temp);
- end;
- procedure Push (Data : Char); // добавить в стек
- Var
- NewNode : PStack;
- begin
- New(NewNode);
- NewNode^.Next := Stack;
- NewNode^.Data := Data;
- Stack := NewNode;
- end;
- function ShowTop () : Char;
- begin
- ShowTop := Stack.Data;
- end;
- function IsEmpty(): Boolean;
- begin
- Result := (Stack = nil);
- end;
- function Convert (Str : String) : String;
- Var
- I : Integer;
- Const
- Chars = ['+', '-','^', '*','/', '(', ')'];
- begin
- I := 1;
- While (I <> (Str.Length + 1)) Do
- begin
- if Str[I] in Chars then
- begin
- If IsEmpty() then
- Push(Str[I])
- else
- begin
- case Str[I] of
- '+':
- begin
- While (Stack <> nil) and (ShowTop <> '(') Do
- begin
- Result := Result + Pop();
- end;
- Push(Str[I]);
- end;
- '-':
- begin
- While (Stack <> nil) and (ShowTop <> '(') Do
- begin
- Result := Result + Pop();
- end;
- Push(Str[I]);
- end;
- '*':
- begin
- While (Stack <> nil) and (ShowTop() <> '+') and (ShowTop() <> '-') and (ShowTop <> '(') Do
- begin
- Result := Result + Pop();
- end;
- Push(Str[I]);
- end;
- '/':
- begin
- While (Stack <> nil) and (ShowTop() <> '+') and (ShowTop() <> '-') and (ShowTop <> '(') Do
- begin
- Result := Result + Pop();
- end;
- Push(Str[I]);
- end;
- '^':
- begin
- While (Stack <> nil) and (ShowTop() <> '+') and (ShowTop() <> '-') and (ShowTop() <> '*') and (ShowTop() <> '/') and (ShowTop <> '(') Do
- begin
- Result := Result + Pop();
- end;
- Push(Str[I]);
- end;
- '(':
- begin
- Push(Str[I]);
- end;
- ')':
- begin
- While (Stack.Data <> '(') Do
- Result := Result + Pop();
- Pop();
- end;
- end;
- end;
- end
- else
- begin
- Result := Result + Str[I];
- end;
- Inc(I);
- end;
- While (Stack <> nil) Do
- begin
- Result := Result + Pop();
- end;
- Convert := Result;
- end;
- Var
- Str : String;
- begin
- Writeln('Введите строку:');
- Readln(Str);
- Str := Convert(Str);
- Writeln('Преобразованная строка:');
- Writeln(Str);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement