fork download
  1. using System;
  2. using System.IO;
  3.  
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. MemoryStream ms = new MemoryStream();
  9. ms.WriteByte(100);
  10. ms.WriteByte(100);
  11. ms.WriteByte(0);
  12. ms.WriteByte(200);
  13. ms.WriteByte(200);
  14. ms.WriteByte(1);
  15. ms.WriteByte(50);
  16.  
  17. ms.Seek(0, SeekOrigin.Begin);
  18. Console.WriteLine(Factory.FromStream(ms));
  19. Console.WriteLine(Factory.FromStream(ms));
  20. }
  21. }
  22.  
  23. public struct Point
  24. {
  25. public int X;
  26.  
  27. public int Y;
  28.  
  29. public override string ToString()
  30. {
  31. return string.Format("<Point(X={0}, Y={1})>", X, Y);
  32. }
  33. }
  34.  
  35. public class Shape
  36. {
  37. protected Point origin;
  38.  
  39. public Shape(Stream stream)
  40. {
  41. origin.X = stream.ReadByte();
  42. origin.Y = stream.ReadByte();
  43. }
  44.  
  45. public Shape(Shape shape)
  46. {
  47. origin.X = shape.origin.X;
  48. origin.Y = shape.origin.Y;
  49. }
  50.  
  51. public override string ToString()
  52. {
  53. return string.Format("<Shape(origin={0})>", origin);
  54. }
  55. }
  56.  
  57. public class Circle : Shape
  58. {
  59. private double r;
  60.  
  61. public Circle(Shape shape, int r) : base(shape)
  62. {
  63. this.r = r;
  64. }
  65.  
  66. public override string ToString()
  67. {
  68. return string.Format("<Circle(origin={0}, r={1})>", origin, r);
  69. }
  70. }
  71.  
  72. public class Factory
  73. {
  74. public static Shape FromStream(Stream stream)
  75. {
  76. Shape shape = new Shape(stream);
  77. switch (stream.ReadByte())
  78. {
  79. case 0:
  80. return shape;
  81. case 1:
  82. return new Circle(shape, stream.ReadByte());
  83. default:
  84. throw new Exception();
  85. }
  86. }
  87. }
  88.  
Success #stdin #stdout 0s 29664KB
stdin
Standard input is empty
stdout
<Shape(origin=<Point(X=100, Y=100)>)>
<Circle(origin=<Point(X=200, Y=200)>, r=50)>