计算类的封装
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace wpfone1
{
interface operater1
{
int calculate(int a, int b);
}
class Add : operater1
{
public int calculate(int a, int b)
{
return a + b;
}
}
class Sub : operater1
{
public int calculate(int a, int b)
{
return a - b;
}
}
class Mul : operater1
{
public int calculate(int a, int b)
{
return a * b;
}
}
class Div : operater1
{
public int calculate(int a, int b)
{
if (b == 0)
{
throw new Exception("除数不能为零!");
}
else
{
return a / b;
}
}
}
}
用策咯模式实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace wpfone1
{
public class Clacuter
{
private operater1 oper1;
public Clacuter(string aSS)
{
switch (aSS)
{
case "+":
oper1 = new Add();
break;
case "-":
oper1 = new Sub();
break;
case "*":
oper1 = new Mul();
break;
case "/":
oper1 = new Div();
break;
}
}
public int Calculation(int a, int b)
{
return oper1.calculate(a, b);
}
}
}
写入类以及清除题库的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace wpfone1
{
class Writes
{
public void inscribe(string a, string b)
{
StreamWriter aaa = new StreamWriter(a, true);
aaa.WriteLine(b);
aaa.Close();
}
public void cleanup(string c, string d, string e)
{
StreamWriter ddd = new StreamWriter(c);
ddd.WriteLine(" ");
ddd.Close();
StreamWriter aaa = new StreamWriter(d);
aaa.WriteLine("");
aaa.Close();
StreamWriter fff = new StreamWriter(e);
fff.WriteLine("");
fff.Close();
}
}
}
window1的设计以及代码

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 wpfone1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)//写入题
{
Writes writ = new Writes();
string fnm = @"one";
string text1 = this.textBox1.Text;
writ.inscribe(fnm, text1);
string fnmm = @"tow";
string text2 = this.textBox2.Text;
writ.inscribe(fnmm, text2);
string fnm1 = @"fuhao";
string text3 = this.comboBox1.Text;
writ.inscribe(fnm1, text3);
textBox1.Clear();
textBox2.Clear();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string a = @"tow";
string b = @"one";
string c = @"fuhao";
Writes clean = new Writes();
clean.cleanup(a, b, c);
}
private void button3_Click(object sender, RoutedEventArgs e)//开始做题
{
Window2 wind = new Window2();
wind.ShowDialog();
}
}
}
Window2的设计及代码

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;
using System.IO;
namespace wpfone1
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private int i = 1;
public static int count;
public static int right;
private void button1_Click(object sender, RoutedEventArgs e)
{
duti();
}
private void textBox4_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Clacuter clacuter = new Clacuter(textBox2.Text);
int B = clacuter.Calculation(int.Parse(textBox1.Text), int.Parse(textBox3.Text));
if (textBox4.Text == B.ToString())
{
MessageBox.Show("回答正确!");
right++;
}
else
{
MessageBox.Show("回答错误!");
}
count++;
textBox4.Clear();
duti();
}
}
public void duti()
{
string[] line = File.ReadAllLines("one");
if (i < line.Length)
{
textBox1.Text = line[i];
string[] lines = File.ReadAllLines("tow");
textBox3.Text = lines[i];
string[] lin = File.ReadAllLines("fuhao");
textBox2.Text = lin[i];
}
i++;
if (i == line.Length + 1)
{
MessageBox.Show("你的题做完了,可以去休息一下了!");
Window3 win3 = new Window3();
win3.ShowDialog();
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
this.Close();
Window3 win3 = new Window3();
win3.ShowDialog();
}
}
}
window3的设计及代码

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 wpfone1
{
/// <summary>
/// Window3.xaml 的交互逻辑
/// </summary>
public partial class Window3 : Window
{
public Window3()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
textBox1.Text = Window2.right.ToString();
textBox2.Text = (Window2.count - Window2.right).ToString();
textBox3.Text = ((Window2.right / (double)(Window2.count)) * 100).ToString() + "%";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
测试




总结;
在这里实现感觉跟在windowform中差不太多只有很少的一部分代码有点不一样,用法也有些少许不同!
总的感觉就是在窗体的设计中比较容易操作,感觉比在windowform中更容易些!