using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ConsoleApplication1 { abstract class Body { public Body() { } public abstract double Area(); public abstract double Volume(); public abstract void WriteLine(); public abstract string SetOutLines(); } class Parallelepiped : Body { private double wight;//ширина public double Wight { get { return wight; } set { if (value < 0) { throw new FormatException("Неверно задана ширина параллелепипеда"); } wight = value; } } private double height;//высота public double Height { get { return height; } set { if (value < 0) { throw new FormatException("Неверно задана высота параллелепипеда"); } height = value; } } private double lenght;//длина public double Lenght { get { return lenght; } set { if (value < 0) { throw new FormatException("Неверно задана длина параллелепипеда"); } lenght = value; } } public Parallelepiped() { wight = 0; height = 0; lenght = 0; } public Parallelepiped(double a, double b, double c) { if (a < 0 || b < 0 || c < 0) { throw new FormatException("Неверно заданы параметры параллелепипеда"); } wight = a; height = b; lenght = c; } override public double Area() { return (wight * height + wight * lenght + lenght * height) * 2; } override public double Volume() { return wight * height * lenght; } const string BodyName = "Parallelepiped"; override public void WriteLine() { Console.WriteLine("\t" + BodyName + "\nШирина {0}\nВысота {1}\nДлина {2}\n\n", wight, height, lenght); } override public string SetOutLines() { return "\t" + BodyName + "\nШирина " + wight.ToString() + "\nВысота " + height.ToString() + "\nДлина " + lenght.ToString() + "\n\n"; } } class Cone : Body { private double height;//высота public double Height { get { return height; } set { if (value < 0) throw new FormatException("Неверно задана высота конуса"); height = value; } } private double r;//радиус основания public double R { get { return r; } set { if (value < 0) { throw new FormatException("Неверно задан радиус конуса"); } r = value; } } private double l;//образующая public Cone() { height = 0; r = 0; } public Cone(double a, double b) { if (a < 0 || b < 0) { throw new FormatException("Неверно заданы параметры конуса"); } height = a; r = b; } override public double Area() { Set_l(); return Math.PI * r * (r + l); } override public double Volume() { return Math.PI * r * r * height / 3; } private void Set_l() { l = Math.Pow(r * r + height * height, 0.5); } const string BodyName = "Cone"; override public void WriteLine() { Set_l(); Console.WriteLine("\t" + BodyName + "\nВысота {0}\nРадиус основания {1}\nОбразующая {2}\n\n", height, r, l); } override public string SetOutLines() { Set_l(); return "\t" + BodyName + "\nВысота " + height.ToString() + "\nРадиус основания " + r.ToString() + "\nОбразующая " + l.ToString() + "\n\n"; } } class Ball : Body { private double r; public double R { get { return r; } set { if (value < 0) { throw new FormatException("Неверно задан радиус шара"); } r = value; } }//радиус public Ball() { R = 0; } public Ball(double a) { if (a < 0) { throw new FormatException("Неверно задан радиус шара"); } r = a; } override public double Area() { return Math.PI * Math.Pow(r, 2) * 4; } override public double Volume() { return Math.PI * Math.Pow(r, 3) * 4 / 3; } const string BodyName = "Ball"; override public void WriteLine() { Console.WriteLine("\t" + BodyName + "\nРадиус " + r + "\n\n"); } override public string SetOutLines() { return "\t" + BodyName + "\nРадиус " + r.ToString() + "\n\n"; } } class Series { public List elements = new List(); public Series() { } public void Add(Body z) { elements.Add(z); } public void WriteLine() { foreach (Body g in elements) g.WriteLine(); } public void SortArea() { bool t = true; while (t) { t = false; for (int j = 0; j < elements.Count; ++j) { if (elements[j].Area() > elements[j + 1].Area()) { elements[elements.Count] = elements[j]; elements[j] = elements[j + 1]; elements[j + 1] = elements[elements.Count]; t = true; } } } } public void WriteToFile() { foreach (Body q in elements) File.AppendAllText(@"d:\Лаб3.doc", q.SetOutLines()); } public void ReadNewElements(string s) { string[] read = File.ReadAllLines(s); List L = new List(); for (int i = 0; i < read.Length; ++i) L.Add(read[i].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)); foreach (string[] d in L) { if (d[0] == "Parallelepiped") { try { Parallelepiped y = new Parallelepiped(double.Parse(d[1]), double.Parse(d[2]), double.Parse(d[3])); Add(y); } catch { throw new Exception("Ошибка при создании тела Параллелепипед "); } } else if (d[0] == "Cone") { try { Cone y = new Cone(double.Parse(d[1]), double.Parse(d[2])); Add(y); } catch { throw new Exception("Ошибка при создании тела Конус "); } } else if (d[0] == "Ball") { try { Ball y = new Ball(double.Parse(d[1])); Add(y); } catch { throw new Exception("Ошибка при создании тела Шар "); } } } } } class CompareArea : IComparer { public int Compare(Body a, Body b) { double A = a.Area(); double B = b.Area(); if (A > B) return 1; else if (A < B) return -1; else return 0; } } class CompareVolume : IComparer { public int Compare(Body a, Body b) { double A = a.Volume(); double B = b.Volume(); if (A > B) return 1; else if (A < B) return -1; else return 0; } } class Program { static void Main(string[] args) { try { Parallelepiped p = new Parallelepiped(); Cone c = new Cone(); Ball b = new Ball(); Series s = new Series(); s.Add(p); s.Add(c); s.Add(b); s.WriteLine(); s.WriteToFile(); Series s2 = new Series(); s2.ReadNewElements(@"d:\4"); Console.WriteLine("Считанное из файла:\n "); s2.WriteLine(); Console.WriteLine("Площадь первого " + Math.Round(s2.elements[0].Area(),3)); Console.WriteLine("Объем второго " + Math.Round(s2.elements[1].Volume(),3)); s2.elements.Sort(new CompareArea()); foreach(Body g in s2.elements) Console.Write(Math.Round(g.Area(),3)+ " "); Console.WriteLine("\n\n\n"); s2.elements.Sort(new CompareVolume()); foreach (Body g in s2.elements) Console.Write(Math.Round(g.Volume(),3) + " "); } catch (Exception e) { Console.WriteLine(e.Message); } } } }