laba 4

mail@pastecode.io avatar
unknown
csharp
7 months ago
12 kB
4
Indexable
Never
using System;
using System.Windows.Forms;

public class Polynomial
{
    private class Node
    {
        public int Coefficient { get; set; }
        public int Exponent { get; set; }
        public Node Next { get; set; }

        public Node(int coefficient, int exponent)
        {
            Coefficient = coefficient;
            Exponent = exponent;
            Next = null;
        }
    }

    private Node head;

    public Polynomial()
    {
        head = null;
    }

    public void AddTerm(int coefficient, int exponent)
    {
        Node newNode = new Node(coefficient, exponent);

        if (head == null)
        {
            head = newNode;
        }
        else
        {
            Node current = head;
            while (current.Next != null)
            {
                current = current.Next;
            }

            current.Next = newNode;
        }
    }

    public Polynomial Add(Polynomial polynomial)
    {
        Polynomial result = new Polynomial();
        Node currentA = this.head;
        Node currentB = polynomial.head;

        while (currentA != null && currentB != null)
        {
            if (currentA.Exponent == currentB.Exponent)
            {
                int sum = currentA.Coefficient + currentB.Coefficient;
                if (sum != 0)
                {
                    result.AddTerm(sum, currentA.Exponent);
                }
                currentA = currentA.Next;
                currentB = currentB.Next;
            }
            else if (currentA.Exponent > currentB.Exponent)
            {
                result.AddTerm(currentA.Coefficient, currentA.Exponent);
                currentA = currentA.Next;
            }
            else
            {
                result.AddTerm(currentB.Coefficient, currentB.Exponent);
                currentB = currentB.Next;
            }
        }

        while (currentA != null)
        {
            result.AddTerm(currentA.Coefficient, currentA.Exponent);
            currentA = currentA.Next;
        }

        while (currentB != null)
        {
            result.AddTerm(currentB.Coefficient, currentB.Exponent);
            currentB = currentB.Next;
        }

        return result;
    }

    public Polynomial Multiply(Polynomial polynomial)
    {
        Polynomial result = new Polynomial();
        Node currentA = this.head;

        while (currentA != null)
        {
            Node currentB = polynomial.head;
            while (currentB != null)
            {
                int coefficient = currentA.Coefficient * currentB.Coefficient;
                int exponent = currentA.Exponent + currentB.Exponent;
                result.AddTerm(coefficient, exponent);
                currentB = currentB.Next;
            }
            currentA = currentA.Next;
        }

        return result;
    }

    public Polynomial Differentiate()
    {
        Polynomial result = new Polynomial();
        Node current = this.head;

        while (current != null)
        {
            if (current.Exponent > 0)
            {
                int coefficient = current.Exponent * current.Coefficient;
                int exponent = current.Exponent - 1;
                result.AddTerm(coefficient, exponent);
            }
            current = current.Next;
        }

        return result;
    }

    public Polynomial Integrate()
    {
        Polynomial result = new Polynomial();
        Node current = this.head;

        while (current != null)
        {
            int coefficient = current.Coefficient / (current.Exponent + 1);
            int exponent = current.Exponent + 1;
            result.AddTerm(coefficient, exponent);
            current = current.Next;
        }

        return result;
    }

    public override string ToString()
    {
        Node current = head;
        string polynomialString = "";

        while (current != null)
        {
            if (current.Coefficient != 0)
            {
                if (polynomialString != "")
                {
                    polynomialString += " + ";
                }

                polynomialString += current.Coefficient;
                if (current.Exponent != 0)
                {
                    polynomialString += "x^" + current.Exponent;
                }
            }

            current = current.Next;
        }

        if (polynomialString == "")
        {
            polynomialString = "0";
        }

        return polynomialString;
    }
}

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

    private void addButton_Click(object sender, EventArgs e)
    {
        try
        {
            Polynomial polynomial1 = ParsePolynomial(polynomial1TextBox.Text);
            Polynomial polynomial2 = ParsePolynomial(polynomial2TextBox.Text);
            Polynomial sum = polynomial1.Add(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.Multiply(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 input)
    {
    Polynomial polynomial = new Polynomial();

    string[] terms = input.Split('+');
    foreach (string term in terms)
    {
        string[] parts = term.Trim().Split('x');
        if (parts.Length != 2)
        {
            throw new FormatException();
        }

        int coefficient = int.Parse(parts[0].Trim());
        int exponent = int.Parse(parts[1].Trim().Substring(1));
        polynomial.AddTerm(coefficient, exponent);
    }

    return polynomial;
    }

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