class Program { /// <summary> /// 解一元二次方程 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> /// <param name="d"></param> /// <param name="e"></param> /// <returns></returns> public int jiefangcheng(int a, int b, int c, out double x1, out double x2) { x1 = 0; x2 = 0; if (a == 0) { return 3; } else { int del = b * b - 4 * a * c; if (del > 0) { x1 = (-b + Math.Sqrt(del)) / (2 * a); x2 = (-b - Math.Sqrt(del)) / (2 * a); return 2; } else if (del == 0) { x1 = (-b + Math.Sqrt(del)) / (2 * a); x2 = x1; return 1; } else { return 0; } } } static void Main(string[] args) { //解一元二次方程 Console.Write("请输入a="); int a = int.Parse(Console.ReadLine()); Console.Write("请输入b="); int b = int.Parse(Console.ReadLine()); Console.Write("请输入c="); int c = int.Parse(Console.ReadLine()); double x1, x2; int jieguo = new Program().jiefangcheng(a,b,c,out x1,out x2); if(jieguo ==0) { Console.WriteLine("方程无实根"); } if (jieguo == 1) { Console.WriteLine("方程有两个相等实根x1=x2="+x1); } if (jieguo == 2) { Console.WriteLine("方程有两个相等实根x1=" + x1 + "x2=" + x2); } if (jieguo == 3) { Console.WriteLine("不是一元二次方程"); } Console.ReadLine(); } }