1.(1)を2番まで実行した
2.添付した
3.実行結果
表示の方法を数字で選らんでくたさい
勝ち:1 負け:2 引分:3 勝率:4
2
昇順:1 降順:2
1
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
クリケッツ 50 83 3 0.376
スネイルズ 52 82 2 0.388
モスキートーズ 65 65 6 0.500
アンツ 78 53 5 0.595
ブックウォームス 75 52 9 0.591
ドラゴンフライズ 77 51 8 0.602
4.プログラムの説明
まず初めに1番は、読み込んだデータから以下の式で勝率を求め、配列にnを代入した
n =$2.to_f/ ($2.to_f + $3.to_f)
2番はまず初めにopenメソッドで変数名をつけgetsでファイル読み込みだけをさ
せた
次に表示方法を促し、勝ち数、負け数、引き分け数、勝率の昇順を数字で分けx
に数字を入れさせ降順、昇順も同じように入力させた
if文&&を使いでそれぞれの表示方法で表示させるようした
softball2.rb
#!/usr/koeki/bin/ruby
# -*- coding: utf-8 -*-
require 'kconv'
soft = Hash.new
open("softball.txt","r:utf-8") do |read|
while line =read.gets
if /(\S+)\s+(\d+)\s+(\d+)\s+(\d+)/ =~ line
# 1個目の( ) (\S+)→チーム名が入る
# 2個目の( ) (\d+)→勝ち数が入る
# 3個目の( ) (\d+)→負け数が入る
# 4個目の( ) (\d+)→引分数が入る
n =$2.to_f/ ($2.to_f + $3.to_f)
soft[$1] = [$2.to_f,$3.to_f,$4,n] # 配列を代入
end
end
end
print"表示の方法を数字で選らんでくたさい\n"
print"勝ち:1 負け:2 引分:3 勝率:4\n"
x = gets.chomp!.to_i
print"昇順:1 降順:2\n"
y = gets.chomp!.to_i
if x == 1 && y == 1
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[b][0] <=> soft[a][0]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
elsif x == 1 && y == 2
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[a][0] <=> soft[b][0]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
elsif x ==2 && y ==1
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[b][1] <=> soft[a][1]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
elsif x ==2 && y ==2
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[a][1] <=> soft[b][1]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
elsif x ==3 && y ==1
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[b][2] <=> soft[a][2]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
elsif x ==3 && y ==2
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[a][2] <=> soft[b][2]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
elsif x ==4 && y ==1
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[b][3] <=> soft[a][3]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
elsif x ==4 && y ==2
print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"
for team in soft.keys.sort{|a, b| soft[a][3] <=> soft[b][3]}
result = sprintf("%-23s%4d%7d%6d%10.3f\n",
team.toeuc.force_encoding("binary"),
soft[team][0], soft[team][1], soft[team][2],
soft[team][0]/(soft[team][0]+soft[team][1])).toutf8
puts result
end
end