计算器软件实现系列(六)windowform窗体+SQL+策略模式
一 整体概述
这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是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+策略模式的更多相关文章
- Java 设计模式系列(十二)策略模式(Strategy)
Java 设计模式系列(十二)策略模式(Strategy) 策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换.策略模式使得算法可以 ...
- 计算器软件实现系列(七)WPF+SQL+策略模式
一 整体概述 本次设计主要是在WPF的页面中实现的,属于表现层的更换,数据库部分用的还是数据库的封装,其中引用了策略模式 二 设计思路 1 在出题页面,进行试题的编辑,在编辑后会自动保存到数据库中 ...
- 计算器软件实现系列(五)策略模式+asp.net
一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...
- SpringCloud系列六:Eureka的自我保护模式、IP选择、健康检查
1. 回顾 前面讲了很多Eureka的用法,比如Eureka Server.Eureka Server的高可用.Eureka Server的用户认证(虽然未完全实现).元数据等, 这章将讲解剩下的自我 ...
- 学C#之设计模式系列笔记(1)策略模式
一.借鉴说明 1.<Head First Design Patterns>(中文名<深入浅出设计模式>) 2.维基百科,策略模式,https://zh.wikipedia.or ...
- mybatis(六):设计模式 - 策略模式
- Java设计模式系列之策略模式
策略模式的定义: 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换,策略模式让算法独立于使用它的客户而独立变化. 策略模式使这些算法在客户端调用它们的时候能够互不影响地变化 ...
- SQL Server 2008空间数据应用系列六:基于SQLCRL的空间数据可编程性
原文:SQL Server 2008空间数据应用系列六:基于SQLCRL的空间数据可编程性 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 ...
- GIS基础软件及操作(六)
原文 GIS基础软件及操作(六) 练习六.空间分析的应用 1.加深对缓冲区分析基本原理.方法的认识:2.熟练掌握距离制图创建缓冲区技术方法.3.掌握利用缓冲区分析方法解决地学空间分析问题的能力. 1. ...
随机推荐
- 1. 了解HTML
HTML概念 HTML,超文本标记语言.它由一套标签组成用来描述网页,值得我们注意的是HTML并不是编程语言,它只是一种标记,我们通过HTML定义了网页的结构,然后再利用其他技术装饰这个结构,赋予这个 ...
- thinkphp5 toArray()报错
//DB操作返回是数组.模型直接操作返回是对象 //对象类型转换数组 //打开 database.php 增加或修改参数 'resultset_type' => '\think\Collecti ...
- 中国软件大会上大快搜索入选中国数字化转型TOP100服务商
大快搜索自荣获“2018中国大数据企业50强”殊荣,12月20日在由工信部指导,中国电子信息产业化发展研究院主办的2018中国软件大会上,大快搜索获评“2018中国大数据基础软件领域领军企业”称号,入 ...
- 7、Linux应用程序地址布局
程序构成 在学习Linux应用程序开发时,经常会遇到如下概念: 代码段.数据段.BSS段(Block Started by Symbol,又名:未始化数据段) .堆(heap)和栈(stack).始化 ...
- 结合《需求征集系统》谈MVC框架
结合<需求征集系统>分析MVC框架. 六个质量属性: 可用性:在系统压力过大时,会提示系统繁忙. 可修改性:使用配置文件,修改配置文件即可.对于一些公共的方法,进行封装,修改时,只需修改封 ...
- 北京Uber优步司机奖励政策(1月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- CF 1051 F. The Shortest Statement
F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...
- Java:多线程中的volatile
一.为什么使用volatile 首先,通过一段简单的代码来理解为什么要使用volatile: public class RunThread extends Thread{ private boolea ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- 封装Excls数据导出功能 返回一个下载链接地址
/// <summary> /// 获取本地存储地址 /// </summary> /// <param name="dt"></para ...