策略模式
Window窗体中
1:Form连接数据库,出题,可以进行增加、删除、修改!
namespace Windows10_10 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static int Count = 0; public static int t = 0; public static int Right = 0; string selStr = @"select Num,n1,ys,n2,sum from exam"; DBCon db = new DBCon(); private void Form1_Load(object sender, EventArgs e) { db.dbcon(); db.dbFill(selStr); comboBox1.ValueMember = "Num"; comboBox1.DataSource = db.dt.DefaultView; } private void button2_Click(object sender, EventArgs e) { db.dbcon(); db.dbFill(selStr); dataGridView1.DataSource = db.dt; } private void button3_Click(object sender, EventArgs e) { db.dbcon(); string insertInfo = "insert into exam(Num,n1,ys,n2) values('" + comboBox1.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')"; db.dbInsert(insertInfo); db.dbFill(selStr); dataGridView1.DataSource = db.dt; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string selInfo = "select n1,ys,n2,sum from exam where Num='" + comboBox1.Text.ToString().Trim() + "'"; db.dbcon(); db.dbSelect(selInfo); if (db.sdr.Read()) { textBox1.Text = db.sdr["n1"].ToString(); textBox2.Text = db.sdr["ys"].ToString(); textBox3.Text = db.sdr["n2"].ToString(); textBox4.Text = db.sdr["sum"].ToString(); } } private void button4_Click(object sender, EventArgs e) { db.dbcon(); string strUpd = "update exam set n1='" + textBox1.Text.Trim() + "',ys='" + textBox2.Text.Trim() + "',n2='" + textBox3.Text.Trim() + "'where Num='" + comboBox1.Text.Trim() + "'"; db.dbUpdate(strUpd); db.dbFill(selStr); dataGridView1.DataSource = db.dt; } private void button5_Click(object sender, EventArgs e) { db.dbcon(); string strUpd = "delete from exam where Num='" + comboBox1.Text.Trim() + "'"; db.dbDelete(strUpd); MessageBox.Show("数据删除成功!"); db.dbFill(selStr); dataGridView1.DataSource = db.dt; } private void button6_Click(object sender, EventArgs e) { db.dbcon(); db.dbGridViewUpd(); db.dbFill(selStr); dataGridView1.DataSource = db.dt; } private void button7_Click(object sender, EventArgs e) { Application.Exit(); } private void button1_Click(object sender, EventArgs e) { textBox4.Enabled = false; timer1.Enabled =false; Form2 frm = new Form2(); frm.ShowDialog(); } private void button9_Click(object sender, EventArgs e) { label7.Text = t.ToString(); timer1.Enabled = true; timer1.Interval = 1000; timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { t = t + 1; label7.Text = t.ToString(); } private void textBox4_KeyDown(object sender, KeyEventArgs e) { Windows10_10.Class1.JiSuan JS = null; int m = int.Parse(textBox1.Text.Trim()); int n = int.Parse(textBox3.Text.Trim()); string a = textBox2.Text.Trim(); switch (a) { case "+": JS = new Windows10_10.Class1.JiSuan(new Windows10_10.Class1.Add()); break; case "-": JS = new Windows10_10.Class1.JiSuan(new Windows10_10.Class1.Sub()); break; case "*": JS = new Windows10_10.Class1.JiSuan(new Windows10_10.Class1.Mul()); break; case "/": JS = new Windows10_10.Class1.JiSuan(new Windows10_10.Class1.Div()); break; default: break; } if (e.KeyCode == Keys.Enter) { string answer = JS.Vol(m, n, a).ToString(); if (textBox4.Text == answer.ToString()) { MessageBox.Show("回答正确"); Count++; Right++; db.dbcon(); string strUpd = "update exam set n1='" + textBox1.Text.Trim() + "',ys='" + textBox2.Text.Trim() + "',n2='" + textBox3.Text.Trim() + "',sum='" + textBox4.Text.Trim() + "'where Num='" + comboBox1.Text.Trim() + "'"; db.dbUpdate(strUpd); db.dbFill(selStr); dataGridView1.DataSource = db.dt; } else { MessageBox.Show("回答错误"); Count++; } } } } }
2:连接数据库及增加、删除、修改的方法:
namespace Windows10_10 { class DBCon { public string strCon = @"Data Source=.;Initial Catalog=Solution; Integrated Security=true"; public SqlConnection sqlCon = new SqlConnection(); public SqlDataAdapter sda = new SqlDataAdapter(); public DataSet ds = new DataSet(); public DataTable dt = new DataTable(); public SqlDataReader sdr; public void dbcon() { try { sqlCon = new SqlConnection(strCon); } catch (Exception e) { MessageBox.Show("数据连接不成功:" + e.ToString()); } } public void dbFill(string selstr) { dt.Clear(); sda = new SqlDataAdapter(selstr, strCon); sda.Fill(ds, "exam"); dt = ds.Tables["exam"]; } public void dbSelect(string showInfo) { sqlCon.Open(); SqlCommand sqlcmd = new SqlCommand(showInfo, sqlCon); sdr = sqlcmd.ExecuteReader(); } public void dbInsert(string insertInfo) { sqlCon.Open(); SqlCommand sqlcmd = new SqlCommand(insertInfo, sqlCon); try { sqlcmd.ExecuteNonQuery(); MessageBox.Show("成功!"); } catch (Exception e) { MessageBox.Show("数据插入失败" + e.ToString()); } sqlCon.Close(); } public void dbGridViewUpd() { SqlCommandBuilder scb = new SqlCommandBuilder(sda); DialogResult result; result = MessageBox.Show("确定保存修改过的数据吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result == DialogResult.OK) { dt = ds.Tables["exam"]; sda.Update(dt); dt.AcceptChanges(); } } public void dbUpdate(string updStr) { sqlCon.Open(); SqlCommand sqlcmd = new SqlCommand(updStr, sqlCon); try { sqlcmd.ExecuteNonQuery(); MessageBox.Show("数据修改成功!"); } catch (Exception e) { MessageBox.Show("数据修改失败!" + e.ToString()); } sqlCon.Close(); } public void dbDelete(string delStr) { sqlCon.Open(); SqlCommand sqlcmd = new SqlCommand(delStr, sqlCon); try { sqlcmd.ExecuteNonQuery(); } catch (Exception e) { MessageBox.Show("数据删除失败!" + e.ToString()); } sqlCon.Close(); } } }
3策略模式——计算方法:
namespace Windows10_10 { class Class1 { public interface Calculator { int Vol(int m, int n); } private int m; public int M { get { return m; } set { m = value; } } private int n; private int N { get { return n; } set { n = value; } } public class Add : Calculator { public int Vol(int m, int n) { int result = 0; result = m + n; return result; } } public class Sub : Calculator { public int Vol(int m, int n) { int result = 0; result = m - n; return result; } } public class Mul : Calculator { public int Vol(int m, int n) { int result = 0; result = m * n; return result; } } public class Div : Calculator { public int Vol(int m, int n) { int result = 0; result = m / n; return result; } } public class JiSuan { private Calculator calculate; public JiSuan(Calculator calculate) { this.calculate = calculate; } public double Vol(int m, int n, String a) { return this.calculate.Vol(m, n); } } } }
4 Form2:
namespace Windows10_10 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { textBox1.Text = Form1.Count.ToString(); textBox2.Text = Form1.Right.ToString(); textBox3.Text = ((Form1.Right / (double)(Form1.Count)) * 100).ToString() + "%"; textBox4.Text = ((Form1.Count / (double)(Form1.t)) * 100).ToString() + "%"; } } }
Web窗体中
1计算方法-策略模式:
public abstract class Calculator { public abstract int Vol(int m, int n); } public class Add : Calculator { public override int Vol(int m, int n) { int answer = 0; answer = m + n; return answer; } } public class Sub : Calculator { public override int Vol(int m, int n) { int answer = 0; answer = m - n; return answer; } } public class Mul : Calculator { public override int Vol(int m, int n) { int answer = 0; answer = m * n; return answer; } } public class Div : Calculator { public override int Vol(int m, int n) { int answer = 0; answer = m / n; return answer; } } public class JiSuan { private Calculator calculate=null; public JiSuan(Calculator calculate) { this.calculate = calculate; } public int Vol(int m, int n, String a) { return this.calculate.Vol(m, n); } }
2方法调用:
public partial class Web_Caculate : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public static int Count = 0; public static int Right = 0; protected void Button1_Click(object sender, EventArgs e) { JiSuan JS = null; int m = int.Parse(TextBox1.Text.Trim()); int n = int.Parse(TextBox3.Text.Trim()); string a = TextBox2.Text.Trim(); switch (a) { case "+": JS = new JiSuan(new Add()); break; case "-": JS = new JiSuan(new Sub()); break; case "*": JS = new JiSuan(new Mul()); break; default: JS = new JiSuan(new Div()); break; } string result = JS.Vol(m, n, a).ToString(); string answer = TextBox1.Text + TextBox2.Text.ToString() + TextBox3.Text; if (TextBox4.Text == result) { Count++; Right++; Label1.Text=("回答正确"); } else { Count++; Label1.Text=("回答错误"); } //TextBox1.Text = ""; //TextBox2.Text = ""; //TextBox3.Text = ""; //TextBox4.Text = ""; } protected void Button2_Click(object sender, EventArgs e) { TextBox5.Text = Count.ToString(); TextBox6.Text = Right.ToString(); TextBox7.Text = ((Right / (double)Count) * 100).ToString() + "%"; } }
3源代码:
<head runat="server"> <title></title> <style type="text/css"> .style1 { width: 38%; } .style2 { width: 132px; } </style> </head> <body> <form id="form1" runat="server"> <div style="height: 571px; font-size: xx-large; background-color: #00FFFF;"> <asp:Label ID="Label1" runat="server" Text=" "></asp:Label> <br /> <br /> <br /> <br /> <asp:TextBox ID="TextBox1" runat="server" BackColor="#FFCC99" Height="39px" Font-Size="XX-Large" MaxLength="32" Width="148px"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server" BackColor="#FFCC99" Height="37px" Width="40px" Font-Size="XX-Large"></asp:TextBox> <asp:TextBox ID="TextBox3" runat="server" BackColor="#FFCC99" Height="36px" Font-Size="XX-Large" Width="143px"></asp:TextBox> = <asp:TextBox ID="TextBox4" runat="server" BackColor="#FFCC99" Height="36px" Font-Size="XX-Large" Width="126px"></asp:TextBox> <br /> <br /> <br /> <asp:Button ID="Button1" runat="server" BackColor="#99CCFF" Height="31px" onclick="Button1_Click" Text="确定" Width="76px" /> <asp:Button ID="Button2" runat="server" BackColor="#99CCFF" Height="31px" onclick="Button2_Click" Text="完成" Width="65px" /> <br /> <br /> <table class="style1"> <tr> <td class="style2"> 总数:</td> <td> <asp:TextBox ID="TextBox5" runat="server" BackColor="#FFFF99" Height="26px"></asp:TextBox> </td> </tr> <tr> <td class="style2"> 正确:</td> <td> <asp:TextBox ID="TextBox6" runat="server" BackColor="#FFFF99" Height="27px"></asp:TextBox> </td> </tr> <tr> <td class="style2"> 正确率:</td> <td> <asp:TextBox ID="TextBox7" runat="server" BackColor="#FFFF99" Height="26px"></asp:TextBox> </td> </tr> </table> <br /> <br /> <br /> <br /> </div> </form> </body>
总结:
老师说的很对:"只有做作业,才能进步!",通过这几次作业的练习,
又学到了不少知识,弄懂了以前不懂得知识。这次又学会了接口的用法,
以后学以致用,通过不断做题,积累知识!