C++11到C#代码转换问题
本文关键字:转换 问题 代码 C++11 | 更新日期: 2023-09-27 17:58:36
我正在尝试使用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]);
}
}
}