Skip to content

Instantly share code, notes, and snippets.

@mooz
Created March 2, 2010 10:40
Show Gist options
  • Save mooz/319422 to your computer and use it in GitHub Desktop.
Save mooz/319422 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
require "cgi"
cgi = CGI.new
$KCODE = "UTF8"
class String
def fragments(n)
len = self.length
buffer = []
result = []
len.times {|i|
if ((i % n) == 0 && (buffer.length > 0))
result.push(buffer.join(""))
buffer = []
end
buffer.push(self.slice(i, 1))
}
if (buffer.length > 0)
result.push(buffer.join(""))
end
result
end
end
def digit2str(d)
return {
"0" => "零",
"1" => "一",
"2" => "二",
"3" => "三",
"4" => "四",
"5" => "五",
"6" => "六",
"7" => "七",
"8" => "八",
"9" => "九",
}[d]
end
def fragment2str(a)
result = ""
if (a == "0")
return "零"
end
a.split("").reverse.each_with_index {|d, n|
if (d == "0")
next
end
c = digit2str(d)
case n
when 0
suffix = ""
when 1
suffix = "十"
when 2
suffix = "百"
when 3
suffix = "千"
end
if (d == "1" && n != 0)
c = ""
end
result = c + suffix + result
}
result
end
def convert(number)
result = ""
number.reverse.fragments(4).each_with_index {|fragment, n|
str = fragment2str(fragment.reverse)
if (str == "")
next
end
case n
when 0
postfix = ""
when 1
postfix = "万"
when 2
postfix = "億"
when 3
postfix = "兆"
end
result = str + postfix + result
}
result
end
def kanji2alph(str)
str.split("").map{|c|
{
"零" => "C",
"一" => "X",
"二" => "K",
"三" => "G",
"四" => "D",
"五" => "A",
"六" => "P",
"七" => "S",
"八" => "Q",
"九" => "Y",
"十" => "Z",
"百" => "T",
"千" => "R",
"万" => "L",
"億" => "F",
"兆" => "U"
}[c]
}.join("")
end
n = cgi['n']
begin
File.open("log.txt", 'a') {|f|
f.puts n
}
rescue
cgi.out("text/plain") {
"Error"
}
end
cgi.out("text/plain"){
kanji2alph(convert(n))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment