# laba 4

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;
}
}

public Polynomial()
{
}

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

{
}
else
{
while (current.Next != null)
{
current = current.Next;
}

current.Next = newNode;
}
}

{
Polynomial result = new Polynomial();

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

while (currentA != null)
{
currentA = currentA.Next;
}

while (currentB != null)
{
currentB = currentB.Next;
}

return result;
}

public Polynomial Multiply(Polynomial polynomial)
{
Polynomial result = new Polynomial();

while (currentA != null)
{
while (currentB != null)
{
int coefficient = currentA.Coefficient * currentB.Coefficient;
int exponent = currentA.Exponent + currentB.Exponent;
currentB = currentB.Next;
}
currentA = currentA.Next;
}

return result;
}

public Polynomial Differentiate()
{
Polynomial result = new Polynomial();

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

return result;
}

public Polynomial Integrate()
{
Polynomial result = new Polynomial();

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

return result;
}

public override string ToString()
{
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 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.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;
//
//
//
// 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.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.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);
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));