C ++ 11到C#代码转换问题

时间:2016-07-25 18:52:39

标签: c# c++ c++11 visual-studio-2012

我正在尝试使用Visual Studio 2012将以下C ++ 11代码转换为C#:

typedef enum { _A,_B,_C,_D,_E,_F,_G,_H,_I,_J,_K,_L,_M,_N,_O,_1,_2,_3 }  TKeyIdentity;
typedef std::vector<TKeyIdentity const>     TKeyPath;
typedef std::vector<TKeyPath const>         TKeyMap;

const TKeyMap keyPad =
{
    { _H, _L },         // A
    { _I, _K, _M },     // B
    { _F, _J, _L, _N }, // C
    { _G, _M, _O },     // D
     { _H, _N }         // E  
}

const TKeyPath keyPadRoot =
{
    _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _1, _2, _3
};

TraverseKeyPaths( TKeyPath const &keyPath, int pressesRemaining, int vowelsAllowed )
{
    for ( auto pressedKey: keyPath )
    {
          int value = TraverseKeyPaths(keyPad[ pressedKey ],pressesRemaining, vowelsAllowed - isVowel[pressedKey] );    
    }
}

完整的C ++代码可用:http://lucid-motif.blogspot.com/2013/11/coding-puzzle-knight-sequences.html

C#代码:

enum TKeyIdentity { _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _1, _2, _3 };
List<string> keyPadRoot = new List<string> { "_A", "_B", "_C", "_D", "_E", "_F", "_G", "_H", "_I", "_J", "_K", "_L", "_M", "_N", "_O", "_1", "_2", "_3" };

string[] A = new string[] { "_H", "_L" }; //A
string[] B = new string[] { "_I", "_K", "_M" }; //B
string[] C = new string[] { "_F", "_J", "_L", "_N" }; //C
string[] D = new string[] { "_G", "_M", "_O" }; //D

List<string> keyPadMoves = new List<string>();
keyPadMoves.AddRange(A);
keyPadMoves.AddRange(B);
keyPadMoves.AddRange(C);
keyPadMoves.AddRange(D);
.
.

int TraverseKeyPaths(List<string> keyPadRoot, int pressesRemaining, int vowelsAllowed)
{
   foreach (TKeyIdentity pressedKey in Enum.GetValues(typeof(TKeyIdentity)))
   {
      int value = TraverseKeyPaths(keyPadRoot, pressesRemaining, vowelsAllowed);
   }
 }

C#代码未按预期工作。问题在于以下行:

 TraverseKeyPaths(keyPadRoot, pressesRemaining, vowelsAllowed);

我需要将第一个参数作为keyPadMoves传递;但如果我通过keyPadMoves,则递归调用进入无限循环。

1 个答案:

答案 0 :(得分:0)

您尝试的C#等价物中的各种代码似乎与原始C ++代码不对应。 这是一个直接可编译的翻译,假设你有'isVowel&#39;在某处定义:

using System.Collections.Generic;

public enum TKeyIdentity
{
    _A, _B, _C, _D, _E, _F, _G, _H, _I, _J, _K, _L, _M, _N, _O, _1, _2, _3
}

public class TraverseKeyPaths
{
    public readonly List<List<TKeyIdentity>> keyPad = new List<List<TKeyIdentity>>()
    {
        new List<TKeyIdentity> {TKeyIdentity._H, TKeyIdentity._L},
        new List<TKeyIdentity> {TKeyIdentity._I, TKeyIdentity._K, TKeyIdentity._M},
        new List<TKeyIdentity> {TKeyIdentity._F, TKeyIdentity._J, TKeyIdentity._L, TKeyIdentity._N},
        new List<TKeyIdentity> {TKeyIdentity._G, TKeyIdentity._M, TKeyIdentity._O},
        new List<TKeyIdentity> {TKeyIdentity._H, TKeyIdentity._N}
    };

    public readonly List<TKeyIdentity> keyPadRoot = new List<TKeyIdentity>() { TKeyIdentity._A, TKeyIdentity._B, TKeyIdentity._C, TKeyIdentity._D, TKeyIdentity._E, TKeyIdentity._F, TKeyIdentity._G, TKeyIdentity._H, TKeyIdentity._I, TKeyIdentity._J, TKeyIdentity._K, TKeyIdentity._L, TKeyIdentity._M, TKeyIdentity._N, TKeyIdentity._O, TKeyIdentity._1, TKeyIdentity._2, TKeyIdentity._3 };

    public TraverseKeyPaths(List<TKeyIdentity> keyPath, int pressesRemaining, int vowelsAllowed)
    {
        foreach (var pressedKey in keyPath)
        {
            int value = new TraverseKeyPaths(keyPad[(int)pressedKey], pressesRemaining, vowelsAllowed - isVowel[pressedKey]);
        }
    }
}