• 四则运算《《《《SQL出题


    设计思路:

           这次要用数据库存储题目,我想到的是用SQL server数据库,用dataGridView控件读取数据。

    具体实现:

    DBCon.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Data;
     6 using System.Data.SqlClient;
     7 using System.Windows.Forms;
     8 namespace Dataopear
     9 {
    10     class DBCon
    11     {
    12 
    13         public string strCon = @"Data Source=.;Initial Catalog=aritemtic;Integrated Security=True";  //连接数据库
    14         public SqlConnection sqlCon = new SqlConnection();
    15         public SqlDataAdapter sda = new SqlDataAdapter();
    16         public DataSet ds = new DataSet();
    17         public DataTable dt = new DataTable(); //数据源
    18         public SqlDataReader sdr; //数据阅读器
    19         public void dbcon()
    20         {
    21             try
    22             {
    23                 sqlCon = new SqlConnection(strCon);
    24             }
    25             catch (Exception e)
    26             {
    27                 MessageBox.Show("数据库连接不成功:" + e.ToString());
    28             }
    29         }
    30         public void dbFill(string selstr)
    31         {
    32             dt.Clear();  //清空
    33             sda = new SqlDataAdapter(selstr, strCon);//填充数据集
    34             sda.Fill(ds, "opear");
    35             dt = ds.Tables["opear"];
    36         }
    37         public void dbSelect(string showInfo)  //查询的方法
    38         {
    39            
    40             sqlCon.Open();
    41             SqlCommand sqlcmd = new SqlCommand(showInfo, sqlCon);
    42             sdr = sqlcmd.ExecuteReader();
    43            
    44         }
    45         public void dbInsert(string insertInfo)  //插入的方法
    46         {
    47             sqlCon.Open();
    48             SqlCommand sqlcmd = new SqlCommand(insertInfo, sqlCon);
    49             try
    50             {
    51                 sqlcmd.ExecuteNonQuery();
    52             }
    53             catch (Exception e)
    54             {
    55                 MessageBox.Show("数据插入失败" + e.ToString());
    56             }
    57             sqlCon.Close();
    58         }
    59        
    60         public void dbUpdate(string updStr)  //修改的方法
    61         {
    62             sqlCon.Open();
    63             SqlCommand sqlcmd = new SqlCommand(updStr, sqlCon);
    64             try
    65             {
    66                 sqlcmd.ExecuteNonQuery();
    67                 MessageBox.Show("数据修改成功!");
    68             }
    69             catch (Exception e)
    70             {
    71                 MessageBox.Show("数据库修改失败" + e.ToString());
    72             }
    73             sqlCon.Close();
    74         }
    75         public void dbDelete(string delStr)  //删除的方法
    76         {
    77             sqlCon.Open();
    78             SqlCommand sqlcmd = new SqlCommand(delStr, sqlCon);
    79             try
    80             {
    81                 sqlcmd.ExecuteNonQuery();
    82                MessageBox.Show("数据删除成功!");
    83             }
    84             catch (Exception e)
    85             {
    86                 MessageBox.Show("数据删除失败" + e.ToString());
    87             }
    88             sqlCon.Close();
    89         }
    90     }
    91 
    92 
    93         }
    94 
    95     

    策略模式 arctor.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Dataopear
     7 {
     8     class arictor
     9     {
    10          public interface Calculator        //声明计算接口
    11         {
    12             double Cal(double x, double y);
    13         }
    14         private double x;                //定义x变量;
    15 
    16         public double X                  //封装字段
    17         {
    18             get { return x; }
    19             set { x = value; }
    20 
    21         }
    22         private double y;               //定义y变量
    23 
    24 
    25         public double Y                  //封装字段
    26         {
    27             get { return y; }
    28             set { y = value; }
    29 
    30         }
    31         public class Add : Calculator            //接口法运算
    32         {
    33             public double Cal(double x, double y)
    34             {
    35                 double result = 0;
    36                 result = x + y;
    37                 return result;
    38             }
    39         }
    40         public class Sub : Calculator 
    41         {
    42             public double Cal(double x, double y)
    43             {
    44                 double result = 0;
    45                 result = x - y;
    46                 return result;
    47             }
    48         }
    49         public class Mul : Calculator 
    50         {
    51             public double Cal(double x, double y)
    52             {
    53                 double result = 0;
    54                 result = x * y;
    55                 return result;
    56             }
    57         }
    58         public class Div : Calculator  
    59         {
    60             public double Cal(double x, double y)
    61             {
    62                 double result = 0;
    63                 result = x/ y;
    64                 return result;
    65             }
    66         }
    67         public class Opear                  //定义运算符  
    68         {
    69             private Calculator calculate;
    70             public Opear(Calculator calculate)
    71             {
    72                 this.calculate = calculate;
    73             }
    74             public double Cal(double x, double y, String op)  //返回运算结果
    75             {
    76                 return this.calculate.Cal(x, y);
    77             }
    78         }
    79     }
    80 }
    81     

    Form1.cs

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.Data.SqlClient;
     10 namespace Dataopear
     11 {
     12     public partial class Form1 : Form
     13     {
     14 
     15         public Form1()
     16         {
     17             InitializeComponent();
     18         }
     19         public static int Count = 0;               //计算所做的题数
     20         public static int Right = 0;               //回答正确的题数
     21         string seltStr = @"select numberID,number1,symbol,number2 from opear";
     22         DBCon db = new DBCon();
     23 
     24         private void Form1_Load(object sender, EventArgs e)  //窗体加载
     25         {
     26             db.dbcon();
     27             db.dbFill(seltStr);
     28             comboBox1.ValueMember = "numberID";
     29             comboBox1.DataSource = db.dt;
     30 
     31         }
     32 
     33         private void dbSelect_Click(object sender, EventArgs e) //调用查询的方法
     34         {
     35 
     36             db.dbcon();
     37             db.dbFill(seltStr);
     38             dataGridView1.DataSource = db.dt;
     39 
     40         }
     41 
     42         private void dbAdd_Click(object sender, EventArgs e) //调用插入的方法
     43         {
     44             db.dbcon();
     45             string insertInfo = "insert into opear(numberID,number1,symbol,number2)values('" + comboBox1.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
     46             db.dbInsert(insertInfo);
     47             db.dbFill(seltStr);
     48             dataGridView1.DataSource = db.dt;
     49             MessageBox.Show("增加试题成功!");
     50         }
     51 
     52         private void dbUpdate_Click(object sender, EventArgs e) //调用修改的方法
     53         {
     54             db.dbcon();
     55             string strUpd = "update opear set number1='" + textBox1.Text.Trim() + "',symbol='" + textBox2.Text.Trim() + "',number2='" +
     56                 textBox3.Text.Trim() + "' where numberID='" + comboBox1.Text.Trim() + "'";
     57             db.dbUpdate(strUpd);
     58             db.dbFill(seltStr);
     59             dataGridView1.DataSource = db.dt;
     60             MessageBox.Show("修改成功!");
     61 
     62         }
     63 
     64         private void dbDelete_Click(object sender, EventArgs e) //调用删除的方法
     65         {
     66             db.dbcon();
     67             string strsUpd = "delete from opear where numberID='" + comboBox1.Text.Trim() + "'";
     68             db.dbDelete(strsUpd);
     69             db.dbFill(seltStr);
     70             dataGridView1.DataSource = db.dt;
     71 
     72         }
     73 
     74         private void dbClose_Click(object sender, EventArgs e) //关闭程序
     75         {
     76             Application.Exit();
     77         }
     78 
     79         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)   //numberID的事件
     80         {
     81             string selinfo = "select numberID,number1,symbol,number2 from opear where numberID='" + comboBox1.Text.ToString().Trim() + "'";
     82             db.dbcon();
     83             db.dbSelect(selinfo);
     84             if (db.sdr.Read())
     85             {
     86                 textBox1.Text = db.sdr["number1"].ToString();
     87                 textBox2.Text = db.sdr["symbol"].ToString();
     88                 textBox3.Text = db.sdr["number2"].ToString();
     89             }
     90         }
     91         private void textBox4_KeyDown(object sender, KeyEventArgs e) 
     92         {
     93             arictor.Opear opear = null;
     94             double x = Convert.ToDouble(textBox1.Text); //为相关的变量赋值
     95             double y = Convert.ToDouble(textBox3.Text);
     96             string op = textBox2.Text;
     97             switch (op)
     98             {
     99                 case "+":
    100                     opear = new arictor.Opear(new arictor.Add()); //策略模式的引用
    101                     break;
    102                 case "-":
    103                     opear = new arictor.Opear(new arictor.Sub());
    104 
    105                     break;
    106                 case "*":
    107                     opear = new arictor.Opear(new arictor.Mul());
    108 
    109                     break;
    110                 case "÷":
    111                     opear = new arictor.Opear(new arictor.Div());
    112 
    113                     break;
    114                 default:
    115                     break;
    116             }
    117 
    118             if (e.KeyCode == Keys.Enter)
    119             {
    120                 string answer = opear.Cal(x, y, op).ToString();
    121                 if (textBox4.Text == answer.ToString())
    122                 {
    123                     MessageBox.Show("回答正确");
    124                     Count++;
    125                     Right++;
    126                     textBox1.Clear();
    127                     textBox2.Clear();
    128                     textBox3.Clear();
    129                     textBox4.Clear();
    130 
    131                 }
    132                 else
    133                 {
    134                     MessageBox.Show("回答错误");
    135                     Count++;
    136                     textBox1.Clear();
    137                     textBox2.Clear();
    138                     textBox3.Clear();
    139                     textBox4.Clear();
    140                 }
    141             }
    142         }
    143 
    144         private void end_Click(object sender, EventArgs e)
    145         {
    146             Form2 frm2 = new Form2();
    147             frm2.ShowDialog();
    148         }
    149 
    150       
    151     }
    152 }
    153     

    Form2.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 
    10 namespace Dataopear
    11 {
    12     public partial class Form2 : Form
    13     {
    14         public Form2()
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         private void Form2_Load(object sender, EventArgs e)
    20         {
    21             textBox1.Text = Form1.Count.ToString();
    22             textBox2.Text = Form1.Right.ToString();
    23             textBox3.Text = ((Form1.Right / (double)(Form1.Count)) * 100).ToString() + "%";
    24         }
    25 
    26       
    27     }
    28 }

    原始数据:

    导入数据:

    修改试题:

    增加试题:

    删除试题:

    答题:

    结束运算:

    总结:

          这次作业比之前的作业复杂一点,工厂模式暂时还没有弄清楚,所以用的策略模式实现的运算。

  • 相关阅读:
    2017浙江工业大学-校赛决赛 BugZhu抽抽抽!!
    数据可视化建设是企业战略决策之刚需
    SIMPLE_DEV_PM_OPS宏
    Java连接程序数据源
    ROS(indigo)一个简单灵活和可扩展的2D多机器人仿真器stdr_simulator
    ROS_Kinetic_21 使用Qt Creator Plug in即ros_qtc_plugin
    ROS(indigo)使用Qt Creator Plug in即ros_qtc_plugin
    现代控制理论教学与半年工作总结(未完成待补充)
    企业应该如何运用商业智能
    USB有时adb shell连不上设备
  • 原文地址:https://www.cnblogs.com/thinking-star/p/5042873.html
Copyright © 2020-2023  润新知