码迷,mamicode.com
首页 > Windows程序 > 详细

C#根据规则生成6位随机码

时间:2015-02-09 18:02:31      阅读:573      评论:0      收藏:0      [点我收藏+]

标签:

  1     #region 获得6位优惠码 zhy
  2     public static string CreatePromoCode(string code)
  3     {
  4         if (code == "")
  5         {
  6             return "a00001";
  7         }
  8         else
  9         {
 10             string new_code = "";
 11             new_code = CreateGrapheme(code);
 12             new_code += CreateNumber(code);
 13             return new_code;
 14         }
 15     }
 16     #endregion
 17 
 18     #region 拼接字母 zhy
 19     public static string CreateGrapheme(string code)
 20     {
 21         string new_code = "";
 22         //获取字母
 23         string grapheme = GetGrapheme(code);
 24         //获得数字
 25         string num = GetNumber(code);
 26         //最后一个字母为z从新拼接否则获取该字母下一个字母拼接
 27         if (grapheme.Substring(grapheme.Length - 1) == "Z")
 28         {
 29             switch (grapheme.Length)
 30             {
 31                 case 1:
 32                     if (Convert.ToInt32(num) >= 99999)
 33                     {
 34                         new_code = "AA";
 35                     }
 36                     else
 37                     {
 38                         new_code += grapheme;
 39                     }
 40                     break;
 41                 case 2:
 42                     if (Convert.ToInt32(num) >= 9999)
 43                     {
 44                         new_code = "AAA";
 45                     }
 46                     else
 47                     {
 48                         new_code += grapheme;
 49                     }
 50                     break;
 51                 case 3:
 52                     if (Convert.ToInt32(num) >= 999)
 53                     {
 54                         new_code = "AAAA";
 55                     }
 56                     else
 57                     {
 58                         new_code += grapheme;
 59                     }
 60                     break;
 61                 case 4:
 62                     if (Convert.ToInt32(num) >= 99)
 63                     {
 64                         new_code = "AAAAA";
 65                     }
 66                     else
 67                     {
 68                         new_code += grapheme;
 69                     }
 70                     break;
 71             }
 72         }
 73         else
 74         {
 75             switch (grapheme.Length)
 76             {
 77                 case 1:
 78                     if (Convert.ToInt32(num) >= 99999)
 79                     {
 80                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
 81                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
 82                         num = "00000";
 83                     }
 84                     else
 85                     {
 86                         new_code += grapheme;
 87                     }
 88                     break;
 89                 case 2:
 90                     if (Convert.ToInt32(num) >= 9999)
 91                     {
 92                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
 93                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
 94                         num = "0000";
 95                     }
 96                     else
 97                     {
 98                         new_code += grapheme;
 99                     }
100                     break;
101                 case 3:
102                     if (Convert.ToInt32(num) >= 999)
103                     {
104                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
105                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
106                         num = "000";
107                     }
108                     else
109                     {
110                         new_code += grapheme;
111                     }
112                     break;
113                 case 4:
114                     if (Convert.ToInt32(num) >= 99)
115                     {
116                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
117                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
118                         num = "00";
119                     }
120                     else
121                     {
122                         new_code += grapheme;
123                     }
124                     break;
125                 case 5:
126                     if (Convert.ToInt32(num) >= 9)
127                     {
128                         char new_grapheme = Convert.ToChar(Convert.ToInt16(grapheme.Substring(new_code.Length - 1).ToCharArray()[0]) + 1);
129                         new_code += grapheme.Substring(0, new_code.Length - 1) + new_grapheme.ToString();
130                         num = "0";
131                     }
132                     else
133                     {
134                         new_code += grapheme;
135                     }
136                     break;
137             }
138         }
139         return new_code;
140     }
141     #endregion
142 
143     #region 获得字母 zhy
144     public static string GetGrapheme(string code)
145     {
146         //定义获取字母的正则
147         Regex grapheme_regex = new Regex(@"[A-Z]+");
148         //找到字符串中的匹配项
149         Match grapheme_match = grapheme_regex.Match(code);
150         return grapheme_match.Value;
151     }
152     #endregion
153 
154     #region 获得数字 zhy
155     public static string GetNumber(string code)
156     {
157         //定义获取数字的正则
158         Regex num_regex = new Regex(@"[^\d.\d]");
159         //剔除字符串中除数字以外的字符
160         string num = Regex.Replace(code, @"[^\d.\d]", "").ToString();
161         return num;
162     }
163     #endregion
164 
165     #region 拼接数字 zhy
166     public static string CreateNumber(string code)
167     {
168         string new_code = "";
169         //获取字母
170         string grapheme = GetGrapheme(code);
171         //获得数字
172         string num = GetNumber(code);
173 
174         int old_num = Convert.ToInt32(num);
175         //计算字母后的数字
176         switch (num.ToString().Length)
177         {
178             case 1:
179                 if (old_num < 9)
180                 {
181                     new_code = new_code + (old_num++);
182                 }
183                 else
184                 {
185                     //优惠码已经配到头
186                     new_code = "";
187                 }
188                 break;
189             case 2:
190                 if (old_num < 99)
191                 {
192                     int temporary_num = old_num;
193                     temporary_num++;
194                     //判断数字是否从01开始
195                     if (temporary_num.ToString().Length == 1)
196                     {
197                         new_code = new_code + "0" + temporary_num.ToString();
198                     }
199                     else
200                     {
201                         new_code = new_code + temporary_num.ToString();
202                     }
203                 }
204                 else
205                 {
206                     //优惠码已经配到头
207                     new_code = new_code + "0";
208                 }
209                 break;
210             case 3:
211                 if (old_num < 999)
212                 {
213                     int temporary_num = old_num;
214                     temporary_num++;
215                     //判断数字是否从01开始
216                     if (temporary_num.ToString().Length == 1)
217                     {
218                         new_code = new_code + "00" + temporary_num.ToString();
219                     }
220                     else if (temporary_num.ToString().Length == 2)
221                     {
222                         new_code = new_code + "0" + temporary_num.ToString();
223                     }
224                     else
225                     {
226                         new_code = new_code + temporary_num.ToString();
227                     }
228                 }
229                 else
230                 {
231                     //优惠码已经配到头
232                     new_code = "01";
233                 }
234                 break;
235             case 4:
236                 if (old_num < 9999)
237                 {
238                     int temporary_num = old_num;
239                     temporary_num++;
240                     //判断数字是否从01开始
241                     if (temporary_num.ToString().Length == 1)
242                     {
243                         new_code = new_code + "000" + temporary_num.ToString();
244                     }
245                     else if (temporary_num.ToString().Length == 2)
246                     {
247                         new_code = new_code + "00" + temporary_num.ToString();
248                     }
249                     else if (temporary_num.ToString().Length == 3)
250                     {
251                         new_code = new_code + "0" + temporary_num.ToString();
252                     }
253                     else
254                     {
255                         new_code = new_code + temporary_num.ToString();
256                     }
257                 }
258                 else
259                 {
260                     //优惠码已经配到头
261                     new_code = "001";
262                 }
263                 break;
264             case 5:
265                 if (old_num < 99999)
266                 {
267                     int temporary_num = old_num;
268                     temporary_num++;
269                     //判断数字是否从01开始
270                     if (temporary_num.ToString().Length == 1)
271                     {
272                         new_code = new_code + "0000" + temporary_num.ToString();
273                     }
274                     else if (temporary_num.ToString().Length == 2)
275                     {
276                         new_code = new_code + "000" + temporary_num.ToString();
277                     }
278                     else if (temporary_num.ToString().Length == 3)
279                     {
280                         new_code = new_code + "00" + temporary_num.ToString();
281                     }
282                     else if (temporary_num.ToString().Length == 4)
283                     {
284                         new_code = new_code + "0" + temporary_num.ToString();
285                     }
286                     else
287                     {
288                         new_code = new_code + temporary_num.ToString();
289                     }
290                 }
291                 else
292                 {
293                     //优惠码已经配到头
294                     new_code = "0001";
295                 }
296                 break;
297         }
298         return new_code;
299     }
300     #endregion

 

C#根据规则生成6位随机码

标签:

原文地址:http://www.cnblogs.com/zhhying/p/4281820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!