一元一次方程解在生活中的做法大多是一下几步↓
举例:5x+6=7x+2
1.换位(5x-7x=2-6)
2.计算(-2x = -4)
3.返回结果(x = 2)
在编程中,我们要实现一元一次方程解也是以下几步↓
1.分割字符串整合成list类型的数据(["5x","+","6"……])
2.将所有含x的数放在一个list列表里
3.将除=以外所有数字放在另一个列表里
4.去掉x
5.计算
6.返回后的结果相除
7.返回
几个重要思路↓
1.分割时其实主要就是拿一个数作为下标,每经过+-*/=就将前面的数(where下标)和后面的数放在一起也就成了这次要分割的数,直到最后把最后一次的那个数也给加进去就可以了
2.通常我们说的移位在这里其实就是将x所在的数和普通数字进行分类,所以只要设置两个列表即可实现移位的作用
3.计算方面其实我们通常做的加减法也就是第一项和第三项对第二项进行加减法,如1+2其实就是第一项(1)与第三项(2)中间为加法(第二项)得出来的结果(3),长式也是这样,只需要添加一个循环即可
4.当然,如果第一项是+-*/时,其实只需要在列表前面加个0就行(加个0对此式不影响)
这样就可以走完全部流程了!
接下来我们就给这个流程提供必要的函数算法
1.添加包
using System.Collections.Generic;
2.分割算法
public static List<string> sp(string get)
{
char[] ge = get.ToCharArray();
int where = 0;//当前的下标
List<string> re = new List<string>();
string a = string.Empty;
for(int i =0;i<ge.Length;i++)
{
if (ge[i] == '+' || ge[i] == '-' || ge[i] == '*' || ge[i] == '/' || ge[i] == '=')
{
for (int j = where; j < i; j++)
{
a += ge[j];
}
re.Add(a);
switch (get[i])
{
case '+': re.Add("+"); break;
case '-': re.Add("-"); break;
case '*': re.Add("*"); break;
case '/': re.Add("/"); break;
case '=': re.Add("="); break;
}
where = i + 1;
a = string.Empty;
}
}
for(int k = where; k < ge.Length; k++)
{
a += ge[k];
}
re.Add(a);
return re;
}
3.整合方面
public static List<string> tidyleft(List<string> get)
{
List<string> newget = new List<string>();
for(int i = 0; i < get.Count; i++)
{
if (get[i] == "+" || get[i] == "-" || get[i] == "*" || get[i] == "/")
{
newget.Add(get[i]);
}
else
{
if(get[i] == "x")
{
newget.Add("1");
}
else
{
newget.Add(get[i].Substring(0, get[i].Length - 1));
}
}
}
return newget;
}
4.计算方面
①检验块
public static bool have(List<string> get)
{
bool h = false;
for (int i = 0; i < get.Count; i++)
{
if (get[i] == "*" || get[i] == "/")
{
h = true;
return h;
}
}
return h;
}
②计算快
public static double calulater(List<string> get, bool hav)
{
int i = 0;
double c = 0;//计算结果
bool h;
if(get[0] == "+" || get[0] == "-")
{
get.Insert(0, "0");
}
if (hav)
{
while (true)
{
i++;
if (get[i] == "*")
{
c = int.Parse(get[i - 1]) * int.Parse(get[i + 1]);
get[i - 1] = Convert.ToString(c);
get.RemoveAt(i);
get.RemoveAt(i);
i = 0;
}
else if (get[i] == "/")
{
if (int.Parse(get[i - 1]) % int.Parse(get[i + 1]) == 0)
{
c = int.Parse(get[i - 1]) / int.Parse(get[i + 1]);
get[i - 1] = Convert.ToString(c);
get.RemoveAt(i);
get.RemoveAt(i);
i = 0;
}
else
{
c = double.Parse(get[i - 1]) / double.Parse(get[i + 1]);
get[i - 1] = Convert.ToString(c);
get.RemoveAt(i);
get.RemoveAt(i);
i = 0;
}
}
h = have(get);
if (!h)
{
break;
}
c = 0;
}
c = 0;
}
if (get.Count != 1)
{
while (true)
{
if (get[1] == "+")
{
c = int.Parse(get[0]) + int.Parse(get[2]);
get[0] = Convert.ToString(c);
get.RemoveAt(1);
get.RemoveAt(1);
}
else if (get[1] == "-")
{
c = int.Parse(get[0]) - int.Parse(get[2]);
get[0] = Convert.ToString(c);
get.RemoveAt(1);
get.RemoveAt(1);
}
c = 0;
if (get.Count == 1)
{
break;
}
}
}
return int.Parse(get[0]);
}
完成以上方法后,我们就可以编写主程序了!!!
主程序其实很简单,只要把以上方法调用后就可以完成了
public static double Univariate_equation(string input)
{
List<string> finalget = new List<string>();
finalget = sp(input);
int equal = finalget.IndexOf("=");
string num = string.Empty;
List<string> left = new List<string>();
List<string> right = new List<string>();
for(int i = 0; i < finalget.Count; i++)
{
if(i == equal)
{
continue;
}
if (finalget[i].Contains("x"))
{
if (i < equal)
{
if(i == 0)
{
left.Add(finalget[i]);
}
else
{
left.Add(finalget[i - 1]);
left.Add(finalget[i]);
}
}
else
{
if (i == equal+1)
{
left.Add("-");
left.Add(finalget[i]);
}
else
{
switch(finalget[i - 1])
{
case "+": left.Add("-");break;
case "-": left.Add("+"); break;
case "*": left.Add("/"); break;
case "/": left.Add("*"); break;
}
left.Add(finalget[i]);
}
}
}
else
{
if (finalget[i] != "+" && finalget[i] != "-" && finalget[i] != "*" && finalget[i] != "/")
{
if (i < equal)
{
if (i > 0)
{
switch (finalget[i - 1])
{
case "+": right.Add("-"); break;
case "-": right.Add("+"); break;
case "*": right.Add("/"); break;
case "/": right.Add("*"); break;
}
right.Add(finalget[i]);
}
else
{
right.Add("-");
right.Add(finalget[i]);
}
}
else
{
if (i == equal + 1)
{
right.Add("+");
right.Add(finalget[i]);
}
else
{
right.Add(finalget[i - 1]);
right.Add(finalget[i]);
}
}
}
}
}
left = tidyleft(left);
bool h;
h = have(left);
double le = calulater(left, h);
h = have(right);
double ri = calulater(right, h);
double re = ri / le;
return re;
}
最后返回的那个double值就是最后算出来的结果
测试↓
但这种做法也有一下缺点
1.不支持括号(后面可以写相关算法)
2.有点慢(十几毫秒,但有可能是cpu的问题,看情况)
3.可能不是最快的算法
4.只支持x为未知数的,但可以在源代码里改
所以以上就是文章的全部内容,若有问题,请在下方评论区回复,谢谢!