Skip to content

Instantly share code, notes, and snippets.

@42thcoder
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save 42thcoder/8856254 to your computer and use it in GitHub Desktop.

Select an option

Save 42thcoder/8856254 to your computer and use it in GitHub Desktop.

Revisions

  1. 42thcoder revised this gist Feb 8, 2014. 1 changed file with 82 additions and 56 deletions.
    138 changes: 82 additions & 56 deletions Reverse Polish Notation Calculator
    Original file line number Diff line number Diff line change
    @@ -1,71 +1,97 @@
    # 许多特殊情况都没考虑
    # 除数为0 优先级 用户输入字母 括号
    # 除数为0 用户输入字母 括号

    class Stack
    def initialize(size)
    @stack = Array.new(size)
    @sp = 0
    end

    def push(value)
    @stack[@sp] = value
    @sp += 1
    end
    class Stack
    def initialize(size)
    @stack = Array.new(size)
    @sp = 0
    end

    def pop
    return nil if @sp == 0
    @sp -= 1
    @stack[@sp]
    end
    def push(value)
    @stack[@sp] = value
    @sp += 1
    end

    def size
    @sp
    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]}"
    def to_string
    "#{@sp} elements: #{@stack[0...@sp]}"
    end
    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

    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
    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
    def operate(operator, fo, so)
    case operator
    when '+'
    fo + so
    when '-'
    fo - so
    when '*'
    fo * so
    when '/'
    fo / so
    end
    end

    @stack.push result
    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

    def to_string
    @stack.to_string
    @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
    end

    puts 'Give me a string'
    input = gets
    cal = Calculator.new(input)
    p cal.to_string
    puts 'Give me a string'
    input = gets
    cal = Calculator.new(input)
    cal.result
    # p cal.to_string
  2. 42thcoder revised this gist Feb 7, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Reverse Polish Notation Calculator
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # 许多特殊情况都没考虑
    # 除数为0 用户输入字母 括号
    # 除数为0 优先级 用户输入字母 括号

    class Stack
    def initialize(size)
  3. 42thcoder revised this gist Feb 7, 2014. No changes.
  4. 42thcoder revised this gist Feb 7, 2014. No changes.
  5. 42thcoder revised this gist Feb 7, 2014. 1 changed file with 49 additions and 49 deletions.
    98 changes: 49 additions & 49 deletions Reverse Polish Notation Calculator
    Original 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 initialize(size)
    @stack = Array.new(size)
    @sp = 0
    end

    def push(value)
    @stack[@sp] = value
    @sp += 1
    end
    def push(value)
    @stack[@sp] = value
    @sp += 1
    end

    def pop
    return nil if @sp == 0
    @sp -= 1
    @stack[@sp]
    end
    def pop
    return nil if @sp == 0
    @sp -= 1
    @stack[@sp]
    end

    def size
    @sp
    end
    def size
    @sp
    end

    def to_string
    "#{@sp} elements: #{@stack[0...@sp]}"
    end
    def to_string
    "#{@sp} elements: #{@stack[0...@sp]}"
    end
    end

    class Calculator
    def initialize(input)
    @stack = Stack.new(input.length)
    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
    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
    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
    case operator
    when '+'
    result = fo + so
    when '-'
    result = fo - so
    when '*'
    result = fo * so
    when '/'
    result = fo / so
    end

    @stack.push result
    end
    @stack.push result
    end

    def to_string
    @stack.to_string
    end
    def to_string
    @stack.to_string
    end
    end

    puts 'Give me a string'
  6. 42thcoder revised this gist Feb 7, 2014. No changes.
  7. 42thcoder revised this gist Feb 7, 2014. No changes.
  8. 42thcoder revised this gist Feb 7, 2014. No changes.
  9. 42thcoder created this gist Feb 7, 2014.
    71 changes: 71 additions & 0 deletions Reverse Polish Notation Calculator
    Original 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