ruby计算器

#coding:utf-8
require "stringio"

def log(*args)
    # puts args.join(" ")
end

class Calculator
    Symbol = ["+", "-", "*", "/", "(", ")"]
    public
    def calcExpr(text)
        symbol = Calculator::Symbol
        symbol_array = []
        number_array = []
        text.gsub! "\x20", ""

        log text
        io = StringIO.new text
        until io.eof?
            ch = io.getc
            if isdigit ch
                io.ungetc ch
                number_array.push (peek_number io)
            elsif ch == '('
                # push stack
                symbol_array.push ch
            elsif ch == ")"
                # pop
                while symbol_array.size>0
                    op = symbol_array.pop
                    if op =="("
                        break
                    end
                    op1 = number_array.pop
                    op2 = number_array.pop
                    
                    log "#{op1} #{op2} #{op}"
                    number_array.push calc(op1, op2, op)
                end
            elsif symbol.include? ch
                # stack top
                while symbol_array.size > 0
                    if compare_symbol symbol_array[-1], ch
                        op1 = number_array.pop
                        op2 = number_array.pop
                        sy = symbol_array.pop
                        log "#{op1} #{op2} #{sy}"
                        number_array.push calc(op1, op2, sy)
                    else
                        break
                    end
                end
                symbol_array.push ch    
            end
        end
        while symbol_array.size>0
            op1 = number_array.pop
            op2 = number_array.pop
            sy = symbol_array.pop
            log "#{op1} #{op2} #{sy}"
            number_array.push calc(op1, op2, sy)
        end
        if number_array.size>0
            number_array[0]
        else
            puts "wrong"
        end


    end
    private
    def isdigit(c)
        (c =~ /\d/) == 0
    end
    def compare_symbol s1, s2
        if s2 == "+" || s2 =="-"    
            if (s1 == "*" || s1 == "/")
                return true
            end
        end
        return false
    end
    # TODO:// exception
    def calc(op1, op2, op)
        case op
        when "+"
            return op1+op2
        when "-"
            return op1-op2
        when "*"
            return op1*op2
        when "/"
            return op1/op2
        end
    end

    def is_float(number)
        (number =~ /\d+.\d+/) == 0
    end

    def peek_number (io)
        number = ""
        until io.eof?
            ch = io.getc
            if isdigit(ch) ||  (ch == ".")
                number << ch
            else
                io.ungetc ch
                break
            end
        end
        
        if is_float number
            number.to_f
        else
            number.to_i
        end
    end

end

c = Calculator.new
r = c.calcExpr "(2+2)*10+9+8*200"
puts "result:#{r}"

你可能感兴趣的:(ruby计算器)