
- •Навчальної практики
- •Задача 2
- •Перевизначення методу Len() з урахуванням способу завдання вектора,
- •Код програми
- •Задача 7
- •Код програми
- •Результат роботи програми:
- •Задача 8
- •Код програми
- •Результат роботи програми:
- •Задача 10
- •Код програми
- •Результат роботи програми:
- •Задача 14
- •Код програми
- •Результат роботи програми:
- •Задача 15
- •Код програми
- •Результат роботи програми:
- •Задача 17
- •Код програми
- •Результат роботи програми:
Задача 7
Мовою C# розробити приклад операцій над об'єктами різних класів:
Створити клас «Генератор постійного струму», поля якого тільки такі:
напруга U, В,
потужність P, Вт,
Максимальний струм генератора обчислюється по формулі Imax=P/U.
Час роботи генератора не обмежено, і тому в даний клас не включається.
Створити клас «Акумулятор», поля якого тільки такі:
напруга U, В,
ємність E, А∙год,
Максимальний струм розряду Imax, А.
Найменший час роботи обчислюється по формулі t=E/Imax.
Створити клас «Джерело безперебійного живлення» , поля якого тільки такі:
напруга U, В,
максимальний струм Imax, А.
час автономної роботи при максимальному струмі t, год.
Забезпечити можливість обчислення виразів над об'єктами цих класів за допомогою операції «*».
Ця операція припустима тільки для однотипних об'єктів з однаковою вихідною напругою і відповідає їх паралельному з’єднанню.
У результаті операції * утворюється новий об'єкт класу «Джерело безперебійного живлення» з відповідною напругою, сумарним струмом і найменшим із двох часом автономної роботи.
Скласти програму, що перевіряє виконання вказаних операцій шляхом створення декількох об'єктів різних класів, програмування схеми їхнього паралельного з'єднання для розрахунку сумарного струму і мінімального часу роботи
Код програми
using System;
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;
namespace Num_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
GetDCGeneratorSum();
GetAccumulatorSum();
GetUPSSum();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GetDCGeneratorSum()
{
if (dataGridView1.Rows.Count > 0)
{
DCGenerator generatorSum = null;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["DCGenerator_U"].Value != null && row.Cells["DCGenerator_P"].Value != null)
{
var tmpDCGenerator = new DCGenerator
{
U = double.Parse(row.Cells["DCGenerator_U"].Value.ToString()),
P = double.Parse(row.Cells["DCGenerator_P"].Value.ToString())
};
if (generatorSum == null) generatorSum = tmpDCGenerator;
else generatorSum = generatorSum + tmpDCGenerator;
}
}
if (generatorSum != null)
{
DCGeneratorU.Text = String.Format("{0:0.00}", generatorSum.U);
DCGeneratorP.Text = String.Format("{0:0.00}", generatorSum.P);
DCGeneratorImax.Text = String.Format("{0:0.00}", generatorSum.Imax);
}
}
}
private void GetAccumulatorSum()
{
if (dataGridView2.Rows.Count > 0)
{
Acсumulator acсumulatorSum = null;
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if (row.Cells["Accumulator_U"].Value != null && row.Cells["Accumulator_E"].Value != null && row.Cells["Accumulator_Imax"].Value != null)
{
var tmpAccumulator = new Acсumulator
{
U = double.Parse(row.Cells["Accumulator_U"].Value.ToString()),
E = double.Parse(row.Cells["Accumulator_E"].Value.ToString()),
Imax = double.Parse(row.Cells["Accumulator_Imax"].Value.ToString())
};
if (acсumulatorSum == null) acсumulatorSum = tmpAccumulator;
else acсumulatorSum = acсumulatorSum + tmpAccumulator;
}
}
if (acсumulatorSum != null)
{
AccumulatorU.Text = String.Format("{0:0.00}", acсumulatorSum.U);
AccumulatorE.Text = String.Format("{0:0.00}", acсumulatorSum.E);
AccumulatorImax.Text = String.Format("{0:0.00}", acсumulatorSum.Imax);
Accumulatort.Text = String.Format("{0:0.00}", acсumulatorSum.t);
}
}
}
private void GetUPSSum()
{
if (dataGridView3.Rows.Count > 0)
{
UPS UPSSum = null;
foreach (DataGridViewRow row in dataGridView3.Rows)
{
if (row.Cells["UPS_U"].Value != null && row.Cells["UPS_t"].Value != null && row.Cells["UPS_Imax"].Value != null)
{
var tmpUPS = new UPS
{
U = double.Parse(row.Cells["UPS_U"].Value.ToString()),
t = double.Parse(row.Cells["UPS_t"].Value.ToString()),
Imax = double.Parse(row.Cells["UPS_Imax"].Value.ToString())
};
if (UPSSum == null) UPSSum = tmpUPS;
else UPSSum = UPSSum + tmpUPS;
}
}
if (UPSSum != null)
{
UPSU.Text = String.Format("{0:0.00}", UPSSum.U);
UPSImax.Text = String.Format("{0:0.00}", UPSSum.Imax);
UPSt.Text = String.Format("{0:0.00}", UPSSum.t);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public class DCGenerator
{
public double U { get; set; }
public double P { get; set; }
public double Imax { get { return P / U; } }
public static DCGenerator operator +(DCGenerator DCGeneratorObj1, DCGenerator DCGeneratorObj2)
{
return new DCGenerator
{
U = DCGeneratorObj1.U + DCGeneratorObj2.U,
//т.к. при последовательном соединении сопротивление увеличивается вдвое (если элементы одинаковы), то мощность падает в 2 раза
P = (DCGeneratorObj1.P + DCGeneratorObj2.P) / 2
};
}
}
public class Acсumulator
{
public double U { get; set; }
public double E { get; set; }
public double Imax { get; set; }
public double t { get { return E / Imax; } }
public static Acсumulator operator +(Acсumulator AcсumulatorObj1, Acсumulator AcсumulatorObj2)
{
return new Acсumulator
{
E = Math.Max(AcсumulatorObj1.E, AcсumulatorObj2.E),
U = AcсumulatorObj1.U + AcсumulatorObj2.U
};
}
}
public class UPS
{
public double U { get; set; }
public double Imax { get; set; }
public double t { get; set; }
public static UPS operator +(UPS UPSObj1, UPS UPSObj2)
{
return new UPS
{
Imax = Math.Min(UPSObj1.Imax, UPSObj2.Imax),
t = Math.Min(UPSObj1.t, UPSObj2.t),
U = UPSObj1.U + UPSObj2.U
};
}
}
}