2012-02-14 79 views
0

的我想提請在控制檯應用程序的時間表:繪製在控制檯的時間表進行3D陣

--------------------------------------------------- 
| Mo  | Tu  | We  | ...  | 
--------------------------------------------------- 
1| Math  |   |   |   | 
| Teacher |   |   |   | 
| Room  |   |   |   | 
--------------------------------------------------- 
2| ... 

我想這是一個三維數組。維度1是x,維度2是y,維度3是一個稱爲「課程」的對象。 的教訓,對象的屬性是話題,老師,房間和其他人(開始,結束,...)

------------------------------------------------------------------------- 
| Mo   | Tu  | We  | ...  | 
------------------------------------------------------------------------- 
| Math   |   |   |   | 
| Teacher   |   |   |   | 
| Room   |   |   |   | 
| Begin: 2012.1.1 |   |   |   | 
------------------------------------------------------------------------- 

的外觀應自動貼合行,寬度等。

一個很酷的解決方案可以found here

但解決的只是2D。我做了一些調查,但不能填寫完整的對象。

+2

我只能看到2D'o_O'此表沒有深度或任何類似的任何... – Shai 2012-02-14 08:31:34

+0

您能不能告訴我們這個「3D陣列」的樣本? – 2012-02-14 08:38:49

+0

我認爲3D是指每一行都包含不同的信息,如主題,教師等。 – ramsesoriginal 2012-02-14 08:40:09

回答

1

這是不是最漂亮或最優雅的解決方案,也不是最有效的一個,但它的作品。

假設一個課看起來有點像這樣:

 class Lesson 
     { 
      public string topic = "math"; 
      public string teacher = "mandelbrot"; 
      public string room = "E215"; 
      public string begin = "08:00"; 
      public string end = "09:00"; 

      public Lesson() 
       : this(null, null, null, null, null) 
      { 
      } 

      public Lesson(string t) 
       : this(t, null, null, null, null) 
      { 
      } 


      public Lesson(string t, string t2) 
       : this(t, t2, null, null, null) 
      { 
      } 


      public Lesson(string t, string t2, string r) 
       : this(t, t2, r, null, null) 
      { 
      } 


      public Lesson(string t, string t2, string r, string b) 
       : this(t, t2, r, b, null) 
      { 
      } 
      public Lesson(string t, string t2, string r, string b, string e) 
      { 
       topic = t??"math"; 
       teacher = t2??"mandelbrot"; 
       room = r??"E215"; 
       begin = b??"08:00"; 
       end = e??"09:00"; 

      } 

     } 

我創建的轉換一個Lesson[][]陣列以一個相當格式化的字符串一類:

 class OutPutter 
     { 
      private static int[] maxlength = new int[7]; 
      public static Lesson[][] timetable = new Lesson[7][]; 

      public OutPutter() 
       : this(null) 
      { 
      } 

      public OutPutter(Lesson[][] tt) 
      { 
       for (int i = 0; i < 7; i++) 
       { 
        maxlength[i] = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ElementAt(i).Length; 
        timetable[i] = ((tt == null || tt[i] == null) ? new Lesson[24] : tt[i]); 
       } 
      } 


      public void add(int day, int hour, Lesson lesson) 
      { 
       day -= 1; 
       timetable[day][hour]=lesson; 
       int currentLongest = maxlength[day]; 
       if (lesson.begin.Length > currentLongest) 
        currentLongest = lesson.begin.Length; 
       if (lesson.end.Length > currentLongest) 
        currentLongest = lesson.end.Length; 
       if (lesson.teacher.Length > currentLongest) 
        currentLongest = lesson.teacher.Length; 
       if (lesson.topic.Length > currentLongest) 
        currentLongest = lesson.topic.Length; 
       if (lesson.room.Length > currentLongest) 
        currentLongest = lesson.room.Length; 
       if (currentLongest != maxlength[day]) 
        maxlength[day] = currentLongest; 
      } 

      private static Lesson getLesson(int i2, int i) 
      { 
       try 
       { 
        return timetable[i][i2] ?? new Lesson("", "", "", "", ""); 
       } 
       catch 
       { 
        return new Lesson("", "", "", "", ""); 
       } 
      } 

      public override string ToString() 
      { 
       StringBuilder sb = new StringBuilder(); 
       int maxLessons = 0; 
       foreach (Lesson[] current in timetable) 
       { 
        if (current.Length > maxLessons) 
         maxLessons = current.Length; 
       } 
       int lineLength = 1; 
       List<string> formatstrings = new List<string>(); 
       for (int i = 0; i < 7; i++) 
       { 
        formatstrings.Add(" {0,"+maxlength[i].ToString()+"}"); 
        sb.Append("|"); 
        sb.AppendFormat(formatstrings.ElementAt(i), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames.ElementAt(i)); 
        lineLength += maxlength[i] + 2; 
       } 
       sb.Append("|"); 
       sb.AppendLine(); 
       sb.Append("".PadLeft(lineLength, '=')); 
       for (int i2 = 0; i2 < maxLessons; i2++) 
       { 
        sb.AppendLine(); 
        for (int i = 0; i < 7; i++) 
        { 
         sb.Append("|"); 
         sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).topic); 
        } 
        sb.Append("|"); 
        sb.AppendLine(); 
        for (int i = 0; i < 7; i++) 
        { 
         sb.Append("|"); 
         sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).teacher); 
        } 
        sb.Append("|"); 
        sb.AppendLine(); 
        for (int i = 0; i < 7; i++) 
        { 
         sb.Append("|"); 
         sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).room); 
        } 
        sb.Append("|"); 
        sb.AppendLine(); 
        for (int i = 0; i < 7; i++) 
        { 
         sb.Append("|"); 
         sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).begin); 
        } 
        sb.Append("|"); 
        sb.AppendLine(); 
        for (int i = 0; i < 7; i++) 
        { 
         sb.Append("|"); 
         sb.AppendFormat(formatstrings.ElementAt(i), getLesson(i2, i).end); 
        } 
        sb.Append("|"); 
        sb.AppendLine(); 
        sb.Append("".PadLeft(lineLength, '-')); 
       } 
       return sb.ToString(); 
      } 

     } 

樣品用量:

 class Program 
     { 
      static void Main(string[] args) 
      { 
       OutPutter op = new OutPutter(); 
       op.add(1,1, new Lesson()); 
       op.add(1,3, new Lesson("Analysis")); 
       op.add(2,5, new Lesson()); 
       op.add(3,0, new Lesson()); 
       op.add(5,7, new Lesson("CompSci", "A. S. Tann.")); 
       Console.Write(op.ToString()); 
       Console.Read(); 
      } 
     } 

就像我說的,這是只是歸仁CK和骯髒的解決方案,只給你這到底是怎麼實現的一些想法。它絕對不乾淨或最佳,所以你應該重構它。 很多。但作爲一個例子,它應該工作。

編輯

而這裏的輸出,這將產生:

|  Sunday|  Monday| Tuesday| Wednesday| Thursday| Friday| Saturday| 
=============================================================================== 
|   |   |  math|   |   |  |   | 
|   |   | mandelbrot|   |   |  |   | 
|   |   |  E215|   |   |  |   | 
|   |   |  08:00|   |   |  |   | 
|   |   |  09:00|   |   |  |   | 
------------------------------------------------------------------------------- 
|  math|   |   |   |   |  |   | 
| mandelbrot|   |   |   |   |  |   | 
|  E215|   |   |   |   |  |   | 
|  08:00|   |   |   |   |  |   | 
|  09:00|   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
| Analysis|   |   |   |   |  |   | 
| mandelbrot|   |   |   |   |  |   | 
|  E215|   |   |   |   |  |   | 
|  08:00|   |   |   |   |  |   | 
|  09:00|   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |  math|   |   |   |  |   | 
|   | mandelbrot|   |   |   |  |   | 
|   |  E215|   |   |   |  |   | 
|   |  08:00|   |   |   |  |   | 
|   |  09:00|   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |  CompSci|  |   | 
|   |   |   |   | A. S. Tann.|  |   | 
|   |   |   |   |  E215|  |   | 
|   |   |   |   |  08:00|  |   | 
|   |   |   |   |  09:00|  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
------------------------------------------------------------------------------- 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
|   |   |   |   |   |  |   | 
-------------------------------------------------------------------------------