Advertisement
Guest User

math pars

a guest
Apr 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.15 KB | None | 0 0
  1. #parsing math
  2.  
  3. input = gets.chop
  4. operators=["+","-","*","/"]
  5. brackets_count = input.count '('
  6. if brackets_count > 0
  7. end
  8.  
  9. numbers=['']
  10. buffer=[]
  11. available=0
  12. ncount=0
  13. operators_in=[]
  14. @out=[]
  15.  
  16. for i in 0...input.size
  17.     unless ('0'..'9')===input[i] or input[i]=='.'
  18.         numbers << ''
  19.         @out[ncount]=[] unless @out[ncount]
  20.         @out[ncount]<<input[i]
  21.         ncount+=1
  22.     else
  23.         numbers[ncount]<<input[i]
  24.         @out[ncount]=[] unless @out[ncount]
  25.         @out[ncount][1]=''unless @out[ncount][1]
  26.         @out[ncount][1]<<input[i]
  27.     end
  28. end
  29. numbers.size.times do |i|
  30.     numbers[i]=numbers[i].to_i
  31. end
  32.  
  33. @out = @out.inject:+
  34. for i in 0...@out.size
  35.     if @out[i]==nil
  36.         @out.delete_at i
  37.     end
  38. end
  39. @out = @out.map do |str|
  40.     if ('0'...'9')===str[0] then
  41.         str=str.to_f
  42.     elsif not operators.index(str)
  43.         str=nil
  44.     end
  45.     str
  46.  end
  47. @out.delete nil
  48. p @out
  49.  
  50. ###part 2
  51.  
  52. if operators.index(@out[0])
  53.     x=@out[1].to_s
  54.     x.insert(0,@out[0])
  55.     x=x.to_f
  56. end
  57. while @out[2].class==Float
  58.     x=@out[0].method(@out[1]).call(@out[2])
  59.     @out[0]=x
  60.     @out.slice! 1..2
  61. end
  62. p @out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement