c#实现一元二次方程求解器示例分享
时间:2023-01-03 21:15:30
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;
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot实现WebSocket即时通讯的示例代码
![](https://img.aspxhome.com/file/2023/8/78868_0s.jpg)
简单的理解java集合中的HashSet和HashTree几个重写方法
C#和Java有什么区别和联系
java面试常见模式问题---代理模式
arthas jprofiler做复杂链路的调用分析
![](https://img.aspxhome.com/file/2023/6/78956_0s.png)
Android实现APP秒表功能
![](https://img.aspxhome.com/file/2023/8/95988_0s.jpg)
Java内存区域管理详解
![](https://img.aspxhome.com/file/2023/3/59173_0s.png)
IDEA导入外部项目报Error:java: 无效的目标发行版: 11的解决方法
![](https://img.aspxhome.com/file/2023/4/62704_0s.png)
Java 归并排序算法、堆排序算法实例详解
![](https://img.aspxhome.com/file/2023/6/59896_0s.jpg)
老生常谈Scanner的基本用法
Java轻量级权限认证框架Sa-Token的使用
![](https://img.aspxhome.com/file/2023/8/105948_0s.jpg)
Android编程使用pull方式解析xml格式文件的方法详解
![](https://img.aspxhome.com/file/2023/5/93295_0s.png)
Unity动画混合树实例详解
![](https://img.aspxhome.com/file/2023/8/104298_0s.gif)
Android实现图片自动轮播并且支持手势左右无限滑动
Android 多国语言value文件夹命名的方法
![](https://img.aspxhome.com/file/2023/9/137619_0s.png)
关注Ionic底部导航按钮tabs在android情况下浮在上面的处理
![](https://img.aspxhome.com/file/2023/9/137189_0s.jpg)
Java Spring5学习之JdbcTemplate详解
![](https://img.aspxhome.com/file/2023/2/60412_0s.png)
Android线程管理之ActivityThread
Java 实战项目之精美物流管理系统的实现流程
![](https://img.aspxhome.com/file/2023/8/125248_0s.png)
Java非侵入式API接口文档工具apigcc用法详解
![](https://img.aspxhome.com/file/2023/1/59331_0s.png)