Skip to content
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
99 changes: 99 additions & 0 deletions Calculator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Brittany Jones
# Ada Calculator
# Last Edited 2/7/18
# The program should ask the user for an operation (string or numeric symbol) and two numbers.

#Primary Requirements
#The program should use the input operation and two numbers to provide the result of applying the operation to the two numbers.
#The program should have support for these four operations: addition, subtraction, multiplication, and division.
#The program should accept both the name (add) and the symbol (+) for each possible operation.


# Prompt user.
puts "Let us do math!"

# Get first number from user.
puts "\nEnter the the first number:"
a = gets.chomp
while true
if a.to_f.to_s == a || a.to_i.to_s == a

Choose a reason for hiding this comment

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

Works great, but I'd encourage you to make a loop that repeats while the input is wrong and not infinitely and then break out when the input is valid. It just makes reading the code flow a little harder.

Also a doesn't make a good variable name. The name doesn't tell anyone what the variable would be used for.

Lastly you also indented too much here. Indent by 1 tab or 2 spaces and be consistent.

break
else
print "Please enter a valid number:"
a = gets.chomp
end
end

# Get the operation they want to use.
puts "Now enter your favorite operator:"
op = gets.chomp

# Get another number from the user.
puts "Give me another number:"
b = gets.chomp

Choose a reason for hiding this comment

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

More cryptic variable names noooooooo!!!

while true
if b.to_f.to_s == b || b.to_i.to_s == b
break
else
print "Please enter a valid number:"
b = gets.chomp
end
end
# While loop, if input goes from integer to string and it is still == to c, then it is a number.
# To validate if user inputs are valid numbers? (Not sure if this part is needed anymore.)
def valid_num(num)
num == "0" || num.to_i != 0
end

def dec_num(num)
num.include?(".")
end
##### NOT SURE IF THIS SECTION IS NEEDED AT ALL, ESPECIALLY IF I HAVE CONVERTED THE USER INPUT INTO FLOAT/Integer.#####
# change data types of number to float to account for decimal numbers.
def num_class(i)

Choose a reason for hiding this comment

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

If you want to avoid showing a decimal for integer math, then it's necessary.

if dec_num (i)
i.to_f
else
i.to_i
end
end
####### End of some unworthy code ######

def domath(a, op, b)
if valid_num(a) && valid_num(b)
a = num_class(a)
b = num_class(b)
#### End of unworthy code which depend upon the previously written uworthy code####
# Case Loop

Choose a reason for hiding this comment

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

A case is not a loop, but a conditional like an if statement.

ans_wer = case op
when '+' , 'add'
a + b
when '-' , 'subtract'
a - b
when ' * ' , 'multiply'
a * b
when '/', 'divide'
# What to do when user tries to use division by 0
if b != 0
a / b
else puts "Please enter a number other than '0' when using division"
end
#Add support for the modulo operator (10 % 3 = 1).
when '%' , 'modulo'

Choose a reason for hiding this comment

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

nice

a % b
#Add support for computing exponents (2^4 = 2 * 2 * 2 * 2 = 16)
when ' ** ' , 'exponents'
a ** b
else
# If they enter some kind of operator that isnt supported.
puts "Sorry, this calculator only does calculations with +, -, %, /, * , and exponents. Please enter your favorite operator:"
op = gets.chomp
end
# print it out when def method domath
puts "\nLet there be math!"
puts "#{a} #{op} #{b} = #{ans_wer}"
end
end

#Print out the formula in addition to the result, i.e. 2 + 2 = 4
domath(a, op ,b)