класс Program

2 часть
mail@pastecode.io avatar
unknown
csharp
7 months ago
7.2 kB
3
Indexable
Never
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public class MainForm : Form
    {
        private TextBox polynomial1TextBox;
        private TextBox polynomial2TextBox;
        private Button addButton;
        private Button multiplyButton;
        private Button differentiateButton;
        private Button integrateButton;
        private TextBox resultTextBox;

        public MainForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            polynomial1TextBox = new TextBox();
            polynomial2TextBox = new TextBox();
            addButton = new Button();
            multiplyButton = new Button();
            differentiateButton = new Button();
            integrateButton = new Button();
            resultTextBox = new TextBox();
            SuspendLayout();
            // 
            // polynomial1TextBox
            // 
            polynomial1TextBox.Location = new Point(12, 12);
            polynomial1TextBox.Name = "polynomial1TextBox";
            polynomial1TextBox.Size = new Size(200, 20);
            polynomial1TextBox.TabIndex = 0;
            // 
            // polynomial2TextBox
            // 
            polynomial2TextBox.Location = new Point(12, 38);
            polynomial2TextBox.Name = "polynomial2TextBox";
            polynomial2TextBox.Size = new Size(200, 20);
            polynomial2TextBox.TabIndex = 1;
            // 
            // addButton
            // 
            addButton.Location = new Point(12, 64);
            addButton.Name = "addButton";
            addButton.Size = new Size(75, 23);
            addButton.TabIndex = 2;
            addButton.Text = "Add";
            addButton.UseVisualStyleBackColor = true;
            addButton.Click += addButton_Click;
            // 
            // multiplyButton
            // 
            multiplyButton.Location = new Point(93, 64);
            multiplyButton.Name = "multiplyButton";
            multiplyButton.Size = new Size(75, 23);
            multiplyButton.TabIndex = 3;
            multiplyButton.Text = "Multiply";
            multiplyButton.UseVisualStyleBackColor = true;
            multiplyButton.Click += multiplyButton_Click;
            // 
            // differentiateButton
            // 
            differentiateButton.Location = new Point(12, 93);
            differentiateButton.Name = "differentiateButton";
            differentiateButton.Size = new Size(75, 23);
            differentiateButton.TabIndex = 4;
            differentiateButton.Text = "Differentiate";
            differentiateButton.UseVisualStyleBackColor = true;
            differentiateButton.Click += differentiateButton_Click;
            // 
            // integrateButton
            // 
            integrateButton.Location = new Point(93, 93);
            integrateButton.Name = "integrateButton";
            integrateButton.Size = new Size(75, 23);
            integrateButton.TabIndex = 5;
            integrateButton.Text = "Integrate";
            integrateButton.UseVisualStyleBackColor = true;
            integrateButton.Click += integrateButton_Click;
            // 
            // resultTextBox
            // 
            resultTextBox.Location = new Point(12, 123);
            resultTextBox.Multiline = true;
            resultTextBox.Name = "resultTextBox";
            resultTextBox.ReadOnly = true;
            resultTextBox.Size = new Size(200, 100);
            resultTextBox.TabIndex = 6;
            // 
            // MainForm
            // 
            AutoScaleDimensions = new SizeF(6F, 13F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(224, 235);
            Controls.Add(resultTextBox);
            Controls.Add(integrateButton);
            Controls.Add(differentiateButton);
            Controls.Add(multiplyButton);
            Controls.Add(addButton);
            Controls.Add(polynomial2TextBox);
            Controls.Add(polynomial1TextBox);
            Name = "MainForm";
            Text = "Polynomial";
            ResumeLayout(false);
            PerformLayout();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                Polynomial polynomial1 = ParsePolynomial(polynomial1TextBox.Text);
                Polynomial polynomial2 = ParsePolynomial(polynomial2TextBox.Text);
                Polynomial sum = polynomial1 + polynomial2;
                resultTextBox.Text = sum.ToString();
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid polynomial format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void multiplyButton_Click(object sender, EventArgs e)
        {
            try
            {
                Polynomial polynomial1 = ParsePolynomial(polynomial1TextBox.Text);
                Polynomial polynomial2 = ParsePolynomial(polynomial2TextBox.Text);
                Polynomial product = polynomial1 * polynomial2;
                resultTextBox.Text = product.ToString();
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid polynomial format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void differentiateButton_Click(object sender, EventArgs e)
        {
            try
            {
                Polynomial polynomial = ParsePolynomial(polynomial1TextBox.Text);
                Polynomial derivative = polynomial.Differentiate();
                resultTextBox.Text = derivative.ToString();
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid polynomial format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void integrateButton_Click(object sender, EventArgs e)
        {
            try
            {
                Polynomial polynomial = ParsePolynomial(polynomial1TextBox.Text);
                Polynomial integral = polynomial.Integrate();
                resultTextBox.Text = integral.ToString();
            }
            catch (FormatException)
            {
                MessageBox.Show("Invalid polynomial format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private Polynomial ParsePolynomial(string polynomial)
        {
            
            var terms = polynomial.Split('+', '-');

            var n = terms.Length;
            var coeff = new double[n];

            for (var i = 0; i < n; ++i) {
            
                coeff[i] = double.Parse(terms[i]);
            }
            return new Polynomial(coeff);
        }

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }
}
Leave a Comment