C# 每次使用新值迭代 If 语句

C# Iterating an If statement with new value each time

提问人:HipPriest 提问时间:10/13/2014 最后编辑:Paul RoubHipPriest 更新时间:10/23/2014 访问量:215

问:

是否可以在每次迭代中迭代具有新值的嵌套 if 语句?我正在尝试构建一个一维元胞自动机(对于家庭作业,我不能否认它),并且我对 C# 完全陌生,因为以下代码无疑可以确保。我试图使用最直接、最基本的 DIY 方法创建这个程序,但自己陷入了困境。

比如说,我有一串长度为 8 的 1 和 0

string y;
y = "11110000";

我想将此设置分解为 8 个子字符串集,每组 3 个,每个子字符串集由 y 中的一个值以及其两侧的单个值组成。所以从 0 开始算,第 3 组是 110,第 7 组是 001。但是,子字符串只会提供第 1 到第 6 组,因为我无法根据自己的喜好将它们循环到 y,所以我定义了以下内容-

y1=y.Substring(7,1)+y+y.Substring(0,1);

使用 y1,我能够取出所有必要的子字符串。这些基本上定义如下——

string a0, a1, a2, a3, a4, a5, a6, a7;
                    a0 = y1.Substring(0, 3);
                    a1 = y1.Substring(1, 3);
                    a2 = y1.Substring(2, 3);
                    a3 = y1.Substring(3, 3);
                    a4 = y1.Substring(4, 3);
                    a5 = y1.Substring(5, 3);
                    a6 = y1.Substring(6, 3);
                    a7 = y1.Substring(7, 3);

下一代元胞自动机的规则由用户决定,也就是说,用户可以选择是否使用子字符串,例如所有迭代的 111->0 或 1。我以以下方式为每个子字符串使用了(非常多的)if 表

                     {
                        if (a0=="000")
                    {
                        Console.Write(a);
                        }
                        else if (a0=="001")
                        {
                            Console.Write(b);
                        }
                        else if (a0 =="010")
                        {
                            Console.Write(c);
                        }
                        else if (a0 == "011")
                        {
                            Console.Write(d);
                        }
                        else if (a0 == "100")
                        {
                            Console.Write(e);
                        }
                        else if (a0 == "101")
                        {
                            Console.Write(f);
                        }
                        else if (a0 == "110")
                        {
                            Console.Write(g);
                        }
                        else if (a0 == "111")
                        {
                            Console.Write(h);
                        }
                    }

其中 A,B,C,D,E,F,G,H 是整数,是用户选择的规则。例如,假设用户决定每个集合 000 应产生 1 值,则 a=1。b 对应于 {0,0,1},c 对应于 {0,1,0},依此类推。然而,这种方法相当明显的问题是,我最终只有 1 代我无法得到的 ints。我很想用这个新一代(转换为字符串)替换 y1。如果这是不可能的,请告诉我!

这个链接也可以把事情弄清楚一点

C# visual-studio-2012 IF-语句 蜂窝自动机

评论

0赞 LordTitiKaka 10/13/2014
尝试使用 int.parse 和 later 。ToString 和 than 您可以使用某种运行索引
0赞 Evil Dog Pie 10/13/2014
这不是你问题的答案,但你采取了错误的方法。尝试重新阅读您发布的链接并了解元胞自动机背后的数学原理。老实说,这比你做的要容易得多!
0赞 HipPriest 10/13/2014
我毫不怀疑没有更简单的方法,但对我来说,这是最直观的。我真的非常非常不熟悉 C# 和整个编程,如果表格有点像我的作案手法(拉丁语?)(墨西哥语)。

答:

0赞 Scott Morken 10/23/2014 #1

以下是您获得 A+ :D 的方法

  private static int[,] HipPriestsHomework()
    {
        string y = "11110000";
        Console.WriteLine(y);
        var rules = new[]
        {
            new {pattern = 0, result = 0},
            new {pattern = 1, result = 1},
            new {pattern = 2, result = 1},
            new {pattern = 3, result = 1},
            new {pattern = 4, result = 1},
            new {pattern = 5, result = 0},
            new {pattern = 6, result = 0},
            new {pattern = 7, result = 0},
        };
        Dictionary<int, int> rulesLookup = new Dictionary<int, int>();
        foreach(var rule in rules)
        {
            rulesLookup.Add(rule.pattern, rule.result);
        }

        int numGenerations = 10;
        int inputSize = y.Length;
        int[,] output = new int[numGenerations, inputSize];

        int[] items = new int[y.Length];
        for(int inputIndex = 0; inputIndex< y.Length; inputIndex++)
        {
            string token = y.Substring(inputIndex, 1);
            int item = Convert.ToInt32(token);
            items[inputIndex] = item;
        }

        int[] working = new int[items.Length];
        items.CopyTo(working, 0);
        for (int generation = 0; generation < numGenerations; generation++)
        {
            for (uint y_scan = 0; y_scan < items.Length; y_scan++)
            {
                int a = items[(y_scan - 1) % items.Length];
                int b = items[y_scan % items.Length];
                int c = items[(y_scan + 1) % items.Length];
                int pattern = a << 2 | b << 1 | c;
                var match = rules[pattern];
                output[generation, y_scan] = match.result;
                working[y_scan] = match.result;
                Console.Write(match.result);
            }
            working.CopyTo(items, 0);
            Console.WriteLine();
        }

        return output;
    }