Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Equation Solver - Form1.cs

Form1.cs

Caricato da: Matthew
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Collections;
  9.  
  10. public struct Complex
  11. {
  12.     // Real and Imaginary parts of a complex number
  13.     private double real, imaginary;
  14.  
  15.     public Complex(double real, double imaginary)
  16.     {
  17.         this.real = real;
  18.         this.imaginary = imaginary;
  19.     }
  20.  
  21.     // Accessor methods for accessing/setting private variables
  22.     public double Real
  23.     {
  24.         get { return real; }
  25.         set { real = value; }
  26.     }
  27.  
  28.     public double Imaginary
  29.     {
  30.         get { return imaginary; }
  31.         set { imaginary = value; }
  32.     }
  33.  
  34.     //////////////////////////////////////////////
  35.     //
  36.     //  Implicit and Explicit conversion operators
  37.     //
  38.  
  39.     // Implicit conversion of Complex-to-double
  40.     public static implicit operator double(Complex c)
  41.     {
  42.         return c.Real;
  43.     }
  44.  
  45.     // Explicit conversion of double-to-complex (requires explicit cast)
  46.     public static explicit operator Complex(double f)
  47.     {
  48.         return new Complex(f, 0);
  49.     }
  50.  
  51.     //////////////////////////////////////////////
  52.     //
  53.     //  Arithmetic overloaded operators:
  54.     //  +, -, *, /, ==, !=
  55.     //
  56.  
  57.     public static Complex operator +(Complex c)
  58.     {
  59.         return c;
  60.     }
  61.  
  62.     public static Complex operator -(Complex c)
  63.     {
  64.         return new Complex(-c.Real, -c.Imaginary);
  65.     }
  66.  
  67.     public static Complex operator +(Complex c1, Complex c2)
  68.     {
  69.         return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
  70.     }
  71.  
  72.     public static Complex operator +(Complex c1, double num)
  73.     {
  74.         return new Complex(c1.Real + num, c1.Imaginary);
  75.     }
  76.  
  77.     public static Complex operator +(double num, Complex c1)
  78.     {
  79.         return new Complex(c1.Real + num, c1.Imaginary);
  80.     }
  81.  
  82.     public static Complex operator -(Complex c1, double num)
  83.     {
  84.         return new Complex(c1.Real - num, c1.Imaginary);
  85.     }
  86.  
  87.     public static Complex operator -(double num, Complex c1)
  88.     {
  89.         return new Complex(num - c1.Real, -c1.Imaginary);
  90.     }
  91.  
  92.     public static Complex operator -(Complex c1, Complex c2)
  93.     {
  94.         return new Complex(c1.Real - c2.Real, c1.Imaginary - c2.Imaginary);
  95.     }
  96.  
  97.     public static Complex operator *(Complex c1, Complex c2)
  98.     {
  99.         return new Complex((c1.Real * c2.Real) - (c1.Imaginary * c2.Imaginary),
  100.                     (c1.Real * c2.Imaginary) + (c1.Imaginary * c2.Real));
  101.     }
  102.  
  103.     public static Complex operator *(Complex c1, double num)
  104.     {
  105.         return new Complex(c1.Real * num, c1.Imaginary * num);
  106.     }
  107.  
  108.     public static Complex operator *(double num, Complex c1)
  109.     { return new Complex(c1.Real * num, c1.Imaginary * num); }
  110.  
  111.  
  112.     public static Complex operator /(Complex c1, Complex c2)
  113.     {
  114.         double div = c2.Real * c2.Real + c2.Imaginary * c2.Imaginary;
  115.         if (div == 0) throw new DivideByZeroException();
  116.  
  117.         return new Complex((c1.Real * c2.Real + c1.Imaginary * c2.Imaginary) / div,
  118.                             (c1.Imaginary * c2.Real - c1.Real * c2.Imaginary) / div);
  119.     }
  120.  
  121.     public static bool operator ==(Complex c1, Complex c2)
  122.     {
  123.         return (c1.Real == c2.Real) && (c1.Imaginary == c2.Imaginary);
  124.     }
  125.  
  126.     public static bool operator !=(Complex c1, Complex c2)
  127.     {
  128.         return (c1.Real != c2.Real) || (c1.Imaginary != c2.Imaginary);
  129.     }
  130.  
  131.     public override int GetHashCode()
  132.     {
  133.         return (Real.GetHashCode() ^ Imaginary.GetHashCode());
  134.     }
  135.  
  136.     public override bool Equals(object o)
  137.     {
  138.         return (o is Complex) ? (this == (Complex)o) : false;
  139.     }
  140.  
  141.     // Display the Complex Number in natural form:
  142.     // ------------------------------------------
  143.     // Note that calling this method will box the value into a string object
  144.     // and thus cause it to be allocated on the heap with a size of 24 bytes
  145.     public override string ToString()
  146.     {
  147.         return (String.Format("{0} + {1}i", real, imaginary));
  148.     }
  149. }
  150.  
  151. namespace Equazioni_2
  152. {
  153.     public partial class Form1 : Form
  154.     {
  155.         public string prova2;
  156.         public Form1()
  157.         {
  158.             InitializeComponent();
  159.             calcoli = new Form3();
  160.         }
  161.         Form calcoli;
  162.         public static bool ok = false;
  163.         public static bool der = false;
  164.         public static bool intr = false;
  165.         public static int grado, cont1;
  166.         public static Complex[] coefficiente, coeffieciente2, coefGraf;
  167.         public static Complex Resto, alfa, massimo, a, b, c, d, fA, fB, fC, A, incremento, a1, r1, A2, an,bn,cn,fAn,fBn,fCn;
  168.         public static int ecc2, gr2;
  169.         public static Double[] derivata;
  170.  
  171.         private void Scrivi_Equazione()
  172.         {
  173.             coefGraf = coefficiente;
  174.             gr2 = grado;
  175.  
  176.             int esponente = grado;
  177.             modificaToolStripMenuItem.Enabled = true;
  178.             risolviToolStripMenuItem.Enabled = true;
  179.             toolStripButton1.Enabled = true;
  180.             textBox5.Text = "";
  181.             textBox5.TextAlign = HorizontalAlignment.Center;
  182.             textBox5.Font = new Font("Arial", 11.5f, FontStyle.Regular);
  183.  
  184.             textBox5.AppendText(coefficiente[esponente].Real + "x^"+esponente.ToString());
  185.             esponente--;
  186.             if (coefficiente[esponente].Real > 0)
  187.                 textBox5.AppendText(" + ");
  188.             else if (coefficiente[esponente].Real < 0)
  189.                 textBox5.AppendText(" - ");
  190.  
  191.             while(esponente > 0)
  192.             {
  193.                 if (coefficiente[esponente].Real != 0)
  194.                 {
  195.                     if (esponente == 1)
  196.                     {
  197.                         textBox5.AppendText(Math.Abs(coefficiente[esponente].Real) + "x");
  198.                         esponente--;
  199.                     }
  200.                     else
  201.                     {
  202.                         textBox5.AppendText(Math.Abs(coefficiente[esponente].Real) + "x^" + esponente.ToString());
  203.                         esponente--;
  204.                     }
  205.  
  206.                     if (coefficiente[esponente].Real > 0)
  207.                         textBox5.AppendText(" + ");
  208.                     else if (coefficiente[esponente].Real < 0)
  209.                         textBox5.AppendText(" - ");
  210.                 }
  211.  
  212.                 else esponente--;
  213.             }
  214.             if(coefficiente[esponente].Real != 0)
  215.                 textBox5.AppendText(Math.Abs(coefficiente[esponente].Real).ToString());
  216.             textBox5.AppendText(" = 0");
  217.  
  218.             textBox8.SelectionColor = Color.BlueViolet;
  219.             textBox8.AppendText("\n"+textBox5.Text + "\n");
  220.             textBox8.SelectionColor = Color.Black;
  221.         }
  222.  
  223.         public static Complex Pow(Complex x1, int esp1)
  224.         {
  225.             int cont1;
  226.             Complex res = new Complex(1, 0);
  227.  
  228.             for (cont1 = 0; cont1 < esp1; cont1++)
  229.             {
  230.                 res = res * x1; ;
  231.             }
  232.             return res;
  233.         }
  234.  
  235.         public static Complex resto(Complex x2)
  236.         {
  237.             int esp1;
  238.             Resto = new Complex(0,0);
  239.  
  240.             for (esp1 = 0; esp1 <= grado; esp1++)
  241.             {
  242.                 Resto += coefficiente[esp1] * Pow(x2, esp1);
  243.             }
  244.             return Resto;
  245.            
  246.  
  247.         }
  248.  
  249.         private void Separazione_delle_radici()
  250.         {  
  251.             d.Real = 0.001;
  252.  
  253.             for (; ; )
  254.             {
  255.                 b = (Complex)Math.Round(a + d,3);
  256.                 fA = resto(a);
  257.                 fB = resto(b);
  258.                 if (fA == 0)
  259.                 {
  260.                     A = a;
  261.                     Stampa_soluzione(A);
  262.                     break;
  263.                 }
  264.                 if (fB == 0)
  265.                 {
  266.                     A = b;
  267.                     Stampa_soluzione(A);
  268.                     break;
  269.                 }
  270.                 else
  271.                 {
  272.                     if ((fA / fB) < 0)
  273.                     {
  274.                         Metodo_di_bisezione();
  275.                         break;
  276.                     }
  277.                     else
  278.                         a = b;
  279.                 }
  280.  
  281.                 if (b > alfa)
  282.                 {
  283.                     if (grado == 2)
  284.                         Complessi_parte_reale();
  285.                     break;
  286.                 }
  287.             }
  288.         }
  289.        
  290.         private Complex Max(Complex[] array1)
  291.         {
  292.             int cont1 = 0;
  293.            
  294.             massimo.Real = Math.Abs(coefficiente[cont1].Real);
  295.            
  296.             for (cont1 = 1; cont1 < array1.Length; cont1++)
  297.             {
  298.                 if (Math.Abs(coefficiente[cont1].Real) > massimo.Real)
  299.                     massimo.Real = Math.Abs(coefficiente[cont1].Real);
  300.             }
  301.             return massimo;
  302.         }
  303.  
  304.         private void Stampa_soluzione(Complex X)
  305.         {
  306.             textBox8.AppendText("X = " + X.ToString() + "     ->    Y = " + resto(X).ToString() + "\n");
  307.             if (grado > 2)
  308.                 Ruffini();
  309.             else if (X.Imaginary == 0)
  310.                 Ruffini();
  311.         }
  312.  
  313.         private void Metodo_di_bisezione()
  314.         {
  315.             Complex c_vecchio = c;
  316.             Form3.textBox2.SelectionColor = Color.Red;
  317.            
  318.             Form3.textBox2.AppendText("   Equazione:   " + textBox5.Text + "\n");
  319.  
  320.             Form3.textBox2.SelectionColor = Color.Black;
  321.             //Ora abbiamo ottenuto gli estremi all'interno dei quali si deve cercare la soluzione
  322.             for (int contatore = 0; contatore < 75; contatore++)
  323.             {
  324.                 c.Real = (a.Real + b.Real) / 2;
  325.                 a.Real = Math.Round(a, 15);
  326.  
  327.                 if (c == c_vecchio)
  328.                     break;
  329.                 Form3.textBox2.AppendText("X = " + a.ToString() + " -> Y = " + resto(a).ToString() + "\n");
  330.  
  331.                 fA = resto(a);
  332.                 fB = resto(b);
  333.                 fC = resto(c);
  334.  
  335.                 if (fA == 0)
  336.                 {
  337.                     break;
  338.                 }
  339.  
  340.                 if (fB == 0)
  341.                 {
  342.                     a = b;
  343.                     break;
  344.                 }
  345.  
  346.                 if (fC == 0)
  347.                 {
  348.                     a = c;
  349.                     break;
  350.                 }
  351.  
  352.                 if ((fA * fC) < 0)
  353.                     b = c;
  354.  
  355.                 else if ((fB * fC) < 0)
  356.                     a = c;
  357.                 c_vecchio = c;
  358.             }
  359.             A = a;
  360.             Stampa_soluzione(A);
  361.         }
  362.  
  363.         private void Complessi_parte_reale()
  364.         {
  365.             A.Real = -alfa;
  366.             incremento.Real = 0.1;
  367.  
  368.             for (; ; )
  369.             {
  370.                 a1 = A + incremento;
  371.  
  372.                 r1.Real = Math.Abs(resto(a1).Real);
  373.  
  374.                 if (resto(A).Real == 0)
  375.                 {
  376.                     Stampa_soluzione(A);
  377.                     break;
  378.                 }
  379.  
  380.                 if (r1.Real > Math.Abs(resto(A).Real))
  381.                 {
  382.                     //Trovato intervallo: A e a1
  383.                     Bisezione2();
  384.                     break;
  385.                 }
  386.  
  387.                 Form3.textBox2.AppendText("X = " + A.ToString() + " -> Y = " + resto(A).ToString() + "\n");
  388.  
  389.                 if (A.Real == alfa)
  390.                     break;
  391.  
  392.                 A += incremento;
  393.             }
  394.  
  395.         }
  396.  
  397.         private void ControlloImmaginari()
  398.         {
  399.             incremento.Imaginary = 0.01;
  400.             incremento.Real = 0;
  401.             A.Imaginary = 0;
  402.             Complex prova;
  403.  
  404.             for (; ; )
  405.             {
  406.                 A.Imaginary += incremento.Imaginary;
  407.                 Form3.textBox2.AppendText("X = " + A.ToString() + " -> Y = " + resto(A).ToString() + "\n");
  408.                 if (grado == 2)
  409.                 {
  410.                     if ((resto(A).Real) / (resto(A - incremento).Real) < 0)
  411.                     {
  412.                         //Trovati estremi: A e A-2*incremanto
  413.                         prova = A - incremento;
  414.                         break;
  415.                     }
  416.                 }
  417.                 else
  418.                 {
  419.                     if (Math.Abs(resto(A).Real) > (Math.Abs(resto(A - incremento).Real)))
  420.                         break;
  421.                 }
  422.             }
  423.  
  424.             a = A - incremento;
  425.             b = A;
  426.  
  427.             for (int contatore = 0; contatore < 50; contatore++)
  428.             {
  429.                 c.Imaginary = (a.Imaginary + b.Imaginary) / 2;
  430.                 c.Real = a.Real;
  431.                 Form3.textBox2.AppendText("X = " + c.ToString() + " -> Y = " + resto(c).ToString() + "\n");
  432.  
  433.                 fA = resto(a);
  434.                 fB = resto(b);
  435.                 fC = resto(c);
  436.  
  437.                 if (fA == 0)
  438.                 {
  439.                     break;
  440.                 }
  441.  
  442.                 if (fB == 0)
  443.                 {
  444.                     a = b;
  445.                     break;
  446.                 }
  447.  
  448.                 if (fC == 0)
  449.                 {
  450.                     a = c;
  451.                     break;
  452.                 }
  453.  
  454.                 if (a.Imaginary == 3) break;
  455.  
  456.                 if ((fA.Real * fC.Real) < 0)
  457.                     b = c;
  458.  
  459.                 else if ((fB.Real * fC.Real) < 0)
  460.                     a = c;
  461.             }
  462.             A = a;
  463.             if (Math.Abs(resto(A).Imaginary) < 0.0000001)
  464.             {
  465.                 A.Imaginary = Math.Round(A.Imaginary, 15);
  466.                 A.Real = Math.Round(A.Real, 15);
  467.                 A2.Real = A.Real;
  468.                 A2.Imaginary = -A.Imaginary;
  469.                 Stampa_soluzione(A);
  470.                 Stampa_soluzione(A2);
  471.                 grado--;
  472.             }
  473.             else Secondo_Controllo();
  474.         }
  475.  
  476.         private void Secondo_Controllo()
  477.         {
  478.             incremento.Imaginary = 0;
  479.             incremento.Real = 1;
  480.             A.Real = -alfa;
  481.  
  482.             for (; ; )
  483.             {
  484.                 A += incremento;
  485.                 Form3.textBox2.AppendText("X = " + A.ToString() + " -> Y = " + resto(A).ToString() + "\n");
  486.                 fA = resto(A);
  487.                 if (grado == 2)
  488.                 {
  489.                     if ((resto(A).Imaginary) / (resto(A - incremento).Imaginary) < 0)
  490.                         break;
  491.                 }
  492.             }
  493.  
  494.             a = A - incremento;
  495.             b = A;
  496.  
  497.             for (int contatore = 0; contatore < 100; contatore++)
  498.             {
  499.                 c = (a + b) / (Complex)2;
  500.                 Form3.textBox2.AppendText("X = " + c.ToString() + " -> Y = " + resto(c).ToString() + "\n");
  501.  
  502.                 fA = resto(a);
  503.                 fB = resto(b);
  504.                 fC = resto(c);
  505.  
  506.                 if (fA == 0)
  507.                 {
  508.                     break;
  509.                 }
  510.  
  511.                 if (fB == 0)
  512.                 {
  513.                     a = b;
  514.                     break;
  515.                 }
  516.  
  517.                 if (fC == 0)
  518.                 {
  519.                     a = c;
  520.                     break;
  521.                 }
  522.                 if ((fA.Imaginary * fC.Imaginary) < 0)
  523.                     b = c;
  524.  
  525.                 else if ((fB.Imaginary * fC.Imaginary) < 0)
  526.                     a = c;
  527.  
  528.             }
  529.             A = a;
  530.  
  531.             ControlloImmaginari();
  532.         }
  533.  
  534.         private void Bisezione2()
  535.         {
  536.             a.Real = A.Real - incremento.Real;
  537.             a.Imaginary = A.Imaginary;
  538.             b.Imaginary = a1.Imaginary;
  539.             b.Real = a1.Real;
  540.  
  541.             for (int contatore = 0; contatore < 250; contatore++)
  542.             {
  543.                 c.Real = (a.Real + b.Real) / 2;
  544.                 c.Imaginary = a.Imaginary;
  545.                 a.Real = Math.Round(a, 15);
  546.  
  547.                 fA.Real = Math.Abs(resto(a).Real);
  548.                 fB.Real = Math.Abs(resto(b).Real);
  549.  
  550.                 if (fA <= fB)
  551.                     b = c;
  552.  
  553.                 else if (fB < fA)
  554.                     a = c;
  555.             }
  556.  
  557.             A = a;
  558.  
  559.             if (Math.Abs(resto(A).Real) > 0.000001)
  560.             {
  561.                 //Ottenuta la parte reale, si passa al calcolo di quella immaginaria
  562.                 ControlloImmaginari();
  563.             }
  564.         }
  565.  
  566.         private void calcoliToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
  567.         {
  568.             if (calcoli.Visible == true)
  569.                 calcoli.Hide();
  570.             else
  571.             {
  572.                 calcoli.SetDesktopLocation(this.Location.X, this.Location.Y + this.Height);
  573.                 calcoli.ShowInTaskbar = false;
  574.                 calcoli.Show();
  575.             }
  576.         }
  577.  
  578.         private void Form1_Move(object sender, EventArgs e)
  579.         {
  580.             calcoli.SetDesktopLocation(this.Location.X, this.Location.Y + this.Height);
  581.         }
  582.  
  583.         private void calcoliToolStripMenuItem_Click(object sender, EventArgs e)
  584.         {
  585.             calcoli.SetDesktopLocation(this.Location.X, this.Location.Y + this.Height);
  586.         }
  587.  
  588.         private void modificaToolStripMenuItem_Click(object sender, EventArgs e)
  589.         {
  590.             Form edit = new Form2();
  591.             edit.ShowDialog();
  592.             Scrivi_Equazione();
  593.         }
  594.  
  595.         private void risolviToolStripMenuItem_Click(object sender, EventArgs e)
  596.         {
  597.             groupBox6.Enabled = true;
  598.             massimo.Real = Max(coefficiente).Real;//Qui c'è qualcosa che non va
  599.             alfa.Real = Math.Round(1 + Math.Abs(massimo / coefficiente[grado].Real), 2);
  600.             a.Real = -alfa;
  601.             Separazione_delle_radici();
  602.         }
  603.  
  604.         private void reset()
  605.         {
  606.             Resto = alfa = massimo = a = b = c = d = fA = fB = fC = A = incremento = a1 = r1 = A2 = new Complex(0, 0);
  607.         }
  608.  
  609.         private void toolStripButton1_Click(object sender, EventArgs e)
  610.         {
  611.             reset();
  612.             Form4 grado = new Form4();
  613.             grado.ShowDialog();
  614.             if (ok == true)
  615.             {
  616.                 cont1 = Form1.grado;
  617.                 coefficiente = new Complex[Form1.grado + 1];
  618.                 ok = false;
  619.                 Form coe = new Form5();
  620.                 coe.ShowDialog();
  621.                 Scrivi_Equazione();
  622.             }
  623.            
  624.         }
  625.  
  626.         private void Ruffini()
  627.         {
  628.             coeffieciente2 = new Complex[grado];
  629.             coeffieciente2[grado - 1] = coefficiente[grado];
  630.  
  631.             for (int e = grado-2; e != -1; e--)
  632.             {
  633.                 coeffieciente2[e] = coefficiente[e + 1] + coeffieciente2[e + 1] * A;
  634.             }
  635.             grado--;
  636.             coefficiente = coeffieciente2;
  637.             if (grado != 0) Separazione_delle_radici();
  638.  
  639.         }
  640.  
  641.         private void toolStripButton2_Click(object sender, EventArgs e)
  642.         {
  643.             MessageBox.Show("Istruzioni per l'utilizzo della funzione grafico:\n"+
  644.                 "Tasto + (tastierino alfanumerico)   --> Zoom in avanti\n"+
  645.                 "Tasto  - (tastierino alfanumerico)  --> Zoom indietro\n"+
  646.                 "Frecce direzionali per muoversi");
  647.         }
  648.  
  649.         private void disegnaGraficoToolStripMenuItem_Click(object sender, EventArgs e)
  650.         {
  651.             Form grafico = new Form6();
  652.             grafico.Show();
  653.         }
  654.  
  655.         private void disegnaIntegraleToolStripMenuItem_Click(object sender, EventArgs e)
  656.         {
  657.             Form7 opzioni = new Form7();
  658.             opzioni.ShowDialog();
  659.         }
  660.  
  661.     }
  662. }