大佬教程收集整理的这篇文章主要介绍了快捷完成复杂的公式运算——自定义公式并自动计算的Ruby应用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
class CustomFunctionsmanager attr_accessor :functions def initialize(DATAFILEPath) @dateFilePath = DATAFILEPath @functions = [] loadFunctions end def loadFunctions if File.exist?(@dateFilePath) fileHandle = File.new(@dateFilePath,'r') fileHandle.each_line do |line| @functions << CustomFunction.new(line.chomp) end fileHandle.close return @functions end return "" end def appendCustomFunction(customFunctionString) @functions << CustomFunction.new(customFunctionString.chomp) updateDATAFILE end def deleteCustomFunction(function) @functions.delete(function) updateDATAFILE end def updateDATAFILE fileHandle = File.new(@dateFilePath,'w') @functions.each do |func| fileHandle.puts func.to_s end fileHandle.close end end class CustomFunction attr_accessor :customFuncionString def initialize(customFuncionString) #init BB-4aC to B*B-4*a*C strTem = customFuncionString.gsub(/([\w])([A-Z])/,'\1*\2') strTem = strTem.gsub(/(\d)([a-z])/,'\1*\2') while !strTem.eql?(customFuncionString) customFuncionString = strTem strTem = customFuncionString.gsub(/([\w])([A-Z])/,'\1*\2') strTem = strTem.gsub(/(\d)([a-z])/,'\1*\2') end @customFuncionString = strTem #init 'a/b' to 'a.0/b'? end def to_s @customFuncionString end def calculate params = [] customFuncionStringTem = @customFuncionString puts "请依次输入函数 '#{CustomFuncionStringTem}' 的参数:" @customFuncionString.scan(/[A-Z]{1}[a-z]*|[a-z]+/) do |param| params << param unless params.include?(param) end params.each do |param| puts param + ' = ?' inputs = gets.chomp value = inputs.index('.') || @customFuncionString.index('/') ? inputs.to_f : inputs.to_i customFuncionStringTem = customFuncionStringTem.gsub(eval('/' + param + '/'),value.to_s) end print @customFuncionString + ' = ' + customFuncionStringTem + ' = ' puts eval(customFuncionStringTem) end end class UI attr_accessor :functionManager def initialize(DATAFILEPath) @functionManager = CustomFunctionsmanager.new(DATAFILEPath) end def displaymenu value = 0 while value != 4 puts "请输入功能序号:" puts "0.显示函数" puts "1.增加函数" puts "2.使用函数" puts "3.删除函数" puts "4.退出程序" value = gets.chomp.to_i displayAllFunction(@functionManager.functions) if value == 0 appendCustomFunction if value == 1 useCustomFunction if value == 2 deleteCustomFunction if value == 3 end puts "程序已退出。" end def displayAllFunction(functions) index = 1; functions.each do |func| puts " #{index}.#{func.to_s}" index += 1 end end def SELEcTingFunctions puts "请选择一条函数:" functions = @functionManager.functions displayAllFunction(functions) value = gets.chomp.to_i - 1 if value < 0 || value > functions.length - 1 puts "错误的数值!" return nil end functions[value] end def appendCustomFunction puts "请输入函数:" str = gets.chomp @functionManager.appendCustomFunction(str) end def useCustomFunction function = SELEcTingFunctions function.calculate if function end def deleteCustomFunction function = SELEcTingFunctions @functionManager.deleteCustomFunction(function) if function end end UI.new("CustomFunctions.txt").displaymenu
以上是大佬教程为你收集整理的快捷完成复杂的公式运算——自定义公式并自动计算的Ruby应用全部内容,希望文章能够帮你解决快捷完成复杂的公式运算——自定义公式并自动计算的Ruby应用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。