2009/05/21(Thu) 09:15:56 編集(投稿者)
↓アドリブで書いてみた。
Module Module1
Sub AddRecord(ByVal dic As SortedDictionary(Of String, SortedDictionary(Of String, Integer)), ByVal name As String, ByVal ds As String, ByVal price As Integer)
If Not dic.ContainsKey(name) Then
dic.Add(name, New SortedDictionary(Of String, Integer)) ' "商品名"追加
End If
If Not dic(name).ContainsKey(ds) Then
dic(name).Add(ds, 0) ' "日付"追加
End If
dic(name)(ds) = dic(name)(ds) + price ' ここで積算
End Sub
Sub MakeSummary(ByVal dic As SortedDictionary(Of String, SortedDictionary(Of String, Integer)))
For Each name As String In dic.Keys
Console.Write("{0} ", name)
For Each datestr As String In dic(name).Keys
Console.Write(",{0},{1} ", datestr, dic(name)(datestr))
Next
Console.WriteLine()
Next
End Sub
Sub Main()
Dim dic As New SortedDictionary(Of String, SortedDictionary(Of String, Integer))
AddRecord(dic, "りんご", "0521", 10)
AddRecord(dic, "みかん", "0521", 20)
AddRecord(dic, "ぶどう", "0521", 30)
AddRecord(dic, "りんご", "0522", 40)
AddRecord(dic, "みかん", "0522", 50)
AddRecord(dic, "ぶどう", "0522", 60)
AddRecord(dic, "りんご", "0521", 10)
AddRecord(dic, "みかん", "0521", 20)
AddRecord(dic, "ぶどう", "0521", 30)
AddRecord(dic, "りんご", "0522", 40)
AddRecord(dic, "みかん", "0522", 50)
AddRecord(dic, "ぶどう", "0522", 60)
MakeSummary(dic)
End Sub
End Module
--- 実行結果 : "品名, 日付, 金額, 日付, 金額 ..." ---
ぶどう ,0521,60 ,0522,120
みかん ,0521,40 ,0522,100
りんご ,0521,20 ,0522,80
※ わざと「要求とは異なる仕様」になってます。