c#實現(xiàn)一元二次方程求解器示例分享
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 = "此參數(shù)下的一元二次方程無解";
return 0;
}
}
相關(guān)文章
C#使用Pipelines實現(xiàn)處理Socket數(shù)據(jù)包
這篇文章主要為大家詳細介紹了C#如何使用Pipelines實現(xiàn)處理Socket數(shù)據(jù)包,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12
C#中如何自定義配置上周和本周起始日來查詢業(yè)務數(shù)據(jù)(思路詳解)
在C#中并沒有封裝的方法根據(jù)我們需要來直接獲取上一周某天到某天、本周某天到某天,所以需要我們自己封裝方法來實現(xiàn)(我們也可以按照這個思路使用其他語言來實現(xiàn)),感興趣的朋友跟隨小編一起看看吧2023-09-09

