//將可用 char 放入 array 中
 /*public char[] aryChar = {'A', 'B', '2', 'C', 'D', 'E', 'F', '3', 'H', '4', 'J', 'K', '5', 'M', 'N',
          '6', 'P', 'Q', 'R', '7', 'S', 'T', 'U', '9', 'V', 'W', 'X', 'Y', 'Z'};*/

 public char[] aryChar;

 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack) {
   TB_intSerial.Text = "9";
   TB_limit.Text = "5";
   TB_Char.Text = "AB2CDEF3H4JK5MN6PQR7STU9VWXYZ";
  }
 }


 protected void But_submit_Click(object sender, EventArgs e)
 {
   Random rnd = new Random();

   aryChar = TB_Char.Text.ToCharArray();
   int MinValue = 0; //亂數最小值
   int MaxValue = aryChar.Length; //亂數最大值不超過 MaxValue
   int intSerial = Convert.ToInt16(TB_intSerial.Text); //要產生的位數(排除開頭碼)
   int limit = Convert.ToInt32(TB_limit.Text); //總共要幾個序號
   int SuccessCnt = 0;

   string rStr = "";
   Lit_Result.Text = "";

   for (int i = 0; i < limit; i++)
   {
    for (int j = 0; j < intSerial; j++)
    {
     int posit = rnd.Next(MinValue, MaxValue); //取亂數
     int currPosi = posit;
     currPosi = Check(rStr, currPosi); //檢查 char 有無重複在 rStr 中
     rStr = aryChar[currPosi] + rStr;
    }

    //存入資料庫
    //(略)

    //結果
    /*if (存入DB有誤)
    {
     Lit_Result.Text += 錯誤訊息 + ":" + BegChar + rStr + "  ";
    }
    else
    {
     SuccessCnt++; //計算成功筆數
    }*/


    rStr = "";
   }
 }



 //檢查亂數取出的 char 有與其他碼重複,若重複則取新值
 //string tStr:被檢查的 string
 //int tIndex:亂數字元陣列的 index

 public int Check(string tStr, int tIndex)
 {
  int vNewIndex = tIndex;

  if (tStr.IndexOf(aryChar[vNewIndex]) > -1) //tStr中完全不得有重複的 char
  //if (!string.IsNullOrEmpty(tStr) && tStr.Substring(0, 1) == Convert.ToString(aryChar[vNewIndex])) //新取出的 char 有無與相鄰碼重複
  {
   vNewIndex = ((vNewIndex + 1) % aryChar.Length); //若重複,則取陣列中下一個 char
   vNewIndex = Check(tStr, vNewIndex); //如果tStr中完全不得有重複的 char 需再次檢查有無重複,否則不需要再檢查。
  }
  else
  {
   return vNewIndex;
  }

  return vNewIndex;
 }
相關文章