提出課題:04回目レポート
本文:
1.(1)の全部と(2)の3番まで
(1)1.
#!/usr/koeki/bin/ruby
# -*- coding: utf-8 -*-

require 'kconv'

soft = Hash.new

print"何について並べ替えたいですか?\n(1.勝ち数 2.負け数 3.引き分け数 4.勝率)\n"
choice = gets.chomp!.to_i

print"昇順ですか?降順ですか?\n(1.昇順 2.降順)\n"
shoukou = gets.chomp!.to_i

open("softball.txt","r:utf-8") do |deta|
  while line = deta.gets
    if /(\S+)\s+(\d+)\s+(\d+)\s+(\d+)/ =~ line
      
      soft[$1] = [$2.to_f,$3.to_f,$4] 
    end
  end
end

print"--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---\n"

if choice == 1 && shoukou == 1
  for team in soft.keys.sort {|a, b| soft[b][0] <=> soft[a][0]}
    
    result =  sprintf("%-23s%d%6d%6d%9.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 choice == 1 && shoukou == 2
  for team in soft.keys.sort {|a, b| soft[a][0] <=> soft[b][0]}
    
    result =  sprintf("%-23s%d%6d%6d%9.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 choice == 2 && shoukou == 1
  for team in soft.keys.sort {|a, b| soft[b][1] <=> soft[a][1]}
    
    result =  sprintf("%-23s%d%6d%6d%9.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 choice == 2 && shoukou == 2
  for team in soft.keys.sort {|a, b| soft[a][1] <=> soft[b][1]} 
    
    result =  sprintf("%-23s%d%6d%6d%9.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 choice == 3 && shoukou == 1
  for team in soft.keys.sort {|a, b| soft[b][2] <=> soft[a][2]}
    
    result =  sprintf("%-23s%d%6d%6d%9.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 choice == 3 && shoukou == 2
for team in soft.keys.sort {|a, b| soft[a][2] <=> soft[b][2]} 
  
  result =  sprintf("%-23s%d%6d%6d%9.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 choice == 4 && shoukou == 1
  for team in soft.keys.sort {|a, b| soft[b][0]/(soft[b][0]+soft[b][1]) <=> soft[a][0]/(soft[a][0]+soft[a][1])}
    
    result =  sprintf("%-23s%d%6d%6d%9.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 choice == 4 && shoukou == 2
  for team in soft.keys.sort {|a, b| soft[a][0]/(soft[a][0]+soft[a][1]) <=> soft[b][0]/(soft[b][0]+soft[b][1])}
    
    result =  sprintf("%-23s%d%6d%6d%9.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
else
  print"そんなものないです。\n"
end
3.(勝ち数の昇順)
何について並べ替えたいですか?
(1.勝ち数 2.負け数 3.引き分け数 4.勝率)
1
昇順ですか?降順ですか?
(1.昇順 2.降順)
1
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
アンツ                 78    53     5         0.595
ドラゴンフライズ       77    51     8         0.602
ブックウォームス       75    52     9         0.591
モスキートーズ         65    65     6         0.500
スネイルズ             52    82     2         0.388
クリケッツ             50    83     3         0.376

(勝ち数の降順)                                  [~/program]
何について並べ替えたいですか?
(1.勝ち数 2.負け数 3.引き分け数 4.勝率)
1
昇順ですか?降順ですか?
(1.昇順 2.降順)
2
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
クリケッツ             50    83     3         0.376
スネイルズ             52    82     2         0.388
モスキートーズ         65    65     6         0.500
ブックウォームス       75    52     9         0.591
ドラゴンフライズ       77    51     8         0.602
アンツ                 78    53     5         0.595

(負け数の昇順)                                          [~/program]
何について並べ替えたいですか?
(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

(負け数の降順)                                          [~/program]
何について並べ替えたいですか?
(1.勝ち数 2.負け数 3.引き分け数 4.勝率)
2
昇順ですか?降順ですか?
(1.昇順 2.降順)
2
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
ドラゴンフライズ       77    51     8         0.602
ブックウォームス       75    52     9         0.591
アンツ                 78    53     5         0.595
モスキートーズ         65    65     6         0.500
スネイルズ             52    82     2         0.388
クリケッツ             50    83     3         0.376

(引き分け数の昇順)                                          [~/program]
何について並べ替えたいですか?
(1.勝ち数 2.負け数 3.引き分け数 4.勝率)
3
昇順ですか?降順ですか?
(1.昇順 2.降順)
1
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
ブックウォームス       75    52     9         0.591
ドラゴンフライズ       77    51     8         0.602
モスキートーズ         65    65     6         0.500
アンツ                 78    53     5         0.595
クリケッツ             50    83     3         0.376
スネイルズ             52    82     2         0.388

(引き分け数の降順)                                         [~/program]
何について並べ替えたいですか?
(1.勝ち数 2.負け数 3.引き分け数 4.勝率)
3
昇順ですか?降順ですか?
(1.昇順 2.降順)
2
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
スネイルズ             52    82     2         0.388
クリケッツ             50    83     3         0.376
アンツ                 78    53     5         0.595
モスキートーズ         65    65     6         0.500
ドラゴンフライズ       77    51     8         0.602
ブックウォームス       75    52     9         0.591

(勝率の昇順)                                     [~/program]
何について並べ替えたいですか?
(1.勝ち数 2.負け数 3.引き分け数 4.勝率)
4
昇順ですか?降順ですか?
(1.昇順 2.降順)
1
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
ドラゴンフライズ       77    51     8         0.602
アンツ                 78    53     5         0.595
ブックウォームス       75    52     9         0.591
モスキートーズ         65    65     6         0.500
スネイルズ             52    82     2         0.388
クリケッツ             50    83     3         0.376

(勝率の降順)                                          [~/program]
何について並べ替えたいですか?
(1.勝ち数 2.負け数 3.引き分け数 4.勝率)
4
昇順ですか?降順ですか?
(1.昇順 2.降順)
2
--チーム名------------+-勝ち-+-負け-+-引分--+-勝率---
クリケッツ             50    83     3         0.376
スネイルズ             52    82     2         0.388
モスキートーズ         65    65     6         0.500
ブックウォームス       75    52     9         0.591
アンツ                 78    53     5         0.595
ドラゴンフライズ       77    51     8         0.602

4.
4行目、文字コード変換を行うkconvを呼び出す。これで、toeucやtoutf8メソッドが利用できる。
6行目、softはHashであると指定。
14〜21行目、ファイルの出力。
18行目、正規表現で抽出したものをHashに格納。
25〜42行目、条件が勝ち数を選んで昇順だった場合。
26行目、valueが配列だった場合のハッシュの並べ替え。valueは3つの値を持ち、第0要素の勝ち数を基準とするため、soft[a][0]やsoft[b][0]として勝ち数を指定。|x,y|とtest[x] <=> test[y]の部分でx、yの順番が同じである場合keyが昇順に並べ替えられ、逆の場合keyが降順に並べ替えられる、はずなのだがならなかった。
以降他の条件での繰り返し。

(2)1.
#!/usr/koeki/bin/ruby
# -*- coding: utf-8 -*-

require 'kconv'

food = Hash.new

print"なんの商品をしらべますか?\n(A,B,C)\n"
abc = gets.chomp!

open("foods.txt","r:utf-8") do |deta|
while line = deta.gets
  if /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\S+)/ =~ line
    food[$1.to_f] = [$2.to_f,$3.to_f,$4.to_f,$5]
  end
end
end
print"--店舗コード-----+-販売個数-+-立地-\n"

i=0
a = 0
b = 0
o = 0
g = 0
k = 0

if abc == "A"
 
for shop in food.keys.sort {|a, b| food[b][0] <=> food[a][0]} 

  result =  sprintf("  %-16d%-11d%s\n",shop,food[shop][0],food[shop][3]).toutf8
  puts result
  i += 1
  if food[shop][3] == "住宅街A"
    a += 1
  elsif food[shop][3] == "住宅街B"
    b += 1
  elsif food[shop][3] == "オフィス街"
    o += 1
  elsif food[shop][3] == "学校"
    g += 1
  else
    k += 1
  end
  
  if i == 10
    break
  end
  
end

  elsif abc == "B"
for shop in food.keys.sort {|a, b| food[b][1] <=> food[a][1]}

  result =  sprintf("  %-16d%-11d%s\n",shop,food[shop][1],food[shop][3]).toutf8
  puts result
  i += 1
  if food[shop][3] == "住宅街A"
    a += 1
  elsif food[shop][3] == "住宅街B"
    b += 1
  elsif food[shop][3] == "オフィス街"
    o += 1
  elsif food[shop][3] == "学校"
    g += 1
  else
    k += 1
  end
  if i == 10
    break
  end
end

elsif abc == "C"
for shop in food.keys.sort {|a, b| food[b][2] <=> food[a][2]}

  result =  sprintf("  %-16d%-11d%s\n",shop,food[shop][2],food[shop][3]).toutf8
  puts result
  i += 1
  if food[shop][3] == "住宅街A"
    a += 1
  elsif food[shop][3] == "住宅街B"
    b += 1
  elsif food[shop][3] == "オフィス街"
    o += 1
  elsif food[shop][3] == "学校"
    g += 1
  else
    k += 1
  end
  if i == 10
    break
  end
end

else
  print"そんなものありません。\n"
end

  if a >= 5
    print"特定の客層にターゲットを絞った広告を出しましょう。\n客層は高齢者です。\n"
  elsif b  >= 5
    print"特定の客層にターゲットを絞った広告を出しましょう。\n客層は若者です。\n"
  elsif o >= 5
    print"特定の客層にターゲットを絞った広告を出しましょう。\n客層は会社員です。\n"
  elsif g >= 5
    print"特定の客層にターゲットを絞った広告を出しましょう。\n客層は在校生です。\n"
   
  else
    print"客層を絞らない広告を出しましょう。\n"
  end

3. 
(商品Aの場合)                                        [~/program]
なんの商品をしらべますか?
(A,B,C)
A
--店舗コード-----+-販売個数-+-立地-
  25              1405       住宅街A
  2               1393       オフィス街
  14              1329       住宅街A
  8               1326       住宅街A
  4               1260       オフィス街
  27              1218       住宅街A
  20              1190       オフィス街
  21              1145       住宅街B
  7               1143       幹線道路
  10              1137       住宅街B
客層を絞らない広告を出しましょう。

(商品Bの場合)                                        [~/program]
なんの商品をしらべますか?
(A,B,C)
B
--店舗コード-----+-販売個数-+-立地-
  24              1495       オフィス街
  21              1375       住宅街B
  4               1362       オフィス街
  2               1340       オフィス街
  17              1321       住宅街B
  20              1307       オフィス街
  23              1268       幹線道路
  29              1168       住宅街B
  11              1134       幹線道路
  6               1024       オフィス街
特定の客層にターゲットを絞った広告を出しましょう。
客層は会社員です。
(商品Cの場合)                                        [~/program]
なんの商品をしらべますか?
(A,B,C)
C
--店舗コード-----+-販売個数-+-立地-
  14              1483       住宅街A
  25              1473       住宅街A
  19              1439       住宅街B
  21              1416       住宅街B
  28              1357       幹線道路
  17              1214       住宅街B
  16              1210       住宅街B
  2               1206       オフィス街
  7               1186       幹線道路
  10              1084       住宅街B
特定の客層にターゲットを絞った広告を出しましょう。
客層は若者です。
  
4.�‐楮戮寮睫世呂曚箸鵑鼻複院砲汎韻検�
��46〜48行目のように繰り返しの中にiに1足していき、それが10になった時に繰り返しから抜ける処理をしている。
��food[shop][3](立地の部分)の中身が住宅地Aだった場合にaに1足すというようにしてそれぞれの立地の回数をカウントし、その数が5以上だった場合に客層を表示。

5.ほとんど無理やりな感じで美しくないプログラムになってしまったと思う。もっときれいに書ける方法を知りたい。

添付:
ruby2-3.rb
ruby2-3-2.rb