C++ 自定义订单号

自定义订单号

 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 }

 

转载于:https://www.cnblogs.com/tinghaiku/p/10221976.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值