Ruby calculator-哈希未正确存储
因此,我首先要写的是,我是这个网站的新手(今天),也是Ruby编程语言的新手(3天前),所以不要害怕撕碎我的代码——我正在努力学习并变得更好 基本上。。我正在创建一个控制台计算器,它能够从用户那里读取一个简单的数学问题(或一系列数学问题)并求解方程。它不使用操作顺序或任何花哨的东西(现在),它基本上可以正常工作,除了这个我无法理解的奇怪错误Ruby calculator-哈希未正确存储,ruby,hash,calculator,Ruby,Hash,Calculator,因此,我首先要写的是,我是这个网站的新手(今天),也是Ruby编程语言的新手(3天前),所以不要害怕撕碎我的代码——我正在努力学习并变得更好 基本上。。我正在创建一个控制台计算器,它能够从用户那里读取一个简单的数学问题(或一系列数学问题)并求解方程。它不使用操作顺序或任何花哨的东西(现在),它基本上可以正常工作,除了这个我无法理解的奇怪错误 Userinput = "1 + 2 + 3 - 4" # First I split the user input into an a
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)的问题时,程序会随机删除一些键和操作符。我一直在尝试不同的例子来寻找一种模式,但我找不到来源。下面我将发布问题示例及其输出,以及哈希本身,然后是完整的源代码。提前感谢您的帮助或批评
示例格式
程序输入(用户提示、用户输入)--
程序输出(方程式之和)--
执行结束时的哈希
例1
键入一道数学题(例如40/5):40/5+2-5*5-5*5-100
-450
{nil=>40,“/”=>5,“+”=>2,“-”=>100,“*”=>5}
例2
键入一道数学题(例如40/5):1+2-0+3
四,
{nil=>1,“+”=>3,“-”=>0}
例3
键入一道数学题(例如40/5):10-5*2+8+2
十二,
{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
=开始
main.rb
版本1.0
Alex Hail撰写-2016年10月16日
解析用户输入的基本算术公式并进行求解
=结束
@operationsParser=”“#全局解析器
@lastKeyAdded=“”
私有的
def任命类型(sv)
如果sv=~/\d/
sv.to_i
其他的
sv
结束
结束
私有的
def操作(操作)
总和=0
操作。每个do | k,v|
如果k.nil?
总和+=v
其他的
案例k
当“+”时,则sum+=v
当“-”时,则和-=v
当“*”时,sum=sum*v
当“/”时,则sum=sum/v
其他的
结束
结束
结束
总和
结束
私有的
解方程
打印“键入数学问题(例40/5):”
userInput=gets.chomp
#数组以保存所有数字及其操作
操作={}40,“/”=>5)--将是40/5
@operationsParser.each do | stringValue|
如果指定类型(stringValue)。是否为?整数
操作[@lastKeyAdded!=“”?@lastKeyAdded:nil]=appointType(stringValue)
否则#appointType将默认返回字符串
keyToAdd=指定类型(stringValue)
@lastKeyAdded=keyToAdd
结束
结束
#检查键中的运算符(+、*、-、/、或nil)是否有效,如果无效,则出错并退出,如果有效,则进行操作
操作。每个do | k,v|
案例k
当“+”时
当“-”
当“*”时
当“/”时
当零
其他的
#如果散列中有无效运算符,请退出程序
puts“退出程序时出错-使用了无效运算符(仅+,-,*,/请)”
返回
结束
结束
总和=操作(操作)
求和运算
结束
解方程
好的,所以问题是您选择的数据结构,根据定义,哈希必须始终维护一组唯一键以映射到其值。现在,如果你死心塌地地想使用散列,你可以尝试将所有键映射到空数组,然后向其中添加数值,然后对每个数组中的值处理该操作(因为你忽略了操作的顺序)
然后,当处理数组时,它应该如下所示
{nil =>[1], '+' => [1, 2, 3], '-' => [3, 7], '*' => [4, 47], '/' => [3, 5]}
你不能有
{“+”=>1,“+”=>2}
,这是不可能的。请记住,哈希值仅限于每个键一个实例,因此如果令牌出现两次,则会出现故障。阵列呢?哦,你说得很对!我真傻。。我可能只需要使用一个数组并以这种方式解析它。谢谢你可以将你的序列转换成一个数组,例如:2+3*4-1
变成['-',['+',2,['*',3,4]],1]
。塔德曼从我嘴里说出了这句话,这是大学本科时的java作业。Alex你也在找解析答案吗?今晚下班后我会研究一下波兰语符号。我会更新这个计算器一段时间,这样我就有足够的时间来重构。谢谢大家!有人在我的原始帖子的评论中回答了我的问题,但这也很有帮助,所以我将把这个作为公认的答案,让人们知道我的问题已经解决。好的,祝你在追求知识的过程中好运,这是一个很好的练习。
{nil =>[1], '+' => [1, 2, 3], '-' => [3, 7], '*' => [4, 47], '/' => [3, 5]}