#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计算器
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- packagework11; importjava.io.*; importjava.net.*; public ...
- 翻译自Flip Flop Operator in Rubyreddit上看到的,因为从来没看到过这种用法,一起学起...