SSブログ

C#の勉強の第5回は、Consoleへの出力をアレンジ(2) [初めてのC#]

今回は、前回のアレンジにもう少し手を加えて、1行に表示できる項目数をConsleに合わせて可変にしてみよー[わーい(嬉しい顔)]
だんだんと、ソースが長くなっていきますが、

ex06
using System;
//クラス配列の作成
namespace ex06
{
    class MyTable
    {
        public string name = "";
        public int select = 0;
    }

    class ActionTable
    {
            //クラス配列の作成
            public MyTable[] tb = new MyTable[0];
            //クラス配列へ値をセット
            public int SetTableItem()
            {
                //配列のサイズ変更&インスタンス作成&データ格納
                Array.Resize(ref tb, 1);
                tb[0] = new MyTable();
                tb[0].name = "table001";
                tb[0].select = 0;

                Array.Resize(ref tb, 2);
                tb[1] = new MyTable();
                tb[1].name = "table0000002";
                tb[1].select = 1;

                Array.Resize(ref tb, 3);            
                tb[2] = new MyTable();
                tb[2].name = "table3";
                tb[2].select = 2;

                Array.Resize(ref tb, 4);
                tb[3] = new MyTable();
                tb[3].name = "table004";
                tb[3].select = 0;

                Array.Resize(ref tb, 5);
                tb[4] = new MyTable();
                tb[4].name = "table000000005";
                tb[4].select = 1;

                Array.Resize(ref tb, 6);            
                tb[5] = new MyTable();
                tb[5].name = "table6";
                tb[5].select = 2;

                Array.Resize(ref tb, 7);            
                tb[6] = new MyTable();
                tb[6].name = "table000000007";
                tb[6].select = 2;

                return 0;
            }

            //最長の文字列に合わせてコンソールに1行3項目で整列する。
            public int ShowTableItem()
            {
                int inv = 4;    //項目毎の間隔    
                string invstr = new string(' ', inv);
                //Nameに入っている文字列の最大文字数
                int maxLen = TableNameMaxLen;
                //Console1行の最大文字数             
                int maxConLen = Console.WindowWidth;       
                //Console1行の最大文字数に合わせて表示可能項目数を計算
                int LineElement = 1;                       
                if(maxLen != 0) { 
                    LineElement = (maxConLen + inv) / (maxLen + inv);  
                } 
                
                int cnt = 0;
                foreach(MyTable wtb in tb)
                {
                    Console.Write(String.Format("{0," 
                              + (maxLen * -1).ToString() + "}",wtb.name));
                    cnt ++;
                    if((cnt % LineElement)!=0) 
                    {
                        Console.Write(invstr);      //項目間にスペースを入れる
                    }else{
                        Console.WriteLine();
                    }
                    
                }
                Console.WriteLine();
                return 0;
            }

            //テーブルに保存されている文字列の最長文字数を取得する
            public int TableNameMaxLen            {
                get
                {
                    int maxLen = 0;
                    foreach(MyTable wtb in tb)
                    {
                        if(maxLen < wtb.name.Length )
                        {
                            maxLen = wtb.name.Length;
                        }
                    }                    
                    return maxLen;
                }
            }
    }

    class Program
    {
        public static void Main(string[] args)
        {
            ActionTable Act = new ActionTable ();
            //クラス配列を作成して、値をセットする
            int ret1 = Act.SetTableItem();
            //コンソールに格納した情報を出力
            int ret2 = Act.ShowTableItem();
        }
    }
}


これを実行してみると
20180728_1.jpg
少しコンソールの幅を狭くして、実行すると
20180728_2.jpg

うん、うん。まーそんな感じ[ぴかぴか(新しい)]
nice!(0)  コメント(0) 
共通テーマ:パソコン・インターネット

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。