<pre class="csharp" name="code">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 可视化计算器
{
public class FuncProcess
{
public class ResultXY
{
public double X { get; set; }
public double Y { get; set; }
}
bool IsOperator(char c)
{
if (c == '*' || c == '/' || c == '+' || c == '-')
{
return true;
}
return false;
}
string FormatFunc(string func)
{
func = func.Replace("×", "*");
func = func.Replace("÷", "/");
func = func.Replace("·", "*");
func = func.Replace(" ", "");
func = func.Replace("(", "(");
func = func.Replace(")", ")");
func = func.Replace("X", "x");
for (int i = 0; i < func.Length; i++)
{
if (func[i] == '+')
{
if (i == 0)
{
func = func.Remove(0, 1);
}
else
{
if (IsOperator(func[i - 1]) || func[i - 1] == '(')
{
func = func.Remove(i, 1);
}
}
}
}
for (int i = 0; i < func.Length; i++)
{
if (func[i] == '(')
{
if (i == 0)
{
continue;
}
if (!IsOperator(func[i - 1]) && func[i - 1] != '(')
{
func = func.Insert(i, "*");
}
}
else if (func[i] == ')')
{
if (i == func.Length - 1)
{
continue;
}
if (!IsOperator(func[i + 1]) && func[i + 1] != ')')
{
func = func.Insert(i + 1, "*");
}
}
else if (func[i] == 'x')
{
if (i == 0)
{
if (!IsOperator(func[i + 1]) && func[i] != ')')
{
func = func.Insert(1, "*");
}
}
else if (i == func.Length - 1)
{
if (!IsOperator(func[i - 1]))
{
func = func.Insert(i, "*");
}
}
else
{
if (func[i - 1] != '(' && !IsOperator(func[i - 1]))
{
func = func.Insert(i++, "*");
}
if (func[i + 1] != ')' && !IsOperator(func[i + 1]))
{
func = func.Insert(i + 1, "*");
}
}
}
}
return func;
}
string FuncXToFuncNum(string func, double x)
{
return func.Replace("x", x.ToString());
}
string RemoveOperator(string func, char c)
{
while (func.Contains(c))
{
int indexO = 0;
int indexL = 0;
int indexR = 0;
string num0 = "";
string num1 = "";
StringBuilder sb = new StringBuilder();
char firstChar = func[0];
if (firstChar == '-')
{
func = func.Remove(0, 1);
indexO = func.IndexOf(c);
if (indexO == -1)
{
return "-" + func;
}
}
else
{
firstChar = '+';
indexO = func.IndexOf(c);
}
for (int i = indexO - 1; i >= 0; i--)
{
if (IsOperator(func[i]))
{
break;
}
indexL = i;
}
for (int i = indexL; i < indexO; i++)
{
sb.Append(func[i]);
}
num0 = sb.ToString();
num0 = firstChar == '-' ? "-" + num0 : num0;
sb.Clear();
sb.Append(func[indexO + 1]);
for (int i = indexO + 2; i < func.Length; i++)
{
indexR = i;
if (IsOperator(func[i]))
{
break;
}
sb.Append(func[i]);
}
num1 = sb.ToString();
string strOld = num0 + c.ToString() + num1;
string strNew = "";
switch (c)
{
case '*':
strNew = (Convert.ToDouble(num0) * Convert.ToDouble(num1)).ToString();
break;
case '/':
strNew = (Convert.ToDouble(num0) / Convert.ToDouble(num1)).ToString();
break;
case '+':
strNew = (Convert.ToDouble(num0) + Convert.ToDouble(num1)).ToString();
break;
case '-':
strNew = (Convert.ToDouble(num0) - Convert.ToDouble(num1)).ToString();
break;
}
func = func.Replace(strOld, strNew);
}
return func;
}
string RemoveOperators(string func)
{
return RemoveOperator(RemoveOperator(RemoveOperator(RemoveOperator(func, '*'), '/'), '+'), '-');
}
string RemoveBrackets(string func)
{
while (func.Contains('('))
{
int indexL = func.LastIndexOf('(');
int indexR = 0;
StringBuilder sb = new StringBuilder();
for (int i = indexL + 1; i < func.Length; i++)
{
indexR = i;
if (func[i] == ')')
{
break;
}
sb.Append(func[i]);
}
func = func.Replace("(" + sb.ToString() + ")", RemoveOperators(sb.ToString()));
}
func = RemoveOperators(func);
return func;
}
public double[,] GetResultFromFunc(string func, double min, double dist, int num)
{
func = FormatFunc(func);
double[,] arr = new double[2, num];
for (int i = 0; i < num; i++)
{
arr[0, i] = min + dist * i;
arr[1, i] = Convert.ToDouble(RemoveBrackets(FuncXToFuncNum(func, arr[0, i])));
}
return arr;
}
public double GetResultFromFunc(string func, double x)
{
func = FormatFunc(func);
return Convert.ToDouble(RemoveBrackets(FuncXToFuncNum(func, x)));
}
public List<ResultXY> GetResultList(string func, double min, double dist, int num)
{
func = FormatFunc(func);
List<ResultXY> resultList = new List<ResultXY>();
for (int i = 0; i < num; i++)
{
ResultXY resultXY = new ResultXY();
resultXY.X = min + dist * i;
resultXY.Y = Convert.ToDouble(RemoveBrackets(FuncXToFuncNum(func, resultXY.X)));
resultList.Add(resultXY);
}
return resultList;
}
}
}
可视化计算器
最新推荐文章于 2024-01-15 18:12:33 发布