using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string strSrc = textBox1.Text.Trim();
if (strSrc.Length > 0)
{
int idx = strSrc.IndexOf("=");
if ((idx == strSrc.LastIndexOf("=")) && (idx > 0))
{
strSrc = strSrc.Replace("-", "+-"); string[] strTmp = strSrc.Split( '+' );
if (strTmp.Length > 0)
{
int nloop = 0;
int a = 0, b = 0, c = 0, value = 0;
string[] arrValue = strTmp[strTmp.Length-1].Split( '=' );
strTmp[strTmp.Length - 1] = arrValue[0].Trim();
string strValue = arrValue[1].Trim();
value = int.Parse(strValue); if (nloop < strTmp.Length)
{
string strA = strTmp[nloop].Trim().Replace("x*x", "").Trim();
if (strA.Length < strTmp[nloop].Trim().Length)
{
strA = strA.Replace(" ", "");
if (strA.Length > 0)
{
a = int.Parse(strA);
nloop++;
}
}
} if (nloop < strTmp.Length)
{
string strB = strTmp[nloop].Trim().Replace("x", "").Trim();
if (strB.Length < strTmp[nloop].Trim().Length)
{
strB = strB.Replace(" ", "");
if (strB.Length > 0)
{
b = int.Parse(strB);
nloop++;
}
}
} if (nloop < strTmp.Length)
{
string strC = strTmp[nloop].Trim();
strC = strC.Replace(" ", "");
if (strC.Length > 0)
{
c = int.Parse(strC);
}
} c = c - value; // -b(+-)sqrt(b^2-4ac) / (2 * a)
value = b * b - 4 * a * c;
if( value < 0 )
{
//在实数范围内无解
}
else
{
double x1, x2; if (a != 0)
{
double dblTmp = System.Math.Sqrt(value);
x1 = (-1 * b + dblTmp) / (2 * a);
x2 = (-1 * b - dblTmp) / (2 * a);
}
else
{
x1 = x2 = -1.0 * c / b;
} Console.WriteLine("x1 = " + x1.ToString() + " , x2 = " + x2.ToString());
}
}
else
{
//格式错误
}
}
else
{
//格式错误
}
}
}
}
}注:按此格式输入方程
ax*x + bx + c = value
方程左边等于0的项可以省略不写,方程右边如果等于0则必须写0,如果a、b的值为 1,也必须写出来,请注意乘号( * )仅在x*x中出现
另外,代码没有对形如 0 = 8 等不合理的式子做判断举例
输入
2x*x - 4x + 16 = 23
输出
x1 = 3.12132034355964 , x2 = -1.12132034355964输入
8x+7=25
输出
x1 = 2.25 , x2 = 2.25输入
2x*x + 3 = 11
输出
x1 = 2 , x2 = -2
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string strSrc = textBox1.Text.Trim();
if (strSrc.Length > 0)
{
int idx = strSrc.IndexOf("=");
if ((idx == strSrc.LastIndexOf("=")) && (idx > 0))
{
strSrc = strSrc.Replace("-", "+-"); string[] strTmp = strSrc.Split( '+' );
if (strTmp.Length > 0)
{
int nloop = 0;
int a = 0, b = 0, c = 0, value = 0;
string[] arrValue = strTmp[strTmp.Length-1].Split( '=' );
strTmp[strTmp.Length - 1] = arrValue[0].Trim();
string strValue = arrValue[1].Trim();
value = int.Parse(strValue); if (nloop < strTmp.Length)
{
string strA = strTmp[nloop].Trim().Replace("x*x", "").Trim();
if (strA.Length < strTmp[nloop].Trim().Length)
{
strA = strA.Replace(" ", "");
if (strA.Length > 0)
{
a = int.Parse(strA);
nloop++;
}
}
} if (nloop < strTmp.Length)
{
string strB = strTmp[nloop].Trim().Replace("x", "").Trim();
if (strB.Length < strTmp[nloop].Trim().Length)
{
strB = strB.Replace(" ", "");
if (strB.Length > 0)
{
b = int.Parse(strB);
nloop++;
}
}
} if (nloop < strTmp.Length)
{
string strC = strTmp[nloop].Trim();
strC = strC.Replace(" ", "");
if (strC.Length > 0)
{
c = int.Parse(strC);
}
} c = c - value; // -b(+-)sqrt(b^2-4ac) / (2 * a)
value = b * b - 4 * a * c;
if( value < 0 )
{
//在实数范围内无解
}
else
{
double x1, x2; if (a != 0)
{
double dblTmp = System.Math.Sqrt(value);
x1 = (-1 * b + dblTmp) / (2 * a);
x2 = (-1 * b - dblTmp) / (2 * a);
}
else
{
x1 = x2 = -1.0 * c / b;
} Console.WriteLine("x1 = " + x1.ToString() + " , x2 = " + x2.ToString());
}
}
else
{
//格式错误
}
}
else
{
//格式错误
}
}
}
}
}注:按此格式输入方程
ax*x + bx + c = value
方程左边等于0的项可以省略不写,方程右边如果等于0则必须写0,如果a、b的值为 1,也必须写出来,请注意乘号( * )仅在x*x中出现
另外,代码没有对形如 0 = 8 等不合理的式子做判断举例
输入
2x*x - 4x + 16 = 23
输出
x1 = 3.12132034355964 , x2 = -1.12132034355964输入
8x+7=25
输出
x1 = 2.25 , x2 = 2.25输入
2x*x + 3 = 11
输出
x1 = 2 , x2 = -2
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货