您的位置:首页 > 其它

求一元二次方程

2014-10-27 11:23 375 查看
// Copyright (c) 2014软件技术2班
// All rights reserved.
// 作    者:B14
// 完成日期:2014年 10 月 24 日
// 版 本 号:v1.0
// 问题描述:创建一个程序求一元二次方程
//输入描述:任意输入 a b c 值,根据公式计算x1,x2并输出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace yiyuan
{
class Program
{
static void Main(string[] args)
{
double x1 =0 , x2 = 0;  //定义double类型,名称x1.x2初始化为0
double a, b, c, dt;
Console.Title = ("求一元二次方程");
Console.WriteLine("ax^2+bx=0");
Console.WriteLine("请输入a的值:");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入b的值:");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入c的值:");
c = Convert.ToInt32(Console.ReadLine());
dt = b * b - 4 * a * c;                       //Δ的公式
if (dt > 0)                   //判断Δ
{
x1 = (-b + Math.Sqrt(dt)) / 2.0 * a;   //一元二次方程公式
x2 = (-b - Math.Sqrt(dt)) / 2.0 * a;
Console.WriteLine("有两个实数根");
Console.WriteLine("x1={0}\nx2={1}", x1, x2);
}
else if (dt == 0)
{
x1 = x2 = -b / (2 * a);      //一元二次方程公式
Console.WriteLine("x1=x2={0}", x1);
Console.WriteLine("仅一个解");
}

else
{
Console.WriteLine("无解");

}

Console.Read();

}
}
}


输出:

     

总结:通过这个,我比较明白了变量类型的运用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: