题目中说明中的第一步:偶数*2,奇数*1 但是示例中的员工等级是偶数单乘的是1,我的代码中员工等级的处理方式和员工号一致,如果需要调整,在Calc(string xx)方法中注释的else位置修改。
图片中是窗口截图。代码如下:
namespace Calc
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.txtNumber = new System.Windows.Forms.TextBox();
this.txtPwd = new System.Windows.Forms.TextBox();
this.txtLevel = new System.Windows.Forms.TextBox();
this.txtValCode = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.txtInput = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtNumber
//
this.txtNumber.Location = new System.Drawing.Point(102, 68);
this.txtNumber.Name = "txtNumber";
this.txtNumber.ReadOnly = true;
this.txtNumber.Size = new System.Drawing.Size(126, 21);
this.txtNumber.TabIndex = 0;
//
// txtPwd
//
this.txtPwd.Location = new System.Drawing.Point(102, 105);
this.txtPwd.Name = "txtPwd";
this.txtPwd.PasswordChar = '*';
this.txtPwd.ReadOnly = true;
this.txtPwd.Size = new System.Drawing.Size(126, 21);
this.txtPwd.TabIndex = 0;
//
// txtLevel
//
this.txtLevel.Location = new System.Drawing.Point(102, 136);
this.txtLevel.Name = "txtLevel";
this.txtLevel.ReadOnly = true;
this.txtLevel.Size = new System.Drawing.Size(126, 21);
this.txtLevel.TabIndex = 0;
//
// txtValCode
//
this.txtValCode.Location = new System.Drawing.Point(102, 173);
this.txtValCode.Name = "txtValCode";
this.txtValCode.ReadOnly = true;
this.txtValCode.Size = new System.Drawing.Size(126, 21);
this.txtValCode.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(55, 71);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 4;
this.label1.Text = "工号";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(55, 108);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 5;
this.label2.Text = "密码";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(55, 139);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 6;
this.label3.Text = "级别";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(55, 176);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(41, 12);
this.label4.TabIndex = 7;
this.label4.Text = "验证码";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(55, 31);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(29, 12);
this.label5.TabIndex = 8;
this.label5.Text = "工号";
//
// txtInput
//
this.txtInput.Location = new System.Drawing.Point(102, 28);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(126, 21);
this.txtInput.TabIndex = 1;
this.txtInput.Leave += new System.EventHandler(this.txtInput_Leave);
this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtInput_KeyPress);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(263, 241);
this.Controls.Add(this.txtInput);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtValCode);
this.Controls.Add(this.txtLevel);
this.Controls.Add(this.txtPwd);
this.Controls.Add(this.txtNumber);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox txtNumber;
private System.Windows.Forms.TextBox txtPwd;
private System.Windows.Forms.TextBox txtValCode;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtLevel;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox txtInput;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtInput.Focus();
}
private void txtInput_Leave(object sender, EventArgs e)
{
string content = txtInput.Text.Trim();
Calc(content);
}
private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
int charInt = (int)e.KeyChar;
if (charInt == 8 || charInt ==13)
{
string content = txtInput.Text.Trim();
Calc(content);
}
}
private void Calc(string content)
{
if (string.IsNullOrEmpty(content))
{
MessageBox.Show("请输入工号。");
txtInput.SelectAll();
return;
}
if (content.Length < 11)
{
MessageBox.Show("输入的工号格式不正确。");
txtInput.SelectAll();
return;
}
string number = content.Substring(0, 6);
string pwd = content.Substring(7, 4);
string level = content[10].ToString();
txtNumber.Text = number;
txtPwd.Text = pwd;
txtLevel.Text = level;
//计算验证码
char[] numberChar = number.ToCharArray();
int result = 0;
for (int i = 0; i < numberChar.Length + 1; i++)
{
if (i < numberChar.Length)
{
int calcNumber = int.Parse(numberChar[i] + "");
int element = (calcNumber) % 2 == 0 ? calcNumber * 2 : calcNumber;
if (element > 10)
element = int.Parse(element.ToString()[1] + "") + 1;
result += element;
}
else //级别校验,级别值为1 2 3 4 9按照规则计算结果不会超过10
{
int levelInt = int.Parse(level);
result += levelInt % 2 == 0 ? levelInt * 2 : levelInt;
}
}
result = CalcUntilSingleNumber(result);
txtValCode.Text = result + "";
}
private int CalcUntilSingleNumber(int number)
{
int result = 0;
string resultString = number.ToString();
if (resultString.Length > 1)
{
char[] stringChar = resultString.ToCharArray();
foreach (char item in stringChar)
{
result += int.Parse(item + "");
}
if (result >= 10)
result = CalcUntilSingleNumber(result);
}
else
{
result = number;
}
return result;
}
}
}
private string SetNo(int[] a,int k)
{
int[] ii = new int[6];
for (int i = 1; i <= k; i++)
{
if (i % 2 != 0)
{
ii[i - 1] = a[i - 1] * 1;
}
else
{
ii[i - 1] = a[i - 1] * 2;
if (ii[i - 1] >= 10)
{
ii[i - 1] = Convert.ToInt16(ii[i - 1].ToString().Substring(0, 1)) +
Convert.ToInt16(ii[i - 1].ToString().Substring(1, 1));
}
}
}
return ii[0].ToString() + ii[1].ToString() + ii[2].ToString() + ii[3].ToString() + ii[4].ToString() + ii[5].ToString();
}
private string GetNo(string aa)
{
if ((aa.Length != 6)&& (aa.Length != 1))
{
if (aa.Length == 2) aa = aa + "0000";
else if (aa.Length == 3) aa = aa + "000";
else if (aa.Length == 4) aa = aa + "00";
else if (aa.Length == 5) aa = aa + "0";
}
int a1 = Convert.ToInt16(aa.Substring(0, 1));
int a2 = Convert.ToInt16(aa.Substring(1, 1));
int a3 = Convert.ToInt16(aa.Substring(2, 1));
int a4 = Convert.ToInt16(aa.Substring(3, 1));
int a5 = Convert.ToInt16(aa.Substring(4, 1));
int a6 = Convert.ToInt16(aa.Substring(5, 1));
string a = (a1 + a2 + a3 + a4 + a5 + a6).ToString();
if (a.Length == 1) return a;
else return GetNo(a);
}
private void button3_Click(object sender, EventArgs e)
{
if (FileName.Text == "")
{
MessageBox.Show("The serail number does not be blank!!!!");
}
//长度必须是6码
//不能有数字之外的符号
int a1 = Convert.ToInt16(FileName.Text.ToString().Substring(0, 1));
int a2 = Convert.ToInt16(FileName.Text.ToString().Substring(1, 1));
int a3 = Convert.ToInt16(FileName.Text.ToString().Substring(2, 1));
int a4 = Convert.ToInt16(FileName.Text.ToString().Substring(3, 1));
int a5 = Convert.ToInt16(FileName.Text.ToString().Substring(4, 1));
int a6 = Convert.ToInt16(FileName.Text.ToString().Substring(5, 1));
//label.Text = a2.ToString();
int[] ii = new int[6];
ii[0] = a1;
ii[1] = a2;
ii[2] = a3;
ii[3] = a4;
ii[4] = a5;
ii[5] = a6;
label.Text = GetNo(SetNo(ii,6));
}