一 整体概述

这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是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 = ; //相关变量的声明
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[];
shu1 = dt.Rows[i][].ToString();
fuhao2 = dt.Rows[i][].ToString();
shu2 = dt.Rows[i][].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[];
shu1 = dt.Rows[][].ToString();
fuhao2 = dt.Rows[][].ToString();
shu2 = dt.Rows[][].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[];
for (int i = ; i < dt.Rows.Count; i++)
{
dt.Rows[i][].ToString();
dt.Rows[i][].ToString();
dt.Rows[i][].ToString();
ti += dt.Rows[i][].ToString().Trim() + dt.Rows[i][].ToString().Trim() + dt.Rows[i][].ToString().Trim() + "="+"\n";
}
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 = ;
result = a + b;
return result;
}
}
public class Sub : Calculator //接口实现减法运算
{
public double Cal(double a, double b)
{
double result = ;
result = a - b;
return result;
}
}
public class Mul : Calculator //接口实现乘法运算
{
public double Cal(double a, double b)
{
double result = ;
result = a * b;
return result;
}
}
public class Div : Calculator //接口实现除法运算
{
public double Cal(double a, double b)
{
double result = ;
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:\rtf";
public static int Count = ; // 题目出的数量
public static int zuode = ; //做的题目数量
public static int zhengque = ;
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();
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 = ;
this.timer1.Enabled = true;
this.timer1.Start(); } private void timer1_Tick(object sender, EventArgs e)
{
time = Convert.ToInt32(shijian.Text);
if (lefttime <= )
{
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+"="+"\n";
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 + "\r\n";
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 统计做题情况

计算器软件实现系列(六)windowform窗体+SQL+策略模式的更多相关文章

  1. Java 设计模式系列(十二)策略模式(Strategy)

    Java 设计模式系列(十二)策略模式(Strategy) 策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换.策略模式使得算法可以 ...

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

    一  整体概述 本次设计主要是在WPF的页面中实现的,属于表现层的更换,数据库部分用的还是数据库的封装,其中引用了策略模式 二  设计思路 1 在出题页面,进行试题的编辑,在编辑后会自动保存到数据库中 ...

  3. 计算器软件实现系列(五)策略模式+asp.net

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  4. SpringCloud系列六:Eureka的自我保护模式、IP选择、健康检查

    1. 回顾 前面讲了很多Eureka的用法,比如Eureka Server.Eureka Server的高可用.Eureka Server的用户认证(虽然未完全实现).元数据等, 这章将讲解剩下的自我 ...

  5. 学C#之设计模式系列笔记(1)策略模式

    一.借鉴说明 1.<Head First Design Patterns>(中文名<深入浅出设计模式>) 2.维基百科,策略模式,https://zh.wikipedia.or ...

  6. mybatis(六):设计模式 - 策略模式

  7. Java设计模式系列之策略模式

    策略模式的定义: 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换,策略模式让算法独立于使用它的客户而独立变化. 策略模式使这些算法在客户端调用它们的时候能够互不影响地变化 ...

  8. SQL Server 2008空间数据应用系列六:基于SQLCRL的空间数据可编程性

    原文:SQL Server 2008空间数据应用系列六:基于SQLCRL的空间数据可编程性 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 ...

  9. GIS基础软件及操作(六)

    原文 GIS基础软件及操作(六) 练习六.空间分析的应用 1.加深对缓冲区分析基本原理.方法的认识:2.熟练掌握距离制图创建缓冲区技术方法.3.掌握利用缓冲区分析方法解决地学空间分析问题的能力. 1. ...

随机推荐

  1. Ubuntu 16.04 搭建 ELK

    1.安装Java JDK sudo apt-get install default-jdk 2.安装Elasticsearch 1.导入Elasticsearch的GPG公钥 wget -qO - h ...

  2. Linux中将端口(80)重定向

    在Linux中直接指定命令: iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080 其中80为要访问的端 ...

  3. 针对jquery的ajax中的参数理解

    1. url 发送请求的地址.为空表示当前页. $.ajax({ type: "post", data: studentInfo, contentType: "appli ...

  4. html+css3 实现各种loading效果

    效果见下图 代码: 建议直接去本人github上浏览代码 https://github.com/wuliqiangqiang/loading <!DOCTYPE html> <htm ...

  5. 浅谈vue,小程序,react基础绑定值

    最近一直在用react开发项目,碰见的问题千千万,很多,但是都殊途同源,唯一区别大的就是没有像vue的双向绑定,也没有小程序的单向方便,比如: vue   v-modal="msg" ...

  6. Python提高篇

    Python提高篇 1.模块 1)模块定义 模块就是py文件,可以是你自己写的,也可以是python当中自带的工具,当你在某个py文件下想要引用其他模块的功能,就需要你把你把该py文件导入. 2)导入 ...

  7. [转]去掉IOS下的input 和textarea的内阴影

    在IOS下,input 和textarea表单默认会有个内阴影,一定程度上影响视觉一致,可通过设置下面代码去掉: input{-webkit-appearance: none;}

  8. Python对文件目录的操作

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()返回指定目录下的所有文件和目录 ...

  9. day 13 内置函数

    内置函数思维导图:https://www.processon.com/view/link/5c13ad2de4b0ed122da75668 内置函数 作用域相关:   locals() 返回当前作用域 ...

  10. Linux内核调用SPI平台级驱动_实现OLED的显示功能

    Linux内核调用SPI驱动_实现OLED显示功能 0. 导语 进入Linux的世界,发现真的是无比的有趣,也发现搞Linux驱动从底层嵌入式搞起真的是很有益处.我们在单片机.DSP这些无操作系统的裸 ...