2015-05-10 71 views
-1

我想打一個自動遞增的字母數字,如果我按下一個按鈕,這僅僅是輸出自動遞增字母數字的PHP,JavaScript的

,還有一個輸出AA001,然後我按下一個按鈕再次出現是輸出AA002

+0

你怎麼做到的? –

+0

你是如何獲得按鈕按下的第一個值? –

+0

你忘了問我們一個問題,或者告訴我們你目前所做的最佳努力的任何代碼。此外,你沒有解釋你當前的代碼在哪裏失敗,或者*如何失敗。您是否希望我們幫助解決您的問題,或只是以某種方式鼓勵您? –

回答

0

下面的代碼將生成一個令牌,您可以前進一個。它使用一些小類來實現模塊化。您附加到該按鈕的功能將調用標記的下一個功能,如底部所示。

//Element in a big token 
 
function Increment(startval, endval) { 
 
    this.start = startval.charCodeAt(0); 
 
    this.cur = this.start; 
 
    this.end = endval.charCodeAt(0); 
 

 
    //Returns the current value 
 
    this.get = function() { 
 
    if (this.cur <= this.end) { 
 
     return String.fromCharCode(this.cur); 
 
    } else { 
 
     return null; 
 
    } 
 
    } 
 

 
    //Advances the value we will show 
 
    this.next = function() { 
 
    this.cur += 1; 
 
    return this.get(); 
 
    } 
 

 
    //Reset it to the beginning 
 
    this.reset = function() { 
 
    this.cur = this.start; 
 
    } 
 
} 
 

 
function Token() { 
 
    this.token = [ 
 
    new Increment("A", "Z"), 
 
    new Increment("A", "Z"), 
 
    new Increment("0", "9"), 
 
    new Increment("0", "9"), 
 
    new Increment("0", "9") 
 
    ]; 
 
    this.get = function() { 
 
    return this.token.map(function(cur) { 
 
     return cur.get(); 
 
    }).join(""); 
 
    } 
 
    this.next = function() { 
 
    //From the back to the front 
 
    for (var i = this.token.length - 1; i >= 0; i--) { 
 
     if (this.token[i].next() == null) { 
 
     //If we exhausted all possible values, continue 
 
     this.token[i].reset(); 
 
     } else { 
 
     //Until we advance one that has still values left 
 
     break; 
 
     } 
 
    } 
 
    return this.get(); 
 
    } 
 
} 
 

 
//Now we are just showing off... 
 
var a = new Token(); 
 
for (var i = 0; i < 15; i++) { 
 
    console.log(a.next()); 
 
}

0
const getNextAlphaString = function (str) { 
    "use strict"; 
    str=str.toUpperCase(); 
    let chars; 
    chars = str.split(""); 
    const strLen = str.length; 
    let continueIncermenting = true; 
    for (let i = strLen - 1; i >= 0; i = i - 1) { 
     let asciiVal; 
     asciiVal = chars[i].charCodeAt(0); 
     if (isNaN(asciiVal)) { 
      return str; 
     } 


     if (continueIncermenting === true) { 
      if (asciiVal >= 48 && asciiVal < 57) { 
       chars[i] = String.fromCharCode(asciiVal + 1); 
       continueIncermenting = false; 
       break; 
      } else if (asciiVal == 57) { 
       chars[i] = '0'; 
       continueIncermenting = true; 
      } 
      if (asciiVal >= 65 && asciiVal < 90) { 
       chars[i] = String.fromCharCode(asciiVal + 1); 
       continueIncermenting = false; 
       break; 
      } else if (asciiVal == 90) { 
       chars[i] = String.fromCharCode(65); 

       continueIncermenting = true; 
      } 
     } else { 
      if (asciiVal == 90) { 
       continueIncermenting = true; 
       chars[i] = String.fromCharCode(65); 
      } 
      if (asciiVal == 57) { 
       continueIncermenting = true; 
       chars[i] = '0'; 
      } 
     } 
    } 
    if (continueIncermenting === true) { 
     let firstAcii = chars[0].charCodeAt(0); 
     if (isNaN(firstAcii)) { 
      return str; 
     } 
     if ((firstAcii >= 65 && firstAcii <= 90) || (firstAcii >= 97 && firstAcii <= 122)) { 
      return 'A' + chars.join('').toUpperCase(); 
     } 
     if (firstAcii >= 48 && firstAcii <= 57) { 
      return '0' + chars.join('').toUpperCase(); 
     } 
    } 
    return chars.join('').toUpperCase(); 

}; 

結果將是如下:
ab1zde ---> AB1ZDF
abcdzz ---> ABCEAA
ZZZZZ ---> AAAAAA
abcyzz ---> ABCZAA
9000 ---> 9001

1

這不使用任何'聰明'的技巧,但更短,更容易理解和調整。它也使用數字,因此它不會像字符串一樣快速地增長字符串的長度。它從0到9然後是a-z遞增,但是它意味着你應該以'a'開始。輸出總是以字母字符開始,只要你輸入也以字母字符開始,因此它可以作爲PHP變量名

var nextVarName = function(str) { 
    // Position of char to change. 
    var change = str.length - 1; 
    // The value of that char. 
    var change_char = str[change]; 
    // Number of zeros to append when flipping. 
    var zeros = 0; 
    // Iterate backwards while there's a z (flipping). 
    while (change_char == 'z') { 
    // Increase the length of appended zeros 
    zeros++; 
    // Move the char to change back. 
    change_char = str[--change]; 
    } 
    if (change_char == undefined) { 
    // Full flip - string increases in length. 
    str = 'a' + Array(str.length + 1).join("0"); 
    } else { 
    // Normal increment with partial flip and 9->a handling. 
    str = str.substr(0, change) 
     + (change_char == '9' ? 'a' : String.fromCharCode(str.charCodeAt(change) + 1)) 
     + Array(zeros + 1).join('0'); 
    } 
    return str; 
}; 

var vname = 'a'; 
for (var i = 0; i < 5; i++) { 
    vname = nextVarName(vname); 
    console.log(vname); 
} 

結果將如下:

ž - - > A0

9Z ---> A0(意外輸入)

ab1zde ---> ab1zdf

abcdzz ---> abce00

ZZZZZ ---> A00000

abcyzz ---> abcz00

9000 ---> 9001(意外輸入)

https://jsfiddle.net/e20kfvky/

多少變量的時間表(即創建的每個長度如下:

1:26,2:936,3:33696,4:1213056 ... n:36^n - 10 * 36^(n - 1)