using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace c_equation { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if ((string.IsNullOrEmpty(textBox1.Text)) || (string.IsNullOrEmpty(textBox2.Text)) || (string.IsNullOrEmpty(textBox3.Text)) || (string.IsNullOrEmpty(textBox4.Text))) { MessageBox.Show("数値を入力してください。"); return; } double a = Convert.ToDouble(textBox1.Text); double b = Convert.ToDouble(textBox2.Text); double c = Convert.ToDouble(textBox3.Text); double d = Convert.ToDouble(textBox4.Text); double x1, x2, x3 = 0.0; int result = kai(a, b, c, d, out x1, out x2, out x3); if (result == 0) { textBox5.Text = x1.ToString(); textBox6.Text = "重解"; textBox7.Text = "重解"; } else if (result == 1) { if (x1 == x2) { x1 = x3; x3 = x2; } if (x1 == x3) { x1 = x2; x2 = x3; } textBox5.Text = x1.ToString(); textBox6.Text = x2.ToString(); textBox7.Text = "重解"; } else if (result == 2) { textBox5.Text = x1.ToString(); textBox6.Text = "複素数解"; textBox7.Text = "複素数解"; } else if (result == 3) { textBox5.Text = x1.ToString(); textBox6.Text = x2.ToString(); textBox7.Text = x3.ToString(); } else if (result == 4) { textBox5.Text = x2.ToString(); textBox6.Text = x3.ToString(); textBox7.Text = ""; } else if (result == -1) { textBox5.Text = "複素数解"; textBox6.Text = ""; textBox7.Text = ""; } else if (result == -2) { textBox5.Text = ""; textBox6.Text = x2.ToString(); textBox7.Text = "重解"; } else if (result == -3) { textBox5.Text = x1.ToString(); textBox6.Text = ""; textBox7.Text = ""; } else if (result == -4) { textBox5.Text = "不能"; textBox6.Text = ""; textBox7.Text = ""; } else if (result == -5) { textBox5.Text = "不定"; textBox6.Text = ""; textBox7.Text = ""; } } private int kai(double a, double b, double c, double d, out double x1, out double x2, out double x3) { x1 = x2 = x3 = 0.0; if ((a == 0.0) && (b == 0.0) && (c == 0.0) && (d == 0.0)) { return -5; } else if ((a == 0.0) && (b == 0.0) && (c == 0.0)) { return -4; } else if ((a == 0.0) && (b == 0.0)) { x1 = -c / d; return -3; } else if (a == 0.0) { int result = q_equation(b, c, d, out x2, out x3); return result; } double hanbetu = -4.0 * a * (c * c * c) - 27.0 * (a * a) * (d * d) - 4.0 * (b * b * b) * d + (b * b) * (c * c) + (18.0 * a * b * c * d); bool x_hantei = true; double sanji = 0.0; double x = 1.0; double[] nijihan1 = new double[4] { a, b, c, d }; double[] nijihan2 = new double[2]; if (hanbetu > 0.0) { while (x_hantei) { if (a == 1.0) { sanji = (a * (x * x * x)) + (b * (x * x)) + (c * x) + d; if (sanji == 0.0) { x1 = a = x; nijihan2[0] = 1.0; nijihan2[1] = -(a); x_hantei = false; } } else if (a > 1.0) { x1 = 1.0 / a; nijihan2[0] = a; nijihan2[1] = -(1.0); x_hantei = false; } x = -(x); if (x > 0) { x++; } } nijiOut(nijihan1, nijihan2, out a, out b, out c); int result = q_equation(a, b, c, out x2, out x3); return 3; } else if (hanbetu < 0.0) { while (x_hantei) { sanji = (a * (x * x * x)) + (b * (x * x)) + (c * x) + d; if (sanji == 0.0) { x1 = x; return 2; } x = -(x); if (x > 0) { x++; } } } else if (hanbetu == 0.0) { double hanbetu2 = (-2 * (b * b * b)) + (9 * a * b * c) - (27 * d * (a * a)); if (hanbetu2 == 0) { return 0; } else { nijihan2[0] = x1 = a; nijihan2[1] = -(x); x1 = x; nijiOut(nijihan1, nijihan2, out a, out b, out c); int result = q_equation(a, b, c, out x2, out x3); return 1; } } return 1; } private void nijiOut(double[] nijihan1, double[] nijihan2, out double a, out double b, out double c) { double[] niji = new double[3]; for (int i = 0; i < 3; i++) { double y = nijihan1[i] / nijihan2[0]; nijihan1[i] = nijihan1[i] - (y * nijihan2[0]); nijihan1[i + 1] = nijihan1[i + 1] - (y * nijihan2[1]); niji[i] = y; } a = niji[0]; b = niji[1]; c = niji[2]; } private int q_equation(double a, double b, double c, out double x2, out double x3) { x2 = x3 = 0.0; double root = b * b - 4.0 * a * c; if (Math.Abs(root) < 0.000001) { x2 = x3 = (-b) / (2.0 * a); return -2; } else if (root < 0.0) { return -1; } else { x2 = (-b - Math.Sqrt(root)) / (2.0 * a); x3 = (-b + Math.Sqrt(root)) / (2.0 * a); return 4; } } } }