设计思路:

这次要用数据库存储题目,我想到的是用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出题的更多相关文章

  1. 介绍一款原创的四则运算算式生成器:CalculateIt2

    家里小朋友读一年级了,最近每天都有一些10以内的加减法口算练习,作为程序员爸爸,自然也是想办法能够偷懒,让电脑出题,给小朋友做些练习.于是,自己在业余时间开发了一个四则运算算式生成器,名为:Calcu ...

  2. 作业二:个人编程项目——编写一个能自动生成小学四则运算题目的程序

    1. 编写一个能自动生成小学四则运算题目的程序.(10分)   基本要求: 除了整数以外,还能支持真分数的四则运算. 对实现的功能进行描述,并且对实现结果要求截图.   本题发一篇随笔,内容包括: 题 ...

  3. 四则运算appNABCD模型

    团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  4. 第一章-第一题(小学生四则运算)--By郭青云

    1.项目需求 a) 除了整数以外,还要支持真分数的四则运算. (例如:  1/6 + 1/8 = 7/24) b) 让程序能接受用户输入答案,并判定对错. 最后给出总共 对/错 的数量. c) 逐步扩 ...

  5. 一个简易的四则运算单元...(15.12.15 BUG更新)

    网上找的, 没有作者信息, 只能在这里感谢一下了, 支持标准写法的四则运算 --2015-12-15 修改了一个内存泄漏的BUG - Pop方法没有释放申请的内存 unit Base.Calculat ...

  6. 利用ANTLR4实现一个简单的四则运算计算器

    利用ANTLR4实现一个简单的四则运算计算器 ANTLR4介绍 ANTLR能够自动地帮助你完成词法分析和语法分析的工作, 免去了手写去写词法分析器和语法分析器的麻烦 它是基于LL(k)的, 以递归下降 ...

  7. 【实践】js实现简易的四则运算计算器

    最近看了一个大神推荐的某公司面试程序员的js 面试题,题目是用js 做一个计算器于是跟着大神的思想自己做了一下 ps:功能还没有完善好毕竟自己还是一只菜鸟还在不断学习中. 闲话不多说先上css代码 & ...

  8. HDU 5938 Four Operations(四则运算)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  9. C语言实现四则运算

    学生:宋丹丹 张潇裕 #include<iostream>#include<ctime>using namespace std;void main(){ int x1,x2,a ...

  10. 第五篇——C++实现四则运算

    写一个能自动生成小学四则运算题目的命令行 “软件”, 分别满足下面的各种需求.下面这些需求都可以用命令行参数的形式来指定: a) 除了整数以外,还要支持真分数的四则运算. (例如: 1/6 + 1/8 ...

随机推荐

  1. jQuery的一点小结

    1.jQuery常用选择器 筛选: $('div').has('p'); // 选择包含p元素的div元素 $('div').not('.myClass'); //选择class不等于myClass的 ...

  2. Linux内核模块Makefile学习

    在<Linux设备驱动程序>一书中读到的内核模块编译Makefile,不是非常理解,在查询很多资料后,在这里做个总结. 书中Makefile代码: ifneq ($(KERNELRELEA ...

  3. linux-2.6内核驱动学习——jz2440之输入子系统

    如果按照上一篇记录的那样,只有本公司的人或者自己才能使用驱动.想写出一个通用的驱动程序,让其他应用程序来无缝移植,需要使用现成的驱动——输入子系统. /drivers/input/input.c #d ...

  4. [转]Docker 生产环境之配置容器 - 限制容器资源

    默认情况下,容器没有资源限制,可以使用主机内核调度程序允许的给定资源.Docker 提供了一些方法来控制容器可以使用多少内存.CPU 或块 IO,并设置 docker run 命令的运行时配置标志.本 ...

  5. Bluebox Security最新提报Android漏洞的初步探讨(转)

    Bluebox Security在7月3号的时候,在官网上发布了一个据称99%  Android机器都有的一个漏洞.国内最早在4号开始有媒体报道,并持续升温.该漏洞可使攻击者在不更改Android应用 ...

  6. 【转载】COM 组件设计与应用(十三)——事件和通知(VC6.0)

    原文:http://vckbase.com/index.php/wv/1243.html 一.前言 我的 COM 组件运行时产生一个窗口,当用户双击该窗口的时候,我需要通知调用者: 我的 COM 组件 ...

  7. 15-[mysql内置功能]--函数,流程控制 (未完成)

    1.MySQL中提供了许多内置函数 一.数学函数 ROUND(x,y) 返回参数x的四舍五入的有y位小数的值 RAND() 返回0到1内的随机值,可以通过提供一个参数(种子)使RAND()随机数生成器 ...

  8. AFO预定

    妈耶 数论题都不会 推不出式子 题解都看不懂 还是思维jiang化了 布星了 吃枣药丸 祝yyb进队 祝zsy进队 祝鸡贼进队

  9. UWP 检测网络状态

    最近发现Community Toolkit有了网络辅助类,貌似很早就有了... 很不错,还是用.给大家分享一下. 1. 检测网络是否可用 2. 检测网络是否是计费模式? 3. 检测网络接入类型 4. ...

  10. 服务器路由配置--Route

    第1章 命令配置 虚拟服务器 网卡配置信息 虚拟网卡名称 虚拟网卡模式 服务器01 eth1 10.0.0.10/24 nat模式 服务器02 eth2 10.0.0.11/24 nat模式 eth3 ...