2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

遠い世界の数式

Posted at

いつものように正規表現使うよろしくない解法。

evalex.rb
#!/usr/bin/ruby
#http://nabetani.sakura.ne.jp/kanagawa.rb/evalex/

AddSub = Regexp.compile(/^(.*?)([0-9,-]+)([+Z])([0-9,-]+)(.*)$/)
MulDiv = Regexp.compile(/^(.*?)([0-9,-]+)([*])([0-9,-]+)(.*)$/)
And = Regexp.compile(/^(.*?)([0-9,-]+)([\&])([0-9,-]+)(.*)$/)
Or = Regexp.compile(/^(.*?)([0-9,-]+)([\|])([0-9,-]+)(.*)$/)

def process(s)
	#analyze parens
	bidx=s.index("(")
	while bidx
		count=1
		eidx=bidx+1
		while count!=0
			count+=1 if s[eidx,1]=='('
			count-=1 if s[eidx,1]==')'
			eidx+=1
		end
		s=s[0,bidx]+process(s[bidx+1...eidx-1])+s[eidx..-1]
		bidx=s.index("(")
	end

	while m=Or.match(s)
		if m[3]=='|'
			s=m[1]+(m[2].to_i|m[4].to_i).to_s+m[5]
		end
	end

	while m=And.match(s)
		if m[3]=='&'
			s=m[1]+(m[2].to_i&m[4].to_i).to_s+m[5]
		end
	end

	while m=AddSub.match(s)
		if m[3]=='+'
			s=m[1]+(m[2].to_i+m[4].to_i).to_s+m[5]
		else
			s=m[1]+(m[2].to_i-m[4].to_i).to_s+m[5]
		end
	end

	while m=MulDiv.match(s)
		if m[3]=='*'
			s=m[1]+(m[2].to_i*m[4].to_i).to_s+m[5]
		end
	end
	s
end

while gets
	puts process($_.chomp.tr('-','Z')) # 引き算は出現しないので必要ない処理だけど、まあ…
end
2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?