//2014软件1班
//作者:A03龙智霖
//完成日期:2014年10月24号
//问题描述:编写一个程序求一元二次方程。ax²+bx+c=0(a≠0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
double a, b, c;//a,b,c任意三个常数
Console.WriteLine("请输入a、b、c:");
a = Convert.ToDouble(Console.ReadLine());//输出数据给a
b = Convert.ToDouble(Console.ReadLine());//输出数据给b
c = Convert.ToDouble(Console.ReadLine());//输出数据给c
if (Math.Pow(b, 2) - 4 * a * c >= 0)//按照求根公式判断是否有解
{
Console.WriteLine("x1 = " + (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a));//满足条件第一个解x1
Console.WriteLine("x2 = " + (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a));//满足条件第二个解x2
}
else
{
Console.WriteLine("无解!");//不满足条件则无解
}
Console.Read();//此处用于输出定格
}
}
}
输出:
总结:通过本次作业,我学会了如何用c#编程解一元二次方程