计算器软件实现系列(七)WPF+SQL+策略模式

时间:2023-01-09 22:37:44

一  整体概述

    本次设计主要是在WPF的页面中实现的,属于表现层的更换,数据库部分用的还是数据库的封装,其中引用了策略模式

二  设计思路

   1 在出题页面,进行试题的编辑,在编辑后会自动保存到数据库中

   计算器软件实现系列(七)WPF+SQL+策略模式

  2 试题编辑完毕后,把试题从数据库中导入到文本框中。进行相应的计算

计算器软件实现系列(七)WPF+SQL+策略模式

计算器软件实现系列(七)WPF+SQL+策略模式

  3 判断试题的正确与否,并且做出相关试题结果的统计

计算器软件实现系列(七)WPF+SQL+策略模式

三 代码

1 数据层代码

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

namespace shuxuefudao
{
    class shujuku
    {

        string str = "Data Source=.;Initial Catalog=jisuan;Integrated Security=True"; //连接数据库的字符串
        SqlConnection sqlcon = new SqlConnection(); //声明相关语句的设置
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        public int i = 0;   //相关变量的声明
        public string number1, number2, fuhao1; //出题时的变量
        public string ti, shu1, shu2, fuhao2;//做题时的变量
        
        public void lianjie()  //数据库连接的方法
        {
           
                sqlcon = new SqlConnection(str);

            
        }
        public void bianji()  //数据库中编辑试题的方法
        {
            lianjie();
            sqlcon.Open();
           
                string sqlstr = "insert into shuju(第一个数字,符号,第二个数字) values('" + number1 + "','" + fuhao1 + "','" + number2 + "')";
                SqlCommand comm = new SqlCommand(sqlstr, sqlcon);
                comm.ExecuteNonQuery();
            
            sqlcon.Close();
        }
        public void qingkong() //清空后台数据库的数据
        {
            lianjie();
            sqlcon.Open();
            
                string sqltr = "delete  from shuju";
                SqlCommand comm = new SqlCommand(sqltr, sqlcon);
                comm.ExecuteNonQuery();
                SqlDataReader reder = comm.ExecuteReader();
           
            sqlcon.Close();

        }
        public void JiSuan()  //在第一次计算后的计算方法
        {
            i++;
            lianjie();
            sqlcon.Open();
            string sqltr = "select * from shuju ";
            SqlCommand comm = new SqlCommand(sqltr, sqlcon);
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            shu1 = dt.Rows[i][0].ToString();
            fuhao2 = dt.Rows[i][1].ToString();
            shu2 = dt.Rows[i][2].ToString();
            sqlcon.Close();

        }
        public void JiSuan1()  //初次单击计时开始时调用的方法,即第一个运算式子的调用
        {
            lianjie();
            sqlcon.Open();
            string sqltr = "select * from shuju ";
            SqlCommand comm = new SqlCommand(sqltr, sqlcon);
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            shu1 = dt.Rows[0][0].ToString();
            fuhao2 = dt.Rows[0][1].ToString();
            shu2 = dt.Rows[0][2].ToString();
            sqlcon.Close();
        }
        public void DaoRu()  //把数据库里面的式子全部导入到文本框中的方法
        {
            lianjie();
            sqlcon.Open();
            string sqltr = "select * from shuju ";
            SqlCommand comm = new SqlCommand(sqltr, sqlcon);
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][0].ToString();
                dt.Rows[i][1].ToString();
                dt.Rows[i][2].ToString();
                ti += dt.Rows[i][0].ToString().Trim() + dt.Rows[i][1].ToString().Trim() + dt.Rows[i][2].ToString().Trim() + "=" + "\n";
            }
            sqlcon.Close();
        }
    }
}

 

2 逻辑层代码

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

namespace shuxuefudao
{
    class qita
    {
    }
    public interface Calculator //声明一个计算的接口
    {
        double Cal(double a, double b);
    }
    public class Add : Calculator   //接口实现加法运算
    {
        public double Cal(double a, double b)
        {
            double result = 0;
            result = a + b;
            return result;
        }
    }
    public class Sub : Calculator //接口实现减法运算
    {
        public double Cal(double a, double b)
        {
            double result = 0;
            result = a - b;
            return result;
        }
    }
    public class Mul : Calculator  //接口实现乘法运算
    {
        public double Cal(double a, double b)
        {
            double result = 0;
            result = a * b;
            return result;
        }
    }
    public class Div : Calculator  //接口实现除法运算
    {
        public double Cal(double a, double b)
        {
            double result = 0;
            result = a / b;
            return result;
        }
    }
    public class Environment        //定义那个需要动态改变算法的对象   
    {
        private Calculator calculate;
        public Environment(Calculator calculate)
        {
            this.calculate = calculate;
        }
        public double Cal(double a, double b, String m) //返回运算结果
        {
            return this.calculate.Cal(a, b);
        }
    }
}

 

3 表现层代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace shuxuefudao
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static int Count = 0; // 题目出的数量
        public static int zuode = 0; //做的题目数量
        public static int zhengque = 0;
        shujuku shuju = new shujuku();//实例化数据库对象,调用其中的方法
        private void button4_Click(object sender, RoutedEventArgs e) //编辑下一题的按钮
        {
            Count++;
            shuju.number1 = left.Text;
            shuju.fuhao1 = fuhao.Text;
            shuju.number2 = right.Text;
            shuju.bianji();
            shiti.Text += left.Text + fuhao.Text + right.Text + "=" + "\n";
            left.Clear();
            fuhao.Clear();
            right.Clear();

        }

        private void button1_Click(object sender, RoutedEventArgs e) //清空上次数据的方法调用
        {
            shuju.qingkong();
        }

        private void button2_Click(object sender, RoutedEventArgs e)//单击开始时方法的调用
        {
            shuju.JiSuan1();
            left.Text =shuju.shu1;
            fuhao.Text = shuju.fuhao2;
            right.Text = shuju.shu2;
        }

        private void jieguo_KeyDown(object sender, KeyEventArgs e)
        {
            try //异常处理机制,预防数组发生越界
            {
                Environment environment = null;
                double a = Convert.ToDouble(left.Text.Trim()); //为相关的变量赋值
                double b = Convert.ToDouble(right.Text.Trim());
                string m = fuhao.Text.Trim();
                switch (m)
                {
                    case "+":
                        environment = new Environment(new Add()); //策略模式的引用
                        break;
                    case "-":
                        environment = new Environment(new Sub());

                        break;
                    case "*":
                        environment = new Environment(new Mul());

                        break;
                    case "/":
                        environment = new Environment(new Div());

                        break;
                    default:
                        break;
                }

                if (Keyboard.IsKeyDown(Key.Enter))
                {

                    
                    string answer = environment.Cal(a, b, m).ToString();
                    daan.Text += answer + "\r\n";
                    if (jieguo.Text == answer.ToString())
                    {
                        MessageBox.Show("回答正确");
                        zuode++;
                        zhengque++;
                    }
                    else
                    {
                        MessageBox.Show("回答错误");
                        zuode++;
                    }
                    shuju.JiSuan();
                    left.Text = shuju.shu1;
                    fuhao.Text = shuju.fuhao2;
                    right.Text = shuju.shu2;
                    jieguo.Text = "";
                }

            }


            catch (Exception ex)
            {
                this.Hide();
                jieguo a = new jieguo();
                a.Show(); 
            }
               
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            shuju.DaoRu();
            shiti.Text = shuju.ti;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
            jieguo a = new jieguo();
            a.Show(); 
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace shuxuefudao
{
    /// <summary>
    /// jieguo.xaml 的交互逻辑
    /// </summary>
    public partial class jieguo : Window
    {
        public jieguo()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)//试题的统计结果
        {
            textBox1.Text = MainWindow.zuode.ToString();
            textBox2.Text = MainWindow.zhengque.ToString();
            textBox3.Text = ((MainWindow.zuode - (double)(MainWindow.zhengque))).ToString();
            textBox4.Text = ((MainWindow.zhengque / (double)(MainWindow.zuode)) * 100).ToString("f2") + "%";
        }
    }
}