2012-05-26 39 views
-3

我只是想編寫比較一維數組與二維數組的代碼......我正在編寫一個編譯器,並且想要比較一個包含我的代碼的一維數組和其中我已經創建了一個符號表的一維數組。 我寫了代碼,但它不工作。在C#中比較一個2D和一個一維數組?

for (int x = 0; x < symboltable1.Length; x++) 
{ 
    for (int y = 0; y < symboltable1.Length; y++) 
    { 
     for (int z = 0; z < text.Length; z++) 
     { 
      if (symboltable1[x,y] == text[z]) 
       listBox2.Items.Add(text[z]); 
      else 
       MessageBox.Show("poor"); 
     } 
    } 
} 
+4

之前,我們可以幫你,你需要告訴我們你想在一維和二維數組之間的比較要實現什麼來幫助我們。 – mauris

+1

順便說一句,如果這些2D - 1D數組有很多元素,你應該考慮切換到某種散列表... – digEmAll

+0

如果你想比較對象,你應該使用object.equals(object2) – warbio

回答

0

你的方法太慢O(N * M * L)(其中是文本的長度,ñ & 你的符號表的尺寸。)

將您的符號表轉換爲排序後的索引表會給你一個O(l * log(n * m)),並且使用散列表會給你一個幾乎O(l)。

我實現了三個辦法只是爲了好玩:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace test 
{ 
    class Program 
    { 
     class IndexElement:IComparable<IndexElement> 
     { 
      public string value; 
      public int x; 
      public int y; 
      public IndexElement(string v,int x,int y) 
      { 
       this.value = v; 
       this.x = x; 
       this.y = y; 
      } 

      public int CompareTo(IndexElement other) 
      { 
       return value.CompareTo(other.value); 
      } 
     } 

     struct Position 
     { 
      public int x; 
      public int y; 
      public Position(int x, int y) 
      { 
       this.x = x; 
       this.y = y; 
      } 
     } 
     static void Main(string[] args) 
     { 
      string[,] symbols = new string[,] { { "if", "else" }, { "for", "foreach" }, { "while", "do" } }; 
      string[] text = new string[] { "for", "int", "in", "if", "then" }; 
      Dictionary<string, Position> dictionary = BuildDict(symbols); 
      IndexElement[] index = BuildIndex(symbols); 
      Console.WriteLine("Brute:"); 
      foreach (string s in CompareUsingBrute(text, symbols)) 
      { 
       Console.WriteLine(s); 
      } 
      Console.WriteLine("Dict:"); 
      foreach (string s in CompareUsingIndex(text, dictionary)) 
      { 
       Console.WriteLine(s); 
      } 
      Console.WriteLine("Indexing:"); 
      foreach (string s in CompareUsingDictionary(text, index)) 
      { 
       Console.WriteLine(s); 
      } 
      Console.ReadKey(); 
     } 

     private static List<string> CompareUsingBrute(string[] text, string[,] symbols) 
     { 
      List<string> res = new List<string>(); 
      for (int x = 0; x < symbols.GetLength(0); x++) 
      { 
       for (int y = 0; y < symbols.GetLength(1); y++) 
       { 
        for (int z = 0; z < text.Length; z++) 
        { 
         if (symbols[x, y] == text[z]) 
          res.Add(text[z]);       
        } 
       } 
      } 
      return res; 

     } 

     private static List<string> CompareUsingDictionary(string[] text, IndexElement[] index) 
     { 
      List<string> res = new List<string>(); 
      foreach (string s in text) 
      { 
       if (Array.BinarySearch<IndexElement>(index, new IndexElement(s, 0, 0)) >= 0) 
       { 
        res.Add(s); 
       } 
      } 
      return res; 
     } 

     private static IndexElement[] BuildIndex(string[,] symbols) 
     { 
      IndexElement[] res = new IndexElement[symbols.Length]; 
      int index = 0; 
      for (int i = 0; i < symbols.GetLength(0); i++) 
      { 
       for (int j = 0; j < symbols.GetLength(1); j++) 
       { 
        res[index] = new IndexElement(symbols[i, j], i, j); 
        index++; 
       } 

      } 
      Array.Sort(res); 
      return res; 
     } 

     private static List<string> CompareUsingIndex(string[] text, Dictionary<string, Position> dictionary) 
     { 
      List<string> res = new List<string>(); 
      foreach (string s in text) 
      { 
       Position p; 
       if (dictionary.TryGetValue(s,out p)) 
       { 
        res.Add(s); 
       } 
      } 
      return res; 
     } 

     private static Dictionary<string, Position> BuildDict(string[,] symbols) 
     { 
      Dictionary<string, Position> res = new Dictionary<string, Position>(); 
      for (int i = 0; i < symbols.GetLength(0); i++) 
      { 
       for (int j = 0; j < symbols.GetLength(1); j++) 
       { 
        res.Add(symbols[i, j], new Position(i, j)); 
       } 
      } 
      return res; 
     } 


    } 
} 
+0

謝謝你非常多的幫助...我可以知道如何在列表框中添加res []的元素? –

+0

foreach(IndexElement e in res){Listbox.Items.Add(e.Value);} –

3

我想你的錯誤可能是在檢查數組長度。
試試這個:

for (int x = 0; x < symboltable1.GetLength(0); x++) 
{ 
    for (int y = 0; y < symboltable1.GetLength(1); y++) 
    { 
     for (int z = 0; z < text.Length; z++) 
     { 
      if (symboltable1[x,y] == text[z]) 
       listBox2.Items.Add(text[z]); 
      else 
       MessageBox.Show("poor"); 
     } 
    } 
} 
+0

no ..它genetrats一個錯誤,我們可以改變布爾到字符串和它的正確的原因在邏輯上v正在實現循環錯誤 –

相關問題