-
-
Save amy-mac/3f19e38115c70dca6cfc to your computer and use it in GitHub Desktop.
Calculator I had to create for the WDI homework first week. The instructions are at the top.
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
# Create a command line calculator. It should have a basic and advanced mode. | |
# | |
# * basic calculator (+, -, *, /) | |
# * advanced calculator (exponents, square root) | |
# | |
# * For a basic calculator, press 1. | |
# * For an advanced calculator, press 2. To quit, press Q. | |
# * For calculators, first enter type of calculation, then enter 2 numbers. | |
# | |
# Create methods for each operation, and each one to show and interact with a text menu. | |
# ---------------------------------------------------------------------------- | |
# Add a trip calculator to your program. Ask the user (politely) for four inputs: | |
# | |
# **Distance** – how far will you drive? | |
# **MPG** – what is the fuel efficiency of the car? | |
# **$PG** – how much does gas cost per gallon? | |
# **Speed** – how fast will you drive? | |
# | |
# Your output is a string: “Your trip will take 3.5 hours and cost $255.33.” | |
# | |
# Add this as a menu choice. | |
def add(num1, num2) | |
num1 = num1.to_f | |
num2 = num2.to_f | |
if (num1 && num2).is_a? Float | |
answer = num1 + num2 | |
puts "#{num1} + #{num2} = #{answer}" | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def subtract(num1, num2) | |
num1 = num1.to_f | |
num2 = num2.to_f | |
if (num1 && num2).is_a? Float | |
answer = num1 - num2 | |
puts "#{num1} - #{num2} = #{answer}" | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def multiply(num1, num2) | |
num1 = num1.to_f | |
num2 = num2.to_f | |
if (num1 && num2).is_a? Float | |
answer = num1 * num2 | |
puts "#{num1} * #{num2} = #{answer}" | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def divide(num1, num2) | |
num1 = num1.to_f | |
num2 = num2.to_f | |
if (num1 && num2).is_a? Float | |
answer = num1 / num2 | |
puts "#{num1} / #{num2} = #{answer}" | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def exponent(num, exp) | |
num = num.to_i | |
exp = num.to_i | |
if (num && exp).is_a? Fixnum | |
answer = num ** exp | |
puts "#{num} to the power of #{exp} is #{answer}" | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def square_root(num) | |
num = num.to_i | |
if num.is_a? Fixnum | |
answer = Math.sqrt(num.to_i) | |
puts "The square root of #{num} is #{answer}" | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def trip_time(distance, speed) | |
distance = distance.to_f | |
speed = speed.to_i | |
unless (distance || speed).zero? | |
time = distance / speed | |
time = time.round(2) | |
return time | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def trip_cost(distance, mpg, gas_cost) | |
distance = distance.to_f | |
mpg = mpg.to_f | |
gas_cost = gas_cost.to_f | |
if ((distance && mpg && gas_cost).is_a? Float) && ((distance && mpg && gas_cost).zero? == false) | |
gas_for_trip = distance / mpg | |
trip_cost = gas_cost * gas_for_trip | |
trip_cost = trip_cost.round(2) | |
return trip_cost | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def bmi(height, weight) | |
height = height.to_i | |
weight = weight.to_f | |
unless (height || weight).zero? | |
bmi = (weight / (height * height)) * 703 | |
bmi = bmi.round(2) | |
puts "Your BMI is #{bmi}." | |
else | |
puts 'You did not enter valid numbers. Please try again.' | |
end | |
end | |
def basic | |
puts 'What type of calculation would you like to perform:' | |
puts 'add, subtract, multiply, or divide?' | |
calc_type = gets.chomp.downcase | |
puts "Please enter 2 numbers that you would like to #{calc_type} separated by comma." | |
nums = gets.chomp | |
nums = nums.split(',') | |
case calc_type | |
when 'add' | |
add(nums[0], nums[1]) | |
when 'subtract' | |
subtract(nums[0], nums[1]) | |
when 'multiply' | |
multiply(nums[0], nums[1]) | |
when 'divide' | |
divide(nums[0], nums[1]) | |
else | |
puts 'I didn\'t understand your command.' | |
basic | |
end | |
puts '' | |
start | |
end | |
def advanced | |
puts 'What type of calculation would you like to perform:' | |
puts 'square root, or exponent?' | |
calc_type = gets.chomp.downcase | |
if calc_type == 'exponent' | |
puts 'Enter 2 numbers separated by a comma:' | |
nums = gets.chomp.split(',') | |
elsif calc_type == 'square root' | |
puts 'What is the number you\'d like the square root of?' | |
num = gets.chomp | |
else | |
puts 'I didn\'t understand your command.' | |
advanced | |
end | |
case calc_type | |
when 'square root' | |
square_root(num) | |
when 'exponent' | |
exponent(nums[0], nums[1]) | |
else | |
puts 'I didn\'t understand your command.' | |
advanced | |
end | |
puts '' | |
start | |
end | |
def trip_calc | |
puts 'How many miles are you going to drive?' | |
distance = gets.chomp | |
puts 'What is the fuel efficiency of the car?' | |
mpg = gets.chomp | |
puts 'How much does gas cost per gallon?' | |
gas_cost = gets.chomp | |
puts 'What is the speed you will you drive? (Please enter number only)' | |
speed = gets.chomp | |
time = trip_time(distance, speed) | |
cost = trip_cost(distance, mpg, gas_cost) | |
if (time && cost).is_a? Float | |
puts "Your trip will take #{time} hours and cost $#{cost}." | |
end | |
puts '' | |
start | |
end | |
def bmi_calc | |
puts 'Please enter your height in inches:' | |
height = gets.chomp | |
puts 'Please enter your weight in lbs:' | |
weight = gets.chomp | |
bmi(height, weight) | |
puts '' | |
start | |
end | |
def start | |
puts '---------------------------------------------------' | |
puts 'For a basic calculator, press 1.' | |
puts 'For an advanced calculator, press 2.' | |
puts 'For a trip calculator, press 3.' | |
puts 'For a BMI calculator, press 4. To quit, press Q.' | |
puts '---------------------------------------------------' | |
choice = gets.chomp.downcase | |
if choice == '1' | |
basic | |
elsif choice == '2' | |
advanced | |
elsif choice == '3' | |
trip_calc | |
elsif choice == '4' | |
bmi_calc | |
elsif choice == 'q' | |
Process.exit(0) | |
else | |
puts 'I didn\'t understand your command.' | |
start | |
end | |
end | |
start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Holy smokeys' looks very eleganté