Skip to content

Instantly share code, notes, and snippets.

@jimfoltz
Last active January 3, 2016 13:59
Show Gist options
  • Select an option

  • Save jimfoltz/ae7f2411b822f495a2c3 to your computer and use it in GitHub Desktop.

Select an option

Save jimfoltz/ae7f2411b822f495a2c3 to your computer and use it in GitHub Desktop.

Revisions

  1. jimfoltz revised this gist Jan 3, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion infix2postfix.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    require "ostruct"
    require "awesome_print"

    exp = "1 + 2 * 3"
  2. jimfoltz revised this gist Jan 3, 2016. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions infix2postfix.rb
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@

    exp = "1 + 2 * 3"
    exp = "9 + 24 / (7 - 3)"
    exp = "3 * atan(2/3)"
    exp = "(12.3mm - 1')*52 - 473 mm/(4 + 2^3)"
    exp = "3 * atan(x/y)"


    def prec(op)
    @@ -13,8 +13,6 @@ def prec(op)
    return 10
    when "*", "/"
    return 20
    when "sin", "cos", "tan", "atan"
    return 25
    when "^"
    return 30
    when "(", ")"
    @@ -25,7 +23,7 @@ def prec(op)
    end

    def is_operator(t)
    operators = ["-", "+", "*", "/", "^", "sin", "cos", "tan" , "atan"]
    operators = ["-", "+", "*", "/", "^"]
    operators.include?(t)
    end

    @@ -78,6 +76,13 @@ def infix2postfix(tokens)
    return output
    end

    def eval_postfix(arr)
    stack = []
    arr.each do |elm|

    end
    end

    puts exp
    tokens = tokenize exp
    puts "Tokens:"
  3. jimfoltz renamed this gist Dec 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. jimfoltz created this gist Dec 27, 2015.
    89 changes: 89 additions & 0 deletions i2p.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    require "ostruct"
    require "awesome_print"

    exp = "1 + 2 * 3"
    exp = "9 + 24 / (7 - 3)"
    exp = "(12.3mm - 1')*52 - 473 mm/(4 + 2^3)"
    exp = "3 * atan(x/y)"


    def prec(op)
    case op
    when "+", "-"
    return 10
    when "*", "/"
    return 20
    when "sin", "cos", "tan", "atan"
    return 25
    when "^"
    return 30
    when "(", ")"
    return 40
    else
    return nil
    end
    end

    def is_operator(t)
    operators = ["-", "+", "*", "/", "^", "sin", "cos", "tan" , "atan"]
    operators.include?(t)
    end

    # Anything that is not an operator, parenthesis, or space is a operand
    def tokenize(exp)
    operand = ""
    tokens = []
    exp.each_char do |c|
    if is_operator(c) || c == "(" || c == ")"
    if operand != ""
    tokens << operand
    operand = ""
    end
    tokens << c
    else
    operand << c unless c == " "
    end
    end
    tokens << operand unless operand.empty?
    return tokens
    end

    def infix2postfix(tokens)
    output = []
    stack = []
    tokens.each do |token|
    if is_operator(token)
    while (!stack.empty? && is_operator(stack[-1]) && prec(stack[-1]) >= prec(token))
    output << stack.pop
    end
    stack << token
    next
    end
    if token == "("
    stack.push token
    next
    end
    if token == ")"
    while stack[-1] && stack[-1] != "("
    output << stack.pop
    end
    stack.pop
    next
    end
    output << token
    end # do loop
    while (!stack.empty?)
    output << stack.pop
    end
    return output
    end

    puts exp
    tokens = tokenize exp
    puts "Tokens:"
    ap tokens

    postfix = infix2postfix(tokens)
    puts
    puts "Postfix:"
    ap postfix