Professional Documents
Culture Documents
Лр5
Лр5
Хід роботи
Завдання:
Варіант 8. Створити абстрактний базовий клас Norm(норма) з віртуальною
функцією обчислення норми та модуля, виводу на екран значень. Створити класи-
нащадки Vector3D(вектор у просторі) та Complex(комплексне число: дійсна та уявна
частина) зі своїми методами обчислення норми та модуля. Якщо a + bi– комплексне
число, то його норма n = a2 + b2, а модуль m = . Якщо (х, у, z) – координати вектора , то
модуль або норма вектора обчислюється . Створити масив покажчиків на базовий клас із 6
елементів. Заповнити масив через один елементами-нащадками з рандомними даними та
викликати всі методи для кожного елементу.
Код програми :
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
namespace WindowsFormsApp2
{
{
List<ToWar> aztak = new List<ToWar>();
Hashtable sh = new Hashtable();
int zx = 0;
public Form1()
{
InitializeComponent();
}
if (comboBox1.SelectedIndex == 0)
{
List<yssson> seri = new List<yssson>();
Арк.
2
}
DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<yssson>));
using (FileStream fs = new FileStream("data.json", FileMode.OpenOrCreate))
{
jsonFormatter.WriteObject(fs, seri);
}
}
if (comboBox1.SelectedIndex == 1)
{
List<xmml> seri = new List<xmml>();
foreach (ToWar i in aztak)
{
xmml ax = new xmml(i.Nam, i.Zin, i.Pd, i.Gs, i.Sh, i.PSD);
seri.Add(ax);
}
XmlSerializer formatter = new XmlSerializer(typeof(List<xmml>));
using (FileStream fs = new FileStream("data.xml", FileMode.OpenOrCreate))
{
formatter.Serialize(fs, seri);
}
}
if (comboBox1.SelectedIndex == 2)
{
List<sobin> seri = new List<sobin>();
foreach (ToWar i in aztak)
{
sobin ax = new sobin(i.Nam, i.Zin, i.Pd, i.Gs, i.Sh, i.PSD);
seri.Add(ax);
}
SoapFormatter formatter = new SoapFormatter();
using (FileStream fs = new FileStream("data.soap", FileMode.OpenOrCreate))
{
foreach (sobin i in seri)
{
formatter.Serialize(fs, i);
}
}
}
if (comboBox1.SelectedIndex == 3)
{
List<sobin> seri = new List<sobin>();
foreach (ToWar i in aztak)
{
sobin ax = new sobin(i.Nam, i.Zin, i.Pd, i.Gs, i.Sh, i.PSD);
seri.Add(ax);
}
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fs = new FileStream("data.dat", FileMode.OpenOrCreate))
{
formatter.Serialize(fs, seri);
}
}
}
if (comboBox2.SelectedIndex == 1)
{
XmlSerializer formatter = new XmlSerializer(typeof(List<xmml>));
using (FileStream fs = new FileStream("data.xml", FileMode.OpenOrCreate))
Арк.
3
Змн. Арк. № докум. Підпис Дата
{
List<xmml> seri = (List<xmml>)formatter.Deserialize(fs);
foreach (xmml p in seri)
{
richTextBox1.Text = richTextBox1.Text + "Товар " + p.N_e + " ціна " + p.P_r + " Вжити до " +
p.P_a +" створенний " +p.P_A+" складається з"+p.F_f + " кількість шт" + p.Sh + (char)13;
}
}
}
if (comboBox2.SelectedIndex == 2)
{
SoapFormatter formatter = new SoapFormatter();
using (FileStream fs = new FileStream("data.soap", FileMode.OpenOrCreate))
{
List<sobin> seri = new List<sobin>();
while (true)
{
try
{
seri.Add((sobin)formatter.Deserialize(fs));
}
catch (XmlException) { break; }
}
foreach (sobin p in seri)
{
richTextBox1.Text = richTextBox1.Text + "Товар " + p.N_e + " ціна " + p.P_r + " Вжити до " +
p.P_a + " створенний " + p.P_A + " складається з" + p.F_f + " кількість шт" + p.Sh + (char)13;
}
}
}
if (comboBox2.SelectedIndex == 3)
{
BinaryFormatter formatter = new BinaryFormatter();
using (FileStream fs = new FileStream("data.dat", FileMode.OpenOrCreate))
{
List<sobin> seri = (List<sobin>)formatter.Deserialize(fs);
foreach (sobin p in seri)
{
richTextBox1.Text = richTextBox1.Text + "Товар " + p.N_e + " ціна " + p.P_r + " Вжити до " +
p.P_a + " створенний " + p.P_A + " складається з" + p.F_f + " кількість шт" + p.Sh + (char)13;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
namespace WindowsFormsApp2
{
[DataContract]
public class yssson
{
[DataMember]
public string N_e { get; set; }
[DataMember]
public double P_r { get; set; }
[DataMember]
public DateTime P_a { get; set; }
public string F_f { get; set; }
[DataMember]
public int Sh { get; set; }
[DataMember]
public DateTime P_A { get; set; }
Арк.
4
Змн. Арк. № докум. Підпис Дата
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp2
{
[Serializable]
public class xmml
{
public string N_e { get; set; }
public double P_r { get; set; }
public DateTime P_a { get; set; }
public string F_f { get; set; }
public int Sh { get; set; }
public DateTime P_A { get; set; }
public xmml() { }
public xmml(string a, double b, DateTime c, DateTime d, int e, string f)
{
N_e = a;
P_r = b;
P_a = c;
P_A = d;
Sh = e;
F_f = f;
}
}
}
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;
namespace WindowsFormsApp2
{
abstract class ToWar
{
RichTextBox t = Application.OpenForms["Form1"].Controls["richtextBox1"] as RichTextBox;
public string Nam { get; set; }
public double Zin { get; set; }
public DateTime Pd { get; set; }
public DateTime Gs { get; set; }
public int Sh { get; set; }
public string PSD { get; set; }
public ToWar(string a, double b, DateTime c)
{
Nam = a;
Zin = b;
Pd = c;
}
public virtual void Vivid()
{
t.Text = t.Text + "Товар " +Nam+" коштує "+ Zin + (char)13;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp2
{
[Serializable]
class sobin
{
public string N_e { get; set; }
public double P_r { get; set; }
public DateTime P_a { get; set; }
public string F_f { get; set; }
public int Sh { get; set; }
public DateTime P_A { get; set; }
public sobin(string a, double b, DateTime c, DateTime d, int e, string f)
Арк.
5
Змн. Арк. № докум. Підпис Дата
{
N_e = a;
P_r = b;
P_a = c;
P_A = d;
Sh = e;
F_f = f;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
class Produkt:ToWar
{
RichTextBox t = Application.OpenForms["Form1"].Controls["richtextBox1"] as RichTextBox;
public Produkt(string a, double b,DateTime c) : base(a,b,c) { }
public void Vvid(DateTime n)
{
Gs= n;
}
public override void Vivid()
{
base.Vivid();
t.Text = t.Text + "Дата створення " + Gs + " вжити до " + Pd + (char)13;
}
}
}
Арк.
6
Змн. Арк. № докум. Підпис Дата
Висновок: На цій лабораторній роботі я одержав практичні навички
створення абстрактних класів.