Skip to content

Instantly share code, notes, and snippets.

@maxluzuriaga
Created November 6, 2010 17:30
Show Gist options
  • Save maxluzuriaga/665558 to your computer and use it in GitHub Desktop.
Save maxluzuriaga/665558 to your computer and use it in GitHub Desktop.
Command-line Ruby application for automating some physics equations based on Newton's Laws. Includes weight, gravity, and acceleration due to gravity.
class Calculator
attr_accessor :exits
@@constant = 0.0000000000667
def welcome
puts "\n<< Welcome >>\n\n"
puts 'type in "help" to see all available options'
end
def launch!
welcome
input = ""
until @exits.include?(input)
print "what would you like to calculate? "
input = gets.chomp.downcase
case input
when "help"
help
when "h"
help
when "gravity"
calculate_gravity
when "g"
calculate_gravity
when "time"
calculate_time
when "t"
calculate_time
when "average velocity"
calculate_a_velocity
when "a velocity"
calculate_a_velocity
when "a v"
calculate_a_velocity
when "instantaneous velocity"
calculate_i_velocity
when "i velocity"
calculate_i_velocity
when "i v"
calculate_i_velocity
when "distance"
calculate_distance
when "d"
calculate_distance
when "acceleration"
calculate_acceleration
when "a"
calculate_acceleration
when "acceleration due to gravity"
calculate_acceleration_due_to_gravity
when "a g"
calculate_acceleration_due_to_gravity
when "weight"
calculate_weight
when "w"
calculate_weight
else
error(input) unless @exits.include?(input)
end
end
conclude
end
def help
puts "\ntype in one of the below"
puts " gravity (or 'g')"
puts " time (or 't')"
puts " average velocity (or 'a velocity', or 'a v')"
puts " instantaneous velocity (or 'i velocity', or 'i v'"
puts " distance (or 'd')"
puts " acceleration (or 'a')"
puts " acceleration due to gravity (or 'a g')"
puts " weight (or 'w')" + "\n"
puts "or"
puts " nothing"
puts " quit (or 'q')"
puts " exit (or 'x')"
puts "to quit\n\n"
end
def calculate_gravity
m1 = get_data "mass1", "kg"
m2 = get_data "mass2", "kg"
d = get_data "distance", "meters"
# Constant of gravity replaced with calculated value
result = (@@constant * m1 * m2) / (d**2)
puts get_solution(result, "newtons")
result_in_pounds = convert_to_lbs(result)
puts get_solution(result_in_pounds, "pounds", false)
end
def calculate_time
d = get_data "distance", "meters"
a = get_data "acceleration", "m/s/s"
result = Math.sqrt((2 * d) / a)
puts get_solution(result, "seconds")
end
def calculate_a_velocity
d = get_data "change in distance", "in meters"
t = get_data "time", "seconds"
result = d / t
puts get_solution(result, "meters/second")
end
def calculate_i_velocity
v_initial = get_data "initial velocity", "m/s"
a = get_data "acceleration", "m/s/s"
t = get_data "time", "seconds"
result = v_initial + (a * t)
puts get_solution(resultm "meters/second")
end
def calculate_distance
v_initial = get_data "initial velocity", "m/s"
t = get_data "time", "seconds"
a = get_data "acceleration", "m/s/s"
result = (v_initial * t) + (0.5 * a * t**2)
puts get_solution(result, "meters")
end
def calculate_acceleration
v = get_data "change in velocity", "m/s"
t = get_data "time", "seconds"
result = v / t
puts get_solution(result, "meters/second^2")
end
def calculate_acceleration_due_to_gravity
m = get_data "mass", "kg"
d = get_data "distance", "meters"
result = (@@constant * m) / (d**2)
puts get_solution(result, "meters/second^2")
end
def calculate_weight
m = get_data "mass", "kg"
a = get_data "acceleration due to gravity", "m/s/s"
result = m * a
puts get_solution(result, "newtons")
result_in_pounds = convert_to_lbs(result)
puts get_solution(result_in_pounds, "pounds", false)
end
def error(invalid_input)
puts '"' + invalid_input + '" is not a valid action.'
end
def conclude
puts "\n<< Goodbye >>"
end
private
def convert_to_lbs(n)
n * 0.224808943
end
def get_data(message, units)
input = ""
until input.to_f != 0.0
print "#{message} (in #{units}) = "
input = gets.chomp
if input == "0"
break
end
puts "Please input only digits." unless input.to_f != 0.0
end
return input.to_f
end
def get_solution(result, units, linebreak=true)
lbreak = "\n" if linebreak == true
lbreak ||= ""
"#{lbreak}#{result.to_s.split("e-").join(" * 10^")} #{units}"
end
end
calculator = Calculator.new
calculator.exits = %w(nothing quit q exit x)
calculator.launch!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment