그래서 저는이 사이트 (오늘)와 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)에서 문제가 발생하면 프로그램에서 일부 키와 연산자를 임의로 삭제합니다. 나는 패턴을 찾기 위해 다른 예제를 시도해 왔지만 소스를 찾을 수 없다. 아래에서는 문제의 예제와 출력, 해시 자체 및 전체 소스 코드를 게시합니다. 도움이나 비평에 미리 감사드립니다!
예 형식
프로그램 입력 (사용자 프롬프트, 사용자 입력) - 프로그램 출력 (식의 합) - 실행
예 1
끝에 해시수학 문제 유형 (예 : 40/5) : 40/5 + 2 - 5 * 5 - 5 * 5 - 100
-450
{닐 => 40, "/"=> 5, "+"= "2"- "="100 "*"=> 5}
예 2
수학 문제를 입력하십시오 (예 : 5분의 40) : 1 + 2 - 0 + 3
4
{전무 = "1"+ "=> 3,"- "=> 0}
예 3
형 수학 문제 (예 5분의 40.) : 10 - 5 * 2 + 8 + 2
12
{닐 => 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
처럼 보일 것입니다. 해시는 키당 하나의 인스턴스로 제한되므로 토큰이 두 번 나타나면 해시가 중단됩니다. 배열은 어떻습니까? – tadman
오, 당신이 아주 옳습니다! 어리석은 .. 아마 배열을 사용하여 그 방식으로 파싱 할 수 있습니다. 감사!! –
배열에서 [Polish notation] (https://en.wikipedia.org/wiki/Polish_notation)으로 시퀀스를 변환 할 수 있습니다. 예를 들어 '2 + 3 * 4-1'은'[ '-', [ '+', 2, [ '*', 3, 4]], 1]. – tadman