Last active
January 3, 2016 13:59
-
-
Save jimfoltz/ae7f2411b822f495a2c3 to your computer and use it in GitHub Desktop.
Revisions
-
jimfoltz revised this gist
Jan 3, 2016 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ require "awesome_print" exp = "1 + 2 * 3" -
jimfoltz revised this gist
Jan 3, 2016 . 1 changed file with 9 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)" def prec(op) @@ -13,8 +13,6 @@ def prec(op) return 10 when "*", "/" return 20 when "^" return 30 when "(", ")" @@ -25,7 +23,7 @@ def prec(op) end def is_operator(t) 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:" -
jimfoltz renamed this gist
Dec 27, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jimfoltz created this gist
Dec 27, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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