Загрузка данных
using System;
using System.Windows.Forms;
namespace Lab16_Variant10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Заполняем ComboBox коэффициентами
string[] coeffs = { "1.0", "1.5", "2.0", "2.5", "3.0" };
cbA.Items.AddRange(coeffs);
cbB.Items.AddRange(coeffs);
cbC.Items.AddRange(coeffs);
cbA.SelectedIndex = 0;
cbB.SelectedIndex = 0;
cbC.SelectedIndex = 0;
// По умолчанию выбран Способ 1
rbMethod1.Checked = true;
// Подсказки
toolTip1.SetToolTip(btnCalculate, "Выполнить расчёт по выбранному способу");
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
double a = double.Parse(cbA.Text);
double b = double.Parse(cbB.Text);
double c = double.Parse(cbC.Text);
double x = double.Parse(tbX.Text);
double y = double.Parse(tbY.Text);
double z = double.Parse(tbZ.Text);
// Обработка CheckBox
if (!cbIncludeZ.Checked) z = 0;
double W = 0;
if (rbMethod1.Checked)
{
// Способ 1: прямое вычисление
W = a * x + b * y + c * z;
lbHistory.Items.Add($"Способ 1: W = {W:F3}");
}
else
{
// Способ 2: через массив + цикл
double[] coeffs = { a, b, c };
double[] vars = { x, y, z };
for (int i = 0; i < 3; i++)
{
W += coeffs[i] * vars[i];
}
lbHistory.Items.Add($"Способ 2 (цикл): W = {W:F3}");
}
lblResult.Text = $"Результат: W = {W:F3}";
}
catch (Exception ex)
{
MessageBox.Show("Ошибка ввода данных!\n" + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void rbMethod1_CheckedChanged(object sender, EventArgs e)
{
if (rbMethod1.Checked)
lblMethod.Text = "Выбран: Прямой расчёт";
}
private void rbMethod2_CheckedChanged(object sender, EventArgs e)
{
if (rbMethod2.Checked)
lblMethod.Text = "Выбран: Расчёт через цикл";
}
private void cbIncludeZ_CheckedChanged(object sender, EventArgs e)
{
tbZ.Enabled = cbIncludeZ.Checked;
}
// Designer code (автогенерируется Visual Studio)
private System.ComponentModel.IContainer components = null;
private Label lblMethod, lblResult;
private RadioButton rbMethod1, rbMethod2;
private ComboBox cbA, cbB, cbC;
private TextBox tbX, tbY, tbZ;
private CheckBox cbIncludeZ;
private Button btnCalculate;
private ListBox lbHistory;
private ToolTip toolTip1;
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.lblMethod = new Label();
this.lblResult = new Label();
this.rbMethod1 = new RadioButton();
this.rbMethod2 = new RadioButton();
this.cbA = new ComboBox();
this.cbB = new ComboBox();
this.cbC = new ComboBox();
this.tbX = new TextBox();
this.tbY = new TextBox();
this.tbZ = new TextBox();
this.cbIncludeZ = new CheckBox();
this.btnCalculate = new Button();
this.lbHistory = new ListBox();
this.toolTip1 = new ToolTip(this.components);
// Настройка элементов (координаты и текст)
this.Text = "ЛР16 Вариант 10 - Расчёт W";
this.Size = new System.Drawing.Size(620, 520);
this.lblMethod.Text = "Выберите способ расчёта:";
this.lblMethod.Location = new System.Drawing.Point(20, 20);
this.lblMethod.Size = new System.Drawing.Size(200, 20);
this.rbMethod1.Text = "Способ 1: Прямой расчёт";
this.rbMethod1.Location = new System.Drawing.Point(40, 50);
this.rbMethod1.AutoSize = true;
this.rbMethod1.CheckedChanged += new EventHandler(rbMethod1_CheckedChanged);
this.rbMethod2.Text = "Способ 2: Через цикл";
this.rbMethod2.Location = new System.Drawing.Point(40, 80);
this.rbMethod2.AutoSize = true;
this.rbMethod2.CheckedChanged += new EventHandler(rbMethod2_CheckedChanged);
// Коэффициенты
this.Controls.Add(new Label { Text = "a:", Location = new System.Drawing.Point(20, 130) });
this.cbA.Location = new System.Drawing.Point(60, 127);
this.cbA.Size = new System.Drawing.Size(100, 21);
this.Controls.Add(new Label { Text = "b:", Location = new System.Drawing.Point(20, 160) });
this.cbB.Location = new System.Drawing.Point(60, 157);
this.cbB.Size = new System.Drawing.Size(100, 21);
this.Controls.Add(new Label { Text = "c:", Location = new System.Drawing.Point(20, 190) });
this.cbC.Location = new System.Drawing.Point(60, 187);
this.cbC.Size = new System.Drawing.Size(100, 21);
// Переменные
this.Controls.Add(new Label { Text = "x:", Location = new System.Drawing.Point(220, 130) });
this.tbX.Location = new System.Drawing.Point(260, 127);
this.tbX.Size = new System.Drawing.Size(100, 21);
this.Controls.Add(new Label { Text = "y:", Location = new System.Drawing.Point(220, 160) });
this.tbY.Location = new System.Drawing.Point(260, 157);
this.tbY.Size = new System.Drawing.Size(100, 21);
this.Controls.Add(new Label { Text = "z:", Location = new System.Drawing.Point(220, 190) });
this.tbZ.Location = new System.Drawing.Point(260, 187);
this.tbZ.Size = new System.Drawing.Size(100, 21);
this.cbIncludeZ.Text = "Включить z в расчёт";
this.cbIncludeZ.Location = new System.Drawing.Point(220, 220);
this.cbIncludeZ.Checked = true;
this.cbIncludeZ.CheckedChanged += new EventHandler(cbIncludeZ_CheckedChanged);
this.btnCalculate.Text = "Вычислить";
this.btnCalculate.Location = new System.Drawing.Point(20, 260);
this.btnCalculate.Size = new System.Drawing.Size(120, 40);
this.btnCalculate.Click += new EventHandler(btnCalculate_Click);
this.lblResult.Text = "Результат: W = ?";
this.lblResult.Location = new System.Drawing.Point(170, 270);
this.lblResult.Size = new System.Drawing.Size(300, 30);
this.lblResult.Font = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);
this.lbHistory.Location = new System.Drawing.Point(20, 320);
this.lbHistory.Size = new System.Drawing.Size(560, 150);
this.Controls.Add(this.lblMethod);
this.Controls.Add(this.rbMethod1);
this.Controls.Add(this.rbMethod2);
this.Controls.Add(this.cbA);
this.Controls.Add(this.cbB);
this.Controls.Add(this.cbC);
this.Controls.Add(this.tbX);
this.Controls.Add(this.tbY);
this.Controls.Add(this.tbZ);
this.Controls.Add(this.cbIncludeZ);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.lblResult);
this.Controls.Add(this.lbHistory);
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
}