Created
July 13, 2016 21:00
-
-
Save JasonMadeSomething/bdf87ae046dd66a5da0b5511e998933a to your computer and use it in GitHub Desktop.
Career Prep Assignment 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
equation = ARGV[0].dup | |
my_dear = equation.scan("*").count + equation.scan('/').count | |
aunt_sally = equation.scan("+").count + equation.scan('-').count | |
def operate(operation, number1, number2) | |
case operation | |
when '/' | |
return (number1.to_i / number2.to_i).to_s | |
when '*' | |
return (number1.to_i * number2.to_i).to_s | |
when '+' | |
return (number1.to_i + number2.to_i).to_s | |
when '-' | |
return (number1.to_i - number2.to_i).to_s | |
else | |
puts "ERROR!" | |
end | |
end | |
my_dear.times do | |
division_index = equation.index('/') | |
multiplication_index = equation.index('*') | |
if(division_index && multiplication_index) | |
if(division_index < multiplication_index) | |
spot = division_index | |
operation = '/' | |
else | |
spot = multiplication_index | |
operation = '*' | |
end | |
elsif division_index | |
spot = division_index | |
operation = '/' | |
else | |
spot = multiplication_index | |
operation = '*' | |
end | |
number1 = equation[spot - 1] | |
number2 = equation[spot + 1] | |
equation.sub!("#{number1}#{operation}#{number2}", operate(operation, number1, number2)) | |
end | |
aunt_sally.times do | |
subtraction_index = equation.index('-') | |
addition_index = equation.index('+') | |
if(subtraction_index && addition_index) | |
if(subtraction_index < addition_index) | |
spot = subtraction_index | |
operation = '-' | |
else | |
spot = addition_index | |
operation = '+' | |
end | |
elsif subtraction_index | |
spot = subtraction_index | |
operation = '-' | |
else | |
spot = addition_index | |
operation = '+' | |
end | |
number1 = equation[spot - 1] | |
number2 = equation[spot + 1] | |
equation.sub!("#{number1}#{operation}#{number2}", operate(operation, number1, number2)) | |
end | |
puts equation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment