在C#中比较一个2D和一个1D阵列?

时间:2012-05-26 10:51:25

标签: c#

我只想编写用于比较一维数组和二维数组的代码......我正在编写一个编译器,想要比较包含我的代码的一维数组和其他我做过符号的二维数组表。 我已经编写了代码,但它没有用。

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

2 个答案:

答案 0 :(得分: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");
        }
    }
}

答案 1 :(得分:0)

你的方法太慢O(n * m * l)(其中 l 是文字的长度, n &amp; m 符号表的尺寸。)

将符号表转换为已排序的索引表会给你一个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;
        }


    }
}