三个数混合运算和三层架构

1,数据访问层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace SQL1
{
    class DB1//数据访问层!
    { 

        public SqlConnection conn = new SqlConnection();
        public  DataSet ds = new DataSet();
        public DataTable dt = new DataTable();
        public SqlDataAdapter sda = new SqlDataAdapter();
        public void dbcon(string constr)//连接数据库!
        {
            try
            {
                conn = new SqlConnection(constr);
            }
            catch(Exception e)
            {
                MessageBox.Show("数据库连接失败!" +e.ToString());
            }
 
        }
        public void opens()//打开数据库!
        {
            conn.Open();
        }
        public void closes()//关闭数据库!
        {
            conn.Close();
        }     
        public void Inster(string comstr)//插入数据!
        {
            SqlCommand comm = new SqlCommand(comstr,conn);
            try
            {
                int a = comm.ExecuteNonQuery();
                if (a > 0)
                {
                    MessageBox.Show("保存成功!");
                }
                else
                {
                    MessageBox.Show("保存失败!");
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
 
        }
        public void Read(string comstr)//读取数据!
        {
            sda = new SqlDataAdapter(comstr, conn);
            sda.Fill(ds);
            dt = ds.Tables[0];
 
        }
        public void del(string comstr)//删除数据!
        {
            SqlCommand comm = new SqlCommand(comstr,conn);
            int a = comm.ExecuteNonQuery();
            if (a > 0)
            {
                MessageBox.Show("删除失败!");
            }
            else
            {
                MessageBox.Show("删除成功!");
            }
        }
    }
}

2,业务逻辑层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SQL1
{
    
    class DB2//业务逻辑层
    {
        DB1 db1 = new DB1();
        public string number1;
        public string number2;
        public string number3;
        public string operation1;
        public string operation2;
        private int i = 0;
        string constr = "Data Source=.;Initial Catalog=Tiku1;Integrated Security=True";
        public void lianjie()//连接数据库!
        {
            db1.dbcon(constr);
        }
        public void charu(string a, string f1, string b, string f2, string c)//插入数据!
        {
            lianjie();
            string comstr = @"insert into Ti(number1,operation1,number2,operation2,number3) values('" + a + "','" + f1 + "','" + b + "','"+f2+"','"+c+"')";
            db1.opens();
            db1.Inster(comstr);
            db1.closes();
        }
        public void duqu(string comstr)//读出题
        {
            lianjie();       
            db1.Read(comstr);
            if (i < db1.dt.Rows.Count)
            {
                number1 = db1.dt.Rows[i][0].ToString().Trim();
                operation1 = db1.dt.Rows[i][1].ToString().Trim();
                number2 = db1.dt.Rows[i][2].ToString().Trim();
                operation2 = db1.dt.Rows[i][3].ToString().Trim();
                number3 = db1.dt.Rows[i][4].ToString().Trim();
                if (i == (db1.dt.Rows.Count - 1))
            {
                MessageBox.Show("你的题做完了,去休息吧!一会我们继续!");
            }

            }
            i++;
         
        }
        public void qingchu(string sltstr)//清除题库
        {
            lianjie();
            db1.opens();
            db1.del(sltstr);
            db1.closes();

        }
        public void jisuan(double a, string operation1,double b, string operation2,double c, string rightanswer)
        {
            bool aa = false;
            if (operation1 == "+" || operation1 == "-")
            {
                if (operation2 == "*" || operation2 == "/")
                {
                    aa = true;
                }
                else
                {
                    aa = false;
                }

            }
            else
            {
                aa = false;
            }
            if (aa == true)
            {
                Factionsss fass = new Factionsss(operation2);
                double answer1 = fass.cal(b, c);
                fass = new Factionsss(operation1);
                double answer = fass.cal(a, answer1);
                if (rightanswer == answer.ToString())
                {
                    MessageBox.Show("回答正确!");
                }
                else
                {
                    MessageBox.Show("回答错误!");
                }

            }
            else
            {
                Factionsss fas = new Factionsss(operation1);
                double ans = fas.cal(a, b);
                fas = new Factionsss(operation2);
                double answer = fas.cal(ans, c);
                if (rightanswer == answer.ToString())
                {
                    MessageBox.Show("回答正确!");
                }
                else
                {
                    MessageBox.Show("回答错误!");
                }

 
            }
 
        }
        
    }
}

3,表现层代码

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;

namespace SQL1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DB2 db2 = new DB2();
       
         
        private void button1_Click(object sender, EventArgs e)
        {
         
            db2.charu(textBox1.Text, comboBox1.Text, textBox2.Text, comboBox2.Text, textBox3.Text);
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sltstr = @"truncate table Ti";//清除表中数据
            db2.qingchu(sltstr);
        }

        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;

namespace SQL1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        DB2 db = new DB2();
        private void button1_Click(object sender, EventArgs e)
        {
            chuti();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            double a = double.Parse(textBox1.Text);
            double b = double.Parse(textBox2.Text);
            double c = double.Parse(textBox3.Text);
            string operation1 = label1.Text;
            string operation2 = label2.Text;
            if (e.KeyCode == Keys.Enter)
            {
                db.jisuan(a, operation1, b, operation2, c, textBox4.Text);
                textBox4.Clear();
                chuti();
            }
            

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            chuti();
        }

        private void chuti()
        {
            string comstr = @"select  number1,operation1,number2,operation2,number3  from Ti";
            db.duqu(comstr);
            textBox1.Text = db.number1;
            textBox2.Text = db.number2;
            textBox3.Text = db.number3;
            label1.Text = db.operation1;
            label2.Text = db.operation2;
 
        }
    }
}

4, 计算类以及策略模式的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SQL1
{
    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);
        }
    
    }
}

5,测试

 

 

 

 

6.总结:

不知道这样写属不属于三层架构,我倒目前为止是这样理解的!也请老师指正一下!???

 

posted @ 2015-12-13 10:13  小李少  阅读(289)  评论(4编辑  收藏  举报