可视化计算器

<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;
        }
    }
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值