运行界面:
数据库保存的题:
数据库封装代码:
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() + " "; } 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 + " "; break; case "-": sum = int.Parse(left.Text) - int.Parse(right.Text); daan1.Text += sum + " "; break; case "*": sum = int.Parse(left.Text) * int.Parse(right.Text); daan1.Text += sum + " "; break; case "/": sum = int.Parse(left.Text) / int.Parse(right.Text); daan1.Text += sum + " "; 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); } } } }