Last active
August 29, 2015 13:56
-
-
Save 42thcoder/8856254 to your computer and use it in GitHub Desktop.
Revisions
-
42thcoder revised this gist
Feb 8, 2014 . 1 changed file with 82 additions and 56 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 @@ -1,71 +1,97 @@ # 许多特殊情况都没考虑 # 除数为0 用户输入字母 括号 class Stack def initialize(size) @stack = Array.new(size) @sp = 0 end def push(value) @stack[@sp] = value @sp += 1 end def pop return nil if @sp == 0 @sp -= 1 @stack[@sp] end def size @sp end def to_string "#{@sp} elements: #{@stack[0...@sp]}" end end class Calculator PRIORITY = {"+" => 0, "-" => 0, "*" => 1, "/" => 1} def initialize(input) p PRIORITY['+'] @operands_stack = Stack.new(input.length) @operators_stack = Stack.new(input.length) input.split().each do |chr| case chr when '+', '-', '*', '/' @operators_stack.push chr else @operands_stack.push chr.to_i end end end def operate(operator, fo, so) case operator when '+' fo + so when '-' fo - so when '*' fo * so when '/' fo / so end end def result until @operators_stack.size == 0 p @operands_stack.to_string operator = @operators_stack.pop next_operator = @operators_stack.pop if next_operator.nil? || PRIORITY[operator] >= PRIORITY[next_operator] return nil if @operands_stack.size <= 1 so = @operands_stack.pop fo = @operands_stack.pop @operands_stack.push operate(operator, fo, so) @operators_stack.push next_operator else return nil if @operands_stack.size <= 1 to = @operands_stack.pop so = @operands_stack.pop fo = @operands_stack.pop @operands_stack.push operate(next_operator, fo, so) @operands_stack.push to @operators_stack.push operator end end def to_string @operands_stack.to_string end end end puts 'Give me a string' input = gets cal = Calculator.new(input) cal.result # p cal.to_string -
42thcoder revised this gist
Feb 7, 2014 . 1 changed file with 1 addition 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,5 +1,5 @@ # 许多特殊情况都没考虑 # 除数为0 优先级 用户输入字母 括号 class Stack def initialize(size) -
42thcoder revised this gist
Feb 7, 2014 . No changes.There are no files selected for viewing
-
42thcoder revised this gist
Feb 7, 2014 . No changes.There are no files selected for viewing
-
42thcoder revised this gist
Feb 7, 2014 . 1 changed file with 49 additions and 49 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 @@ -2,67 +2,67 @@ # 除数为0 用户输入字母 括号 class Stack def initialize(size) @stack = Array.new(size) @sp = 0 end def push(value) @stack[@sp] = value @sp += 1 end def pop return nil if @sp == 0 @sp -= 1 @stack[@sp] end def size @sp end def to_string "#{@sp} elements: #{@stack[0...@sp]}" end end class Calculator def initialize(input) @stack = Stack.new(input.length) input.split().each do |chr| case chr when '+', '-', '*', '/' operate(chr) else @stack.push chr.to_i end end end def operate(operator) return nil if @stack.size <= 1 so = @stack.pop fo = @stack.pop case operator when '+' result = fo + so when '-' result = fo - so when '*' result = fo * so when '/' result = fo / so end @stack.push result end def to_string @stack.to_string end end puts 'Give me a string' -
42thcoder revised this gist
Feb 7, 2014 . No changes.There are no files selected for viewing
-
42thcoder revised this gist
Feb 7, 2014 . No changes.There are no files selected for viewing
-
42thcoder revised this gist
Feb 7, 2014 . No changes.There are no files selected for viewing
-
42thcoder created this gist
Feb 7, 2014 .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,71 @@ # 许多特殊情况都没考虑 # 除数为0 用户输入字母 括号 class Stack def initialize(size) @stack = Array.new(size) @sp = 0 end def push(value) @stack[@sp] = value @sp += 1 end def pop return nil if @sp == 0 @sp -= 1 @stack[@sp] end def size @sp end def to_string "#{@sp} elements: #{@stack[0...@sp]}" end end class Calculator def initialize(input) @stack = Stack.new(input.length) input.split().each do |chr| case chr when '+', '-', '*', '/' operate(chr) else @stack.push chr.to_i end end end def operate(operator) return nil if @stack.size <= 1 so = @stack.pop fo = @stack.pop case operator when '+' result = fo + so when '-' result = fo - so when '*' result = fo * so when '/' result = fo / so end @stack.push result end def to_string @stack.to_string end end puts 'Give me a string' input = gets cal = Calculator.new(input) p cal.to_string