using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
public struct Complex
{
// Real and Imaginary parts of a complex number
private double real, imaginary;
public Complex(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
// Accessor methods for accessing/setting private variables
public double Real
{
get { return real; }
set { real = value; }
}
public double Imaginary
{
get { return imaginary; }
set { imaginary = value; }
}
//////////////////////////////////////////////
//
// Implicit and Explicit conversion operators
//
// Implicit conversion of Complex-to-double
public static implicit operator double(Complex c)
{
return c.Real;
}
// Explicit conversion of double-to-complex (requires explicit cast)
public static explicit operator Complex(double f)
{
return new Complex
(f, 0
);
}
//////////////////////////////////////////////
//
// Arithmetic overloaded operators:
// +, -, *, /, ==, !=
//
public static Complex operator +(Complex c)
{
return c;
}
public static Complex operator -(Complex c)
{
return new Complex
(-c.
Real,
-c.
Imaginary);
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex
(c1.
Real + c2.
Real, c1.
Imaginary + c2.
Imaginary);
}
public static Complex operator +(Complex c1, double num)
{
return new Complex
(c1.
Real + num, c1.
Imaginary);
}
public static Complex operator +(double num, Complex c1)
{
return new Complex
(c1.
Real + num, c1.
Imaginary);
}
public static Complex operator -(Complex c1, double num)
{
return new Complex
(c1.
Real - num, c1.
Imaginary);
}
public static Complex operator -(double num, Complex c1)
{
return new Complex
(num
- c1.
Real,
-c1.
Imaginary);
}
public static Complex operator -(Complex c1, Complex c2)
{
return new Complex
(c1.
Real - c2.
Real, c1.
Imaginary - c2.
Imaginary);
}
public static Complex operator *(Complex c1, Complex c2)
{
return new Complex
((c1.
Real * c2.
Real) - (c1.
Imaginary * c2.
Imaginary),
(c1.Real * c2.Imaginary) + (c1.Imaginary * c2.Real));
}
public static Complex operator *(Complex c1, double num)
{
return new Complex
(c1.
Real * num, c1.
Imaginary * num
);
}
public static Complex operator *(double num, Complex c1)
{ return new Complex
(c1.
Real * num, c1.
Imaginary * num
); }
public static Complex operator /(Complex c1, Complex c2)
{
double div = c2.Real * c2.Real + c2.Imaginary * c2.Imaginary;
if (div
== 0
) throw new DivideByZeroException
();
return new Complex
((c1.
Real * c2.
Real + c1.
Imaginary * c2.
Imaginary) / div,
(c1.Imaginary * c2.Real - c1.Real * c2.Imaginary) / div);
}
public static bool operator ==(Complex c1, Complex c2)
{
return (c1.Real == c2.Real) && (c1.Imaginary == c2.Imaginary);
}
public static bool operator !=(Complex c1, Complex c2)
{
return (c1.Real != c2.Real) || (c1.Imaginary != c2.Imaginary);
}
public override int GetHashCode()
{
return (Real.GetHashCode() ^ Imaginary.GetHashCode());
}
public override bool Equals(object o)
{
return (o
is Complex
) ? (this == (Complex
)o
) : false
;
}
// Display the Complex Number in natural form:
// ------------------------------------------
// Note that calling this method will box the value into a string object
// and thus cause it to be allocated on the heap with a size of 24 bytes
public override string ToString()
{
return (String.Format("{0} + {1}i", real, imaginary));
}
}
namespace Equazioni_2
{
public partial class Form1 : Form
{
public string prova2;
public Form1()
{
InitializeComponent();
}
Form calcoli;
public static bool ok = false;
public static bool der = false;
public static bool intr = false;
public static int grado, cont1;
public static Complex[] coefficiente, coeffieciente2, coefGraf;
public static Complex Resto, alfa, massimo, a, b, c, d, fA, fB, fC, A, incremento, a1, r1, A2, an,bn,cn,fAn,fBn,fCn;
public static int ecc2, gr2;
public static Double[] derivata;
private void Scrivi_Equazione()
{
coefGraf = coefficiente;
gr2 = grado;
int esponente = grado;
modificaToolStripMenuItem.Enabled = true;
risolviToolStripMenuItem.Enabled = true;
toolStripButton1.Enabled = true;
textBox5.Text = "";
textBox5.TextAlign = HorizontalAlignment.Center;
textBox5.
Font = new Font
("Arial", 11.5f, FontStyle.
Regular);
textBox5.AppendText(coefficiente[esponente].Real + "x^"+esponente.ToString());
esponente--;
if (coefficiente[esponente].Real > 0)
textBox5.AppendText(" + ");
else if (coefficiente[esponente].Real < 0)
textBox5.AppendText(" - ");
while(esponente > 0)
{
if (coefficiente[esponente].Real != 0)
{
if (esponente == 1)
{
textBox5.AppendText(Math.Abs(coefficiente[esponente].Real) + "x");
esponente--;
}
else
{
textBox5.AppendText(Math.Abs(coefficiente[esponente].Real) + "x^" + esponente.ToString());
esponente--;
}
if (coefficiente[esponente].Real > 0)
textBox5.AppendText(" + ");
else if (coefficiente[esponente].Real < 0)
textBox5.AppendText(" - ");
}
else esponente--;
}
if(coefficiente[esponente].Real != 0)
textBox5.AppendText(Math.Abs(coefficiente[esponente].Real).ToString());
textBox5.AppendText(" = 0");
textBox8.SelectionColor = Color.BlueViolet;
textBox8.AppendText("\n"+textBox5.Text + "\n");
textBox8.SelectionColor = Color.Black;
}
public static Complex Pow(Complex x1, int esp1)
{
int cont1;
Complex res
= new Complex
(1, 0
);
for (cont1 = 0; cont1 < esp1; cont1++)
{
res = res * x1; ;
}
return res;
}
public static Complex resto(Complex x2)
{
int esp1;
Resto
= new Complex
(0,0
);
for (esp1 = 0; esp1 <= grado; esp1++)
{
Resto += coefficiente[esp1] * Pow(x2, esp1);
}
return Resto;
}
private void Separazione_delle_radici()
{
d.Real = 0.001;
for (; ; )
{
b = (Complex)Math.Round(a + d,3);
fA = resto(a);
fB = resto(b);
if (fA == 0)
{
A = a;
Stampa_soluzione(A);
break;
}
if (fB == 0)
{
A = b;
Stampa_soluzione(A);
break;
}
else
{
if ((fA / fB) < 0)
{
Metodo_di_bisezione();
break;
}
else
a = b;
}
if (b > alfa)
{
if (grado == 2)
Complessi_parte_reale();
break;
}
}
}
private Complex Max(Complex[] array1)
{
int cont1 = 0;
massimo.Real = Math.Abs(coefficiente[cont1].Real);
for (cont1 = 1; cont1 < array1.Length; cont1++)
{
if (Math.Abs(coefficiente[cont1].Real) > massimo.Real)
massimo.Real = Math.Abs(coefficiente[cont1].Real);
}
return massimo;
}
private void Stampa_soluzione(Complex X)
{
textBox8.AppendText("X = " + X.ToString() + " -> Y = " + resto(X).ToString() + "\n");
if (grado > 2)
Ruffini();
else if (X.Imaginary == 0)
Ruffini();
}
private void Metodo_di_bisezione()
{
Complex c_vecchio = c;
Form3.textBox2.SelectionColor = Color.Red;
Form3.textBox2.AppendText(" Equazione: " + textBox5.Text + "\n");
Form3.textBox2.SelectionColor = Color.Black;
//Ora abbiamo ottenuto gli estremi all'interno dei quali si deve cercare la soluzione
for (int contatore = 0; contatore < 75; contatore++)
{
c.Real = (a.Real + b.Real) / 2;
a.Real = Math.Round(a, 15);
if (c == c_vecchio)
break;
Form3.textBox2.AppendText("X = " + a.ToString() + " -> Y = " + resto(a).ToString() + "\n");
fA = resto(a);
fB = resto(b);
fC = resto(c);
if (fA == 0)
{
break;
}
if (fB == 0)
{
a = b;
break;
}
if (fC == 0)
{
a = c;
break;
}
if ((fA * fC) < 0)
b = c;
else if ((fB * fC) < 0)
a = c;
c_vecchio = c;
}
A = a;
Stampa_soluzione(A);
}
private void Complessi_parte_reale()
{
A.Real = -alfa;
incremento.Real = 0.1;
for (; ; )
{
a1 = A + incremento;
r1.Real = Math.Abs(resto(a1).Real);
if (resto(A).Real == 0)
{
Stampa_soluzione(A);
break;
}
if (r1.Real > Math.Abs(resto(A).Real))
{
//Trovato intervallo: A e a1
Bisezione2();
break;
}
Form3.textBox2.AppendText("X = " + A.ToString() + " -> Y = " + resto(A).ToString() + "\n");
if (A.Real == alfa)
break;
A += incremento;
}
}
private void ControlloImmaginari()
{
incremento.Imaginary = 0.01;
incremento.Real = 0;
A.Imaginary = 0;
Complex prova;
for (; ; )
{
A.Imaginary += incremento.Imaginary;
Form3.textBox2.AppendText("X = " + A.ToString() + " -> Y = " + resto(A).ToString() + "\n");
if (grado == 2)
{
if ((resto(A).Real) / (resto(A - incremento).Real) < 0)
{
//Trovati estremi: A e A-2*incremanto
prova = A - incremento;
break;
}
}
else
{
if (Math.Abs(resto(A).Real) > (Math.Abs(resto(A - incremento).Real)))
break;
}
}
a = A - incremento;
b = A;
for (int contatore = 0; contatore < 50; contatore++)
{
c.Imaginary = (a.Imaginary + b.Imaginary) / 2;
c.Real = a.Real;
Form3.textBox2.AppendText("X = " + c.ToString() + " -> Y = " + resto(c).ToString() + "\n");
fA = resto(a);
fB = resto(b);
fC = resto(c);
if (fA == 0)
{
break;
}
if (fB == 0)
{
a = b;
break;
}
if (fC == 0)
{
a = c;
break;
}
if (a.Imaginary == 3) break;
if ((fA.Real * fC.Real) < 0)
b = c;
else if ((fB.Real * fC.Real) < 0)
a = c;
}
A = a;
if (Math.Abs(resto(A).Imaginary) < 0.0000001)
{
A.Imaginary = Math.Round(A.Imaginary, 15);
A.Real = Math.Round(A.Real, 15);
A2.Real = A.Real;
A2.Imaginary = -A.Imaginary;
Stampa_soluzione(A);
Stampa_soluzione(A2);
grado--;
}
else Secondo_Controllo();
}
private void Secondo_Controllo()
{
incremento.Imaginary = 0;
incremento.Real = 1;
A.Real = -alfa;
for (; ; )
{
A += incremento;
Form3.textBox2.AppendText("X = " + A.ToString() + " -> Y = " + resto(A).ToString() + "\n");
fA = resto(A);
if (grado == 2)
{
if ((resto(A).Imaginary) / (resto(A - incremento).Imaginary) < 0)
break;
}
}
a = A - incremento;
b = A;
for (int contatore = 0; contatore < 100; contatore++)
{
c = (a + b) / (Complex)2;
Form3.textBox2.AppendText("X = " + c.ToString() + " -> Y = " + resto(c).ToString() + "\n");
fA = resto(a);
fB = resto(b);
fC = resto(c);
if (fA == 0)
{
break;
}
if (fB == 0)
{
a = b;
break;
}
if (fC == 0)
{
a = c;
break;
}
if ((fA.Imaginary * fC.Imaginary) < 0)
b = c;
else if ((fB.Imaginary * fC.Imaginary) < 0)
a = c;
}
A = a;
ControlloImmaginari();
}
private void Bisezione2()
{
a.Real = A.Real - incremento.Real;
a.Imaginary = A.Imaginary;
b.Imaginary = a1.Imaginary;
b.Real = a1.Real;
for (int contatore = 0; contatore < 250; contatore++)
{
c.Real = (a.Real + b.Real) / 2;
c.Imaginary = a.Imaginary;
a.Real = Math.Round(a, 15);
fA.Real = Math.Abs(resto(a).Real);
fB.Real = Math.Abs(resto(b).Real);
if (fA <= fB)
b = c;
else if (fB < fA)
a = c;
}
A = a;
if (Math.Abs(resto(A).Real) > 0.000001)
{
//Ottenuta la parte reale, si passa al calcolo di quella immaginaria
ControlloImmaginari();
}
}
private void calcoliToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
if (calcoli.Visible == true)
calcoli.Hide();
else
{
calcoli.SetDesktopLocation(this.Location.X, this.Location.Y + this.Height);
calcoli.ShowInTaskbar = false;
calcoli.Show();
}
}
private void Form1_Move(object sender, EventArgs e)
{
calcoli.SetDesktopLocation(this.Location.X, this.Location.Y + this.Height);
}
private void calcoliToolStripMenuItem_Click(object sender, EventArgs e)
{
calcoli.SetDesktopLocation(this.Location.X, this.Location.Y + this.Height);
}
private void modificaToolStripMenuItem_Click(object sender, EventArgs e)
{
edit.ShowDialog();
Scrivi_Equazione();
}
private void risolviToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox6.Enabled = true;
massimo.Real = Max(coefficiente).Real;//Qui c'è qualcosa che non va
alfa.Real = Math.Round(1 + Math.Abs(massimo / coefficiente[grado].Real), 2);
a.Real = -alfa;
Separazione_delle_radici();
}
private void reset()
{
Resto
= alfa
= massimo
= a
= b
= c
= d
= fA
= fB
= fC
= A
= incremento
= a1
= r1
= A2
= new Complex
(0, 0
);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
reset();
Form4 grado
= new Form4
();
grado.ShowDialog();
if (ok == true)
{
cont1 = Form1.grado;
coefficiente
= new Complex
[Form1.
grado + 1
];
ok = false;
coe.ShowDialog();
Scrivi_Equazione();
}
}
private void Ruffini()
{
coeffieciente2
= new Complex
[grado
];
coeffieciente2[grado - 1] = coefficiente[grado];
for (int e = grado-2; e != -1; e--)
{
coeffieciente2[e] = coefficiente[e + 1] + coeffieciente2[e + 1] * A;
}
grado--;
coefficiente = coeffieciente2;
if (grado != 0) Separazione_delle_radici();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
MessageBox.Show("Istruzioni per l'utilizzo della funzione grafico:\n"+
"Tasto + (tastierino alfanumerico) --> Zoom in avanti\n"+
"Tasto - (tastierino alfanumerico) --> Zoom indietro\n"+
"Frecce direzionali per muoversi");
}
private void disegnaGraficoToolStripMenuItem_Click(object sender, EventArgs e)
{
Form grafico
= new Form6
();
grafico.Show();
}
private void disegnaIntegraleToolStripMenuItem_Click(object sender, EventArgs e)
{
Form7 opzioni
= new Form7
();
opzioni.ShowDialog();
}
}
}