四则运算《《《《SQL出题

时间:2021-03-07 19:13:11

设计思路:

这次要用数据库存储题目,我想到的是用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

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Dataopear
{
class arictor
{
public interface Calculator //声明计算接口
{
double Cal(double x, double y);
}
private double x; //定义x变量; public double X //封装字段
{
get { return x; }
set { x = value; } }
private double y; //定义y变量 public double Y //封装字段
{
get { return y; }
set { y = value; } }
public class Add : Calculator //接口法运算
{
public double Cal(double x, double y)
{
double result = ;
result = x + y;
return result;
}
}
public class Sub : Calculator
{
public double Cal(double x, double y)
{
double result = ;
result = x - y;
return result;
}
}
public class Mul : Calculator
{
public double Cal(double x, double y)
{
double result = ;
result = x * y;
return result;
}
}
public class Div : Calculator
{
public double Cal(double x, double y)
{
double result = ;
result = x/ y;
return result;
}
}
public class Opear //定义运算符
{
private Calculator calculate;
public Opear(Calculator calculate)
{
this.calculate = calculate;
}
public double Cal(double x, double y, String op) //返回运算结果
{
return this.calculate.Cal(x, y);
}
}
}
}

Form1.cs

 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.Data.SqlClient;
namespace Dataopear
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
}
public static int Count = ; //计算所做的题数
public static int Right = ; //回答正确的题数
string seltStr = @"select numberID,number1,symbol,number2 from opear";
DBCon db = new DBCon(); private void Form1_Load(object sender, EventArgs e) //窗体加载
{
db.dbcon();
db.dbFill(seltStr);
comboBox1.ValueMember = "numberID";
comboBox1.DataSource = db.dt; } private void dbSelect_Click(object sender, EventArgs e) //调用查询的方法
{ db.dbcon();
db.dbFill(seltStr);
dataGridView1.DataSource = db.dt; } private void dbAdd_Click(object sender, EventArgs e) //调用插入的方法
{
db.dbcon();
string insertInfo = "insert into opear(numberID,number1,symbol,number2)values('" + comboBox1.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
db.dbInsert(insertInfo);
db.dbFill(seltStr);
dataGridView1.DataSource = db.dt;
MessageBox.Show("增加试题成功!");
} private void dbUpdate_Click(object sender, EventArgs e) //调用修改的方法
{
db.dbcon();
string strUpd = "update opear set number1='" + textBox1.Text.Trim() + "',symbol='" + textBox2.Text.Trim() + "',number2='" +
textBox3.Text.Trim() + "' where numberID='" + comboBox1.Text.Trim() + "'";
db.dbUpdate(strUpd);
db.dbFill(seltStr);
dataGridView1.DataSource = db.dt;
MessageBox.Show("修改成功!"); } private void dbDelete_Click(object sender, EventArgs e) //调用删除的方法
{
db.dbcon();
string strsUpd = "delete from opear where numberID='" + comboBox1.Text.Trim() + "'";
db.dbDelete(strsUpd);
db.dbFill(seltStr);
dataGridView1.DataSource = db.dt; } private void dbClose_Click(object sender, EventArgs e) //关闭程序
{
Application.Exit();
} private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) //numberID的事件
{
string selinfo = "select numberID,number1,symbol,number2 from opear where numberID='" + comboBox1.Text.ToString().Trim() + "'";
db.dbcon();
db.dbSelect(selinfo);
if (db.sdr.Read())
{
textBox1.Text = db.sdr["number1"].ToString();
textBox2.Text = db.sdr["symbol"].ToString();
textBox3.Text = db.sdr["number2"].ToString();
}
}
private void textBox4_KeyDown(object sender, KeyEventArgs e)
{
arictor.Opear opear = null;
double x = Convert.ToDouble(textBox1.Text); //为相关的变量赋值
double y = Convert.ToDouble(textBox3.Text);
string op = textBox2.Text;
switch (op)
{
case "+":
opear = new arictor.Opear(new arictor.Add()); //策略模式的引用
break;
case "-":
opear = new arictor.Opear(new arictor.Sub()); break;
case "*":
opear = new arictor.Opear(new arictor.Mul()); break;
case "÷":
opear = new arictor.Opear(new arictor.Div()); break;
default:
break;
} if (e.KeyCode == Keys.Enter)
{
string answer = opear.Cal(x, y, op).ToString();
if (textBox4.Text == answer.ToString())
{
MessageBox.Show("回答正确");
Count++;
Right++;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear(); }
else
{
MessageBox.Show("回答错误");
Count++;
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
}
} private void end_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
} }
}

Form2.cs

 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 Dataopear
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.Count.ToString();
textBox2.Text = Form1.Right.ToString();
textBox3.Text = ((Form1.Right / (double)(Form1.Count)) * ).ToString() + "%";
} }
}

原始数据:

四则运算《《《《SQL出题

导入数据:

四则运算《《《《SQL出题

修改试题:

四则运算《《《《SQL出题

增加试题:

四则运算《《《《SQL出题

删除试题:

四则运算《《《《SQL出题

答题:

四则运算《《《《SQL出题

结束运算:

四则运算《《《《SQL出题

总结:

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