[题解](续II)Let’s Celebrate the 100th Contest on ZOJ! » ZOJ3442ruby

ZOJ3442ruby
ZOJ3442ruby.rb


require 'complex'

module Math
	def Math.pow(x, y)
		x ** y
	end

	{:conj => :conjugate, :abs => :abs, :real => :real, :imag => :image, :arg => :arg}.each do |k, v|
		module_eval <<-"end_eval"
			def Math.#{k}(c)
				c.#{v}
			end
		end_eval
	end
end

def gao(c)
	c.abs < 5e-7 ? 0 : c
end

i = Complex.new(0, 1)

readlines.each do |l|
	l.gsub!(/[a-z]{2,}/){"Math.#{$&}"}
	l.gsub!(/(^|[^.\d])(\d+)(?!\.|\d)/){"#{$1}#{$2}.to_f"}
	ans = eval l
	puts '%+.6f%+.6f*i' % [gao(ans.real), gao(ans.image)]
end

__END__
acos(1.125)=+0.000000+0.494933*i; +0.000000-0.494933*i
acosh(-1.125)=-0.494933+3.141593*i; +0.494933+3.141593*i
asin(1.125)=+1.570796-0.494933*i; +1.570796+0.494933*i
acosh(-2-3*i)=-1.983387+2.141449*i; +1.983387-2.141449*i
Leave a Reply