Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Pipes - Nkiru- Calculator.rb #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions Pipes - <Nkiru> - <Calculator.rb>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Nkiru Onwuneme
# Last edited 8/11/17
# Create a calculator

##Issues: user entry set to interger or in Method - fixed
# consolidate methods

# Methods for computing math operations
def add(num_1, num_2)
result = num_1 + num_2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that each of these is its own method. It makes it easy to find where in the code a certain operation occurs, and means that if you need to add a long complex operation (think finding the least common multiple of 2 numbers) the pattern will still work.

return result
end

def subtract(num_1, num_2)
result = num_1 - num_2
return result
end

def multiply(num_1, num_2)
result = num_1 * num_2
return result
end

# method to check if denominator is o
def divide(num_1, num_2)
if num_2 == 0
puts "Invalid number, numerator indivisible by 0"
else
result = num_1.to_f/num_2.to_f
end
return result.round(6)
end

def exponent(num_1, num_2)
result = num_1 ** num_2
return result
end

def modulo(num_1, num_2)
result = num_1 % num_2
return result
end

def num_check(num)
begin
num = gets.chomp
if num.include?(".")
return num = Float(num)
else
return num = Integer(num)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch your spacing here - the else should be at the same level as the if above, and the return on line 50 should be indented more. When in doubt, use Atom's auto-indent.

end
rescue
print "Please enter an integer number:"
retry
end
end

# Greeting, welcome message/instructions
puts "What your name?"
name = gets.chomp
puts "Hello #{name}!! I am C3P0 your human calculator................\n
These are the operations that I can perform: add(+), subtract(-), multiply(*),
divide(/), exponent(^) and modulo(%)......\n
To exit the calculator enter \'quit'\ for operation...\n
Let's get started: enter two numbers\n\n"

puts "First number"
num_1 = num_check(num_1)
puts "Second number"
num_2 = num_check(num_2)

puts "Enter operation you want to perform...."
prompt = "> "
while operation = gets.chomp
case operation
when "add" , "+"
puts add(num_1, num_2)
break
when "subtract" , "-"
puts subtract(num_1, num_2)
break
when "divide" , "/"
puts divide(num_1, num_2)
break
when "multiply" , "*"
puts multiply(num_1, num_2)
break
when "exponent" , "^"
puts exponent(num_1, num_2)
break
when "modulo" , "%"
puts modulo(num_1, num_2)
break
when "quit"
break
else
puts "Invalid operation, please select again"
print prompt
end
end

# output calculation