Exemple #1
0
        static void Main(string[] args)
        {
            IShape ishape = new Square("长方形");
            //Square square = new Square("方形");
            //Shape shape = (Shape)square;
            //shape.Length = 10.0m;
            //shape.Width = 20m;
            //Console.WriteLine(shape.Area());
            //Console.WriteLine("shape type is: " + shape.GetType());
            //if (shape is App.Shape)
            //    Console.WriteLine("shape type is Square");
            ishape.Length = 10m;
            ishape.Width = 20m;
            Console.WriteLine("ishape type is: " + ishape.GetType());
            Console.WriteLine(ishape.Area());
            Console.WriteLine(Environment.CurrentDirectory);

            Console.ReadKey();
        }
Exemple #2
0
 public static void Main(string[] args)
 {
     Square sq = new Square();
 }
Exemple #3
0
        static void Main(string[] args)
        {
            Square[] spisok;
            //(1; 10) (-2; 20) 5 Piramid
            //(2.222; 2) (3; 333) 10 Cube
            Regex rex = new Regex(@"^\((-*\d+(?:[\.,]\d+)?);\ (-*\d+(?:[\.,]\d+)?)\)\ \((-*\d+(?:[\.,]\d+)?);\ (-*\d+(?:[\.,]\d+)?)\)\ (-*\d+(?:[\.,]\d+)?)\ (Piramid|Cube|Square)$");

            string[] file = File.ReadAllLines("data.txt");
            spisok = new Square[file.Length];
            for (int i = 0; i < file.Length; i++)
            {
                if (!rex.IsMatch(file[i]))
                {
                    Console.WriteLine($"В файле обнаружена ошибка на строке {i}. Я вынуждена прекратить работать:(");
                    return;
                }

                var match  = rex.Match(file[i]);
                var groups = match.Groups;
                try
                {
                    switch (groups[5].Value)
                    {
                    case "Cube":
                    {
                        spisok[i] = new Cube(ParseDouble(groups[0].ToString()), ParseDouble(groups[1].ToString()), ParseDouble(groups[2].ToString()), ParseDouble(groups[3].ToString()), ParseDouble(groups[4].ToString()));
                        break;
                    }

                    case "Piramid":
                    {
                        spisok[i] = new Piramid(ParseDouble(groups[0].ToString()), ParseDouble(groups[1].ToString()), ParseDouble(groups[2].ToString()), ParseDouble(groups[3].ToString()), ParseDouble(groups[4].ToString()));
                        break;
                    }

                    case "Square":
                    {
                        spisok[i] = new Square(ParseDouble(groups[0].ToString()), ParseDouble(groups[1].ToString()), ParseDouble(groups[2].ToString()), ParseDouble(groups[3].ToString()));
                        break;
                    }
                    }
                }
                catch (FormatException e)
                {
                    Console.WriteLine($"Ошибка в строке номер {i}");
                    return;
                }
            }

            BinaryFormatter ser = new BinaryFormatter();

            using (FileStream fs = new FileStream("out.bin", FileMode.Create))
            {
                ser.Serialize(fs, spisok);
            }
            Console.WriteLine("Сериализован!");
            Square[] deSerSpisok;
            using (FileStream fs = new FileStream("out.bin", FileMode.Open))
            {
                deSerSpisok = (Square[])ser.Deserialize(fs);
            }

            for (int i = 0; i < deSerSpisok.Length; i++)
            {
                Console.WriteLine(deSerSpisok[i].ToString());
            }
        }