Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program AiSD_Stack;
- {$APPTYPE CONSOLE}
- {$R *.res}
- Uses
- System.SysUtils,
- StrUtils;
- Type
- PElem = ^Elem;
- Elem = Record
- Data: Char;
- Next: PElem;
- End;
- IntList = Record
- FirstElem: PElem;
- Length: Integer;
- Rang: Integer;
- End;
- Function IsExpressionNotEmpty(Expression: String): Boolean; StdCall; External 'libInput.dll';
- Function IsExpressionBracketBalanced(Expression: String): Boolean; StdCall; External 'libInput.dll';
- Function IsExpressionNotContainsIllegalCharacters(Expression: String): Boolean; StdCall; External 'libInput.dll';
- Function InputExpression: String; StdCall; External 'libInput.dll';
- Function IsEmptyStack(Stack: IntList): Boolean; //проверка
- Begin
- Result := Stack.FirstElem = Nil;
- End;
- Function Pop(Var Stack: IntList): Char; // вынимание из стека
- Begin
- If IsEmptyStack(Stack) Then
- Result := #0
- Else
- Begin
- Result := Stack.FirstElem^.Data;
- Dec(Stack.Length);
- Stack.FirstElem := Stack.FirstElem^.Next;
- End;
- End;
- Function Peek(Stack: IntList): Char; // вынимание без удаления
- Begin
- If IsEmptyStack(Stack) Then
- Result := #0
- Else
- Result := Stack.FirstElem^.Data;
- End;
- Procedure Push(Var Stack: IntList; Data: Char); // занос в стек
- Var
- NewElem: PElem;
- Begin
- New(NewElem);
- NewElem^.Next := Stack.FirstElem;
- Stack.FirstElem := NewElem;
- Stack.FirstElem^.Data := Data;
- Inc(Stack.Length);
- End;
- Function GetStackPriority(Character: Char): Integer; //стековый приоритет
- Begin
- Case Character Of
- '+', '-':
- Result := 2;
- '/', '*':
- Result := 4;
- '(':
- Result := 0;
- 'a' .. 'z':
- Result := 8;
- '^':
- Result := 5;
- End;
- End;
- Function GetPriority(Character: Char): Integer; //относительный приоритет
- Begin
- Case Character Of
- '+', '-':
- Result := 1;
- '/', '*':
- Result := 3;
- '(':
- Result := 9;
- 'a' .. 'z':
- Result := 7;
- '^':
- Result := 6;
- End;
- End;
- Procedure СalculationOfRang(Var Stack: IntList); //счет ранга выражения
- Begin
- If Peek(Stack) In ['a' .. 'z'] Then
- Inc(Stack.Rang)
- Else
- Dec(Stack.Rang);
- End;
- Function Reverse(Expression: String): String; //суффиксное переворачиваем в префиксное
- Var
- Output: String;
- I: Integer;
- Begin
- Output := AnsiReverseString(Expression);
- For I := 1 To Length(Output) Do
- If Output[I] = '(' Then
- Output[I] := ')'
- Else If Output[I] = ')' Then
- Output[I] := '(';
- Result := Output;
- End;
- Function ConvertToSuffix(Var Stack: IntList; Expression: String): String; //перевод в суффиксное
- Var
- FinalAnswer: String;
- I: Integer;
- Begin
- Stack.Rang := 0;
- Push(Stack, Expression[1]);
- FinalAnswer := '';
- For I := 2 To Length(Expression) Do
- Begin
- If Expression[I] = ')' Then
- Begin
- While Peek(Stack) <> '(' Do
- Begin
- СalculationOfRang(Stack);
- FinalAnswer := Concat(FinalAnswer, Pop(Stack));
- End;
- Pop(Stack);
- End
- Else
- Begin
- If GetPriority(Expression[I]) > GetStackPriority(Peek(Stack)) Then
- Push(Stack, Expression[I])
- Else
- Begin
- While Not(IsEmptyStack(Stack)) And
- (GetPriority(Expression[I]) <= GetStackPriority(Peek(Stack))) Do
- Begin
- СalculationOfRang(Stack);
- FinalAnswer := Concat(FinalAnswer, Pop(Stack));
- End;
- Push(Stack, Expression[I]);
- End;
- End;
- End;
- While Stack.FirstElem <> Nil Do
- Begin
- СalculationOfRang(Stack);
- FinalAnswer := Concat(FinalAnswer, Pop(Stack));
- End;
- Result := FinalAnswer;
- End;
- //Main
- Var
- Stack: IntList;
- Expression: String;
- Begin
- Expression := InputExpression;
- Writeln('Суффиксное выражение равно = ', ConvertToSuffix(Stack, Expression));
- Writeln('Ранг равен = ', Stack.Rang);
- Writeln(Reverse(Expression));
- Writeln('Префиксное выражение равно = ',
- Reverse(ConvertToSuffix(Stack, Reverse(Expression))));
- Writeln('Ранг равен = ', Stack.Rang);
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement