Guest User

Untitled

a guest
Feb 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. 7 8 9 +
  2. 4 5 6 -
  3. 1 2 3 *
  4. 0 = /
  5.  
  6. ## Language Name, N bytes
  7.  
  8. ## Ruby, <s>104</s> <s>101</s> 96 bytes
  9.  
  10. ## Perl, 43 + 2 (-p flag) = 45 bytes
  11.  
  12. ## [><>](http://esolangs.org/wiki/Fish), 121 bytes
  13.  
  14. import re
  15. def v(s):
  16. if s[:1]=='(':l,s=e(s[1:]);return l,s[1:]
  17. m=re.match(r"d+.?d*|.d+",s)
  18. if m:return[m.group()],s[m.end():]
  19. def b(s,f,*O):
  20. l,s=f(s)
  21. while s[:1]in O:
  22. o=s[0]
  23. r,s=f(s[1:])
  24. if len(r)>1:l,r=r,l
  25. l+=[o]+r
  26. return l,s
  27. m=lambda s:b(s,v,'*','/')
  28. e=lambda s:b(s,m,'+','-')
  29. print(' '.join(e(input())[0]))
  30.  
  31. Input "",Str1
  32. 0->dim(L1
  33. 0->dim(L2
  34. 1->I
  35. DelVar S
  36. While I<length(Str1
  37. DelVar T
  38. ".->Str3
  39. 1->C
  40. While C and I<length(Str1
  41. sub(Str1,I,1->Str2
  42. inString("0123456789.",Str2->C
  43. If C:Then
  44. I+1->I
  45. 1->T
  46. Str3+Str2->Str3
  47. End
  48. End
  49. If T=0:Str3+Str2->Str3
  50. sub(Str3,2,length(Str3)-1->Str3
  51. If T=1:Then
  52. expr(Str3->L2(1+dim(L2
  53. End
  54. inString("*+/-",Str3->M
  55. If M:Then
  56. I+1->I
  57. 2->T
  58. 1->C
  59. While C
  60. dim(L1->C
  61. If C:Then
  62. 2fPart(L1(dim(L1))/2)>2fPart(M/2->C
  63. If C:Then
  64. ~L1(dim(L1->L2(1+dim(L2
  65. dim(L1)-1->dim(L1
  66. End
  67. End
  68. End
  69. M->L1(1+dim(L1
  70. End
  71. If Str3="(":Then
  72. If S=1:Then
  73. sub(Str1,1,I-1)+"*"+sub(Str1,I,length(Str1)-I+1)->Str1
  74. Else
  75. I+1->I
  76. 0->L1(dim(L1)+1
  77. End
  78. End
  79. If Str3=")":Then
  80. I+1->I
  81. While L1(dim(L1
  82. ~L1(dim(L1->L2(1+dim(L2
  83. dim(L1)-1->dim(L1
  84. End
  85. dim(L1)-1->dim(L1
  86. End
  87. T->S
  88. End
  89. augment(L2,-seq(L1(X),X,dim(L1),1,-1)->L2
  90. 0->dim(L1
  91. 1->C
  92. {0,1->L3
  93. For(I,1,dim(L2
  94. L2(I->M
  95. If M>=0:Then
  96. M->L1(dim(L1)+1
  97. Else
  98. If C:Then
  99. L1(dim(L1)-1
  100. Goto S
  101. Lbl A
  102. Ans->Str1
  103. L1(dim(L1->L1(dim(L1)-1
  104. dim(L1)-1->dim(L1
  105. 0->C
  106. End
  107. Str1+" "+sub("*+/-",-M,1)+" ->Str1
  108. L1(dim(L1
  109. Goto S
  110. Lbl B
  111. Str1+Ans->Str1
  112. dim(L1)-1->dim(L1
  113. End
  114. End
  115. Goto F
  116. Lbl S
  117. L3Ans->L4
  118. LinReg(ax+b) L3,L4,Y1
  119. Equ►String(Y1,Str2
  120. sub(Str2,1,length(Str2)-3
  121. If C:Goto A
  122. Goto B
  123. Lbl F
  124. Str1
  125.  
  126. Str1 The input string.
  127. Str2 Counter variable (e.g. current character)
  128. Str3 The current token being built.
  129. L1 The operator stack
  130. L2 The output queue
  131. L3 Temporary list
  132. L4 Temporary list
  133. I Iterator index
  134. T Type of token (enum)
  135. S Type of previous token (enum)
  136. M Temporary variable
  137. C Conditional variable
  138.  
  139.  
  140. Token Types (T)
  141. 0 Undefined
  142. 1 Number
  143. 2 Operator
  144. 3 Open Parenthesis
  145.  
  146. Operator Elements
  147. 0 left parenthesis ("(")
  148. 1 multiplication ("*")
  149. 2 addition ("+")
  150. 3 division ("/")
  151. 4 subtraction ("-")
  152. 5 right parenthesis (")")
  153.  
  154. Precedence Rule: Remainder(prec, levelno)
  155. 0 - add, sub
  156. 1 - mul, div
  157. (levelno = 2)
Add Comment
Please, Sign In to add comment