A
A
ALaLaLh2020-04-09 23:08:47
ruby
ALaLaLh, 2020-04-09 23:08:47

How to make the calculator be chained? How to write stacks in code? How to display the last prime number?

you need to be able to chain actions

i.e.

2
+
2
4
+
5
9
, etc., and work with the stack (the first container entered - the last one left), and so that the calculator could display the last prime number (and write it to the stack)
Calculator has started

3

primes

10

7 // displays the last prime number, writes all numbers to the stack

pop

7

pop

5

pop
Error

I don't understand what and how and where to enter. Please help me (
code:

print 'Type number: '
first_number = gets.strip.to_i
print 'Type operation: '
operation = gets.strip
if %w[+ - * / ** %].include? operation
  print 'Type second number: '
  second_number = gets.strip.to_i
  puts first_number.send(operation, second_number)
elsif %w[sqrt sin cos tan exp].include? operation
  puts Math.send(operation, first_number)
else
  puts 'error'
if fibonacci(n)
  n < 3
        1
  else
        fibonacci(n - 1) + fibonacci(n - 2)
        (1..16).each {|n| puts "#{fibonacci(n)}, "}
puts "..."
    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 %w[+ - * / ** %].include? operation
  print 'Type second number: '
  second_number = gets.strip.to_i
  puts first_number.send(operation, second_number)
elsif %w[sqrt sin cos tan exp].include? operation
  puts Math.send(operation, first_number)
else
  puts 'error'

    
      end
    end
    print 'Result: '
    puts result
  end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alfss, 2020-04-10
@alfss

https://ru.m.wikipedia.org/wiki/%D0%9E%D0%B1%D1%80...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question