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]);
}
}
}
相关文章:
- 模板类转换问题 - 无法推断调用的函数
- 复制构造函数隐式转换问题
- 与模板 (c++) 相关的转换问题
- 将文件复制到自定义位置,存在字符串转换问题
- 从基指针到派生的强制转换问题
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 类型转换问题
- 拆分 pybind11 模块和自动类型转换问题
- 类型转换问题:返回为整数而不是浮点/类型
- 如何解决ostream_iterator转换问题?
- C++转换问题
- 代码 c++ VS 2010 中的 LPCWSTR 到字符串转换问题
- 罗马数字C++编程转换问题
- C++十进制到时间的转换问题
- C++到 C 的转换问题
- C 复合分配和类型转换问题
- CPP预处理器到C预处理器转换问题
- 将数据类型从C#封送至C++的类型转换问题
- COM对象,并返回QVariant强制转换问题
- C 汇编器转换问题