“身份证号”的数字分析

** 对“学号”、“身份证”的数字分析 **

问题的提出:
在现代社会中,一个人总是离不开数字。人在社会上总要有一个身份证号码,学生在学校里读书一定会有一个学号,而且这些号码不都是一些无意义的数字。我写的这个程序是用来分析这些数字,并把其中有意义的意思表达出来。

编程环境:
VS.NET

实现技术:
ASP.NET

关键:
String.Substring(Int32,Int32)方法的运用,Literal控件的使用,switch语句的使用。

正文:
在Web窗体上,放上一个Label控件,一个Literal控件,一个TextBox控件,一个Button控件。设置Label控件的Text属性为“您的学号:”,Literal控件的Visible属性设置为“False”。我主要是对Button控件的Click()事件进行编码。当点击一下按钮后,对输入的数字进行分析,然后把分析的内容用Literal控件显示出来。

Button控件的Click()事件:
string studentNo = txtNo.Text; // 将学号赋给studentNo字符串

if (!studentInfo.Visible)
{
studentInfo.Visible = true; // 如果Literal控件是不可见的,则显示它.
}

try
{
// 取子串操作
string strStartYear = studentNo.Substring(0,2); // 入学年份
string strTotalYears = studentNo.Substring(2,1); // 学制
string strSchool = studentNo.Substring(3,2); // 学院
string strClass = studentNo.Substring(5,1); // 班级
string strNumber = studentNo.Substring(6,2); // 号码

// 将数字跟文字对应起来
// 内容纯属虚构
switch (strSchool)
{
case "01":
strSchool = "文学院";
break;
case "02":
strSchool = "理学院";
break;
case "03":
strSchool = "工学院";
break;
case "04":
strSchool = "科技学院";
break;
case "05":
strSchool = "传播与艺术学院";
break;
case "06":
strSchool = "商学院";
break;
case "07":
strSchool = "法学院";
break;
case "08":
strSchool = "职教学院";
break;
case "09":
strSchool = "建工学院";
break;
case "10":
strSchool = "信息学院";
break;
default:
strSchool = "子虚乌有";
break;

}

studentInfo.Text = "您于"+strStartYear+"年入学"+",所选专业是"+strTotalYears+"年制的。"+
"您现在在"+strSchool+"学院"+strClass+"班学习"+",您的号码是:"+strNumber+"。";
}
catch
{
Response.Write("取子串操作越界!");
}
finally
{
}

注:这里的学号是8位的。

举例:
Web应用程序运行后,在文本框内输入:02408122。看看结果是什么?:)

效果图:

按此在新窗口打开图片
对程序的扩展:
为了禁止输入错误的内容,可以添加一个RegularExpressionValidator和一个ValidationSummary控件,正则表达式为“\d{8}”,当输入的不是8位数字,则会在页面上显示出错信息。
摘要:
对身份证号码的分析同对学号的分析类似。

正文:
这里认为身份证号是18位的。

在页面上添加一个Label控件,一个TextBox控件,一个Button控件,一个Literal控件。Label控件的Text属性设为“身份证号:”,Literal控件将显示身份证号里的信息。关键还是在Button控件的Click()事件中。

Button控件的Click()事件:
string strID = txtID.Text;

if (!txtID.Visible)
{
txtID.Visible = true;
}

try
{
string strYear = strID.Substring(6,4); // 年
string strMonth = strID.Substring(10,2); // 月
string strDay = strID.Substring(12,2); // 日

Literal1.Text = "您的生日是:"+strYear+"年"+strMonth+"月"+strDay+"号";
}
catch
{
Response.Write("程序有错误!");
}
finally
{
}
显示效果图:

按此在新窗口打开图片

Published At
Categories with Web编程
Tagged with
comments powered by Disqus