快捷完成复杂的公式运算——自定义公式并自动计算的Ruby应用

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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