2016-06-09 60 views
2

我的輸入是兩個字符串。我需要遍歷這兩個字符串之間的每個值(包括值本身)。迭代兩個字符串之間的每個值

例如從AACZ(例如AA AB AC .. AY AZ BA BB .. CY CZ

合法值應該是可能的從0 - 9和AZ

迭代從01AD應導致01 02 .. 09 0A .. 0Z .. 10 11 12 .. 9Z AA AB AC AD

字符串長度應可變但開始和結束始終具有相同的長度

AAA to ZZZ有效

目前我完全卡住了。有什麼建議麼?

這是我目前的做法(我有限的字符串lengt 6和左填充值「0」,例如AAA成爲000AAA

value1 = "A01" 
    value2 = "A20" 
    value1 = value1.PadLeft(6, "0") 
    value2 = value2.PadLeft(6, "0") 

    Dim start1 As Integer = Asc(value1.Substring(0, 1)) 
    Dim start2 As Integer = Asc(value1.Substring(1, 1)) 
    Dim start3 As Integer = Asc(value1.Substring(2, 1)) 
    Dim start4 As Integer = Asc(value1.Substring(3, 1)) 
    Dim start5 As Integer = Asc(value1.Substring(4, 1)) 
    Dim start6 As Integer = Asc(value1.Substring(5, 1)) 

    Dim stop1 As Integer = Asc(value2.Substring(0, 1)) 
    Dim stop2 As Integer = Asc(value2.Substring(1, 1)) 
    Dim stop3 As Integer = Asc(value2.Substring(2, 1)) 
    Dim stop4 As Integer = Asc(value2.Substring(3, 1)) 
    Dim stop5 As Integer = Asc(value2.Substring(4, 1)) 
    Dim stop6 As Integer = Asc(value2.Substring(5, 1)) 

    For p1 As Integer = start1 To stop1 
     For p2 As Integer = start2 To stop2 
     For p3 As Integer = start3 To stop3 
      For p4 As Integer = start4 To stop4 
      For p5 As Integer = start5 To stop5 
       For p6 As Integer = start6 To stop6 

       Dim result as string = Convert.ToChar(p1) &Convert.ToChar(p2) & Convert.ToChar(p3) & Convert.ToChar(p4) & Convert.ToChar(p5) & Convert.ToChar(p6) 
       Console.WriteLine(result) 

       Next 
      Next 
      Next 
     Next 
     Next 
    Next 

這個工程相當不錯000001009999但棘手的問題是,如果我有000A01000A20。在第一次運行start6 equals 1stop6 equals 0的用於循環退出。

+1

@TimSchmelter我想避免因爲我當前的代碼是可怕的,但確定。 –

+0

你的意思是start6等於1,** stop6 **等於0,對吧? – Han

+0

@ Handoko.chen fixed,thx –

回答

2

這是一個有點棘手,但以爲我是來一個想法對待NU後以基數36的數字表示。我不寫代碼來將base-n數字轉換爲另一個base-n,但是我在StackOverflow中找到了它。

我做了2項目:

  1. 一個C#類庫項目中,我把基地-N轉換器,我發現。這個類庫將包含在第二個項目中。
  2. 一個VB控制檯項目來測試你的問題。

首先將開始和結束編號從base-36轉換爲base-10。然後做一個從開始號碼到結束號碼的循環。在循環體中,將迭代從base-10轉換爲base-36。

C#for base-n轉換器。項目名稱是BaseN。不要忘記在第二個項目中包含這個項目。

using System; 

namespace BaseN 
{ 
    public class BaseConverter 
    { 
     /// <summary> 
     /// Converts the given decimal number to the numeral system with the 
     /// specified radix (in the range [2, 36]). 
     /// </summary> 
     /// <param name="decimalNumber">The number to convert.</param> 
     /// <param name="radix">The radix of the destination numeral system (in the range [2, 36]).</param> 
     /// <returns></returns> 
     public static string DecimalToArbitrarySystem(long decimalNumber, int radix) 
     { 
      const int BitsInLong = 64; 
      const string Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

      if (radix < 2 || radix > Digits.Length) 
       throw new ArgumentException("The radix must be >= 2 and <= " + Digits.Length.ToString()); 

      if (decimalNumber == 0) 
       return "0"; 

      int index = BitsInLong - 1; 
      long currentNumber = Math.Abs(decimalNumber); 
      char[] charArray = new char[BitsInLong]; 

      while (currentNumber != 0) 
      { 
       int remainder = (int)(currentNumber % radix); 
       charArray[index--] = Digits[remainder]; 
       currentNumber = currentNumber/radix; 
      } 

      string result = new String(charArray, index + 1, BitsInLong - index - 1); 
      if (decimalNumber < 0) 
      { 
       result = "-" + result; 
      } 

      return result; 
     } 

     /// <summary> 
     /// Converts the given number from the numeral system with the specified 
     /// radix (in the range [2, 36]) to decimal numeral system. 
     /// </summary> 
     /// <param name="number">The arbitrary numeral system number to convert.</param> 
     /// <param name="radix">The radix of the numeral system the given number 
     /// is in (in the range [2, 36]).</param> 
     /// <returns></returns> 
     public static long ArbitraryToDecimalSystem(string number, int radix) 
     { 
      const string Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 

      if (radix < 2 || radix > Digits.Length) 
       throw new ArgumentException("The radix must be >= 2 and <= " + 
        Digits.Length.ToString()); 

      if (String.IsNullOrEmpty(number)) 
       return 0; 

      // Make sure the arbitrary numeral system number is in upper case 
      number = number.ToUpperInvariant(); 

      long result = 0; 
      long multiplier = 1; 
      for (int i = number.Length - 1; i >= 0; i--) 
      { 
       char c = number[i]; 
       if (i == 0 && c == '-') 
       { 
        // This is the negative sign symbol 
        result = -result; 
        break; 
       } 

       int digit = Digits.IndexOf(c); 
       if (digit == -1) 
        throw new ArgumentException(
         "Invalid character in the arbitrary numeral system number", 
         "number"); 

       result += digit * multiplier; 
       multiplier *= radix; 
      } 

      return result; 
     } 
    } 
} 

VB項目。項目名稱是ConsoleApplication1。

Imports BaseN 

Module Module1 

    Sub Main() 

     Dim value1 = "A01" 
     Dim value2 = "A20" 

     For i = BaseConverter.ArbitraryToDecimalSystem(value1, 36) To BaseConverter.ArbitraryToDecimalSystem(value2, 36) 
      Console.WriteLine(BaseConverter.DecimalToArbitrarySystem(i, 36)) 
     Next 

     Console.WriteLine("Press any key...") 
     Console.ReadKey(True) 

    End Sub 

End Module 

結果。

A01 
A02 
A03 
A04 
A05 
A06 
A07 
A08 
A09 
A0A 
A0B 
A0C 
A0D 
A0E 
A0F 
A0G 
A0H 
A0I 
A0J 
A0K 
A0L 
A0M 
A0N 
A0O 
A0P 
A0Q 
A0R 
A0S 
A0T 
A0U 
A0V 
A0W 
A0X 
A0Y 
A0Z 
A10 
A11 
A12 
A13 
A14 
A15 
A16 
A17 
A18 
A19 
A1A 
A1B 
A1C 
A1D 
A1E 
A1F 
A1G 
A1H 
A1I 
A1J 
A1K 
A1L 
A1M 
A1N 
A1O 
A1P 
A1Q 
A1R 
A1S 
A1T 
A1U 
A1V 
A1W 
A1X 
A1Y 
A1Z 
A20 
Press any key... 

我沒有把前導零,我相信你不會有太大的麻煩自己添加前導零。

通過引用帕維爾Vladov:

Quickest way to convert a base 10 number to any base in .NET? http://www.pvladov.com/2012/07/arbitrary-to-decimal-numeral-system.html

+1

雖然我編碼的時候是關於基數36的數字系統,但是因爲我認爲它會變得複雜,所以就駁回了這個想法。輝煌的解決方案,thx。關於前導零「BaseConverter.DecimalToArbitrarySystem(i,36).PadLeft(value1.Length,」0「)' –

+1

感謝Pavel的base-n轉換器。這很快。 – Han

相關問題