Ruby - Štatistiky hokeja v.2
Vzorky kód hokejových štatistík verzia 2 v Ruby. Príklady využitia funkcií, čítanie zo súboru a ďalších konštrukcií.
require "rexml/document" if (ARGV.length == 3) INPUT_FILE = ARGV[0] COMMANDS_FILE = ARGV[1] output_file = ARGV[2] else puts "Wrong argumnts number" exit end class Team attr_accessor :name, :attack_home, :attack_away, :defense_home, :defense_away, :points_home, :points_away, :points_all @@teams = [] def initialize(name) @name = name @attack_home = 0 @attack_away = 0 @defense_home = 0 @defense_away = 0 @points_home = 0 @points_away = 0 @points_all = 0 @@teams << self end def Team.contains?(name) return @@teams.find{ |t| t.name == name } end def Team.printa @@teams.each {|t| p t} end def Team.get_most_points(num, type) case type when "all" return @@teams.sort_by{ |t| t.points_all }.reverse[0,num].map{|t| [t.name, t.points_all]} when "home" return @@teams.sort_by{ |t| t.points_home }.reverse[0,num].map{|t| [t.name, t.points_home]} when "away" return @@teams.sort_by{ |t| t.points_away }.reverse[0,num].map{|t| [t.name, t.points_away]} end end end class Match attr_accessor :date, :round, :home, :visitors, :attack_home, :attack_visitors, :delayed @@matches = [] def initialize(date, round, home, visitors, attack_home, attack_visitors, delayed) @date = date @round = round @home = home @visitors = visitors @attack_home = attack_home @attack_visitors = attack_visitors @delayed = delayed @@matches << self end def Match.load(filename) File.open(filename, "r") do |f| f.each_line do |line| splited = line.strip.split(",") result = splited[4].split(":") Match.new(splited[0], splited[1].to_i, splited[2], splited[3], result[0].to_i, result[1].to_i, (splited[4][splited[4].length - 1].chr == ")")) end end end def Match.analyze(selection) @@matches.each do |match| if (selection.include?(match.round)) team_home = Team.contains?(match.home) team_home = Team.new(match.home) if (team_home == nil) team_visitors = Team.contains?(match.visitors) team_visitors = Team.new(match.visitors) if (team_visitors == nil) team_home.attack_home += match.attack_home team_home.defense_home += match.attack_visitors team_visitors.attack_away += match.attack_visitors team_visitors.defense_away += match.attack_home if match.delayed team_home.points_home += 2 if (match.attack_home > match.attack_visitors) team_visitors.points_away += 2 if (match.attack_visitors > match.attack_home) team_home.points_home +=1 if (match.attack_home < match.attack_visitors) team_visitors.points_away +=1 if (match.attack_visitors < match.attack_home) else team_home.points_home += 3 if (match.attack_home > match.attack_visitors) team_visitors.points_away += 3 if (match.attack_visitors > match.attack_home) end team_home.points_all = team_home.points_home + team_home.points_away team_visitors.points_all = team_visitors.points_home + team_visitors.points_away end end end end class Statistic attr_reader :name, :type, :num @@rounds = [] @@statistics = [] def Statistic.get_rounds return @@rounds end def initialize(name, num, type) @name = name @num = num @type = type @@statistics << self end def Statistic.load(filename) File.open(filename,"r") do |f| doc = REXML::Document.new(f) doc.root.elements.each do |element| if element.name == "rounds" #element.name == "rounds" element.elements.each do |round| if round.name == "round" #element.name == "round" @@rounds << round.text.to_i else #element.name == "range" from = round.elements[1].text.to_i to = round.elements[2].text.to_i count = ((to - from) + 1) count.times { |i| @@rounds << from + i } end end else #element.name == "statistics" element.elements.each { |s| Statistic.new(s.name, s.attributes["num"].to_i, s.attributes["type"]) } end end end end def Statistic.process @@statistics.each do |statistic| case statistic.name when "most-points" Team.get_most_points(statistic.num, statistic.type).each_with_index { |t,i| puts "#{i + 1}. #{t[0]} #{t[1]}"} end puts end end def Statistic.printa p @@statistics end end Statistic.load(COMMANDS_FILE) Match.load(INPUT_FILE) Match.analyze(Statistic.get_rounds) Statistic.process()
K behu nasledujúceho programu sú potrebné vstupné súbory, ktoré pochádza z materiálov Unicorn College.
Stiahnuť
Stiahnutím nasledujúceho súboru súhlasíš s licenčnými podmienkami
Stiahnuté 301x (3.03 kB)
Aplikácia je vrátane zdrojových kódov v jazyku Ruby