/tutorial/fibonacci.e

http://github.com/tybor/Liberty · Specman e · 39 lines · 34 code · 5 blank · 0 comment · 3 complexity · 353392e800322ad6f334123e64355568 MD5 · raw file

  1. class FIBONACCI
  2. insert
  3. ARGUMENTS
  4. create {ANY}
  5. make
  6. feature {ANY}
  7. make
  8. local
  9. arg: STRING
  10. do
  11. if argument_count = 0 then
  12. std_input.read_line
  13. arg := std_input.last_string
  14. elseif argument_count = 1 then
  15. arg := argument(1)
  16. end
  17. if not arg.is_integer then
  18. std_error.put_line("Expecting an integer")
  19. die_with_code(exit_failure_code)
  20. end
  21. io.put_integer(fibonacci(arg.to_integer))
  22. io.put_new_line
  23. end
  24. fibonacci (i: INTEGER): INTEGER
  25. require
  26. i >= 0
  27. do
  28. if i < 2 then
  29. Result := i
  30. else
  31. Result := fibonacci(i - 2) + fibonacci(i - 1)
  32. end
  33. end
  34. end -- class FIBONACCI