一 整体概述

这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是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. Webstorm新建vue类型文件设置

    今天安装了Node.js,配置了vue需要的框架,发现原有的wenstorm新建文件的时候没有vue文件选项,因此,学习了一下webstorm如何配置创建vue文件 具体过程如下: 第一步,打开web ...

  2. Redis(八):Redis的复制(Master/Slave)

    Redis的复制(Master/Slave)目录导航: 是什么 能干嘛 怎么玩 复制原理 哨兵模式(sentinel) 复制的缺点 是什么 官网 行话:也就是我们所说的主从复制,主机数据更新后根据配置 ...

  3. MySQL 半同步重要状态参数说明

    如果配置了MySQL半同步复制,可以在主库查询半同步状态,命令为: SHOW GLOBAL STATUS LIKE '%semi%'; 参数说明      Rpl_semi_sync_master_c ...

  4. Flink基本概念

    Flink基本概念 1.The history of Flink? 2.What is Flink? Apache Flink是一个开源的分布式.高性能.高可用.准确的流处理框架,主要由Java代码实 ...

  5. Python学习手册之__main__ 模块,常用第三方模块和打包发布

    在上一篇文章中,我们介绍了 Python 的 元组拆包.三元运算符和对 Python 的 else 语句进行了深入讲解,现在我们介绍 Python 的 __main__ 模块.常用第三方模块和打包发布 ...

  6. 2017Facebook面试题改编“一面砖墙 ”

    题目:一面砖墙 这道题改编自网上Facebook去年的一道面试题,是hihoCoder的1494题(https://hihocoder.com/problemset/problem/1494) 这道题 ...

  7. PWA-网络

    PWA-网络 fetch API Get fetch('/some/url',{ method:'GET' }).then(function(resp){ //success }).catch(fun ...

  8. JavaSE思维导图

    Java基础知识:  面向对象:  集合:  多线程.网络编程.反射.设计模式:  常用API:  转载 https://blog.csdn.net/qq_34983808/article/detai ...

  9. java入门---基本数据类型之引用数据类型&数据类型转换

        接着上一篇文章来,这次就先看看什么是引用数据类型?首先得满足以下条件: 在Java中,引用类型的变量非常类似于C/C++的指针.引用类型指向一个对象,指向对象的变量是引用变量.这些变量在声明时 ...

  10. 北京Uber优步司机奖励政策(1月16日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...