Advertisement
Narzew

Srednia wazona

Apr 13th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.19 KB | None | 0 0
  1. module SR
  2.     def self.initialize
  3.         $srednia = []
  4.     end
  5.     def self.srednia_wazona(x)
  6.         $result = 0.0
  7.         $sum = 0.0
  8.         x.each{|y|
  9.         $result += ((y[0].to_f)*(y[1].to_f))
  10.         $sum += y[1].to_f
  11.         }
  12.         $result /= $sum
  13.         return $result
  14.     end
  15.     def self.do_nothing
  16.     end
  17.     def self.main_loop
  18.     print "Obliczanie sredniej wazonej\nby Narzew\n"
  19. loop do
  20. loop do
  21. print "Podaj liczbe\n"
  22. liczba = gets.chomp!.to_i
  23. print "Podaj mnoznik\n"
  24. mnoznik = gets.chomp!.to_i
  25. unless mnoznik == 0
  26.     unless liczba == 0
  27.         $srednia << [liczba, mnoznik]
  28.     else
  29.         print "Podaj poprawne dane!\n"
  30.     end
  31. else
  32.     print "Podaj poprawne dane!\n"
  33. end
  34. print "Nastepna ocena ? [Y/N]\n"
  35. mode = gets.chomp!
  36. case mode.to_s
  37. when "Y"
  38.     SR.do_nothing
  39. when "y"
  40.     SR.do_nothing
  41. else
  42.     break
  43. end
  44. end
  45.  
  46. print "Srednia wazona to #{SR.srednia_wazona($srednia)}\n"
  47.  
  48. print "Policzyc kolejna srednia ? [Y/N]\n"
  49. mode = gets.chomp!
  50. case mode.to_s
  51. when "Y"
  52.     SR.do_nothing
  53. when "y"
  54.     SR.do_nothing
  55. else
  56.     break
  57. end
  58. end
  59. end
  60. def self.exit_program
  61.     print "Dziekuje za uruchomienie mojego programu ;)\nCopyright by Narzew\n12.04.2013\nnarzew@gmail.com\n"
  62.     $stdin.gets
  63. end
  64. end
  65.  
  66. begin
  67.     SR.initialize
  68.     SR.main_loop
  69.     SR.exit_program
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement