You are on page 1of 7

Лабораторна робота № 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
{

public partial class Form1 : Form

{
List<ToWar> aztak = new List<ToWar>();
Hashtable sh = new Hashtable();
int zx = 0;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
radioButton1.Checked = true;
}

Змн. Арк. № докум. Підпис Дата


Розроб. Дядик Літ. Арк. Аркушів
Перевір. Стукан 1 6
Абстрактні класи.
Н. Контр. ХПК
Затверд.
private void button4_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
Produkt a1 = new
Produkt(textBox1.Text,Convert.ToDouble(textBox2.Text),Convert.ToDateTime(textBox4.Text));
a1.Vvid(Convert.ToDateTime(textBox3.Text));
a1.Vivid();
aztak.Add(a1);
sh.Add(zx, a1);
zx++;
}
else if (radioButton2.Checked == true)
{
Partiy a1 = new Partiy(textBox1.Text, Convert.ToDouble(textBox2.Text),
Convert.ToDateTime(textBox4.Text));
a1.Vvid(Convert.ToDateTime(textBox3.Text), Convert.ToInt32(textBox5.Text));
a1.Vivid();
aztak.Add(a1);
sh.Add(zx, a1);
zx++;
}
else if (radioButton3.Checked == true)
{
Complect a1 = new Complect(textBox1.Text, Convert.ToDouble(textBox2.Text),
Convert.ToDateTime(textBox4.Text));
a1.Vvid(Convert.ToDateTime(textBox3.Text),textBox5.Text);
a1.Vivid();
aztak.Add(a1);
sh.Add(zx, a1);
zx++;
}
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)


{
textBox3.Enabled = true;
label3.Text = "Дата створення";
textBox5.Enabled = false;
label5.Text = "";
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)


{
textBox3.Enabled = true;
label3.Text = "Дата створення";
textBox5.Enabled = true;
label5.Text = "Кількість шт";
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)


{
textBox3.Enabled = true;
label3.Text = "Склад";
textBox5.Enabled = false;
label5.Text = "";
}

private void button3_Click(object sender, EventArgs e)


{
richTextBox1.Clear();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
}

private void button5_Click(object sender, EventArgs e)


{
zx = 0;
for (int i = 0; i < sh.Count; i++)
{
if (((ToWar)sh[i]).Pd < DateTime.Today)
((ToWar)sh[i]).Vivid();
}

private void button1_Click(object sender, EventArgs e)


{

if (comboBox1.SelectedIndex == 0)
{
List<yssson> seri = new List<yssson>();

Арк.

Змн. Арк. № докум. Підпис Дата


foreach (ToWar i in aztak)
{
yssson ax = new yssson(i.Nam, i.Zin, i.Pd,i.Gs,i.Sh,i.PSD);
seri.Add(ax);

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);
}
}

private void button2_Click(object sender, EventArgs e)


{
if (comboBox2.SelectedIndex == 0)
{
DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(List<yssson>));
using (FileStream fs = new FileStream("data.json", FileMode.OpenOrCreate))
{
List<yssson> seri = (List<yssson>)jsonFormatter.ReadObject(fs);
foreach (yssson 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 == 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; }

public yssson(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;
}
}
}

Арк.

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
Змн. Арк. № докум. Підпис Дата
Висновок: На цій лабораторній роботі я одержав практичні навички
створення абстрактних класів.

You might also like