加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
本帖最后由 876加几 于 2013-11-23 16:45 编辑
脚本:class Numeric def sqr # 平方 return self ** 2 end def cube # 立方 return self ** 3 end def powten # 10 的 self 次方 return 10 ** self end def sqrt # 开平方 if self.abs != self raise ArgumentError.new("NaN") end num = self ** 0.5 return num.truncate == num ? num.truncate : num end def cuberoot # 开立方 if self.abs != self num = (self * (-1)) ** (1 / 3.0) return num.truncate == num ? -num.truncate : -num end num = self ** (1 / 3.0) return num.truncate == num ? num.truncate : num end def root(y) # 开任意次方 if self.abs != self and y % 2 == 0 raise ArgumentError.new("NaN") end if self.abs != self num = (self * (-1)) ** (1.0 / y) return num.truncate == num ? -num.truncate : -num end num = self ** (1.0 / y) return num.truncate == num ? num.truncate : num end end class Array def average(need_dec = true) # 平均数 (是否使用浮点数) if self.include?(nil) ary = self.compact else ary = self end num = 0 if ary.size == 0 raise ZeroDivisionError.new("divided by 0") end for i in 0...ary.size if ary[i].is_a?(Numeric) num += ary[i] else raise TypeError.new("connot convert #{ary[i].class} into Numeric") end end num /= ary.size.to_f unless need_dec return num.truncate end return num.truncate == num ? num.truncate : num end end
class Numeric
def sqr # 平方
return self ** 2
end
def cube # 立方
return self ** 3
end
def powten # 10 的 self 次方
return 10 ** self
end
def sqrt # 开平方
if self.abs != self
raise ArgumentError.new("NaN")
end
num = self ** 0.5
return num.truncate == num ? num.truncate : num
end
def cuberoot # 开立方
if self.abs != self
num = (self * (-1)) ** (1 / 3.0)
return num.truncate == num ? -num.truncate : -num
end
num = self ** (1 / 3.0)
return num.truncate == num ? num.truncate : num
end
def root(y) # 开任意次方
if self.abs != self and y % 2 == 0
raise ArgumentError.new("NaN")
end
if self.abs != self
num = (self * (-1)) ** (1.0 / y)
return num.truncate == num ? -num.truncate : -num
end
num = self ** (1.0 / y)
return num.truncate == num ? num.truncate : num
end
end
class Array
def average(need_dec = true) # 平均数 (是否使用浮点数)
if self.include?(nil)
ary = self.compact
else
ary = self
end
num = 0
if ary.size == 0
raise ZeroDivisionError.new("divided by 0")
end
for i in 0...ary.size
if ary[i].is_a?(Numeric)
num += ary[i]
else
raise TypeError.new("connot convert #{ary[i].class} into Numeric")
end
end
num /= ary.size.to_f
unless need_dec
return num.truncate
end
return num.truncate == num ? num.truncate : num
end
end
演示:p 8.sqr >= 64 p 2.cube >= 8 p 9.sqrt >= 3 p 125.cuberoot >= 5 p 65536.root(16) >= 2 p 3.powten >= 1000 p [3, 8, 1].average >= 4 p -8.sqr >= 64 p -2.cube >= -8 p -3.powten >= 0.001 p -125.cuberoot >= -5 p -512.root(9) >= -2 p [1, 0, 1].average >= 0.666666666666666666... p [8, 32, 1].average(false) >= 13 p [4, nil, 2].average >= 3 p [[20, 10, 0].average, 80].average >= 45 p -9.sqrt >= ArgumentError p -65536.root(16) >= ArgumentError p [].average >= ZeroDivisionError p 8.root >= ArgumentError p [32, "5"].average >= TypeError
p 8.sqr >= 64
p 2.cube >= 8
p 9.sqrt >= 3
p 125.cuberoot >= 5
p 65536.root(16) >= 2
p 3.powten >= 1000
p [3, 8, 1].average >= 4
p -8.sqr >= 64
p -2.cube >= -8
p -3.powten >= 0.001
p -125.cuberoot >= -5
p -512.root(9) >= -2
p [1, 0, 1].average >= 0.666666666666666666...
p [8, 32, 1].average(false) >= 13
p [4, nil, 2].average >= 3
p [[20, 10, 0].average, 80].average >= 45
p -9.sqrt >= ArgumentError
p -65536.root(16) >= ArgumentError
p [].average >= ZeroDivisionError
p 8.root >= ArgumentError
p [32, "5"].average >= TypeError
17行以后是错误的方法,其它的全是正确的。
根据这个方法,我们还可以求出队伍中角色平均等级:class Game_Party #-------------------------------------------------------------------------- # ● 获取平均等级 #-------------------------------------------------------------------------- def average_level # 同伴人数为 0 的情况下 (避免除以零的错误) if @actors.size == 0 return 0 end level = 0 actor_level = [] for i in 0...@actors.size actor_level[i] = @actors[i].level end return actor_level.average(false) end end
class Game_Party
#--------------------------------------------------------------------------
# ● 获取平均等级
#--------------------------------------------------------------------------
def average_level
# 同伴人数为 0 的情况下 (避免除以零的错误)
if @actors.size == 0
return 0
end
level = 0
actor_level = []
for i in 0...@actors.size
actor_level[i] = @actors[i].level
end
return actor_level.average(false)
end
end
|