Function IntToStr(i: Integer): String; Var s: String; Begin Str(i, s); IntToStr := s End; Function StrToInt(s: String): Integer; Var i, Code: Integer; Begin Val(s, i, Code); StrToInt := i End; Function min(a, b: Integer): Integer; Begin min := a; If b < a Then min := b End; Function max(a, b: Integer): Integer; Begin max := a; If b > a Then max := b End; Type TokenType = (tkOpen, tkMax, tkMin, tkClose, tkComma, tkNumber); Type TType = string[10]; {TokenType;} PTItem = ^TItem; TStack = PTItem; TItem = Record info: TType; next: PTItem; End; Const tokenID: Array[TokenType] Of TType = ('(', 'M', 'm', ')', ',', ''); Procedure initStack(Var s: TStack); Begin s := nil End; Function isEmpty(Var s: TStack): boolean; Begin isEmpty := (s = nil) End; Procedure PushStack(Var s: TStack; T: TType); Var nItem: PTItem; Begin new(nItem); nItem^.next := s; nItem^.info := T; s := nItem; End; (* function topStack(var s: TStack): TType; begin topStack := s^.info end; *) Function PopStack(Var s: TStack): TType; Var ToDelete: PTItem; Begin If not IsEmpty(s) Then Begin ToDelete := s; s := s^.next; popStack := ToDelete^.info; Dispose(ToDelete) End; End; Function getNextToken(Var s, sT: String): TokenType; Begin Case UpCase(s[1]) Of '(', ')', 'M', ',': Begin Case s[1] Of '(': getNextToken := tkOpen; ')': getNextToken := tkClose; 'm': getNextToken := tkMin; 'M': getNextToken := tkMax; ',': getNextToken := tkComma; End; st := ''; Delete(s, 1, 1) End Else Begin st := ''; While (s <> '') and (s[1] In ['0'..'9', '.']) Do Begin st := st + s[1]; Delete(s, 1, 1); End; getNextToken := tkNumber End; End End; Function Operation(OpCode: String; Op1, Op2: TType): TType; Begin Case OpCode[1] Of 'm': Operation := IntToStr(min(StrToInt(Op1), StrToInt(Op2))); 'M': Operation := IntToStr(max(StrToInt(Op1), StrToInt(Op2))); End; End; Const s: String = 'M(5,m(6,8))'; Var sToken: String; nextToken: TokenType; actionStack, operandStack: TStack; begin initStack(actionStack); initStack(operandStack); While s <> '' Do Begin nextToken := GetNextToken(s, sToken); Case nextToken Of tkMax, tkMin: pushStack(actionStack, tokenID[nextToken]); tkNumber: pushStack(operandStack, sToken); tkOpen, tkComma: ; tkClose: Begin pushStack(operandStack, Operation( popStack(actionStack), popStack(operandStack), opStack(operandStack) ) ) End; End; End; WriteLn(popStack(operandStack)); end.