2016-07-25 114 views
0

我试图使用Visual Studio 2012以下的C++ 11的代码转换为C#: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,则递归调用进入无限循环。

+0

其中标识'vowels'来自哪里?它也不同于你的C++代码,它通过下面的表达式:'vowelsAllowed - isVowel [pressedKey]' – Jakotheshadows

+1

它看起来不像你在你的递归函数中有一个基本情况。 pressRemaining参数是否应该在任何地方使用?它没有在你的函数中引用或修改。 – Tofystedeth

+1

C++代码不运行。 C#代码不会出于同样的原因运行,并且包含与您的问题无关的显而易见的拼写错误。除了没有运行,C++代码也是一个格式不正确的程序,由于标识符以'_'开始,后跟大写字母。最重要的是,你还没有描述你试图做什么来解决你的问题(你是否使用了调试器?) – Yakk

回答

0

在您尝试的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]); 
     } 
    } 
} 
+0

是的 - 你是对的 - 我正在使用C + +和C# - 非常感谢你 – Peti