随机组卷算法

这是课程设计的时候写的一个随机组卷的原理代码 ,没有进行相关的优化处理,但是功能已经实现,能够从数据库中随机的取出n条数据组成试卷。<br>代码如下:<br>Imports System.Data<br>Imports System.Data.SqlClient<br>Imports System.Configuration.AppSettingsReader<br><br><br>Namespace WebApplication1<br><br>Partial Class SuiJi<br> Inherits System.Web.UI.Page<br><br>#Region " Web 窗体设计器生成的代码 "<br><br> '该调用是 Web 窗体设计器所必需的。<br> &lt;System.Diagnostics.DebuggerStepThrough()&gt; Private Sub InitializeComponent()<br><br> End Sub<br> Public db As New SupperSDR.DbConnection<br> Dim strconn As String = ConfigurationSettings.AppSettings("strdata")<br> '注意: 以下占位符声明是 Web 窗体设计器所必需的。<br> '不要删除或移动它。<br><br> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<br> 'CODEGEN: 此方法调用是 Web 窗体设计器所必需的<br> '不要使用代码编辑器修改它。<br> InitializeComponent()<br> End Sub<br> '*************张海波定义的秘密变量嘿嘿**************<br> Dim ArrNum, Num As Integer '这个变量是数组长度减去1所余下的数值(arrnum),Num是所要产生的小题的数目<br><br> '************秘密变量就藏在这里,一般不会被小菜鸟发现滴**********<br>#End Region<br> Dim ArrListXz, ArrListPd, ArrListTk As ArrayList '声明选择判断填空数组用来保存该类试题<br> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<br> '在此处放置初始化页的用户代码<br> If Not IsPostBack Then<br> BindKc()<br> End If<br> End Sub<br> Sub BindKc() '绑定课程<br> db.DbConn.Open()<br><br> db.DbCmd.CommandText = "select distinct(CourseType) from testType"<br> Dim dr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader<br> While dr.Read<br> KcName.Items.Add(dr(0))<br> End While<br><br> db.DbConn.Close()<br> End Sub<br><br> Private Sub Type_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Type.SelectedIndexChanged<br> Try<br> IDArrlist()<br> Catch ex As Exception<br><br> End Try<br><br> End Sub<br><br> Function IDArrlist() As Integer<br> '先根据表TestInfo中的CourseType和Type项目索引出试题的Id号码,组成一个以逗号为分隔符的字符串<br> db.DbConn.Open()<br> db.DbCmd.CommandText = "select id from testinfo where CourseType=@kecheng and type=@leixing"<br> db.DbCmd.Parameters.Add("@kecheng", KcName.SelectedValue)<br> db.DbCmd.Parameters.Add("@leixing", Type.SelectedValue)<br> Dim dr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader<br> ltID.Text = ""<br> While dr.Read<br> ltID.Text += dr(0) &amp; ","<br> End While<br> Session.Add("ID", ltID.Text)<br> db.DbConn.Close()<br><br><br> '******开始调用相关函数****<br><br> 'ChuLiStr()<br> '*********结束*************<br> Dim StrId As String = Session("id")<br> Dim Num As Integer = 2<br><br><br> Dim str() As String = StrId.Replace("(", "").Replace(")", "").Split(",")<br> Dim ArrNum As Integer = str.Length<br> Select Case Type.SelectedValue<br> Case "选择"<br> Session("xz") = ""<br> LtXiaoTi.Text += "《选择题》" &amp; "&lt;br&gt;"<br> Case "判断"<br> Session("pd") = ""<br> LtXiaoTi.Text += "《判断题》" &amp; "&lt;br&gt;"<br> Case "填空"<br> Session("tk") = ""<br> LtXiaoTi.Text += "《填空题》" &amp; "&lt;br&gt;"<br> End Select<br> For Num = 1 To CInt(txtNUm.Text)<br> Dim i As Integer = CInt(Int((ArrNum - 1 + 1) * Rnd() + 1))<br><br> db.DbConn.Close()<br> db.DbConn.Open()<br><br> db.DbCmd.CommandText = "select * from testinfo where id=" &amp; str(i)<br><br> Dim SdrDr As SqlClient.SqlDataReader = db.DbCmd.ExecuteReader<br><br> While SdrDr.Read<br><br> Select Case Type.SelectedValue<br> Case "选择"<br><br> LtXiaoTi.Text += "【" &amp; SdrDr(0) &amp; "】、" &amp; SdrDr(1) &amp; "&lt;br&gt;"<br> Session.Add("xz", LtXiaoTi.Text)<br> Case "判断"<br><br> LtXiaoTi.Text += "【" &amp; SdrDr(0) &amp; "】、" &amp; SdrDr(1) &amp; "&lt;br&gt;"<br> Session.Add("pd", LtXiaoTi.Text)<br> Case "填空"<br><br> LtXiaoTi.Text += "【" &amp; SdrDr(0) &amp; "】、" &amp; SdrDr(1) &amp; "&lt;br&gt;"<br> Session.Add("tk", LtXiaoTi.Text)<br> End Select<br><br> End While<br> 'Response.Write("这是下标" &amp; i &amp; "值" &amp; str(i))<br> Next<br> End Function<br><br><br> 'Function ChuLiStr() '分割字符串操作将分割好的字符串组成数组,并返回数组长度<br> ' Dim StrId As String = Session("id")<br> ' Dim str() As String = StrId.Replace("(", "").Replace(")", "").Split(",")<br> ' ArrNum = str.Length - 1<br> ' Response.Write("数组长度" &amp; ArrNum &amp; "&lt;br&gt;" &amp; "这是数组的第一项" &amp; str(0) &amp; "&lt;br&gt;")<br><br> ' ' Return ArrNum '返回数组的长度<br><br> ' Num = 3000<br> ' Dim i As Integer<br><br> ' 'Response.Write("这是什么" &amp; str(CInt(Sjhanshu(1, ArrNum, Num))))<br> ' Response.Write("数组下标:" &amp; Sjhanshu(1, ArrNum, Num) &amp; "&lt;br&gt;对应ID号" &amp; str(CInt(lbNum.Text)) &amp; "&lt;br&gt;" &amp; "数组的第三项是:" &amp; str(2))<br> ' For i = 1 To Num<br><br> ' 'db.DbConn.Open()<br> ' 'db.DbCmd.CommandText = "select * from testinfo where id=" &amp; Str(Sjhanshu(1, ArrNum, Num)) &amp; ""<br> ' 'Dim SdrDr As OleDbDataReader = db.DbCmd.ExecuteReader<br> ' 'While SdrDr.Read<br> ' ' Response.Write("这是随机取出的题号" &amp; SdrDr(0) &amp; "这是题干" &amp; SdrDr(1) &amp; "结束输出")<br> ' 'End While<br><br><br> ' Next<br><br><br><br><br> 'End Function<br> Function Sjhanshu(ByVal lowerbound As Integer, ByVal upperbound As Integer) As Integer '这是一个产生随机数字的函数<br> '这是产生随机数字用的代码<br> lbNum.Text = ""<br> lbNum.Text = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))<br> End Function<br><br><br> Private Sub btnCreatPaper_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreatPaper.Click<br><br> LtXiaoTi.Visible = False<br> LtShiJuan.Text = LtXiaoTi.Text<br> End Sub<br><br> Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click<br> db.DbConn.Close()<br> Try<br> db.DbConn.Open()<br> db.DbCmd.CommandText = "insert into paperinfo(TestContent,CourseType) values(@SdrNeiRong,@SdrKecheng)"<br> db.DbCmd.Parameters.Add("@SdrNeiRong", LtShiJuan.Text)<br> db.DbCmd.Parameters.Add("@SdrKecheng", KcName.SelectedValue)<br> db.DbCmd.ExecuteNonQuery()<br> lbmsg.Visible = True<br> lbmsg.Text = "保存成功"<br> db.DbConn.Close()<br> Catch ex As Exception<br> Response.Write("&lt;script language='javascript'&gt;'保存失败'&lt;/script&gt;")<br> End Try<br> End Sub<br><br> Private Sub txtNUm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNUm.TextChanged<br> Try<br> Dim Snum As Integer = CInt(txtNUm.Text)<br> Catch ex As Exception<br> txtNUm.Text = 10<br> End Try<br> End Sub<br>End Class<br><br>End Namespace<br>

你可能感兴趣的:(算法)