标签:
1.Model类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Model { public class TIModel { private string number1; private string number2; private string number3; private string number4; private string operation1; private string operation2; private string operation3; public string Number1 { get { return number1; } set { number1 = value; } } public string Number2 { get { return number2; } set { number2 = value; } } public string Number3 { get { return number3; } set { number3 = value; } } public string Number4 { get { return number4; } set { number4 = value; } } public string Operation1 { get { return operation1; } set { operation1 = value; } } public string Operation2 { get { return operation2; } set { operation2 = value; } } public string Operation3 { get { return operation3; } set { operation3 = value; } } } }
2.Tool公共类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; using System.Data; namespace Tool { public class SqlDBhelper { string constr = "Data Source=.;Initial Catalog=TIKUDB;Integrated Security=True"; public SqlConnection conn = new SqlConnection(); public DataSet ds = new DataSet(); public DataTable dt = new DataTable(); public SqlDataAdapter sda = new SqlDataAdapter(); public void dbcon()//连接数据库! { conn = new SqlConnection(constr); } public void opens()//打开数据库! { conn.Open(); } public void closes()//关闭数据库! { conn.Close(); } public int Inster(string comstr,SqlParameter[] values)//插入数据! { int a = 0; dbcon(); SqlCommand comm = new SqlCommand(comstr, conn); if (values != null) { comm.Parameters.AddRange(values); } opens(); try { a = comm.ExecuteNonQuery(); } finally { closes(); } return a; } public DataTable Read(string comstr)//读取数据! { dbcon(); sda = new SqlDataAdapter(comstr, conn); sda.Fill(ds); dt = ds.Tables[0]; return dt; } public int execSql(string comstr)//删除数据! { dbcon(); int a = 0; SqlCommand comm = new SqlCommand(comstr, conn); opens(); try { a = comm.ExecuteNonQuery(); } finally { closes(); } return a; } } }
3. DAL数据库访问类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Tool; using Model; using System.Data; using System.Data.SqlClient; namespace DAL { public class TIDAL { SqlDBhelper helper = new SqlDBhelper(); public DataTable Read() { string sql = "select * from TI"; return helper.Read(sql); } public int InsterTI(TIModel rn) { string sql = "insert into TI values(@number1,@operation1,@number2,@operation2,@number3,@operation3,@number4)"; SqlParameter[] parms = { new SqlParameter("@number1",rn.Number1), new SqlParameter("@operation1",rn.Operation1), new SqlParameter("@number2",rn.Number2), new SqlParameter("@operation2",rn.Operation2), new SqlParameter("@number3",rn.Number3), new SqlParameter("@operation3",rn.Operation3), new SqlParameter("@number4",rn.Number4) }; return helper.Inster(sql, parms); } public int Delete() { string sql = "truncate table TI"; return helper.execSql(sql); } } }
4.BLL业务逻辑类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Model; using DAL; namespace BLL { public class TIBLL { TIDAL dal = new TIDAL(); public bool Add(TIModel rm)//插入数据 { if (dal.InsterTI(rm) > 0) { return true; } else { return false; } } public bool Delete()//删除数据 { if (dal.Delete() > 0) { return true; } else { return false; } } public DataTable Read() { return dal.Read(); } } }
5.UI层类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace UI { interface Iation//定义计算接口 { double Calation(double a, double b); } class Add : Iation//加法 { public double Calation(double a, double b) { return a + b; } } class Sub : Iation//减法 { public double Calation(double a, double b) { return a - b; } } class Mul : Iation//乘法 { public double Calation(double a, double b) { return a * b; } } class Div : Iation//除法 { public double Calation(double a, double b) { if (b == 0) { throw new Exception("除数不能为零!"); } else { return a / b; } } } class Factionsss//实现策略模式! { private Iation clation; public Factionsss(string operation) { switch (operation) { case "+": clation = new Add(); break; case "-": clation = new Sub(); break; case "*": clation = new Mul(); break; case "/": clation = new Div(); break; } } public double cal(double a, double b) { return clation.Calation(a, b); } } class Calculate { public double Result(double a,string operation1,double b,string operation2,double c,string operation3,double d) { double answer = 0; if (operation2 == "+" || operation2 == "-") { bool leftPriority; if (operation1 == "*" || operation1 == "/") { leftPriority = true; } else if (operation3 == "*" || operation3 == "/") { leftPriority = false; } else { leftPriority = true; } if (leftPriority == true) { Factionsss faction = new Factionsss(operation1); double answer1 = faction.cal(a, b); faction = new Factionsss(operation2); double answer2 = faction.cal(answer1, c); faction = new Factionsss(operation3); answer = faction.cal(answer2, d); } else { Factionsss faction = new Factionsss(operation3); double answer1 = faction.cal(c, d); faction = new Factionsss(operation2); double answer2 = faction.cal(b, answer1); faction = new Factionsss(operation1); answer = faction.cal(a, answer2); } } if (operation2 == "*" || operation2 == "/") { bool leftPriority; if (operation1 == "*" || operation1 == "/") { leftPriority = true; } else if (operation3 == "*" || operation3 == "/") { leftPriority = false; } else { leftPriority = false; } if (leftPriority == true) { Factionsss faction = new Factionsss(operation1); double answer1 = faction.cal(a, b); faction = new Factionsss(operation2); double answer2 = faction.cal(answer1, c); faction = new Factionsss(operation3); answer = faction.cal(answer2, d); } else { Factionsss faction = new Factionsss(operation2); double answer1 = faction.cal(b, c); faction = new Factionsss(operation3); double answer2 = faction.cal(answer1, d); faction = new Factionsss(operation1); answer = faction.cal(answer2, a); } } return answer; } } }
form1代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Model; using BLL; namespace UI { public partial class Form1 : Form { public Form1() { InitializeComponent(); } TIBLL bll = new TIBLL(); private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e)//开始 { TIModel rm = new TIModel(); rm.Number1 = textBox1.Text; rm.Number2 = textBox2.Text; rm.Number3 = textBox3.Text; rm.Number4 = textBox4.Text; rm.Operation1 = comboBox1.Text; rm.Operation2 = comboBox2.Text; rm.Operation3 = comboBox3.Text; if (bll.Add(rm) == true) { MessageBox.Show("保存成功!"); } else { MessageBox.Show("插入失败!"); } textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); } private void button2_Click(object sender, EventArgs e) { if (bll.Delete() == true) { MessageBox.Show("删除失败!"); } else { MessageBox.Show("删除成功!"); } } private void button3_Click(object sender, EventArgs e) { Form2 fas = new Form2(); fas.ShowDialog(); } } }
form2代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Model; using BLL; namespace UI { public partial class Form2 : Form { public Form2() { InitializeComponent(); } TIBLL bll = new TIBLL(); int i=0; private void Form2_Load(object sender, EventArgs e) { Reading(); } private void button1_Click(object sender, EventArgs e) { Reading(); } private void Reading() { DataTable dt = bll.Read(); textBox1.Text = dt.Rows[i][0].ToString().Trim(); label1.Text = dt.Rows[i][1].ToString().Trim(); textBox2.Text = dt.Rows[i][2].ToString().Trim(); label2.Text = dt.Rows[i][3].ToString().Trim(); textBox3.Text = dt.Rows[i][4].ToString().Trim(); label3.Text = dt.Rows[i][5].ToString().Trim(); textBox4.Text = dt.Rows[i][6].ToString().Trim(); i++; if (i == dt.Rows.Count - 1) { MessageBox.Show("你做完了!"); } } private void textBox5_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { Calculate calcuate = new Calculate(); double a=double.Parse(textBox1.Text); double b=double.Parse(textBox2.Text); double c=double.Parse(textBox3.Text); double d=double.Parse(textBox4.Text); string operation1=label1.Text; string operation2=label2.Text; string operation3=label3.Text; double answer = calcuate.Result(a, operation1, b, operation2, c, operation3, d); if (textBox5.Text == answer.ToString()) { MessageBox.Show("回答正确!"); } else { MessageBox.Show("回答错误!"); } textBox5.Clear(); Reading(); } } } }
6,数据库存题测试
7,总结
一开始真的不懂怎么去实现程序集下的三层架构,直到现在依然感觉掌握的很灵活,还有就是四个数的运算,老师让去掉工厂模式,但是去掉工厂模式代码的编写会更加的麻烦,所以就没有去掉工厂模式,但是总感觉这样写程序的性能会大幅度降低,还有就是计算类的存放问题,应该放到哪一个层里面搞不明白,本来想放在业务逻辑层的,但是最终还是放在了UI层!
标签:
原文地址:http://www.cnblogs.com/lizanqirxx/p/5054821.html