Advertisement
dxvmxnd

Untitled

Apr 18th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. Program Progstack;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. System.SysUtils;
  7.  
  8. Type
  9. PNode = ^TStack;
  10.  
  11. TStack = Record
  12. Value: Char;
  13. Top: PNode;
  14. End;
  15. TAoS = Array of String;
  16.  
  17. Procedure Push(Var Stack: TStack; Value: Char);
  18. Var
  19. NewNode: PNode;
  20. Begin
  21. New(NewNode);
  22. NewNode^.Value := Value;
  23. NewNode^.Top := Stack.Top;
  24. Stack.Top := NewNode;
  25. End;
  26.  
  27. Function Pop(Var Stack: TStack): Char;
  28. Var
  29. TempNode: PNode;
  30. Begin
  31. Result := Stack.Top^.Value;
  32. TempNode := Stack.Top;
  33. Stack.Top := Stack.Top^.Top;
  34. Dispose(TempNode);
  35. End;
  36.  
  37. Function Peek(Var Stack: TStack): Char;
  38. Var
  39. TempNode: PNode;
  40. Begin
  41. Result := Stack.Top^.Value;
  42. End;
  43.  
  44. Procedure Initialize(Var Stack: TStack);
  45. Begin
  46. Stack.Top := Nil;
  47. End;
  48.  
  49. function findInt(Ch: Char; Arr: TAoS): Integer;
  50. var
  51. I, Answ: Integer;
  52. begin
  53. if Ch In ['a'..'z'] then
  54. Answ:= 8;
  55. for I := 0 to High(Arr) do
  56. if Arr[I][1]=Ch then
  57. begin
  58. Answ:= strtoint(Arr[i][2]);
  59. Break;
  60. end;
  61. Result:= Answ;
  62. end;
  63.  
  64. procedure printStack(Var Stack: TStack);
  65. var
  66. Temp: PNode;
  67. str: String;
  68. begin
  69. Temp:= Stack.Top;
  70. while Temp<>Nil do
  71. begin
  72. Str:= Temp.Value + Str;
  73. Temp := Temp.Top;
  74. end;
  75. Write(str,' ');
  76. end;
  77.  
  78. Var
  79. Stack: TStack;
  80. Value: Integer;
  81. OtnPr, StPr: TAoS;
  82. Input, Output: String;
  83. Ch: Char;
  84. A: Boolean;
  85.  
  86. Begin
  87. Initialize(Stack);
  88. A:= True;
  89. OtnPr := ['+1','-1','*3','/3','^6','(9'];
  90. StPr := ['+2','-2','*4','/4','^5','(0'];
  91. Input:= 'w*(a+b+c*(y-g*d)^n^k+s*l)/(x-f*t*p+w)^e';
  92. {for var I := 1 to length(Input)-1 do
  93. begin
  94. if (not(Input[I-1] in ['a'..'z']) and not (Input[I] in ['a'..'z'])) or ((Input[I-1] in ['a'..'z']) and (Input[I] in ['a'..'z'])) then
  95. begin
  96. Writeln('Неправильный ввод');
  97. a:= False;
  98. end;
  99. end;}
  100.  
  101. for var I := 0 to length(Input)-1 do
  102. begin
  103. write(Ch,' ');
  104. printStack(Stack);
  105. Writeln(Output);
  106.  
  107. Ch:= Input[I+1];
  108. if (Stack.Top=Nil) or (Ch='(') or (Ch in ['a'..'z']) then
  109. begin
  110. Push(Stack, Ch);
  111. Continue;
  112. end;
  113.  
  114. if (Ch=')') then
  115. begin
  116. while Peek(Stack)<>'(' do
  117. Output:= Output + pop(Stack);
  118. pop(Stack);
  119. Continue;
  120. end;
  121.  
  122.  
  123. while (Stack.Top<>Nil) and (FindInt(Ch, OtnPr)<FindInt(Peek(Stack),StPr)) do
  124. begin
  125. Output:= Output + pop(Stack);
  126. end;
  127. Push(Stack, Ch);
  128. end;
  129.  
  130. while Stack.Top<>Nil do
  131. Output:= Output + pop(Stack);
  132. Writeln(Output);
  133. readln;
  134. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement