Example #1
0
        static void Main(string[] args)
        {
            // コンストラクタ呼び出し
            var abbrs = new Abbreviations();

            //7-2-3
            Console.WriteLine(abbrs.Count);
            if (abbrs.Remove("NPT"))
            {
                Console.WriteLine("削除成功");
            }
            else
            {
                Console.WriteLine("削除失敗");
            }
            //7-2-4

            // Addメソッドの呼び出し例
            abbrs.Add("IOC", "国際オリンピック委員会");
            abbrs.Add("NPT", "核兵器不拡散条約");

            // インデクサの利用例
            var names = new[] { "WHO", "FIFA", "NPT", };

            foreach (var name in names)
            {
                var fullname = abbrs[name];
                if (fullname == null)
                {
                    Console.WriteLine("{0}は見つかりません", name);
                }
                else
                {
                    Console.WriteLine("{0}={1}", name, fullname);
                }
            }
            Console.WriteLine();

            // ToAbbreviationメソッドの利用例
            var japanese     = "東南アジア諸国連合";
            var abbreviation = abbrs.ToAbbreviation(japanese);

            if (abbreviation == null)
            {
                Console.WriteLine("{0} は見つかりません", japanese);
            }
            else
            {
                Console.WriteLine("「{0}」の略語は {1} です", japanese, abbreviation);
            }
            Console.WriteLine();

            // FindAllメソッドの利用例
            foreach (var item in abbrs.FindAll("国際"))
            {
                Console.WriteLine("{0}={1}", item.Key, item.Value);
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            string _text = "Cozy lummox gives smart squid who asks for job pen";

            //7-1-1
            Console.WriteLine("\n-----7.1.1(英字のカウント)-----");
            var dict = new Dictionary <char, int>();

            foreach (var t in _text.ToUpper())
            {
                if ('A' <= t && t <= 'Z')
                {
                    if (dict.ContainsKey(t))
                    {
                        dict[t]++;
                    }
                    else
                    {
                        dict[t] = 1;
                    }
                }
            }
            foreach (var d in dict.OrderBy(x => x.Key))
            {
                Console.WriteLine($"'{d.Key}' : {d.Value}");
            }

            //7-1-2
            Console.WriteLine("\n-----7.1.2(英字のカウント2)-----");
            var sortdict = new SortedDictionary <char, int>();

            foreach (var t in _text.ToUpper())
            {
                if ('A' <= t && t <= 'Z')
                {
                    if (sortdict.ContainsKey(t))
                    {
                        sortdict[t]++;
                    }
                    else
                    {
                        sortdict[t] = 1;
                    }
                }
            }
            foreach (var d in sortdict)
            {
                Console.WriteLine($"'{d.Key}':{d.Value}");
            }

            //7-2-3
            Console.WriteLine("\n-----7.2.3(CountとRemoveの確認)-----");
            var abb = new Abbreviations();

            Console.WriteLine($"行数:{abb.Count}");
            Console.Write("削除する省略語:");
            var abbre = Console.ReadLine();

            Console.WriteLine(abb.Remove(abbre) ? "削除しました" : "削除できませんでした");
            Console.WriteLine($"行数:{abb.Count}");

            //7-2-4
            Console.WriteLine("\n-----7.2.4(3文字の省略語一覧)-----");
            foreach (var a in abb.ThreeAbb())
            {
                Console.WriteLine($"{a.Key}={a.Value}");
            }
            //インターフェースを実装
            //foreach (var a in abb.Where(x => x.Key.Length == 3)) {
            //	Console.WriteLine($"{a.Key}={a.Value}");
            //}

            Console.WriteLine();
        }