C++ 11 到 C# 代码的转换问题
C++ 11 to C# code Conversion issue
我正在尝试使用 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
,递归调用将进入无限循环。
您尝试的 C# 等效代码中有各种代码似乎与原始 C++ 代码不对应。
这是一个直接可编译的翻译,假设您在某处定义了 'isVowel':
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]);
}
}
}
我正在尝试使用 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
,递归调用将进入无限循环。
您尝试的 C# 等效代码中有各种代码似乎与原始 C++ 代码不对应。 这是一个直接可编译的翻译,假设您在某处定义了 'isVowel':
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]);
}
}
}