一、具体代码

类代码:

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
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string path = ".\text1.txt";
public static int right = ;
public static int Count = ;
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(, , ,);
myTimer.Tick += new EventHandler(Timer_Tick);
myTimer.Start();
string[] m = new string[];
m = File.ReadAllLines("baocun1.txt");
textBox1.Text = m[a];
string[] n = new string[];
n = File.ReadAllLines("baocun2.txt");
textBox2.Text = n[a];
string[] v = new string[];
v = File.ReadAllLines("baocun3.txt");
textBox3.Text = v[a];
a++;
} void Timer_Tick(object send, EventArgs e)
{
t = t + ;
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
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
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)) * ).ToString() + "%";
}
}
}

二、测试

三、总结

在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. elasticsearch 请求体查询方式整理

    空查询(empty search) —{}— 在功能上等价于使用 match_all 查询, 正如其名字一样,匹配所有文档: GET /_search { "query": { & ...

  2. Scratch 3.0 版本比较

    新年伊始MIT 就正式 发布了Scratch最新3.0版 ,以 HTML5.0 为基础的版本! 本文就来带看看3.0版有哪些更新的地方. 下图是进入后第一个画面,跟Scratch 2.0 很像,熟悉的 ...

  3. 20145207 2016-2017《Java程序设计》课程总结

    20145207 2016-2017<Java程序设计>课程总结 目录 一.每周作业及实验报告链接汇总 二.关于博客 自认为写得最好一篇博客是?为什么? 作业中阅读量最高的一篇博客是?谈谈 ...

  4. 20145207《Java程序设计》实验四( Android程序设计)实验报告

    <Java 程序设计>实验四( Android程序设计)实验报告 目录 改变 Android开发基础实验要求 实验成果 课后思考 改变 修改了之前仅仅是贴了图片,连代码都没粘的状态.增加了 ...

  5. 查内存命令之free

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页     回到顶级页面:PostgreSQL索引页 [作者 高健@博客园  luckyjackgao@gmail. ...

  6. 【python3】拷贝U盘文件

    一.起因 前天在公众号上,看到一篇如何用python偷偷拷贝别人U盘内容的文章推送,感觉这个想法挺有意思的,可惜是用的是linux系统,而且移动硬盘的盘符也是写死的,不够灵活,于是就自己动手写了一个d ...

  7. VS2013在Windows7 64位上变慢的解决方法

    重装了windows7系统,又重装了vs2013,发现在打开vs2013.编译工程及调试的时候,vs2013都会变的比较慢,参考网上资料,这里列出几种可能的解决方法: 1.      打开工具--&g ...

  8. Openstack入门篇(十二)之neutron服务(计算节点)的部署与测试

    1.Neutron组件安装 [root@linux-node2 ~]# yum install -y openstack-neutron-linuxbridge ebtables ipset 2.配置 ...

  9. underscore.js 分析6 map函数

    作用:通过转换函数(iteratee迭代器)映射列表中的每个值产生价值的新数组.iteratee传递三个参数:value,然后是迭代 index. _.map([1, 2, 3], function( ...

  10. BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)

    我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...