运行界面:

数据库保存的题:

数据库封装代码:

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;
using System.Data.SqlClient;
using System.Data;
namespace WpfApplication1
{
class shujuku
{
public string str = @"Data Source=.;Initial Catalog=四则运算;Integrated Security=True;Pooling=False";
public SqlConnection sqlcon = new SqlConnection();
public SqlDataAdapter sda = new SqlDataAdapter();
public DataSet ds = new DataSet();
public DataTable dt = new DataTable();
public string x1;
public string x2;
public string x3;
public string x4;
public string x5;
public string x6;
public string x7;
public string x8;
public void dbcon()
{
try
{
sqlcon = new SqlConnection(str); }
catch (Exception ex)
{
MessageBox.Show("数据链接不成功"+ex.Message);
}
} public void ChuTi()
{
dbcon();
sqlcon.Open();
string sqltr = "insert into Table2(第一个数,符号,第二个数) values('" + x1 + "','" + x2 + "','" + x3 + "')";
SqlCommand comm = new SqlCommand(sqltr, sqlcon);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("出题成功!"); }
catch (Exception ex)
{
MessageBox.Show("出题失败" + ex.ToString());
}
sqlcon.Close();
}
public void ShanChu()
{
dbcon();
sqlcon.Open();
string sqltr = "delete from Table2";
SqlCommand comm = new SqlCommand(sqltr, sqlcon);
try
{
comm.ExecuteNonQuery();
MessageBox.Show("删除成功");
}
catch (Exception ex)
{
MessageBox.Show("删除失败" + ex.ToString());
} SqlDataReader reder = comm.ExecuteReader();
sqlcon.Close();
}
public void JiSuan()
{ int i = int.Parse(x4);
dbcon();
sqlcon.Open();
string sqltr = "select * from Table2 ";
SqlCommand comm = new SqlCommand(sqltr, sqlcon);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
x5 = dt.Rows[i][0].ToString();
x6 = dt.Rows[i][1].ToString();
x7 = dt.Rows[i][2].ToString();
sqlcon.Close();
}
public void DaoRu()
{
dbcon();
sqlcon.Open();
string sqltr = "select * from Table2 ";
SqlCommand comm = new SqlCommand(sqltr, sqlcon);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i][0].ToString();
dt.Rows[i][1].ToString();
dt.Rows[i][2].ToString();
x8 += dt.Rows[i][0].ToString().Trim() + dt.Rows[i][1].ToString().Trim() + dt.Rows[i][2].ToString().Trim() + "\n";
}
sqlcon.Close();
} }
}

wpf代码:

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;
using System.Data.SqlClient;
using System.Data; namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static int Count = 0;
public static int zhengque = 0;
public static int lefttime;
public static int sum;
shujuku sj = new shujuku();
private void button1_Click(object sender, RoutedEventArgs e)
{ sj.x1 = left.Text;
sj.x2 = fuhao.Text;
sj.x3 = right.Text;
sj.ChuTi();
string f = fuhao.Text;
switch (f)
{
case "+":
sum = int.Parse(left.Text) + int.Parse(right.Text);
daan1.Text += sum + "\r\n"; break;
case "-":
sum = int.Parse(left.Text) - int.Parse(right.Text); daan1.Text += sum + "\r\n";
break;
case "*":
sum = int.Parse(left.Text) * int.Parse(right.Text); daan1.Text += sum + "\r\n";
break;
case "/":
sum = int.Parse(left.Text) / int.Parse(right.Text); daan1.Text += sum + "\r\n";
break;
default:
break; }
left.Clear();
fuhao.Clear();
right.Clear();
} private void button2_Click(object sender, RoutedEventArgs e)
{
sj.ShanChu(); } private void button4_Click(object sender, RoutedEventArgs e)
{
sj.x4=textBox5.Text;
textBox1.Text= sj.x5;
textBox2.Text= sj.x6;
textBox3.Text=sj.x7 ;
sj.JiSuan(); } private void button5_Click(object sender, RoutedEventArgs e)
{
if (daan1.Text == "显示答案")
{
daan1.Visibility = Visibility.Visible;
daan1.Text = "隐藏答案";
}
else if (daan1.Text == "隐藏答案")
{
daan1.Visibility = Visibility.Hidden;
daan1.Text = "显示答案"; }
} private void button3_Click(object sender, RoutedEventArgs e)
{
textBox6.Text = sj.x8;
sj.DaoRu();
} private void textBox4_KeyDown(object sender, KeyEventArgs e)
{
tiaoyong ty = new tiaoyong();
string fuha = textBox2.Text;
ty.c = int.Parse(textBox1.Text);
ty.n = int.Parse(textBox3.Text);
ty.yunsuan(fuha);
if (e.Key == Key.Enter)
{
if (textBox4.Text == ty.result.ToString())
{
MessageBox.Show("回答正确");
zhengque++;
}
else
{
MessageBox.Show("回答错误");
}
textBox4.Clear(); } } private void Window_Loaded(object sender, RoutedEventArgs e)
{
shujuku sj = new shujuku();
} }
}

策略封装代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WpfApplication1
{ public interface ITaxStragety
{
int Calcul(int a,int b); }
class Class2 : ITaxStragety
{ public int Calcul(int a, int b)
{
return a + b;
} }
class sub : ITaxStragety
{
public int Calcul(int a, int b)
{
return a - b;
} }
class sheng : ITaxStragety
{
public int Calcul(int a, int b)
{
return a *b; }
}
class chu : ITaxStragety
{
public int Calcul(int a, int b )
{
return a / b; }
}
class tiaoyong
{
public int result = 0;
public int c=0;
public int n=0;
public void yunsuan(string m)
{
if (m == "+")
{
Class2 jia = new Class2();
result = jia.Calcul(c, n);
}
if (m == "-")
{
sub su = new sub();
result = su.Calcul(c, n);
}
if (m == "*")
{
sheng sh = new sheng();
result = sh.Calcul(c, n); }
if (m == "/")
{ chu ch = new chu();
result = ch.Calcul(c, n); } }
} }

  

Wpf+数据库代码封装+策略模式封装的更多相关文章

  1. 【Android】策略模式封装百度地图路线规划模块

    百度地图的Demo里有个路线规划的功能,但是,这个功能和Activity耦合性太高,所以需要单独抽离出路径规划功能,进行"解耦". 注:由于项目原因,本文只针对驾车路线规划进行封装 ...

  2. 《大话设计模式》ruby版代码:策略模式

    需求: 商场收银软件,根据客户购买物品的单价和数量,计算费用,会有促销活动,打八折,满三百减一百之类的. 一,使用工厂模式. # -*- encoding: utf-8 -*- #现金收费抽象类 cl ...

  3. 有效的减少代码中太多的if、else?-策略模式

    写这篇文章的目的和上一篇单例模式一样,策略模式也是一种常用的设计模式,太多的if-else不仅看着不太美观而且不好维护,对于自己来说也等于复习了一遍策略模式.先说一下策略 模式的定义: 策略模式封装了 ...

  4. WPF 策略模式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  5. 小白写了一堆if-else,大神实在看不下去了,竟然用策略模式直接摆平了

    这里涉及到一个关键词:策略模式,那么到底什么是策略模式呢?本文就来好好给大家讲讲策略模式,大家可以带着如下几个问题来阅读本文:   1. 如何通过策略模式优化业务逻辑代码(可以根据自己从事的工作思考) ...

  6. 理解javascript中的策略模式

    理解javascript中的策略模式 策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换. 使用策略模式的优点如下: 优点:1. 策略模式利用组合,委托等技术和思想,有效 ...

  7. JavaScript设计模式与开发实践 - 策略模式

    引言 本文摘自<JavaScript设计模式与开发实践> 在现实中,很多时候也有多种途径到达同一个目的地.比如我们要去某个地方旅游,可以根据具体的实际情况来选择出行的线路. 如果没有时间但 ...

  8. [.net 面向对象程序设计深入](24)实战设计模式——策略模式(行为型)

    [.net 面向对象程序设计深入](24)实战设计模式——策略模式(行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它 ...

  9. 再起航,我的学习笔记之JavaScript设计模式20(策略模式)

    策略模式 策略模式(Strategy):将定义的一组算法封装起来,使其相互之间可以替换.封装的算法具有一定的独立性,不会随客户端变化而变化. 其实策略模式在我们生活中可应用的地方还是比较多的,比如在商 ...

随机推荐

  1. 基于 HTML5 WebGL 的计量站三维可视化监控系统 Web 组态工控应用

    得益于 HTML5 WebGL 技术的成熟,从技术上对工控管理的可视化,数据可视化变得简单易行!完成对工控设备的管理效率,资源管理,风险管理等的大幅度提高,同时也对国家工业4.0计划作出有力响应! 如 ...

  2. go语言笔记(一)

    packge main import "fmt" func main(){ fmt.println("hello") } 大括号约定 方法名首字母区分方法类型, ...

  3. FPGA入门实例一:LFSR

    一:任务: 要求使用Verilog语言在Xilinx Virtex-6开发板上实现线性反馈移位寄存器(LFSR)的硬件逻辑设计. 二:前期准备: 基本上完成一个简单的设计需要用到以下几个软件 逻辑:U ...

  4. SFTP Using Chilkat Active component

    https://www.example-code.com/vb6/sftp_uploadBandwidthThrottle.asp Private Sub Command1_Click() ' Imp ...

  5. 05- views视图层

    1.配置index 主url from django.contrib import admin from django.urls import path, re_path, include urlpa ...

  6. windows下启动与停止服务

    net start 服务名 例如:net start mysql net stop 服务名 例如:net stop mysql

  7. Linux shell(3)

    shell的运算操作: let整数运算 expr整数运算 bc浮点运算 字符串运算 let命令: let命令让BASH shell 执行算数值的操作,使用let,可以比较两个值或执行加减乘除等这样的算 ...

  8. 《SLAM十四讲》个人学习知识点梳理

    0.引言 从六月末到八月初大概一个月时间一直在啃SLAM十四讲[1]这本书,这本书把SLAM中涉及的基本知识点都涵盖了,所以在这里做一个复习,对这本书自己学到的东西做一个梳理. 书本地址:http:/ ...

  9. 车架号VIN码识别,合格证,购车发票,房产证,车牌,驾驶证,行驶证,征信报告等等识别 从易鑫、大搜车、淘车网,看汽车金融发展新模式

    随着我国汽车保有量和产销量的持续增长,汽车技术的日趋成熟,以及互联网+对汽车行业的不断影响,汽车金融的市场规模逐步扩大,市场主体逐步丰富,汽车金融模式也在不断演进. 2016年左右,美国主要汽车厂商通 ...

  10. 为什么你写的用例测不出Bug来?

    我们写测试用例的目的是为了能够整理思路,把要测试的地方列出来,做为知识的积淀,用例可以交给其他测试人员执行,或者是跟需求提出者进行讨论,对用例进行补充和修改.那么为啥你写的用例测不出Bug来呢,真的是 ...