测试面板对应的代码如下:
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;using System.Data.OleDb;
using System.Data.SqlClient;namespace BriansApplication1
{ public partial class TestPaper : Form { int nTest = 0; int NTEST = 0; int page = -1; int count = 0; string id = null; Login login = new Login();int flagcount = 0;
int[] key = new int[1000];
string keys = null; string question = null; string answer = null;Register register = new Register();
public TestPaper() { InitializeComponent(); } public TestPaper(string id) { InitializeComponent(); this.id = id; }public void TestPaper_Load(object sender, EventArgs e)
{#region label处理
Label[] label = new Label[10]; label[0] = label1; label[1] = label2; label[2] = label3; label[3] = label4; label[4] = label5; label[5] = label6; label[6] = label7; label[7] = label8; label[8] = label9; label[9] = label10; for (int i = 0; i < 10; i++) { label[i].Visible = false; }TextBox[] testbox = new TextBox[10];
testbox[0] = textBox1; testbox[1] = textBox2; testbox[2] = textBox3; testbox[3] = textBox4; testbox[4] = textBox5; testbox[5] = textBox6; testbox[6] = textBox7; testbox[7] = textBox8; testbox[8] = textBox9; testbox[9] = textBox10; for (int i = 0; i < 10; i++) { testbox[i].Visible = false; } #endregion // MessageBox.Show("login.tbID.Text:" + id); }static int GetRandomSeed()
{ byte[] bytes = new byte[4]; System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); rng.GetBytes(bytes); return BitConverter.ToInt32(bytes, 0); }private void btExit_Click(object sender, EventArgs e)
{ #region label处理 Label[] label = new Label[10]; label[0] = label1; label[1] = label2; label[2] = label3; label[3] = label4; label[4] = label5; label[5] = label6; label[6] = label7; label[7] = label8; label[8] = label9; label[9] = label10;TextBox[] testbox = new TextBox[10];
testbox[0] = textBox1; testbox[1] = textBox2; testbox[2] = textBox3; testbox[3] = textBox4; testbox[4] = textBox5; testbox[5] = textBox6; testbox[6] = textBox7; testbox[7] = textBox8; testbox[8] = textBox9; testbox[9] = textBox10;#endregion
if (page >= 0) { for (int i = 0; i < 10 && i < nTest; i++) { answer += testbox[i].Text + "#"; }MessageBox.Show(answer);
} string mysqltest, mystr; SqlConnection myconn = new SqlConnection(); SqlCommand mycmd = new SqlCommand(); mystr = "Data Source=.\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True"; myconn.ConnectionString = mystr; myconn.Open(); mysqltest = string.Format("update student set answer='{0}' where id='{1}'", answer, id); mycmd.CommandText = mysqltest; mycmd.Connection = myconn; try { mycmd.ExecuteReader(); } catch { MessageBox.Show("插入失败1"); } myconn.Close(); this.Dispose(); }private void btPageDown_Click(object sender, EventArgs e)
{ #region label处理 Label[] label = new Label[10]; label[0] = label1; label[1] = label2; label[2] = label3; label[3] = label4; label[4] = label5; label[5] = label6; label[6] = label7; label[7] = label8; label[8] = label9; label[9] = label10; for (int i = 0; i < 10; i++) { label[i].Visible = false; }TextBox[] testbox = new TextBox[10];
testbox[0] = textBox1; testbox[1] = textBox2; testbox[2] = textBox3; testbox[3] = textBox4; testbox[4] = textBox5; testbox[5] = textBox6; testbox[6] = textBox7; testbox[7] = textBox8; testbox[8] = textBox9; testbox[9] = textBox10; for (int i = 0; i < 10; i++) { testbox[i].Visible = false; } #endregion flagcount++; nTest -= 10; if (page >= 0) { for (int i = 0; i < 10 && i < nTest; i++) { answer += testbox[i].Text + "#"; }MessageBox.Show(answer);
} page++; for (int i = 0; i < 10 && i < nTest; i++) { if (page == 0) { this.btPageUp.Enabled = false; } else { this.btPageUp.Enabled = true; }label[i].Visible = true;
testbox[i].Visible = true;if (nTest < 10)
{ this.btPageDown.Enabled = false; } else { this.btPageDown.Enabled = true; } }string testback = null;
string answerback = null; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); string str = "Data Source=.\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True"; conn.ConnectionString = str; conn.Open(); string temp = string.Format("select test from student where id='{0}'", id); cmd.CommandText = temp; cmd.Connection = conn; try { SqlDataReader read1 = cmd.ExecuteReader(); // SqlDataReader read2 = cmd.ExecuteReader(); while (read1.Read()) { try { testback += read1.GetString(0); // answerback += read2.GetString(0); } catch { MessageBox.Show("CHUCUOWU 1"); } } } catch (SqlException ea) { MessageBox.Show(ea.Message); } conn.Close();MessageBox.Show("testback:" + testback);
conn.Open();
temp = string.Format("select answer from student where id='{0}'", id); cmd.CommandText = temp; cmd.Connection = conn; try { //SqlDataReader read1 = cmd.ExecuteReader(); SqlDataReader read2 = cmd.ExecuteReader(); while (read2.Read()) { try { //testback += read1.GetString(0); answerback += read2.GetString(0); } catch { MessageBox.Show("CHUCUOWU 2"); } } } catch (SqlException ea) { MessageBox.Show(ea.Message); } conn.Close(); try { string[] questionback = testback.Split('#'); string[] answer1 = answerback.Split('#');for (int i = 1; i <= 10 && i <= nTest; i++)
{ count = page * 10 + i; MessageBox.Show(count.ToString() + nTest.ToString() + i.ToString() + page.ToString()); label[i - 1].Text = count.ToString() + "、" + questionback[count-1]; testbox[i - 1].Text = answer1[count - 1]; } } catch { MessageBox.Show("answerback:" + answerback); MessageBox.Show(count.ToString() + nTest.ToString() + page.ToString()); } }private void btCreate_Click(object sender, EventArgs e)
{ //this.btCreate.Enabled = false; try { nTest = Convert.ToInt32(tbCount.Text); NTEST = nTest; nTest += 10; lbAmount.Text = tbCount.Text; } catch { MessageBox.Show("请正确输入试题数量!"); return; } #region 生产试题 string[] str = new string[NTEST];int[] countTest = new int[NTEST];
for (int i = 0; i < NTEST; i++) { countTest[i] = new Random(GetRandomSeed()).Next(2, 5); } for (int k = 0; k < NTEST; k++) { int[] num = new int[countTest[k]]; char[] ops = new char[countTest[k] - 1]; for (int i = 0; i < countTest[k]; i++) { num[i] = new Random(GetRandomSeed()).Next(1, 100); } for (int i = 0; i < countTest[k] - 1; i++) { int a = new Random(GetRandomSeed()).Next(1, 100); switch (a % 4) { case 0: ops[i] = '+'; break; case 1: ops[i] = '-'; break; case 2: ops[i] = '*'; break; case 3: ops[i] = '/';break; default: ops[i] = '+'; break; } } for (int i = 0; i < countTest[k] - 1; i++) { str[k] += num[i].ToString() + ops[i]; } str[k] =str[k] + num[countTest[k] - 1]; question += str[k] + "#"; key[k] = Calculate(str[k]); keys += key[k] + "#"; } #endregion // MessageBox.Show(question); #region 存入数据库 string mysqltest, mystr; SqlConnection myconn = new SqlConnection(); SqlCommand mycmd = new SqlCommand(); mystr = "Data Source=.\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True"; myconn.ConnectionString = mystr; myconn.Open(); mysqltest = string.Format("update student set test='{0}',testcount='{1}',result='{2}' where id='{3}'", question, NTEST ,keys,ToString(), id); mycmd.CommandText = mysqltest; mycmd.Connection = myconn; try { mycmd.ExecuteReader(); } catch { MessageBox.Show("插入失败0"); } myconn.Close(); #endregion btPageDown_Click(sender, e); }private void btPageUp_Click(object sender, EventArgs e)
{ #region lable处理 Label[] label = new Label[10]; label[0] = label1; label[1] = label2; label[2] = label3; label[3] = label4; label[4] = label5; label[5] = label6; label[6] = label7; label[7] = label8; label[8] = label9; label[9] = label10; for (int i = 0; i < 10; i++) { label[i].Visible = false; }TextBox[] testbox = new TextBox[10];
testbox[0] = textBox1; testbox[1] = textBox2; testbox[2] = textBox3; testbox[3] = textBox4; testbox[4] = textBox5; testbox[5] = textBox6; testbox[6] = textBox7; testbox[7] = textBox8; testbox[8] = textBox9; testbox[9] = textBox10; for (int i = 0; i < 10; i++) { testbox[i].Visible = false; } #endregion page--; nTest += 10; for (int i = 0; i < 10 && i <= nTest; i++) { if (page == 0) { this.btPageUp.Enabled = false; } else { this.btPageUp.Enabled = true; } label[i].Visible = true; testbox[i].Visible = true; if (nTest < 10) { this.btPageDown.Enabled = false; } else { this.btPageDown.Enabled = true; } } string testback = null; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); string str = "Data Source=.\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True"; conn.ConnectionString = str; conn.Open(); string temp = string.Format("select test from student where id='{0}'", id); cmd.CommandText = temp; cmd.Connection = conn; try { SqlDataReader read = cmd.ExecuteReader(); while (read.Read()) { testback += read.GetString(0); } } catch(SqlException ea) { MessageBox.Show(ea.Message); }string[] questionback = testback.Split('#');
for (int i = 1; i <= 10; i++) { count = page * 10 + i; label[i - 1].Text = count.ToString() + "、" + questionback[count-1];}
}
private void btCurrent_Click(object sender, EventArgs e)
{}
private void btContinue_Click(object sender, EventArgs e)
{ tbCount.Enabled = false; SqlConnection myconn = new SqlConnection(); SqlCommand mycmd = new SqlCommand(); string mystr = "Data Source=.\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True"; myconn.ConnectionString = mystr; myconn.Open(); string mysqltest = string.Format("select testcount from student where id='{0}'",id); mycmd.CommandText = mysqltest; mycmd.Connection = myconn; try { SqlDataReader read = mycmd.ExecuteReader(); while (read.Read()) { nTest = read.GetInt32(0) + 10; NTEST = nTest; lbAmount.Text = (nTest - 10).ToString(); } } catch { MessageBox.Show("获取失败"); } myconn.Close();btPageDown_Click(sender, e);
} static int Calculate(string Expression) { List<int> numList = new List<int>();//运算数字列表 List<char> OperatorList = new List<char>();//运行符列表 bool numState = true;//true:表示刚遇到符号 int num = 0; char[] expression = Expression.ToCharArray(); for (int i = 0; i < expression.Length; i++) { if (Convert.ToInt16(expression[i]) > 58 || Convert.ToInt16(expression[i]) < 48) { switch (expression[i]) { case '+': numState = true; numList.Add(num); break; case '-': numState = true; numList.Add(num); break; case '*': numState = true; numList.Add(num); break; case '/': numState = true; numList.Add(num); break; default: throw new Exception("表达式不正确,含有未知字符!"); } OperatorList.Add(expression[i]); } else if (numState) { num = Convert.ToInt16(expression[i].ToString()); if (i == expression.Length - 1) numList.Add(num); numState = false; } else { num *= 10; num += Convert.ToInt16(expression[i].ToString()); if (i == expression.Length - 1) numList.Add(num); } } return getResults(numList, OperatorList); } static int getResults(List<int> numList, List<char> OperatorList) { if (numList.Count == 1) return numList[0]; List<int> numList1 = new List<int>(); numList1.Add(numList[0]); List<char> OperatorList1 = new List<char>(); for (int i = 0; i < OperatorList.Count; i++) { if (OperatorList[i] == '+' || OperatorList[i] == '-') { numList1.Add(numList[i + 1]); OperatorList1.Add(OperatorList[i]); } else if (OperatorList[i] == '*') { numList1[numList1.Count - 1] *= numList[i + 1]; } else if (OperatorList[i] == '/') { if (numList[i + 1] != 0) { numList1[numList1.Count - 1] /= numList[i + 1]; } else break; } } int results = numList1[0]; for (int i = 0; i < OperatorList1.Count; i++) { if (OperatorList1[i] == '+') results += numList1[i + 1]; else results -= numList1[i + 1]; } return results; } } }