class Object
Public Instance Methods
cli_eval(string,type)
click to toggle source
# File bin/calculo, line 23 def cli_eval(string,type) if type == 'lisp' or type == 'reverse-lisp' or type == 'reverse' cli = Lisp.new(string,type) output = "#{cli.string} : #{cli.result}" return output elsif type == 'rpn' or type == 'postfix' or type == 'pn' or type == 'prefix' or type == 'infix' cli = MathNotation.new(string,type) output = "#{cli.string} : #{cli.result}" return output end end
parse(string)
click to toggle source
# File lib/calculo/parse.rb, line 1 def parse(string) # First regex puts a space after a parenthesis/math operation, # Second regex a space between a digit and a parenthesis/math # operation(s) # Third regex converts any double spaces into single space # Fourth regex converts to the exponent operator used in ruby # split seperates the characters based on a space op_re = Regexp.new(/([^\d\w\s\.\-])/) dig_op_re = Regexp.new(/\d[^\d\w\s\.]+/) dbl_space_re = Regexp.new(/\s{2,}/) exp_re = Regexp.new(/\^/) dig_re = Regexp.new(/\d+/) array = string.gsub(op_re,'\1 ').gsub(dig_op_re){ |s| s.chars.join(' ')}.gsub(dbl_space_re,' ').gsub(exp_re,'**').split(' ') # Regex finds if array element matches a digit, and converts it # to a float. array = array.map{|x| dig_re.match(x) != nil ? x.to_f : x} return array end
shunting_yard(infix_array)
click to toggle source
# File lib/calculo/shunting_yard.rb, line 1 def shunting_yard(infix_array) operators = {'+' => 2, '-' => 2, '*' => 3, '/' => 3, '>' => 4, '<' => 4, '=' => 4, '%' => 4, '**' => 4} rpn_expr = [] op_stack = [] infix_array.each do |item| if operators.has_key?(item) op2 = op_stack.last if operators.has_key?(op2) and ((op2 == "**" and operators[item] < operators[op2]) or (op2 != "**" and operators[item] <= operators[op2])) rpn_expr.push(op_stack.pop) end op_stack.push(item) elsif item == "(" op_stack.push(item) elsif item == ")" until op_stack.last == "(" rpn_expr.push(op_stack.pop) end op_stack.pop else rpn_expr.push(item) end end until op_stack.empty? rpn_expr.push(op_stack.pop) end return rpn_expr end