问题及源程序
//
//Copyright (c) 2014软件技术1班
// All rights reserved.
//作者:A27陈城安
//完成时间:2014年10月25日
//问题描述:创建一个程序来求解一元二次方程。该程序提示用户输入a,b,c数值,然后显示书求解的结果。
//输入描述:3个实数,代表a,b,c的值。
//输出描述:给出两个实数根的解,若非数字就是无解!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 求一元二次方程
{
class Class1
{
static void Main(string[] args)
{
double a, b, c;
Console.WriteLine("请输入a的值,且a不能为0");
a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入b的值");
b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入c的值");
c = Convert.ToDouble(Console.ReadLine());
if (Math.Pow(b, 2) - 4 * a * c >= 0)
Console.WriteLine("x1 = " + (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a));
Console.WriteLine("x2 = " + (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a));
Console.WriteLine("方程的两个解为{0}{1}"," x1", "x2");
Console.Read();
}
}
}
输出结果:(若显示非数字说明方程无解!)