tui_calc.rb
# takes an array and checks whether it is a valid mathematical expression
# i.e. start and end with num, alternate num and op
# need_check_parens determines whether to call check_parens or not
#   calc will send false because it calls check_parens itself since it needs
#   the match index info
takes an array and checks whether it is a valid mathematical expression
i.e. start and end with num, alternate num and op
need_check_parens determines whether to call check_parens or not
calc will send false because it calls check_parens itself since it needs
the match index info
takes an array and checks whether it is a valid mathematical expression i . e . start and end with num alternate num and op need_check_parens determines whether to call check_parens or not calc will send false because it calls check_parens itself since it needs the match index info
def check_expr(expr_arr, need_check_parens=true)
  expr_arr = expr_arr.clone  
  if expr_arr.length < 3  
    return false
  end
  if need_check_parens
    unless check_parens(expr_arr)
      return false
    end
  end
  i = 0
  while i < expr_arr.length do
    if PAIRS.key?(expr_arr[i])
      if OPS.flatten.include?(expr_arr[i+1])
        return false  
      end
      expr_arr.delete_at(i)
      i -= 1  
    elsif PAIRS.value?(expr_arr[i])
      if OPS.flatten.include?(expr_arr[i-1])
        return false  
      end
      expr_arr.delete_at(i)
      i -= 1  
    else
      i += 1
    end
  end
  if expr_arr[0].numeric? and expr_arr[-1].numeric?
    valid = true
    expr_arr.each_index do |i|
      if i % 2 == 0  
        unless expr_arr[i].numeric?  
          valid = false
        end
      else  
        unless OPS.flatten.include?(expr_arr[i])  
          valid = false
        end
      end
    end
    return valid
  else
    return false
  end
end
def check_expr ( expr_arr , need_check_parens = true ) expr_arr = expr_arr . clone if expr_arr . length < 3 return false end if need_check_parens unless check_parens ( expr_arr ) return false end end i = 0 while i < expr_arr . length do if PAIRS . key? ( expr_arr [ i ] ) if OPS . flatten . include? ( expr_arr [ i + 1 ] ) return false end expr_arr . delete_at ( i ) i -= 1 elsif PAIRS . value? ( expr_arr [ i ] ) if OPS . flatten . include? ( expr_arr [ i - 1 ] ) return false end expr_arr . delete_at ( i ) i -= 1 else i += 1 end end if expr_arr [ 0 ] . numeric? and expr_arr [ - 1 ] . numeric? valid = true expr_arr . each_index do | i | if i % 2 == 0 unless expr_arr [ i ] . numeric? valid = false end else unless OPS . flatten . include? ( expr_arr [ i ] ) valid = false end end end return valid else return false end end
takes an array and checks whether it is a valid mathematical expression
i.e.
takes an array and checks whether it is a valid mathematical expression i . e .
# need to change expr_arr later w/o changing source # valid expression is at least 2 numbers and an operation # brackets are valid, so remove them to test the expression # this method means implied multiplication like 5(4+3) doesn't work # this is okay because calc() can't handle implicit mult anyway # before deleting, check for operation immediately inside parens # can't have op immediately inside paren # deleted element, so adjust i # can't have op immediately inside paren # deleted element, so adjust i # first and last must be numbers (if not parentheses) # if i even # even elements must be numbers # if i odd # odd elements must be operations
expr_arr
<not_specific>
need_check_parens
<not_specific>