Ruby 计算器 - 哈希存储不正确
Ruby calculator - hash not storing correctly
所以我将首先写下我是这个网站的新手(今天),以及 Ruby 编程语言(3 天前),所以不要害怕撕裂除了我的代码——我正在努力学习并变得更好。
基本上.. 我正在创建一个控制台计算器,它能够从用户那里读取一个简单的数学问题(或一串数学问题)并求解方程。它不使用操作顺序或任何花哨的东西(还),它基本上可以工作,除了这个我无法弄清楚的奇怪错误。
Userinput = "1 + 2 + 3 - 4"
# First I split the user input into an array of stirngs and then loop over the
# array of strings and depict whether a string is a key or hash (see code below)
# program should store these characters in a hash like so..
hash = { nil=>1, "+"=>2, "+"=>3, "-"=>4 }
然后我会使用散列的键来确定我接下来是加减乘除。
一切正常!只是当我用超过 2 个操作(即 1 + 2 - 0 + 3)来解决问题时,程序会随机遗漏一些键和运算符。我一直在尝试不同的示例来搜索模式,但找不到来源。下面我将 post 问题示例及其输出,以及哈希本身,然后是完整的源代码。在此先感谢您的帮助或批评!
示例格式
程序输入(用户提示,用户输入)--
程序输出(等式之和)——
执行结束时的散列
示例 1
Type a math problem (ex. 40 / 5): 40 / 5 + 2 - 5 * 5 - 5 * 5 - 100
-450
{nil=>40, "/"=>5, "+"=>2, "-"=>100, "*"=>5}
示例 2
Type a math problem (ex. 40 / 5): 1 + 2 - 0 + 3
4
{nil=>1, "+"=>3, "-"=>0}
示例 3
Type a math problem (ex. 40 / 5): 10 - 5 * 2 + 8 + 2
12
{nil=>10, "-"=>5, "*"=>2, "+"=>2}
源代码:main.rb
=begin
main.rb
Version 1.0
Written by Alex Hail - 10/16/2016
Parses a basic, user-entered arithmetic equation and solves it
=end
@operationsParser = "" # global parser
@lastKeyAdded = ""
private
def appointType(sv)
if sv =~ /\d/
sv.to_i
else
sv
end
end
private
def operate(operations)
sum = 0
operations.each do |k, v|
if k.nil?
sum += v
else
case k
when '+' then sum += v
when '-' then sum -= v
when '*' then sum = sum * v
when '/' then sum = sum / v
else
end
end
end
sum
end
private
def solveEquation
print "Type a math problem (ex. 40 / 5): "
userInput = gets.chomp
#array to hold all numbers and their cooresponding operation
operations = {} # <== Empty hash
#split the user input via spaces
@operationsParser = userInput.split(" ")
#convert numbers into numbers store operators in hash ( nil => 40, "/" => 5) -- would be 40 / 5
@operationsParser.each do |stringValue|
if appointType(stringValue).is_a? Integer
operations[@lastKeyAdded != "" ? @lastKeyAdded : nil] = appointType(stringValue)
else #appointType will return a string by default
keyToAdd = appointType(stringValue)
@lastKeyAdded = keyToAdd
end
end
#check if operators(+, *, -, /, or nil) in the keys are valid, if not, error and exit, if so, operate
operations.each do |k,v|
case k
when '+'
when '-'
when '*'
when '/'
when nil
else
# Exit the program if we have an invalid operator in the hash
puts "Exiting program with error - Invalid operator used (Only +, -, *, / please)"
return
end
end
sum = operate(operations)
puts sum, operations
end
solveEquation
好的,问题出在您选择的数据结构上,根据定义,散列必须始终维护一组唯一键以映射到其值。现在,如果您对使用散列死心塌地,您可以尝试将所有键映射到空数组,然后向其添加数值,然后对其各自数组中的每个值处理该操作(因为您以任何方式忽略了操作顺序)
h = Hash.new([]) #to set the default value of each key to an empty arrary
然后当你处理你的数组时它应该看起来像这样
{nil =>[1], '+' => [1, 2, 3], '-' => [3, 7], '*' => [4, 47], '/' => [3, 5]}
所以我将首先写下我是这个网站的新手(今天),以及 Ruby 编程语言(3 天前),所以不要害怕撕裂除了我的代码——我正在努力学习并变得更好。
基本上.. 我正在创建一个控制台计算器,它能够从用户那里读取一个简单的数学问题(或一串数学问题)并求解方程。它不使用操作顺序或任何花哨的东西(还),它基本上可以工作,除了这个我无法弄清楚的奇怪错误。
Userinput = "1 + 2 + 3 - 4"
# First I split the user input into an array of stirngs and then loop over the
# array of strings and depict whether a string is a key or hash (see code below)
# program should store these characters in a hash like so..
hash = { nil=>1, "+"=>2, "+"=>3, "-"=>4 }
然后我会使用散列的键来确定我接下来是加减乘除。
一切正常!只是当我用超过 2 个操作(即 1 + 2 - 0 + 3)来解决问题时,程序会随机遗漏一些键和运算符。我一直在尝试不同的示例来搜索模式,但找不到来源。下面我将 post 问题示例及其输出,以及哈希本身,然后是完整的源代码。在此先感谢您的帮助或批评!
示例格式
程序输入(用户提示,用户输入)-- 程序输出(等式之和)—— 执行结束时的散列
示例 1
Type a math problem (ex. 40 / 5): 40 / 5 + 2 - 5 * 5 - 5 * 5 - 100
-450
{nil=>40, "/"=>5, "+"=>2, "-"=>100, "*"=>5}
示例 2
Type a math problem (ex. 40 / 5): 1 + 2 - 0 + 3
4
{nil=>1, "+"=>3, "-"=>0}
示例 3
Type a math problem (ex. 40 / 5): 10 - 5 * 2 + 8 + 2
12
{nil=>10, "-"=>5, "*"=>2, "+"=>2}
源代码:main.rb
=begin
main.rb
Version 1.0
Written by Alex Hail - 10/16/2016
Parses a basic, user-entered arithmetic equation and solves it
=end
@operationsParser = "" # global parser
@lastKeyAdded = ""
private
def appointType(sv)
if sv =~ /\d/
sv.to_i
else
sv
end
end
private
def operate(operations)
sum = 0
operations.each do |k, v|
if k.nil?
sum += v
else
case k
when '+' then sum += v
when '-' then sum -= v
when '*' then sum = sum * v
when '/' then sum = sum / v
else
end
end
end
sum
end
private
def solveEquation
print "Type a math problem (ex. 40 / 5): "
userInput = gets.chomp
#array to hold all numbers and their cooresponding operation
operations = {} # <== Empty hash
#split the user input via spaces
@operationsParser = userInput.split(" ")
#convert numbers into numbers store operators in hash ( nil => 40, "/" => 5) -- would be 40 / 5
@operationsParser.each do |stringValue|
if appointType(stringValue).is_a? Integer
operations[@lastKeyAdded != "" ? @lastKeyAdded : nil] = appointType(stringValue)
else #appointType will return a string by default
keyToAdd = appointType(stringValue)
@lastKeyAdded = keyToAdd
end
end
#check if operators(+, *, -, /, or nil) in the keys are valid, if not, error and exit, if so, operate
operations.each do |k,v|
case k
when '+'
when '-'
when '*'
when '/'
when nil
else
# Exit the program if we have an invalid operator in the hash
puts "Exiting program with error - Invalid operator used (Only +, -, *, / please)"
return
end
end
sum = operate(operations)
puts sum, operations
end
solveEquation
好的,问题出在您选择的数据结构上,根据定义,散列必须始终维护一组唯一键以映射到其值。现在,如果您对使用散列死心塌地,您可以尝试将所有键映射到空数组,然后向其添加数值,然后对其各自数组中的每个值处理该操作(因为您以任何方式忽略了操作顺序)
h = Hash.new([]) #to set the default value of each key to an empty arrary
然后当你处理你的数组时它应该看起来像这样
{nil =>[1], '+' => [1, 2, 3], '-' => [3, 7], '*' => [4, 47], '/' => [3, 5]}