一、具体代码

类代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6.  
  7. namespace WpfApplication1
  8. {
  9. class lei
  10. {
  11. public interface Strategy
  12. {
  13. int calculate(int a, int b);
  14.  
  15. }
  16. public class Add : Strategy
  17. {
  18. public int calculate(int a, int b)
  19. {
  20. return a + b;
  21. }
  22. }
  23. public class Sub : Strategy
  24. {
  25. public int calculate(int a, int b)
  26. {
  27. return a -b;
  28. }
  29. }
  30. public class Mul : Strategy
  31. {
  32. public int calculate(int a, int b)
  33. {
  34. return a * b;
  35. }
  36. }
  37. public class Div : Strategy
  38. {
  39. public int calculate(int a, int b)
  40. {
  41. return a / b;
  42. }
  43. }
  44.  
  45. public class Content
  46. {
  47. private Strategy strategy;
  48. public Content(Strategy strategy)
  49. {
  50. this.strategy = strategy;
  51. }
  52. public int calculate(int a, int b, String m)
  53. {
  54. return this.strategy.calculate(a, b);
  55. }
  56. }
  57. }
  58. }

MainWindow.xaml.cs代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.IO;
  15.  
  16. using System.Windows.Threading;
  17. using Microsoft.Win32;
  18.  
  19. namespace WpfApplication1
  20. {
  21. /// <summary>
  22. /// MainWindow.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. public MainWindow()
  27. {
  28. InitializeComponent();
  29. }
  30. string path = ".\text1.txt";
  31. public static int right = ;
  32. public static int Count = ;
  33. private int t;
  34. int a;
  35. private void button1_Click(object sender, RoutedEventArgs e)
  36. {
  37.  
  38. StreamWriter baocun1 = File.AppendText("baocun1.txt");
  39. baocun1.WriteLine(textBox1.Text);
  40. baocun1.Close();
  41. StreamWriter baocun2 = File.AppendText("baocun2.txt");
  42. baocun2.WriteLine(textBox2.Text);
  43. baocun2.Close();
  44. StreamWriter baocun3 = File.AppendText("baocun3.txt");
  45. baocun3.WriteLine(textBox3.Text);
  46. baocun3.Close();
  47.  
  48. textBox6.Text += textBox1.Text + textBox2.Text + textBox3.Text + label1.Content + textBox4.Text + "\n";
  49. textBox1.Clear();
  50. textBox2.Clear();
  51. textBox3.Clear();
  52. }
  53.  
  54. private void button2_Click(object sender, RoutedEventArgs e)
  55. {
  56. textBox5.Text = t.ToString();
  57. DispatcherTimer myTimer = new DispatcherTimer();
  58. myTimer.Interval = new TimeSpan(, , ,);
  59. myTimer.Tick += new EventHandler(Timer_Tick);
  60. myTimer.Start();
  61. string[] m = new string[];
  62. m = File.ReadAllLines("baocun1.txt");
  63. textBox1.Text = m[a];
  64. string[] n = new string[];
  65. n = File.ReadAllLines("baocun2.txt");
  66. textBox2.Text = n[a];
  67. string[] v = new string[];
  68. v = File.ReadAllLines("baocun3.txt");
  69. textBox3.Text = v[a];
  70. a++;
  71. }
  72.  
  73. void Timer_Tick(object send, EventArgs e)
  74. {
  75. t = t + ;
  76. textBox5.Text = t.ToString();
  77.  
  78. }
  79.  
  80. private void button3_Click(object sender, RoutedEventArgs e)
  81. {
  82. //调用;
  83. WpfApplication1.lei.Content content = null;
  84. int a = int.Parse(textBox1.Text);
  85. int b = int.Parse(textBox3.Text);
  86. string m = textBox2.Text;
  87. switch (m)
  88. {
  89. case "+":
  90. content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Add());
  91. break;
  92. case "-":
  93. content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Sub());
  94.  
  95. break;
  96. case "*":
  97. content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Mul());
  98.  
  99. break;
  100. case "/":
  101. content = new WpfApplication1.lei.Content(new WpfApplication1.lei.Div());
  102.  
  103. break;
  104. default:
  105. break;
  106. }
  107. string answer = content.calculate(a, b, m).ToString();
  108.  
  109. if (textBox4.Text == answer.ToString())
  110. {
  111. MessageBox.Show("恭喜你,小朋友!回答正确!");
  112. right++;
  113. Count++;
  114.  
  115. }
  116. else
  117. {
  118. MessageBox.Show("回答错误,继续加油!小朋友");
  119. Count++;
  120. }
  121.  
  122. textBox1.Clear();
  123. textBox2.Clear();
  124. textBox3.Clear();
  125. textBox4.Clear();
  126. }
  127.  
  128. private void button4_Click(object sender, RoutedEventArgs e)
  129. {
  130. MessageBox.Show("一共用时" + textBox5.Text + "秒");
  131. Window1 win = new Window1();
  132. win.ShowDialog();
  133. }
  134.  
  135. }
  136. }

Window.xaml.cs代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13.  
  14. namespace WpfApplication1
  15. {
  16. /// <summary>
  17. /// Window1.xaml 的交互逻辑
  18. /// </summary>
  19. public partial class Window1 : Window
  20. {
  21. public Window1()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void Window_Loaded(object sender, RoutedEventArgs e)
  27. {
  28. textBox1.Text = MainWindow.Count.ToString();
  29. textBox2.Text = MainWindow.right.ToString();
  30. textBox3.Text = ((MainWindow.right / (double)(MainWindow.Count)) * ).ToString() + "%";
  31. }
  32. }
  33. }

二、测试

三、总结

在WPF里实现计算器程序跟在WinForm里差不多,就是有些控件的用法有点区别,还有就是没有时间控件,得自己写代码实现,然后就上网搜了搜,很庆幸写出来了。

在WPF里实现计算器软件的更多相关文章

  1. 计算器软件实现系列(六)windowform窗体+SQL+策略模式

    一 整体概述 这个计算器软件的功能和以前的功能基本上一样,只不过是数据的保存形式发生了变化,,以前用的是txt文件保存,现在更正用SQL数据库,现在更改了以前的文件保存形式,是三层架构中数据层的更换, ...

  2. WPF学习开发客户端软件-任务助手(下 2015年2月4日代码更新)

    时光如梭,距离第一次写的 WPF学习开发客户端软件-任务助手(已上传源码)  已有三个多月,期间我断断续续地对该项目做了优化.完善等等工作,现在重新向大家介绍一下,希望各位可以使用,本软件以实用性为主 ...

  3. WPF里的一些Effect特效

    原文:WPF里的一些Effect特效 Blend的特效都在Microsoft.Expression.Media.Effects里,用之前添加一下引用. 可以在前台选中对象后直接点击Effect新建一种 ...

  4. 计算器软件实现系列(七)WPF+SQL+策略模式

    一  整体概述 本次设计主要是在WPF的页面中实现的,属于表现层的更换,数据库部分用的还是数据库的封装,其中引用了策略模式 二  设计思路 1 在出题页面,进行试题的编辑,在编辑后会自动保存到数据库中 ...

  5. WPF学习开发客户端软件-任务助手(已上传源码)

    本人纯属WPF新手,布局和WPF的开发水平相当欠缺,从个人来说,还是比较喜欢WPF的,有人说WPF是界面加上WINFORM,我不这样认为,WPF与WINFORM主要的不同在于数据绑定.   这个软件虽 ...

  6. revit二次开发wpf里button按钮无法实现事务

    不能在revit提供的api外部使用事务,解决此方法, 1.把button里要实现的功能写到外部事件IExternalEventHandler中,注册外部事件,在button事件中.raise()使用 ...

  7. WPF里ItemsControl的分组实现 --listbox 实现分组

    我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...

  8. adb导出安卓 把手机内存文件导入到电脑里 adb安装软件

    记得先找对路劲adb shellls 最上面的ls: ./ 打头的没有权限.而下面的这些acct sdcard等 都有权限. 然后cd sdcardls 看下目录,发现gxm文件夹在sdcard下面. ...

  9. WPF里ItemsControl的分组实现

    我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...

随机推荐

  1. 浅谈基于FormsAuthentication的认证

    一般情况下,在我们做访问权限管理的时候,会把用户的正确登录后的基本信息保存在Session中,以后用户每次请求页面或接口数据的时候,拿到 Session中存储的用户基本信息,查看比较他有没有登录和能否 ...

  2. 如何向女朋友解释int==Integer为true

    原:https://juejin.im/post/5c7f3cb25188251b883cada2 int==Integer为什么返回true 先看现象吧 执行下面的代码及输出结果: int a = ...

  3. Mysql数据库 day1

    Mysql数据库属于关系型数据库(mysql.oracle.sql server),非关系型数据库有DB2.Redis MySQL执行原理,逻辑分层.更改数据库处理引擎 作者:Stanley 罗昊 [ ...

  4. WPF触控方面的技术点

    一.基本的触控事件(原始触控) 二.复杂触控事件(操作)

  5. 20155306 白皎 《网络攻防》 Exp2 后门原理与实践

    20155306 白皎 <网络攻防> Exp2 后门原理与实践 一.实践基础 后门程序又称特洛伊木马,其用途在于潜伏在电脑中,从事搜集信息或便于黑客进入的动作.后程序和电脑病毒最大的差别, ...

  6. PostgreSQL调整内存与IO的参数说明

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 [作者:高健@博客园 luckyjackgao ...

  7. 【转载】D3D中的Texture应用示例

    原文:D3D中的texture应用示例 本文列举了Direct3D中各种纹理应用实现:黑暗贴图,发光贴图,漫反射映射贴图,细节纹理,纹理混合,有较详尽的注解.其中黑暗贴图,发光贴图,细节纹理都是采用多 ...

  8. pandas:对字符串类型做差分比较

    1. 问题需求 某种行为最常发生时段.最少发生时段与X天前是否一致 需求变形:判断上下行数据是否一致 2. 预备知识 2.1 Series.ne(Series) 判断两个Series是否相等 impo ...

  9. SQL Server 中SELECT INTO 和 INSERT INTO SELECT 两种表复制语句

    1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...

  10. Java虚拟机笔记(三):垃圾收集算法

    一.标记-清除(Mark-Sweep)算法 标记清除算法是最基础的收集算法,其他收集算法都是基于这种思想. 标记清除算法分为“标记”和“清除”两个阶段:首先标记出需要回收的对象,标记完成之后统一清除对 ...