C#一元二次方程

来源:互联网 发布:手机扫码软件 编辑:程序博客网 时间:2024/04/28 20:09
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5{    class Program    {        static void Main(string[] args)        {            double a, b, c,x1,x2;            Console.WriteLine("输入三个数a、b、c");            a = Convert.ToDouble(Console.ReadLine());            b = Convert.ToDouble(Console.ReadLine());            c = Convert.ToDouble(Console.ReadLine());                           x1 = (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a);            x2 = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);            if (b* b - 4 * a * c > 0)            Console.WriteLine("x1={0} ,x2={1} ", x1, x2);            else if (b * b - 4 * a * c < 0)            Console.WriteLine("错误,无解");            x1 = x2 = -b / (2 * a);            Console.WriteLine("x1={0} ,x2={1}", x1, x2);            Console.Read();        }    }}

0 0
原创粉丝点击