在WPF里实现计算器软件

2021-09-28 02:14

阅读:891

标签:一、具体代码 类代码:   using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace WpfApplication1 { class lei { public interface Strategy { int calculate(int a, int b); } public class Add : Strategy { public int calculate(int a, int b) { return a + b; } } public class Sub : Strategy { public int calculate(int a, int b) { return a -b; } } public class Mul : Strategy { public int calculate(int a, int b) { return a * b; } } public class Div : Strategy { public int calculate(int a, int b) { return a / b; } } public class Content { private Strategy strategy; public Content(Strategy strategy) { this.strategy = strategy; } public int calculate(int a, int b, String m) { return this.strategy.calculate(a, b); } } } }   MainWindow.xaml.cs代码 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.IO; using System.Windows.Threading; using Microsoft.Win32; namespace WpfApplication1 { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } string path = ".\text1.txt"; public static int right = 0; public static int Count = 0; private int t; int a; private void button1_Click(object sender, RoutedEventArgs e) { StreamWriter baocun1 = File.AppendText("baocun1.txt"); baocun1.WriteLine(textBox1.Text); baocun1.Close(); StreamWriter baocun2 = File.AppendText("baocun2.txt"); baocun2.WriteLine(textBox2.Text); baocun2.Close(); StreamWriter baocun3 = File.AppendText("baocun3.txt"); baocun3.WriteLine(textBox3.Text); baocun3.Close(); textBox6.Text += textBox1.Text + textBox2.Text + textBox3.Text + label1.Content + textBox4.Text + "\n"; textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); } private void button2_Click(object sender, RoutedEventArgs e) { textBox5.Text = t.ToString(); DispatcherTimer myTimer = new DispatcherTimer(); myTimer.Interval = new TimeSpan(0, 0, 0,1); myTimer.Tick += new EventHandler(Timer_Tick); myTimer.Start(); string[] m = new string[100]; m = File.ReadAllLines("baocun1.txt"); textBox1.Text = m[a]; string[] n = new string[100]; n = File.ReadAllLines("baocun2.txt"); textBox2.Text = n[a]; string[] v = new string[100]; v = File.ReadAllLines("baocun3.txt"); textBox3.Text = v[a]; a++; } void Timer_Tick(object send, EventArgs e) { t = t + 1; textBox5.Text = t.ToString(); } private void button3_Click(object sender, RoutedEventArgs e) { //调用; WpfApplication1.lei.Content content = null; int a = int.Parse(textBox1.Text); int b = int.Parse(textBox3.Text); string m = textBox2.Text; switch (m) { case "+": content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Add()); break; case "-": content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Sub()); break; case "*": content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Mul()); break; case "/": content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Div()); break; default: break; } string answer = content.calculate(a, b, m).ToString(); if (textBox4.Text == answer.ToString()) { MessageBox.Show("恭喜你,小朋友!回答正确!"); right++; Count++; } else { MessageBox.Show("回答错误,继续加油!小朋友"); Count++; } textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); textBox4.Clear(); } private void button4_Click(object sender, RoutedEventArgs e) { MessageBox.Show("一共用时" + textBox5.Text + "秒"); Window1 win = new Window1(); win.ShowDialog(); } } } Window.xaml.cs代码 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 WpfApplication1 { /// /// Window1.xaml 的交互逻辑 /// public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { textBox1.Text = MainWindow.Count.ToString(); textBox2.Text = MainWindow.right.ToString(); textBox3.Text = ((MainWindow.right / (double)(MainWindow.Count)) * 100).ToString() + "%"; } } }   二、测试   三、总结     在WPF里实现计算器程序跟在WinForm里差不多,就是有些控件的用法有点区别,还有就是没有时间控件,得自己写代码实现,然后就上网搜了搜,很庆幸写出来了。  在WPF里实现计算器软件标签:原文地址:http://www.cnblogs.com/twinkle-0908/p/5073379.html


评论


亲,登录后才可以留言!