在3D-Array的控制台中绘制时间表

时间:2012-02-14 08:27:48

标签: c#

我想在控制台应用程序中绘制时间表:

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

我猜这是一个3D阵列。尺寸1是x,尺寸2是y,尺寸3是称为“课程”的对象。 课程对象的属性是主题,教师,房间和其他(开始,结束,......)

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

外观应自动适合行,宽度等。

一个非常酷的解决方案可以是found here

但解决方案只是2D。我做了一些调查,但无法填写完整的对象。

1 个答案:

答案 0 :(得分: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();
          }
      }

就像我说的那样,只是一个快速而肮脏的解决方案,只是为了给你一些如何实现这一点的想法。它绝对不是干净或最佳,所以你应该重构它。 很多。但作为一个例子它应该工作。

修改

这是产生的输出:

 |     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|       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 |           |           |           |          |            |       |         |
 -------------------------------------------------------------------------------