热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

利用WPF窗口程序设计简单计算器

这篇文章主要为大家详细介绍了利用WPF窗口程序设计简单计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文中设计的计算器仅支持单次双目运算,可连续计算。

实验要求:

1、在wpf项目中编程实现一个简单计算器,具体要求如下:
1)实现+,-,*,/运算
2)可以连续进行计算。

效果如图:

*该程序中数字通过点击对应按钮输入,运算符包含四种常用运算,除此之外还有退格以及清空操作,输入以及运算结果在上方文本框内显示

1.首先,该程序中只涉及单次运算,所以我们可以在隐藏文件里声明两个全局变量来相应的保存operation前后两个数(字符串)。

string num1 = null; //运算符之前的数
string num2 = null; //运算符之后的数
string ope = null; //运算符

2.每次键入一个位数要判断放在num1里还是num2里。

private void button1_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button1.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button1.Content;
 textBox.Text = num2;
 }
}

3.键入运算符是对变量ope赋值(因为是单次计算,所以运算符没必要在文本框里显示)

private void buttonADD_Click(object sender, RoutedEventArgs e)
 {
 ope = "+";
 }

4.CE清空操作,将textbox中的内容以及所有变量清空

private void buttonCE_Click(object sender, RoutedEventArgs e)
 {
 //if (ope == null)
 //{
 // num1 = textBox.Text;
 //}
 //else
 //{
 // num2 = textBox.Text;
 //}
 this.textBox.Text = "";
 num1 = null;
 num2 = null;
 ope = null;
 }

5.退格操作

private void buttonBK_Click(object sender, RoutedEventArgs e)
 {
 string s = textBox.Text;
 int len = s.Length;
 if (textBox.Text != null && len >= 1)
 textBox.Text = s.Remove(len - 1);
 if (ope == null)
 {
 num1 = textBox.Text;
 }
 else
 {
 num2 = textBox.Text;
 }
}

6.计算结果(利用switch case )

private void buttonEQ_Click(object sender, RoutedEventArgs e)
 {
 switch (ope)
 {
 case "+":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) + Convert.ToDouble(num2));
 break;
 case "-":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) - Convert.ToDouble(num2));
 break;
 case "*":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) * Convert.ToDouble(num2));
 break;
 case "/":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) / Convert.ToDouble(num2));
 break;
 }
 num1 = textBox.Text; 
 num2 = null;
 ope = null;
}

将结果值赋给num1来实现连续计算.

效果如如下:

完整代码如下:

*xaml

Window x:Class="小小计算器.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="计算器"  >
 
 
 
 
 
 

.cs文件

namespace 小小计算器
{
 /// 
 /// MainWindow.xaml 的交互逻辑
 /// 
 public partial class MainWindow : Window
 {
 string num1 = null; //运算符之前的数
 string num2 = null; //运算符之后的数
 string ope = null; //运算符
 public MainWindow()
 {
 InitializeComponent();
 }

 private void button1_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button1.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button1.Content;
 textBox.Text = num2;
 }
 }

 private void button2_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button2.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button2.Content;
 textBox.Text = num2;
 }
 }

 private void button3_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button3.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button3.Content;
 textBox.Text = num2;
 }
 }

 private void button4_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button4.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button4.Content;
 textBox.Text = num2;
 }
 }

 private void button5_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button5.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button5.Content;
 textBox.Text = num2;
 }
 }

 private void button6_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button6.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button6.Content;
 textBox.Text = num2;
 }
 }

 private void button7_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button7.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button7.Content;
 textBox.Text = num2;
 }
 }

 private void button8_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button8.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button8.Content;
 textBox.Text = num2;
 }
 }

 private void button9_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button9.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button9.Content;
 textBox.Text = num2;
 }
 }
 private void button0_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button0.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button0.Content;
 textBox.Text = num2;
 }
 }

 private void buttonADD_Click(object sender, RoutedEventArgs e)
 {
 ope = "+";
 }

 private void buttonSUB_Click(object sender, RoutedEventArgs e)
 {
 ope = "-";
 }

 private void buttonMUP_Click(object sender, RoutedEventArgs e)
 {
 ope = "*";
 }

 private void buttonDIV_Click(object sender, RoutedEventArgs e)
 {
 ope = "/";
 }

 private void buttonDOT_Click(object sender, RoutedEventArgs e)
 {

 if (ope == null &&!num1.Contains(".")) //如果num中已有小数点,则不允许再输入.
 {

 num1 += buttonDOT.Content;
 textBox.Text = num1;

 }
 if(ope!=null&&!num2.Contains("."))  
 {
 num2 += buttonDOT.Content;
 textBox.Text = num2;
 }
 }

 private void buttonCE_Click(object sender, RoutedEventArgs e)
 {
 //if (ope == null)
 //{
 // num1 = textBox.Text;
 //}
 //else
 //{
 // num2 = textBox.Text;
 //}
 this.textBox.Text = "";
 num1 = null;
 num2 = null;
 ope = null;
 }

 private void buttonBK_Click(object sender, RoutedEventArgs e)
 {
 string s = textBox.Text;
 int len = s.Length;
 if (textBox.Text != null && len >= 1)
 textBox.Text = s.Remove(len - 1);
 if (ope == null)
 {
 num1 = textBox.Text;
 }
 else
 {
 num2 = textBox.Text;
 }
 }

 private void buttonEQ_Click(object sender, RoutedEventArgs e)
 {
 switch (ope)
 {
 case "+":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) + Convert.ToDouble(num2));
 break;
 case "-":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) - Convert.ToDouble(num2));
 break;
 case "*":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) * Convert.ToDouble(num2));
 break;
 case "/":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) / Convert.ToDouble(num2));
 break;
 }
 num1 = textBox.Text;
 num2 = null;
 ope = null;
 }


 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文介绍了在使用MSXML解析XML文件时出现DTD禁用问题的解决方案。通过代码示例和错误信息获取方法,解释了默认情况下DTD是禁用的,以及如何启用DTD的方法。此外,还提到了网上关于该问题的信息相对较少,因此本文提供了解决方案以供参考。 ... [详细]
author-avatar
放开丶那手
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有