2017-07-31 45 views
1

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

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

你应该问你最喜欢的搜索引擎关于'递归下降解析器'。 –

+2

滚动one..scroll两个...无聊滚动 - 太多的代码。 – Reniuz

+0

@Reniuz将注意力集中在AddandSubstract和DivideAndMultiply函数上...... –

回答