1. 程式人生 > >C++ 自定義訂單號

C++ 自定義訂單號

pan 自定義格式 tox and system randn zab esp include

自定義訂單號

 1 #include<iostream>
 2 #include<stack>
 3 #include <time.h>
 4 #include <sys/timeb.h>
 5 #include <string>
 6 #include <sstream>
 7 
 8 using namespace std;
 9 
10 //獲取時間戳,精確到毫秒
11 long long getTimeStamp()
12 {
13     timeb t;
14     ftime(&t);
15     return
t.time * 1000 ;//+ t.millitm; 16 } 17 18 //將時間戳轉換為自定義格式 19 const string m_num = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ"; 20 string tenToX(string& str) 21 { 22 long long n = getTimeStamp(); 23 stack<long long> s; 24 25 while(n) 26 { 27 s.push(n%52); 28 29
n/=52; 30 } 31 while(!s.empty()) 32 { 33 // cout<<m_num[s.top()]; 34 str+=m_num[s.top()]; 35 s.pop(); 36 } 37 return str; 38 } 39 40 //將數字轉換為4位字符串 41 string transform(int num) 42 { 43 string res; 44 stringstream ss; 45 ss<<num;
46 ss>>res; 47 if(num<10) 48 { 49 res="000"+res; 50 } 51 else if(num<100) 52 { 53 res ="00"+res; 54 } 55 else if(num<1000) 56 { 57 res="0"+res; 58 } 59 60 return res; 61 } 62 63 //+時間戳轉換為自定義格式+4位數自增+4位數隨機數 64 int main() 65 { 66 int increment=1001; 67 string str=""; 68 for(int i=0;i<10000;i++) 69 { 70 int randnum=1000+rand()%1000; 71 string tmpstr; 72 tenToX(tmpstr); 73 if(tmpstr==str) 74 { 75 increment++; 76 } 77 else 78 { 79 increment=1001; 80 } 81 str=tmpstr; 82 83 tmpstr = tmpstr + to_string(increment)+to_string((randnum)); 84 cout<<tmpstr<<endl; 85 } 86 system("pause"); 87 return 0; 88 }

C++ 自定義訂單號