2016-04-21 60 views
0

表達:C#檢查是否表達是括號有效

「(A [1] + { - 1} *(8-9))」

應該返回真,因爲它是有效地寫這樣的語法。每個左括號在正確的位置右邊靠得更近,所有括號都在合法位置。
我試圖通過一個堆棧做到這一點,我知道我錯了,但我想知道一個相關的方式來解決這個問題。 thx!
我可憐可憐的錯誤代碼:

  string expression = "(a[i]+{-1}*(8-9)) "; 
     Stack<char> expStack = new Stack<char>(); 
     List<char> rightBracketsHolder = new List<char>(); 
     for (int i = 0; i < expression.Length; i++) 
     { 
      if (expression[i] == '{') 
      { 
       expStack.Push('}'); 
       Console.Write("}" + " "); 
      } 
      else if (expression[i] == '(') 
      { 
       expStack.Push(')'); 
       Console.Write(")" + " "); 
      } 
      else if (expression[i] == '[') 
      { 
       expStack.Push(']'); 
       Console.Write("]" + " "); 
      } 
     } 
     Console.WriteLine(); 
     for (int i = 0; i < expression.Length; i++) 
     { 
      if (expression[i] == '}') 
      { 
       rightBracketsHolder.Add('}'); 
       Console.Write(expression[i] + " "); 
      } 
      else if (expression[i] == ')') 
      { 
       rightBracketsHolder.Add(')'); 
       Console.Write(expression[i] + " "); 
      } 
      else if (expression[i] == ']') 
      { 
       rightBracketsHolder.Add(']'); 
       Console.Write(expression[i] + " "); 
      } 
     } 
     Console.WriteLine(); 
     bool stackResult = checkValidity(expStack, rightBracketsHolder); 
     if (stackResult) 
      Console.WriteLine("Expression is Valid."); 
     else 
      Console.WriteLine("\nExpression is not valid."); 
     Console.ReadKey(); 
    } 

    private static bool checkValidity(Stack<char> expStack, List<char> leftBracketsHolder) 
    { 
     Console.WriteLine(); 
     int length = leftBracketsHolder.Count; 
     for (int i = 0; i < length; i++) 
     { 
      if (expStack.Peek().ToString().Contains(leftBracketsHolder.ToString())) 
      { 
       leftBracketsHolder.Remove(expStack.Peek()); 
       expStack.Pop(); 

      } 
     } 
     if (expStack.Count == 0 && leftBracketsHolder.Count ==0) 
     { 
      return true; 
     } 
     return false; 
    } 
} 
+0

我想你應該使用算術表達式分析器。看看這個[問題](http://stackoverflow.com/questions/3972854/parse-math-expression) –

回答

1

此代碼將解決你的目的 -

static void Main(string[] args) 
    { 
     bool error = false; 
     var str = "(a[i]+{-1}*(8-9))"; 
     Stack<char> stack = new Stack<char>(); 
     foreach (var item in str.ToCharArray()) 
     { 
      if (item == '(' || item == '{' || item == '[') 
      { 
       stack.Push(item); 
      } 
      else if(item == ')' || item == '}' || item == ']') 
      { 
       if (stack.Peek() != GetComplementBracket(item)) 
       { 
        error = true; 
        break; 
       } 
      } 
     } 

     if (error) 
      Console.WriteLine("Incorrect brackets"); 
     else 
      Console.WriteLine("Brackets are fine"); 
     Console.ReadLine(); 
    } 

    private static char GetComplementBracket(char item) 
    { 
     switch (item) 
     { 
      case ')': 
       return '('; 
      case '}': 
       return '{'; 
      case ']': 
       return '['; 
      default: 
       return ' '; 
     } 
    } 
0

您需要在交割時彈出的東西堆棧。嘗試下面的代碼。它會在堆棧上推動一個開放的大括號/括號/括號,然後第一個東西會通過相應的關閉從堆棧中彈出。否則它是無效的。如果在遇到關閉時沒有打開堆棧,則該操作無效。如果您在完成時有任何額外的打開,則無效。

我還使用了switch語句而不是if語句,因爲我認爲它更易於閱讀。

using System; 
using System.Collections.Generic; 

public class Program 
{ 
    public static void Main() 
    { 
     string expression = "(a[i]+{-1}*(8-9)) "; 
     bool stackResult = checkValidity(expression); 

     if (stackResult) 
     Console.WriteLine("Expression is Valid."); 
     else 
     Console.WriteLine("\nExpression is not valid."); 
    } 

    private static bool checkValidity(string expression) 
    { 
     Stack<char> openStack = new Stack<char>(); 
     foreach (char c in expression) 
     { 
     switch (c) 
     { 
      case '{': 
      case '(': 
      case '[': 
       openStack.Push(c); 
       break; 
      case '}': 
       if (openStack.Count == 0 || openStack.Peek() != '{') 
       { 
        return false; 
       } 
       openStack.Pop();  
       break; 
      case ')': 
       if (openStack.Count == 0 || openStack.Peek() != '(') 
       { 
        return false; 
       } 
       openStack.Pop();  
       break; 
      case ']': 
       if (openStack.Count == 0 || openStack.Peek() != '[') 
       { 
        return false; 
       } 
       openStack.Pop();  
       break; 
      default: 
       break; 
     } 
     } 
     return openStack.Count == 0; 
    } 
} 
相關問題