Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 7 8 9 +
- 4 5 6 -
- 1 2 3 *
- 0 = /
- ## Language Name, N bytes
- ## Ruby, <s>104</s> <s>101</s> 96 bytes
- ## Perl, 43 + 2 (-p flag) = 45 bytes
- ## [><>](http://esolangs.org/wiki/Fish), 121 bytes
- import re
- def v(s):
- if s[:1]=='(':l,s=e(s[1:]);return l,s[1:]
- m=re.match(r"d+.?d*|.d+",s)
- if m:return[m.group()],s[m.end():]
- def b(s,f,*O):
- l,s=f(s)
- while s[:1]in O:
- o=s[0]
- r,s=f(s[1:])
- if len(r)>1:l,r=r,l
- l+=[o]+r
- return l,s
- m=lambda s:b(s,v,'*','/')
- e=lambda s:b(s,m,'+','-')
- print(' '.join(e(input())[0]))
- Input "",Str1
- 0->dim(L1
- 0->dim(L2
- 1->I
- DelVar S
- While I<length(Str1
- DelVar T
- ".->Str3
- 1->C
- While C and I<length(Str1
- sub(Str1,I,1->Str2
- inString("0123456789.",Str2->C
- If C:Then
- I+1->I
- 1->T
- Str3+Str2->Str3
- End
- End
- If T=0:Str3+Str2->Str3
- sub(Str3,2,length(Str3)-1->Str3
- If T=1:Then
- expr(Str3->L2(1+dim(L2
- End
- inString("*+/-",Str3->M
- If M:Then
- I+1->I
- 2->T
- 1->C
- While C
- dim(L1->C
- If C:Then
- 2fPart(L1(dim(L1))/2)>2fPart(M/2->C
- If C:Then
- ~L1(dim(L1->L2(1+dim(L2
- dim(L1)-1->dim(L1
- End
- End
- End
- M->L1(1+dim(L1
- End
- If Str3="(":Then
- If S=1:Then
- sub(Str1,1,I-1)+"*"+sub(Str1,I,length(Str1)-I+1)->Str1
- Else
- I+1->I
- 0->L1(dim(L1)+1
- End
- End
- If Str3=")":Then
- I+1->I
- While L1(dim(L1
- ~L1(dim(L1->L2(1+dim(L2
- dim(L1)-1->dim(L1
- End
- dim(L1)-1->dim(L1
- End
- T->S
- End
- augment(L2,-seq(L1(X),X,dim(L1),1,-1)->L2
- 0->dim(L1
- 1->C
- {0,1->L3
- For(I,1,dim(L2
- L2(I->M
- If M>=0:Then
- M->L1(dim(L1)+1
- Else
- If C:Then
- L1(dim(L1)-1
- Goto S
- Lbl A
- Ans->Str1
- L1(dim(L1->L1(dim(L1)-1
- dim(L1)-1->dim(L1
- 0->C
- End
- Str1+" "+sub("*+/-",-M,1)+" ->Str1
- L1(dim(L1
- Goto S
- Lbl B
- Str1+Ans->Str1
- dim(L1)-1->dim(L1
- End
- End
- Goto F
- Lbl S
- L3Ans->L4
- LinReg(ax+b) L3,L4,Y1
- Equ►String(Y1,Str2
- sub(Str2,1,length(Str2)-3
- If C:Goto A
- Goto B
- Lbl F
- Str1
- Str1 The input string.
- Str2 Counter variable (e.g. current character)
- Str3 The current token being built.
- L1 The operator stack
- L2 The output queue
- L3 Temporary list
- L4 Temporary list
- I Iterator index
- T Type of token (enum)
- S Type of previous token (enum)
- M Temporary variable
- C Conditional variable
- Token Types (T)
- 0 Undefined
- 1 Number
- 2 Operator
- 3 Open Parenthesis
- Operator Elements
- 0 left parenthesis ("(")
- 1 multiplication ("*")
- 2 addition ("+")
- 3 division ("/")
- 4 subtraction ("-")
- 5 right parenthesis (")")
- Precedence Rule: Remainder(prec, levelno)
- 0 - add, sub
- 1 - mul, div
- (levelno = 2)
Add Comment
Please, Sign In to add comment