[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//求解一元二次方程ax^2+bx+c=0;
namespace 平时练习
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("输入a,b,a的值:");
string a = Console.ReadLine();
string b = Console.ReadLine();
string c = Console.ReadLine();
Console.WriteLine(a+"x^2+"+b+"x+"+c+"=0");
double a1 = double.Parse(a);
double b1 = double.Parse(b);
double c1 = double.Parse(c);
Judge(a1,b1,c1);
}
private static void Judge(double a1, double b1, double c1)
{
double result = b1 * b1 - 4 * a1 * c1;
double d = Math.Sqrt(result);
double m = 2 * a1;
double xs = -b1 / m;
if (result < 0)
Console.WriteLine("无解。");
else if (result == 0)
{
Console.WriteLine("有一个解:{0}", xs);
}else
{
double x1 = xs + d / m;
double x2 = xs - d / m;
Console.WriteLine("有2个解x1={0},x2={1}",x1,x2);
}
}
}
}
|