Загрузка данных


using System;
using System.Drawing;
using System.Windows.Forms;

namespace lr16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();        // ← Обязательно оставляем!
            
            this.Text = "Вычисление выражения — Вариант 14";
            this.Size = new Size(1000, 750);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = Color.WhiteSmoke;

            CreateControls();             // Создаём все элементы вручную
        }

        private void CreateControls()
        {
            // Заголовок
            Label title = new Label
            {
                Text = "Вычисление выражения — Вариант 14",
                Font = new Font("Segoe UI", 14, FontStyle.Bold),
                Location = new Point(30, 20),
                AutoSize = true
            };

            // Группа "Исходные данные"
            GroupBox gbInput = new GroupBox
            {
                Text = "Исходные данные",
                Location = new Point(30, 70),
                Size = new Size(400, 300)
            };

            // Поля ввода
            AddLabelAndText(gbInput, "X =", 20, 30, "2.5", "txtX");
            AddLabelAndText(gbInput, "Y =", 20, 70, "1.5", "txtY");
            AddLabelAndText(gbInput, "N =", 20, 110, "5", "txtN");
            AddLabelAndText(gbInput, "R =", 20, 150, "4", "txtR");

            // a и b
            AddLabelAndCombo(gbInput, "a =", 220, 30, new[] { "1.5", "2.0", "2.5" }, 0, "cmbA");
            AddLabelAndCombo(gbInput, "b =", 220, 70, new[] { "2.0", "3.0", "4.0" }, 0, "cmbB");

            // Группа "Метод расчёта"
            GroupBox gbMethod = new GroupBox
            {
                Text = "Метод расчёта",
                Location = new Point(30, 390),
                Size = new Size(400, 140)
            };

            RadioButton rbMethod1 = new RadioButton
            {
                Text = "Метод 1: сумма ряда (-1/1 + Y/3 − X/5 + ...)",
                Location = new Point(20, 30),
                Size = new Size(360, 40),
                Checked = true,
                Name = "rbMethod1"
            };

            RadioButton rbMethod2 = new RadioButton
            {
                Text = "Метод 2: двойная сумма ΣΣ (a·i² + b·j) / (c^i · i³)",
                Location = new Point(20, 75),
                Size = new Size(360, 40),
                Name = "rbMethod2"
            };

            gbMethod.Controls.Add(rbMethod1);
            gbMethod.Controls.Add(rbMethod2);

            // Кнопка "Рассчитать"
            Button btnCalc = new Button
            {
                Text = "Рассчитать",
                Location = new Point(130, 560),
                Size = new Size(180, 50),
                Font = new Font("Segoe UI", 12, FontStyle.Bold),
                BackColor = Color.LightBlue
            };
            btnCalc.Click += BtnCalc_Click;

            // Группа "Результат"
            GroupBox gbResult = new GroupBox
            {
                Text = "Результат",
                Location = new Point(460, 70),
                Size = new Size(510, 540)
            };

            TextBox txtResult = new TextBox
            {
                Name = "txtResult",
                Multiline = true,
                ScrollBars = ScrollBars.Vertical,
                Location = new Point(15, 30),
                Size = new Size(480, 490),
                Font = new Font("Consolas", 11),
                BackColor = Color.Black,
                ForeColor = Color.LimeGreen,
                ReadOnly = true
            };

            gbResult.Controls.Add(txtResult);

            // Добавляем элементы на форму
            this.Controls.Add(title);
            this.Controls.Add(gbInput);
            this.Controls.Add(gbMethod);
            this.Controls.Add(btnCalc);
            this.Controls.Add(gbResult);
        }

        private void AddLabelAndText(GroupBox gb, string labelText, int x, int y, string defaultValue, string name)
        {
            Label lbl = new Label { Text = labelText, Location = new Point(x, y), AutoSize = true };
            TextBox txt = new TextBox 
            { 
                Text = defaultValue, 
                Location = new Point(x + 60, y - 3), 
                Size = new Size(100, 25), 
                Name = name 
            };
            gb.Controls.Add(lbl);
            gb.Controls.Add(txt);
        }

        private void AddLabelAndCombo(GroupBox gb, string labelText, int x, int y, string[] items, int selectedIndex, string name)
        {
            Label lbl = new Label { Text = labelText, Location = new Point(x, y), AutoSize = true };
            ComboBox cmb = new ComboBox 
            { 
                Location = new Point(x + 60, y - 3), 
                Size = new Size(100, 25), 
                Name = name 
            };
            cmb.Items.AddRange(items);
            cmb.SelectedIndex = selectedIndex;
            gb.Controls.Add(lbl);
            gb.Controls.Add(cmb);
        }

        private void BtnCalc_Click(object sender, EventArgs e)
        {
            var txtResult = (TextBox)this.Controls.Find("txtResult", true)[0];
            txtResult.Clear();

            try
            {
                double x = double.Parse(GetText("txtX"));
                double y = double.Parse(GetText("txtY"));
                int n = int.Parse(GetText("txtN"));
                int r = int.Parse(GetText("txtR"));
                double a = double.Parse(GetComboText("cmbA"));
                double b = double.Parse(GetComboText("cmbB"));

                bool isMethod1 = ((RadioButton)this.Controls.Find("rbMethod1", true)[0]).Checked;

                txtResult.AppendText("Вариант: 14\n");
                txtResult.AppendText($"Выбранный метод: {(isMethod1 ? "1 (сумма ряда)" : "2 (двойная сумма)")}\n\n");
                txtResult.AppendText($"Входные параметры:\n");
                txtResult.AppendText($"X = {x}   Y = {y}\n");
                txtResult.AppendText($"N = {n}   R = {r}\n");
                txtResult.AppendText($"a = {a}   b = {b}\n\n");

                double z = 0.0;

                if (isMethod1)
                {
                    txtResult.AppendText("Расчёт ряда до 10-го члена:\n");
                    for (int k = 1; k <= Math.Min(10, n); k++)
                    {
                        double p = (k % 2 == 1) ? y : x;
                        int sign = (k % 2 == 1) ? -1 : 1;
                        double term = sign * p / (2 * k - 1);

                        txtResult.AppendText($"Член {k}: {term:F8}\n");
                        z += term;
                    }
                }
                else
                {
                    txtResult.AppendText("Расчёт двойной суммы:\n");
                    for (int i = 1; i <= n; i++)
                    {
                        for (int j = 1; j <= r; j++)
                        {
                            double term = (a * i * i + b * j) / (Math.Pow(2, i) * Math.Pow(i, 3));
                            z += term;
                        }
                    }
                }

                txtResult.AppendText($"\nZ = {z:F7}\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка ввода данных!\n" + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string GetText(string name)
        {
            var controls = this.Controls.Find(name, true);
            return controls.Length > 0 && controls[0] is TextBox txt ? txt.Text : "0";
        }

        private string GetComboText(string name)
        {
            var controls = this.Controls.Find(name, true);
            return controls.Length > 0 && controls[0] is ComboBox cmb ? cmb.Text : "0";
        }
    }
}