添加指数

时间:2017-07-31 12:06:25

标签: c#

我做了一个表达式评估器,但我只包括:加法和减法和乘法和除法和括号解析器我想添加指数“^”和之后的三角函数:正弦,余弦和正切但我不知道从哪里开始......这就是我现在所做的:

public static string RemoveBrackets(string text)
    {
        while (text.Contains('(') && text.Contains(')'))
        {
            int openIndex = 0;
            int closeIndex = 0;

            for(int i = 0; i < text.Length; ++i)
            {
                if(text[i] == '(')
                {
                    openIndex = i;
                }
                if(text[i] == ')')
                {
                    closeIndex = i;

                    text = text.Remove(openIndex, closeIndex - openIndex + 1).Insert(openIndex, ResolveBrackets(openIndex, closeIndex, text));

                    break;
                }
            }
        }

        for(int i = 1; i < text.Length; ++i)
        {
            if(text[i] == '-' && (text[i - 1] == '*' || text[i - 1] == '/'))
            {
                for(int j = i - 1; j >= 0; --j)
                {
                    if(text[j] == '+')
                    {
                        StringBuilder text1 = new StringBuilder(text);
                        text1[j] = '-';
                        text = text1.ToString();
                        text = text.Remove(i, 1);
                        break;
                    }
                    else if(text[j] == '-')
                    {
                        StringBuilder text1 = new StringBuilder(text);
                        text1[j] = '+';
                        text = text1.ToString();
                        text = text.Remove(i, 1);
                        break;
                    }
                }
            }
        }

        for (int i = 0; i < text.Length; ++i)
        {
            if (text[i] == '-' && (text[i - 1] == '-' || text[i - 1] == '+'))
            {
                if(text[i - 1] == '-')
                {
                    StringBuilder text1 = new StringBuilder(text);
                    text1[i] = '+';
                    text = text1.ToString();
                    text = text.Remove(i - 1, 1);
                }
                else
                {
                    StringBuilder text1 = new StringBuilder(text);
                    text1[i] = '-';
                    text = text1.ToString();
                    text = text.Remove(i - 1, 1);
                }
            }
            else if (text[i] == '+' && (text[i - 1] == '-' || text[i - 1] == '+'))
            {
                if (text[i - 1] == '-')
                {
                    StringBuilder text1 = new StringBuilder(text);
                    text1[i] = '-';
                    text = text1.ToString();
                    text = text.Remove(i - 1, 1);
                }
                else
                {
                    StringBuilder text1 = new StringBuilder(text);
                    text1[i] = '+';
                    text = text1.ToString();
                    text = text.Remove(i - 1, 1);
                }
            }
        }

        if (text[0] == '-')
        {
            text = '0' + text;
        }

        return Calculate(text);
    }

    public static string ResolveBrackets(int openindex, int closeindex, string text)
    {
        string BracketAnswer = evaluate(text.Substring(openindex + 1, closeindex - openindex - 1));

        return BracketAnswer;
    }

    public static double DivideAndMultiply(string text)
    {
        string[] expr = text.Split('*');
        List<string> textList = new List<string>();

        for (int i = 0; i < expr.Length; ++i)
        {
            textList.Add(expr[i]);
            if (i != expr.Length - 1)
            {
                textList.Add("*");
            }

        }

        for (int i = 0; i < textList.Count; ++i)
        {
            if (textList[i].Contains('/') && textList[i].Length > 1)
            {
                string[] textPart = textList[i].Split('/');

                textList.RemoveAt(i);

                for (int j = textPart.Length - 1; j >= 0; --j)
                {
                    textList.Insert(i, textPart[j]);
                    if (j != 0)
                    {
                        textList.Insert(i, "/");
                    }

                }
            }
        }

        double total;

        if (textList[0].Contains('*') || textList[0].Contains('/'))
        {
            total = textList[0] == "" ? 0 : DivideAndMultiply(textList[0]);
        }
        else
        {
            total = Convert.ToDouble(textList[0]);
        }

            /// 7:30 min tutorial
            for (int i = 2; i < textList.Count; i += 2)
        {
            if (textList[i - 1] == "/")
            {
                total /= Convert.ToDouble(textList[i]);
            }
            else if (textList[i - 1] == "*")
            {
                total *= Convert.ToDouble(textList[i]);
            }
        }

        return total;
    }

    public static double AddAndSubstract(string text)
    {
        string[] expr = text.Split('-');
        List<string> textList = new List<string>();

        for(int i = 0; i < expr.Length; ++i)
        {
            textList.Add(expr[i]);
            if(i != expr.Length - 1)
            {
                textList.Add("-");
            }

        }

        for(int i = 0; i < textList.Count; ++i)
        {
            if (textList[i].Contains('+') && textList[i].Length > 1)
            {
                string[] textPart = textList[i].Split('+');

                textList.RemoveAt(i);

                for (int j = textPart.Length - 1; j >= 0; --j)
                {
                    textList.Insert(i, textPart[j]);
                    if (j != 0)
                    {
                        textList.Insert(i, "+");
                    }

                }
            }
        }

        //double total = Add(expr[0]);
        double total = expr[0] == "" ? 0 : DivideAndMultiply(textList[0]);

        for (int i = 2; i < textList.Count; i += 2)
        {
            if(textList[i - 1] == "-")
            {
                total -= DivideAndMultiply(textList[i]);
            }
            else if(textList[i - 1] == "+")
            {
                total += DivideAndMultiply(textList[i]);
            }
        }


        return total;
    }

有关我应该阅读的内容的任何建议吗?

2 个答案:

答案 0 :(得分:0)

阅读关于程序化部分 - 数学课

https://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx

找一些关于科学计算器的tuturial。这应该给出一些见解。

答案 1 :(得分:0)

使用system.math可以做到这一点