三层架构
运行界面:

数据库保存的题:

业务逻辑层:
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 Class5
{
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 void dbcon()
{
try
{
sqlcon = new SqlConnection(str);
}
catch (Exception ex)
{
MessageBox.Show("数据链接不成功" + ex.Message);
}
}
public void dakai()
{
sqlcon.Open();
}
public void guanbi()
{
sqlcon.Close();
}
public void Charu(string comstr)
{
SqlCommand comm = new SqlCommand(comstr, sqlcon);
try
{
int a = comm.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("出题成功!");
}
else
{
MessageBox.Show("出题失败!");
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
public void Shanchu(string comstr)//删除数据!
{
SqlCommand comm = new SqlCommand(comstr, sqlcon);
int a = comm.ExecuteNonQuery();
if (a > 0)
{
MessageBox.Show("删除失败!");
}
else
{
MessageBox.Show("删除成功!");
}
}
public void Read(string comstr)//读取数据!
{
sda = new SqlDataAdapter(comstr, sqlcon);
sda.Fill(ds);
dt = ds.Tables[0];
}
}
}
数据访问层:
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 Class6
{
public string x1;
public string x2;
public string x3;
public string x4;
public string x5;
public string x6;
public string x7;
public string x8;
Class5 C5 = new Class5();
public void ChuTi()
{
C5.dbcon();
C5.dakai();
string sqltr = "insert into Table2(第一个数,符号,第二个数) values('" + x1 + "','" + x2 + "','" + x3 + "')";
C5.Charu(sqltr);
C5.guanbi();
}
public void ShanChu()
{
C5.dbcon();
C5.dakai();
string sqltr = "delete from Table2";
C5.Shanchu(sqltr);
C5.guanbi();
}
public void JiSuan()
{
int i = int.Parse(x4);
C5.dbcon();
C5.dakai();
string sqltr = "select * from Table2 ";
C5.Read(sqltr);
x5 = C5.dt.Rows[i][0].ToString();
x6 = C5.dt.Rows[i][1].ToString();
x7 = C5.dt.Rows[i][2].ToString();
C5.guanbi();
}
public void DaoRu()
{
C5.dbcon();
C5.dakai();
string sqltr = "select * from Table2 ";
C5.Read(sqltr);
for (int i = 0; i < C5.dt.Rows.Count; i++)
{
C5.dt.Rows[i][0].ToString();
C5.dt.Rows[i][1].ToString();
C5.dt.Rows[i][2].ToString();
x8 += C5.dt.Rows[i][0].ToString().Trim() + C5.dt.Rows[i][1].ToString().Trim() + C5.dt.Rows[i][2].ToString().Trim() + "\n";
}
C5.guanbi();
}
public void jisuan(string str,int a,int b,string c)
{
Cacule oper = new Cacule();
oper = OperationFactory.CreateOperate(str);
oper.NumberA = a;
oper.NumberB = b;
double result = oper.Result();
if (c == result.ToString())
{
MessageBox.Show("回答正确");
}
else
{
MessageBox.Show("回答错误");
}
c = "";
}
}
}
计算类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication1
{
public class Cacule
{
public double NumberA{get;set;}
public double NumberB{get;set;}
public virtual double Result()
{
double result = 0;
return result;
}
}
class Add:Cacule
{
public override double Result()
{
double result = 0;
result= NumberA + NumberB;
return result;
}
}
class Sub:Cacule
{
public override double Result()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
class Multi:Cacule
{
public override double Result()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
class Div:Cacule
{
public override double Result()
{
double result = 0;
result = NumberA / NumberB;
return result;
}
}
}
工厂类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication1
{
public class OperationFactory
{
public static Cacule CreateOperate(string operate)
{
Cacule ca = null;
switch (operate)
{
case "+":
ca = new Add();
break;
case "-":
ca = new Sub();
break;
case "*":
ca = new Multi();
break;
case "/":
ca = new Div();
break;
default:
break;
}
return ca;
}
}
}
计算类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication1
{
public class Cacule
{
public double NumberA{get;set;}
public double NumberB{get;set;}
public virtual double Result()
{
double result = 0;
return result;
}
}
class Add:Cacule
{
public override double Result()
{
double result = 0;
result= NumberA + NumberB;
return result;
}
}
class Sub:Cacule
{
public override double Result()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
class Multi:Cacule
{
public override double Result()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
class Div:Cacule
{
public override double Result()
{
double result = 0;
result = NumberA / NumberB;
return result;
}
}
}
表现层:
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;
Class6 sj = new Class6();
private void button1_Click(object sender, RoutedEventArgs e)
{
sj.x1 = left.Text;
sj.x2 = fuhao.Text;
sj.x3 = right.Text;
sj.ChuTi();
string str = fuhao.Text;
Cacule oper = new Cacule();
oper = OperationFactory.CreateOperate(str);
oper.NumberA = double.Parse(left.Text);
oper.NumberB = double.Parse(right.Text);
double result = oper.Result();
daan1.Text += result + "\r\n";
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)
{
string st = textBox2.Text;
int x1 = int.Parse(textBox1.Text);
int x2 = int.Parse(textBox3.Text);
string x3 = textBox4.Text;
if(e.Key==Key.Enter)
sj.jisuan(st,x1,x2,x3);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
shujuku sj = new shujuku();
}
}
}

浙公网安备 33010602011771号