• 计算器软件实现系列(六)windowform窗体+SQL+策略模式


    一 整体概述

       这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是txt文件保存,现在更正用SQL数据库,现在更改了以前的文件保存形式,是三层架构中数据层的更换,但是仍然采用了设计模式中的策略模式,对于在wpf中实现的要求,会在今后中进一步实现的!

    二 数据库代码的封装 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    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,shu1,fuhao2,shu2,ti;
            public void lianjie()  //数据库连接的方法
            {
                try
                {
                    sqlcon = new SqlConnection(str);
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据连接不成功" + ex.Message);
                }
            }
            public void bianji()  //数据库中编辑试题的方法
            {
                lianjie();
                sqlcon.Open();
                try
                {
                    string sqlstr = "insert into shuju(第一个数字,符号,第二个数字) values('" + number1 + "','" + fuhao1 + "','" + number2 + "')";
                    SqlCommand comm = new SqlCommand(sqlstr, sqlcon);
                    comm.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("出题失败" + ex.ToString());
                }
                sqlcon.Close();
            }
            public void qingkong() //清空后台数据库的数据
            {
                lianjie();
                sqlcon.Open();
                try
                {
                    string sqltr = "delete  from shuju";
                    SqlCommand comm = new SqlCommand(sqltr, sqlcon);
                    comm.ExecuteNonQuery();
                    SqlDataReader reder = comm.ExecuteReader();
                    MessageBox.Show("删除成功");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("删除失败" + ex.ToString());
                }
                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() + "="+"
    ";
                }
                sqlcon.Close();
            }
        }
    }

    三 策略模式的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    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);
        }
     }
     }
     

    四 运算主体的代码

    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 System.IO;
    
    namespace shuxuefudao
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            shujuku bbb = new shujuku();
            string path = "E:
    tf";
            public static int Count = 0; // 题目出的数量
            public static int zuode = 0; //做的题目数量
            public static int zhengque = 0;
            public static int lefttime;
            public static int time;
            public static int sum;
            private void Form1_Load(object sender, EventArgs e)
            {
              
            }
            private void open_Click(object sender, EventArgs e) //打开文件的方法
            {
                OpenFileDialog TxTOpenDialog = new OpenFileDialog();
                TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
                {
                    path = TxTOpenDialog.FileName;
                    this.richTextBox1.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                    save.Enabled = false;
                    open.Enabled = false;
                    MessageBox.Show("读取成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
    
            private void save_Click(object sender, EventArgs e) //保存文件的方法
            {
                SaveFileDialog TxtSaveDialog = new SaveFileDialog();
                TxtSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (File.Exists(path))
                {
    
                    this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                    MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.richTextBox1.Clear();
                    save.Enabled = false;
                }
                else
                {
                    if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
                    {
    
                        this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.RichText);
                        MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.richTextBox1.Clear();
                        save.Enabled = false;
                    }
                }
            }
    
            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                save.Enabled = true;
                if (this.richTextBox1.Text == "" || this.richTextBox1.Text == null)
                {
                    open.Enabled = true;
                }
            }
            private void open2_Click(object sender, EventArgs e) //打开试题的方法
            {
                OpenFileDialog TxTOpenDialog = new OpenFileDialog();
                TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
                {
                    path = TxTOpenDialog.FileName;
                    this.richTextBox2.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                    save.Enabled = false;
                    open.Enabled = false;
                    MessageBox.Show("打开成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
    
            private void daoru_Click(object sender, EventArgs e) //导入试题的方法
            {
                bbb.DaoRu();
                richTextBox2.Text = bbb.ti;
               
            }
    
            private void daan1_Click(object sender, EventArgs e)
            {
                if (daan1.Text == "显示答案")
                {
                    daan.PasswordChar = Convert.ToChar(0);
                    daan1.Text = "隐藏答案";
                }
                else if (daan1.Text == "隐藏答案")
                {
                    daan.PasswordChar = '.';
                    daan1.Text = "显示答案";
    
                }
            }
    
            private void kaishi_Click(object sender, EventArgs e)
            {
    
                bbb.JiSuan1();
                textBox1.Text = bbb.shu1;
                textBox2.Text = bbb.fuhao2;
                textBox3.Text = bbb.shu2; 
                int minute;
                 try
                 {
                     minute = int.Parse(this.shijian.Text);
                 }
                 catch (System.Exception ex)
                 {
                     this.shijian1.Text = "输入错误";
                     return;
                 }
                 lefttime = minute;
                 this.timer1.Interval = 1000;
                 this.timer1.Enabled = true;
                 this.timer1.Start();
    
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                time = Convert.ToInt32(shijian.Text);
                if (lefttime <= 0)
                {
                    timer1.Enabled = false;
                    MessageBox.Show("答题时间到!");
                    textBox4.Enabled = false;
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();
                }
                this.shijian1.Text = "剩余时间" + lefttime.ToString() + "";
                lefttime--;  
            }
    
            private void jieshu_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
    
            private void button1_Click(object sender, EventArgs e) //请编辑下道题的事件
            {
                Count++;
                ti.Text = Count.ToString();
                bbb.number1=left.Text;
                bbb.fuhao1=fuhao.Text;
                bbb.number2=right.Text;
                bbb.bianji();
                richTextBox1.Text += left.Text + fuhao.Text + right.Text+"="+"
    ";
                left.Clear();
                fuhao.Clear();
                right.Clear();
            }
            private void textBox4_KeyDown(object sender, KeyEventArgs e)
            {
                try //异常处理机制,预防数组发生越界
                {
                    Environment environment = null;
                    double a = Convert.ToDouble(textBox1.Text.Trim()); //为相关的变量赋值
                    double b = Convert.ToDouble(textBox3.Text.Trim());
                    string m = textBox2.Text.Trim();
                    int result;
                    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 (e.KeyCode == Keys.Enter)
                    {
    
                        if (int.TryParse(textBox4.Text, out result) == false)
                        {
                            MessageBox.Show("请输入数字");
                        }
                        string answer = environment.Cal(a, b, m).ToString();
                        daan.Text += answer + "
    ";
                        if (textBox4.Text == answer.ToString())
                        {
                            MessageBox.Show("回答正确");
                            zuode++;
                            zhengque++;
                        }
                        else
                        {
                            MessageBox.Show("回答错误");
                            zuode++;
                        }
                    bbb.JiSuan();
                    textBox1.Text = bbb.shu1;
                    textBox2.Text = bbb.fuhao2;
                    textBox3.Text = bbb.shu2;
                    textBox4.Text = "";  
                        }
    
                    }
    
    
                    catch (Exception ex)
                    {
                        Form2 frm = new Form2();
                        frm.ShowDialog();
                    }
                   
            
            }
    
           
            private void button2_Click(object sender, EventArgs e) //清空上次编辑试题的方法
            {
                bbb.qingkong();
            }
            private void button3_Click(object sender, EventArgs e) //退出程序的方法
            {
                Application.Exit();
            }
        }
    }

    五  运行界面的展示

    1 试题的编辑

     

    2 进行计算,并且判断正误

    3 统计做题情况

  • 相关阅读:
    手机端不加载js文件,PC端要加载js文件
    JS数组去重和取重
    jquery遍历一个数组
    2个轮播地址
    动感Loading文字
    仿265网站LOGO,会盯着你看的眼睛
    git学习
    c++ primer 5th 笔记:第十一章
    c++ primer 5th 笔记:第十章
    c++ primer 5th 笔记:第九章
  • 原文地址:https://www.cnblogs.com/wyh19941210/p/5025745.html
Copyright © 2020-2023  润新知