A
A
Aniri142020-04-09 14:56:21
ruby
Aniri14, 2020-04-09 14:56:21

How to add support for unary operations to ruby ​​calculator?

there is a calculator:
--, sqrt, sin, cos, tan, ctan, exp, ln, ! , what would count? at the request of sin, it outputs a sine, and so on. At least just an example for me, I'll deal with the rest. Thank you very much in advance.

how to add support to it

# цепной калькулятор

  while true do
  puts "Calculator has started"
  
  print 'Type number: '
  result = gets.strip.to_i
  print 'Type operation: '
  operation = gets.strip

  while true do
    print 'Type number: '
    second_number = gets.strip.to_i
    if operation == "+"
      result += second_number
    elsif operation == "-"
      result -= second_number
    elsif operation == "*"
      result *= second_number
    elsif operation == "/"
      result /= second_number
      elsif operation == "**"
      result **= second_number
          elsif operation == "%"
      result %= second_number
      
        end
 
    print 'Result: '
    puts result
 
    operation = gets.strip
    break if operation == ""
  end
end

# Ячейка пам¤ти, не сбрасывается при рестарте калькул¤тора
memory = 0
while true
  puts "Calculator has started"
  print 'Type number: '
  result = gets.strip.to_i
 
  while true
    print 'Type operation: '
    operation = gets.strip
    break if operation == ""
 
    if operation == 'mw'
      memory = result
    elsif operation == 'mr'
      result = memory
    else
      print 'Type number: '
      second_number = gets.strip.to_i
    end
 
    if operation == "+"
      result += second_number
    elsif operation == "-"
      result -= second_number
    elsif operation == "*"
      result *= second_number
    elsif operation == "/"
      result /= second_number
      elsif operation == "**"
      result **= second_number
    
      end
    end
    print 'Result: '
    puts result
  end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oh_shi, 2020-04-09
@Aniri14

https://ruby-doc.org/core-2.7.1/Math.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question