⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 paperpruduce.cs

📁 程序设计语言上机考试系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1.KaowuManage
{
    public partial class PaperPruduce : Form
    {
        SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
        baseclass.BaseOperate boperate = new baseclass.BaseOperate();
        baseclass.OperateAndValidate opAndvalidate = new baseclass.OperateAndValidate();
        string  M_str_sql = "select idshijuan as 试卷编号,A as 选择题编号,B as 判断题编号 ,C as 程序题编号 from shiti order by idshijuan";
        public bool isenough = true;
        
        public PaperPruduce()
        {
            InitializeComponent();
        }

        private void PaperPruduce_Load(object sender, EventArgs e)
        {
            opAndvalidate.cboxBind("select distinct idmoban  from moban", "moban", "idmoban", combBianhao);
            DataSet myds = boperate.getds(M_str_sql, "shiti");
            dataGridView1.DataSource = myds.Tables[0];
            if (myds.Tables[0].Rows.Count <= 0)
            {
                dataGridView1.AllowUserToAddRows = true;
            }
        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #region 组卷
        /// <summary>
        /// 组卷
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Click(object sender, EventArgs e)
        {
            //判断试题表是否为空
            if(IsEmpty())
                return;
            if (!opAndvalidate.validateNum(txtnum.Text.Trim()))
                errorPnum.SetError(txtnum, "请输入正整数");
            else
            {
                errorPnum.Clear();
                //num[0]存放的是选择题的数量
                //num[1]存放的是判断题的数量
                //num[2]存放的是程序设计题的数量
                int[] num = new int[3];
                //score[]存放某一类型试题的分值
                decimal[] score = new decimal[3];
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                string[] sql = new string[3];
                sql[0] = " select t_shu ,score from moban where idmoban = '" + combBianhao.Text.Trim() + " 'and types = '" + "选择题" + "'";
                sql[1] = " select t_shu ,score from moban where idmoban = '" + combBianhao.Text.Trim() + " 'and types = '" + "判断题" + "'";
                sql[2] = " select t_shu ,score from moban where idmoban = '" + combBianhao.Text.Trim() + " 'and types = '" + "程序设计题" + "'";

                //存放从题库中抽取出的不同难度的试题id
                //shitiid[0][] 存放的是难度为0的试题id
                string[][] shitiid = new string[3][];

                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                for (int i = 0; i < 3; i++)
                {
                    ds = boperate.getds(sql[i], "moban");
                    dt = ds.Tables[0];
                    foreach (DataRow row in dt.Rows)
                    {
                        num[i] = Convert.ToInt32(row[0].ToString());
                        score[i] = Convert.ToDecimal(row["score"].ToString());
                    }
                }
                string[] sql1 = { "select id from choose where quotiety =", "select id from panduan where quotiety =", "select id from program where quotiety =" };

                string[] table ={ "choose", "panduan", "program" };
                //txtnum.text 为试题套数
                for (int n = 0; n < Convert.ToInt32(txtnum.Text.Trim()); n++)
                {
                    //生成试卷id---自动编号
                    opAndvalidate.autoNum("select idshijuan from shiti", "shiti", "idshijuan", "T", "1000001", txtid);
                    for (int k = 0; k < 3; k++)
                    {
                        string shitibianhao = "";
                        //数组初始化
                        for (int x = 0; x < shitiid.Length; x++)
                        {
                            shitiid[x] = new string[20];
                        }
                        shitiid = getid(num, shitiid, k, sql1, table);
                        //抽取选择题
                        if(!isenough)
                            goto outerr;
                        if (k == 0)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                for (int i = 0; i < shitiid[j].Length; i++)
                                {
                                    if (shitiid[j][i] != "" && shitiid[j][i] != null)
                                    {
                                        if (shitibianhao == "")
                                            shitibianhao += shitiid[j][i];
                                        else
                                            shitibianhao += ";" + shitiid[j][i];
                                    }
                                }
                            }
                            boperate.getcom("insert into shiti(idshijuan,A,idmoban) values('"+ txtid.Text.Trim() + "','" + shitibianhao + "','" + combBianhao.Text.Trim() +"')");
                        }
                        //抽取判断题
                        if (k == 1)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                for (int i = 0; i < shitiid[j].Length; i++)
                                {
                                    if (shitiid[j][i] != "" && shitiid[j][i] != null)
                                    {
                                        if (shitibianhao == "")
                                            shitibianhao += shitiid[j][i];
                                        else
                                            shitibianhao += ";" + shitiid[j][i];
                                    }
                                }
                            }
                            boperate.getcom("update shiti set B ='" + shitibianhao +"'where idshijuan ='"+ txtid.Text.Trim() + "'");
                        }
                        
                        //抽取程序设计题
                        if (k == 2)
                        {
                            for (int j = 0; j < 3; j++)
                            {
                                for (int i = 0; i < shitiid[j].Length; i++)
                                {
                                    if (shitiid[j][i] != "" && shitiid[j][i] != null)
                                    {
                                        if (shitibianhao == "")
                                            shitibianhao += shitiid[j][i];
                                        else
                                            shitibianhao += ";" + shitiid[j][i];
                                    }
                                }
                            }
                            boperate.getcom("update shiti set C ='" + shitibianhao + "'where idshijuan='" + txtid.Text.Trim() + "'");
                        }
                    }
                }
                outerr:
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
            
            DataSet myds = boperate.getds(M_str_sql, "shiti");
            dataGridView1.DataSource = myds.Tables[0];
        }
        #endregion
         
        #region 判断试题表是否为空,不为空才能继续组卷
        public bool IsEmpty()
        {
            bool empty = false;
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            //验证选择题表是否为空
            SqlCommand scd = new SqlCommand("select * from choose",cn);
            if (null == scd.ExecuteScalar())
            {
                empty = true;
                MessageBox.Show("选择题表为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
            //验证判断题表是否为空
            scd = new SqlCommand("select * from panduan", cn);
            if (null == scd.ExecuteScalar())
            {
                empty = true;
                MessageBox.Show("判断题表为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
            //验证程序设计题表是否为空
            scd = new SqlCommand("select * from program", cn);
            if (null == scd.ExecuteScalar())
            {
                empty = true;
                MessageBox.Show("程序设计题表为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (cn.State == ConnectionState.Open)
                cn.Close();
            return empty;
        }
        #endregion

        #region 抽取题库中的id
        /// <summary>
        /// 抽取题库中的id
        /// </summary>
        /// <param name="num">存放模板中选择题、判断题、程序设计题数量的数组</param>
        /// <param name="shitiid">存放从题库中抽取出的试题id</param>
        /// <param name="l"></param>
        /// <param name="sql1">存放sql语句</param>
        /// <param name="table">数据表名数组</param>
        /// <returns></returns>
        public string[][] getid(int[] num, string[][] shitiid,int l,string []sql1,string[] table)
        {
            string[] proportion = boperate.Split(comBoxPortion.Text.Trim(), ":");
            //简单、中等、困难试题的比例
            //simple[0]存放的是难度系数为0的试题的比例
            //simple[1]存放的是难度系数为1的试题的比例
            //simple[2]存放的是难度系数为2的试题的比例
            int[] simple = new int[3];
            simple[0] = Convert.ToInt32(proportion[0]);
            simple[1] = Convert.ToInt32(proportion[1]);
            simple[2] = Convert.ToInt32(proportion[2]);
            int beishu, total;
            string[] idend =new string[40];

            total = simple[0] + simple[1] + simple[2];
            for (int j = 0; j < 3; j++)
            {
                beishu = (num[l] - num[l] % total) / total;
                //取出选择题难度为j的试题的id
                string[] idnum = boperate.getid(sql1[l] + j, table[l]);
                //将随机取出的id存入数组idend中
                if (j == 1)
                {
                    //抽取难度为1的试题
                    if (idnum.Length < simple[j] * beishu + num[l] - total * beishu)
                    {
                        MessageBox.Show(table[l].ToString() +"中难度系数为"+j.ToString()+"的试题数目少于模板需求数目,请清空试题表,设置好后重新组卷。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        isenough = false;
                        break;
                    }
                    else
                        idend = boperate.Rand(simple[j] * beishu + num[l] - total * beishu, idnum);
                }
                else
                {
                    if (idnum.Length < simple[j] * beishu)
                    {
                        MessageBox.Show(table[l].ToString() + "中难度系数为" + j.ToString() + "的试题数目少于模板需求数目,请清空试题表,设置好后重新组卷", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        isenough = false;
                        break;
                    }
                    else
                        idend = boperate.Rand(simple[j] * beishu, idnum);
                }
                for (int i = 0; i < idend.Length; i++)
                    shitiid[j][i] = idend[i];
            }
            return shitiid;
        }
        #endregion
        
        

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
            dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.LightSlateGray;
            dataGridView1.CurrentRow.Selected = true;
        }

        #region 在datagridview中实现鼠标右键单击删除的功能
        private void tSMItemDelete_Click(object sender, EventArgs e)
        {
            //删除选中的多条数据
            boperate.delseldata(dataGridView1, "delete from shiti where idshijuan = ");
            PaperPruduce_Load(sender, e);
            MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -