C#简易一元二次求解器_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#简易一元二次求解器

C#简易一元二次求解器

 2014/3/23 21:11:14  superdisk  博客园  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsFormsApplication4{publicpartialclassForm1:Form{publicForm1()
  • 标签:C#

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;

}
}

发表评论
用户名: 匿名