一  整体概述

本次设计主要是在WPF的页面中实现的,属于表现层的更换,数据库部分用的还是数据库的封装,其中引用了策略模式

二  设计思路

1 在出题页面,进行试题的编辑,在编辑后会自动保存到数据库中

2 试题编辑完毕后,把试题从数据库中导入到文本框中。进行相应的计算

3 判断试题的正确与否,并且做出相关试题结果的统计

三 代码

1 数据层代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
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; //出题时的变量
public string ti, shu1, shu2, fuhao2;//做题时的变量 public void lianjie() //数据库连接的方法
{ sqlcon = new SqlConnection(str); }
public void bianji() //数据库中编辑试题的方法
{
lianjie();
sqlcon.Open(); string sqlstr = "insert into shuju(第一个数字,符号,第二个数字) values('" + number1 + "','" + fuhao1 + "','" + number2 + "')";
SqlCommand comm = new SqlCommand(sqlstr, sqlcon);
comm.ExecuteNonQuery(); sqlcon.Close();
}
public void qingkong() //清空后台数据库的数据
{
lianjie();
sqlcon.Open(); string sqltr = "delete from shuju";
SqlCommand comm = new SqlCommand(sqltr, sqlcon);
comm.ExecuteNonQuery();
SqlDataReader reder = comm.ExecuteReader(); 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();
}
}
}

2 逻辑层代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 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);
}
}
}

3 表现层代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace shuxuefudao
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static int Count = ; // 题目出的数量
public static int zuode = ; //做的题目数量
public static int zhengque = ;
shujuku shuju = new shujuku();//实例化数据库对象,调用其中的方法
private void button4_Click(object sender, RoutedEventArgs e) //编辑下一题的按钮
{
Count++;
shuju.number1 = left.Text;
shuju.fuhao1 = fuhao.Text;
shuju.number2 = right.Text;
shuju.bianji();
shiti.Text += left.Text + fuhao.Text + right.Text + "=" + "\n";
left.Clear();
fuhao.Clear();
right.Clear(); } private void button1_Click(object sender, RoutedEventArgs e) //清空上次数据的方法调用
{
shuju.qingkong();
} private void button2_Click(object sender, RoutedEventArgs e)//单击开始时方法的调用
{
shuju.JiSuan1();
left.Text =shuju.shu1;
fuhao.Text = shuju.fuhao2;
right.Text = shuju.shu2;
} private void jieguo_KeyDown(object sender, KeyEventArgs e)
{
try //异常处理机制,预防数组发生越界
{
Environment environment = null;
double a = Convert.ToDouble(left.Text.Trim()); //为相关的变量赋值
double b = Convert.ToDouble(right.Text.Trim());
string m = fuhao.Text.Trim();
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 (Keyboard.IsKeyDown(Key.Enter))
{ string answer = environment.Cal(a, b, m).ToString();
daan.Text += answer + "\r\n";
if (jieguo.Text == answer.ToString())
{
MessageBox.Show("回答正确");
zuode++;
zhengque++;
}
else
{
MessageBox.Show("回答错误");
zuode++;
}
shuju.JiSuan();
left.Text = shuju.shu1;
fuhao.Text = shuju.fuhao2;
right.Text = shuju.shu2;
jieguo.Text = "";
} } catch (Exception ex)
{
this.Hide();
jieguo a = new jieguo();
a.Show();
} } private void button5_Click(object sender, RoutedEventArgs e)
{
shuju.DaoRu();
shiti.Text = shuju.ti;
} private void button3_Click(object sender, RoutedEventArgs e)
{
this.Hide();
jieguo a = new jieguo();
a.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes; namespace shuxuefudao
{
/// <summary>
/// jieguo.xaml 的交互逻辑
/// </summary>
public partial class jieguo : Window
{
public jieguo()
{
InitializeComponent();
} private void Grid_Loaded(object sender, RoutedEventArgs e)//试题的统计结果
{
textBox1.Text = MainWindow.zuode.ToString();
textBox2.Text = MainWindow.zhengque.ToString();
textBox3.Text = ((MainWindow.zuode - (double)(MainWindow.zhengque))).ToString();
textBox4.Text = ((MainWindow.zhengque / (double)(MainWindow.zuode)) * ).ToString("f2") + "%";
}
}
}

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

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

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

  2. 计算器软件实现系列(六)windowform窗体+SQL+策略模式

    一 整体概述 这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是txt文件保存,现在更正用SQL数据库,现在更改了以前的文件保存形式,是三层架构中数据层的更换, ...

  3. 性能优化系列七:SQL优化

    一.SQL在数据库中的执行过程 二.执行计划 1. ACID 原子性:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节.事务在执行过程中发生错误,会 ...

  4. 行为型模式(七) 策略模式(Stragety)

    一.动机(Motivate) 在软件构建过程中,某些对象使用的算法可能多种多样,经常改变,如果将这些算法都编码到对象中,将会使对象变得异常复杂:而且有时候支持不使用的算法也是一个性能负担.如何在运行时 ...

  5. 使用策略模式重构switch case 代码

    目录 1.背景 2.案例 3.switch…case…方式实现 4.switch…case…带来的问题 5.使用策略模式重构switch…case…代码 6.总结 1.背景 之前在看<重构    ...

  6. 设计模式原来如此-策略模式(Strategy Pattern)

    策略模式中体现了两个非常基本的面向对象设计的原则:1.封装变化的概念.2.编程中使用接口,而不是对接口的实现. 策略模式的定义:定义一组算法,将每个算法都封装起来,并使它们之间可以互换.策略模式使这些 ...

  7. SQL Server 2008空间数据应用系列七:基于Bing Maps(Silverlight) 的空间数据展现

    原文:SQL Server 2008空间数据应用系列七:基于Bing Maps(Silverlight) 的空间数据展现 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft ...

  8. 计算器软件的代码实现 (策略模式+asp.net)

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

  9. WPF快速入门系列(9)——WPF任务管理工具实现

    转载自:http://www.cnblogs.com/shanlin/p/3954531.html WPF系列自然需要以一个实际项目为结束.这里分享一个博客园博客实现的一个项目,我觉得作为一个练手的项 ...

随机推荐

  1. Flume采集目录及文件到HDFS案例

    采集目录到HDFS 使用flume采集目录需要启动hdfs集群 vi spool-hdfs.conf # Name the components on this agent a1.sources = ...

  2. opencv3 学习四 - 图像减色

    程序如下 #include "opencv2/opencv.hpp" using namespace cv; int main() { // 灰度图 Mat original = ...

  3. C语言跳水比赛预测结果

    5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果A选手说:B第二,我第三:B选手说:我第二,E第四:C选手说:我第一,D第二:D选手说:C最后,我第三:E选手说:我第四,A第一:比赛结束后,每 ...

  4. google friendly testing

    https://www.google.com/webmasters/tools/mobile-friendly/?mc_cid=cc21b18877&mc_eid=cf2bbeb9b2 htt ...

  5. mybatis入门(三):mybatis的基础特性

    mybatis的知识点: 1.mybatis和hibernate本质区别和应用场景 hibernate:是一个标准的ORM框架(Ojbect relation mapper对象关系映射).入门门槛较高 ...

  6. 在线接口文档工具——ShowDoc

    ShowDoc:https://www.showdoc.cc/ --待更.

  7. BZOJ4247_挂饰_KEY

    题目传送门 背包的变形,不得不说卡了我很久(估计是下午睡傻了). 设f[i][j]为前i个物品剩下j个挂钩. f[i][j]=max(f[i-1][j],f[i-1][max(j-a[i].x,0)+ ...

  8. JavaScript---通过正则表达式验证表单输入

    验证输入的name只能是数字或字母或下划线 js <script type="text/javascript"> function submitOn(){ var f ...

  9. vue中开发webSocket

    先安装 sockjs-client 和 stompjs npm install sockjs-client npm install stompjs <template> <div&g ...

  10. SpringBoot学习:添加JSP支持

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)pom中添加依赖: <!-- https://mvnrepository.c ...