当前位置: 移动技术网 > IT编程>开发语言>c# > c#实现一元二次方程求解器示例分享

c#实现一元二次方程求解器示例分享

2019年07月18日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
namespace windowsformsapplication4
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
private void form1_load(object sender, eventargs e)
{
this.text = "一元二次方程求解器";
}

private void button1_click(object sender, eventargs e)
{
a t=new a();
t.a = double.parse(textbox1.text);
t.b = double.parse(textbox2.text);
t.c = double.parse(textbox3.text);
if (t.a == 0)
textbox4.text = string.format("此为一元一次方程根为 x = {0}", (-t.c / t.b));
else
{
object box = textbox4;
t.answer(t.a, t.b, t.c, box);
}
}
}
}
class a
{
public double a, b, c;
public double answer(double a, double b, double c, object box)
{
double x1;
double x2;
textbox temp = (textbox)box;

if ((b * b - 4 * a * c) > 0)
{
x1 = ((-b + math.sqrt(b * b - 4 * a * c)) / (2 * a));
x2 = ((-b - math.sqrt(b * b - 4 * a * c)) / (2 * a));
temp.text = string.format("x1={0},x2={1}", x1, x2);

}
else if ((b * b - 4 * a * c) == 0)
{
x1 = x2 = ((-b + math.sqrt(b * b - 4 * a * c)) / (2 * a));
temp.text = string.format("x1={0},x2={1}", x1, x2);
}
else
temp.text = "此参数下的一元二次方程无解";
return 0;
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网