1.   实验目的

(  1)熟悉C#语言的使用和语法知识

2.   实验要求

(1)设计简单的含交互界面的计算器软件,具有较强的用户体验感。

(2)使用C#语言进行编程,创建窗体应用程序。

(3)实现较简单加减乘除等运算。

3.    运行效果如下:

4.    效果特点:

(1)每个按钮均可输入,但会阻止一些非法输入,例如,在没有输入数字的时候就输入运算符,点号的多次输入,这些都会阻止。

(2)实现了运算的优先级,即乘除是先与加减计算的。

(3)可实现小数的计算,即弥补了整型无法得打小数的缺陷

(4)增加了归零按钮,一键返回到初始状态

(5)按钮和键盘绑定,可实现键盘敲击时,界面按钮同时相应

(6)结果可继续运算,在点击“=”完成后,可在第二个文本框中输出结果,这时,如果在点击运算符,可对这个结果继续进行操作,实现完美的可继续性

5.代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace 按键计算器
{
public partial class Form1 : Form
{
double[] number = new double[];
char[] operat = new char[];
bool isNumber = false;
bool isDot = false;
bool isOperat = false;
bool isEqual = false; int numberIndex = ;
int operatIndex = ; int dotCount = ; public Form1()
{
InitializeComponent();
btnleft.Hide();
btnright.Hide(); } //将键盘与按钮关联
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.NumPad1||keyData==Keys.D1)
{
this.btn1.Focus();
this.btn1.PerformClick();
}
if (keyData == Keys.NumPad2 || keyData == Keys.D2)
{
this.btn2.Focus();
this.btn2.PerformClick();
}
if (keyData == Keys.NumPad3 || keyData == Keys.D3)
{
this.btn3.Focus();
this.btn3.PerformClick();
}
if (keyData == Keys.NumPad4 || keyData == Keys.D4)
{
this.btn4.Focus();
this.btn4.PerformClick();
}
if (keyData == Keys.NumPad5 || keyData == Keys.D5)
{
this.btn5.Focus();
this.btn5.PerformClick();
}
if (keyData == Keys.NumPad6 || keyData == Keys.D6)
{
this.btn6.Focus();
this.btn6.PerformClick();
}
if (keyData == Keys.NumPad7 || keyData == Keys.D7)
{
this.btn7.Focus();
this.btn7.PerformClick();
}
if (keyData == Keys.NumPad8 || keyData == Keys.D8)
{
this.btn8.Focus();
this.btn8.PerformClick();
}
if (keyData == Keys.NumPad9 || keyData == Keys.D9)
{
this.btn9.Focus();
this.btn9.PerformClick();
}
if (keyData == Keys.NumPad0 || keyData == Keys.D0)
{
this.btn0.Focus();
this.btn0.PerformClick();
}
if (keyData == Keys.Add)
{
this.btnadd.Focus();
this.btnadd.PerformClick();
}
if (keyData == Keys.Subtract || keyData == Keys.OemMinus)
{
this.btnsub.Focus();
this.btnsub.PerformClick();
}
if (keyData == Keys.Multiply)
{
this.btnmul.Focus();
this.btnmul.PerformClick();
}
if (keyData == Keys.Divide)
{
this.btndiv.Focus();
this.btndiv.PerformClick();
}
if (keyData == Keys.Enter || keyData == Keys.Oemplus)
{
this.btnequ.Focus();
this.btnequ.PerformClick();
}
if (keyData == Keys.Back)
{
this.btncle.Focus();
this.btncle.PerformClick();
}
return base.ProcessCmdKey(ref msg, keyData);
} private void btn1_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text ="";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; }
textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false; } private void btn2_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false; } private void btn3_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false; } private void btn4_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false; } private void btn5_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false; } private void btn6_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + "";
isNumber = true;
isDot = false;
isOperat = false;
isEqual = false;
} private void btn7_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false;
} private void btn8_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false;
} private void btn9_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; }
textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false;
} private void btn0_Click(object sender, EventArgs e)
{
if (isEqual == true)
{ textBox1.Text = "";
textBox2.Text = ""; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; }
textBox1.Text = textBox1.Text + "";
textBox2.Text = textBox2.Text + ""; isNumber = true;
isDot = false;
isOperat = false;
isEqual = false;
} private void btnf_Click(object sender, EventArgs e)
{
if ((isNumber == true)&&(dotCount==))
{
textBox1.Text = textBox1.Text + ".";
textBox2.Text = textBox2.Text + "."; dotCount++;
isNumber = false;
isDot = true;
isOperat = false;
isEqual = false;
}
} private void btnadd_Click(object sender, EventArgs e)
{
if (isEqual == true)
{
number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '+';
textBox1.Text = textBox2.Text + "+";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } if ((isOperat==false)&&(isDot==false)&&(isNumber == true))
{ number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '+';
textBox1.Text = textBox1.Text + "+";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } } private void textBox1_TextChanged(object sender, EventArgs e)
{
} private void btnmul_Click(object sender, EventArgs e)
{
if (isEqual == true)
{
number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '*';
textBox1.Text = textBox2.Text + "*";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } if ((isOperat == false) && (isDot == false) && (isNumber == true))
{ number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '*';
textBox1.Text = textBox1.Text + "*";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } } private void btndiv_Click(object sender, EventArgs e)
{ if (isEqual == true)
{
number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '/';
textBox1.Text = textBox2.Text + "/";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; }
if ((isOperat == false) && (isDot == false) && (isNumber == true))
{ number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '/';
textBox1.Text = textBox1.Text + "/";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; } } private void btnsub_Click(object sender, EventArgs e)
{
if (isEqual == true)
{
number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '-';
textBox1.Text = textBox2.Text + "-";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; }
if ((isOperat == false) && (isDot == false) && (isNumber == true))
{ number[numberIndex] = double.Parse(textBox2.Text);
operat[operatIndex] = '-';
textBox1.Text = textBox1.Text + "-";
textBox2.Text = ""; numberIndex++;
operatIndex++; dotCount = ;
isNumber = false;
isDot = false;
isOperat = true;
isEqual = false; }
} private void btnequ_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
{
number[numberIndex] = double.Parse(textBox2.Text); //判断乘除
for (int i = ; i < operatIndex;)
{ //对乘号分析
if (operat[i] == '*')
{
number[i] = number[i] * number[i + ];
for (int j = i + ; j < operatIndex; j++)
{
number[j] = number[j + ];
operat[j - ] = operat[j]; }
numberIndex--;
operatIndex--; }
//对除号分析
else if (operat[i] == '/')
{
number[i] = number[i] / number[i + ];
for (int j = i + ; j < operatIndex; j++)
{
number[j] = number[j + ];
operat[j - ] = operat[j]; }
numberIndex--;
operatIndex--; }
else
{ i++; } } //判断加减
for (int i = ; i < operatIndex;)
{ //对加号分析
if (operat[i] == '+')
{
number[i] = number[i] + number[i + ];
for (int j = i + ; j < operatIndex; j++)
{
number[j] = number[j + ];
operat[j - ] = operat[j]; }
numberIndex--;
operatIndex--; }
//对减号分析
else if (operat[i] == '-')
{
number[i] = number[i] - number[i + ];
for (int j = i + ; j < operatIndex; j++)
{
number[j] = number[j + ];
operat[j - ] = operat[j]; }
numberIndex--;
operatIndex--; }
else { i++; } } textBox2.Text = number[].ToString();
isNumber = false;
isDot = false;
isOperat = false;
isEqual = true;
numberIndex = ;
operatIndex = ;
} }
private void btncle_Click(object sender, EventArgs e)
{
isNumber = false;
isDot = false;
isOperat = false;
isEqual = false; numberIndex = ;
operatIndex = ; dotCount = ; textBox1.Text = "";
textBox2.Text = ""; } private void btnleft_Click(object sender, EventArgs e)
{ } private void btnright_Click(object sender, EventArgs e)
{ }
}
}

C#实现按键计算器功能2(增强版)的更多相关文章

  1. C#实现按键计算器功能

    C#实现按键计算器功能 (一次失败的编程) 界面: 代码如下: using System; using System.Collections.Generic; using System.Compone ...

  2. python模块——re模块(简单的计算器功能实现_eval版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" # Usage: Make a Diy Calcu ...

  3. 微慕WordPress小程序增强版

    2017年1月9日,张小龙在2017微信公开课Pro上发布的微信小程序正式上线.在过去的2年多的时间里,微信小程序领头,各大互联网平台也不甘落后,陆续推出自己的小程序.2018年7月4日,百度智能小程 ...

  4. jQuery 1.4版本的15个新功能(现在已经发布到jquery1.8,特别是增强版的live事件,支持 submit , change , focus 和 blur 事件)

    1.jQuery()创建DOM元素:支持传参设置属性 之前,jQuery可以通过 attr 方法设置元素的属性,既可传属性的名和值,也可以是包含几组特定 属性名值对 的 对象.在 jQuery 1.4 ...

  5. 个人永久性免费-Excel催化剂功能第60波-数据有效性验证增强版,补足Excel天生不足

    Excel在数据处理.数据分析上已经是公认的最好用的软件之一,其易用性和强大性也吸引无数的初中高级用户每天都在使用Excel.但这些优点的同时,也带出了一些问题,正因为其不同于一般的专业软件,需要专业 ...

  6. 将表里的数据批量生成INSERT语句的存储过程 增强版

    将表里的数据批量生成INSERT语句的存储过程 增强版 有时候,我们需要将某个表里的数据全部或者根据查询条件导出来,迁移到另一个相同结构的库中 目前SQL Server里面是没有相关的工具根据查询条件 ...

  7. 最新GHOST XP系统下载旗舰增强版 V2016年

    系统来自:系统妈:http://www.xitongma.com 深度技术GHOST xp系统旗舰增强版 V2016年3月 系统概述 深度技术ghost xp系统旗舰增强版集合微软JAVA虚拟机IE插 ...

  8. 最新深度技术GHOST XP系统旗舰增强版 V2016年

    来自系统妈:http://www.xitongma.com 深度技术GHOST xp系统旗舰增强版 V2016年 系统概述 深度技术ghost xp系统旗舰增强版集合微软JAVA虚拟机IE插件,增强浏 ...

  9. WinNTSetup v3.8.7 正式版绿色增强版

    最强系统安装利器:WinNTSetup 现已更新至 v3.8.7 正式版!这次更新修复调整了诸多问题,新版非常好用接近完美!WinNTSetup 现在已经自带BCDBoot 选项,并且完全支持Wind ...

随机推荐

  1. Myeclipse和 eclipse中的控制台汉字横着显示修改

    Myeclipse和 eclipse中的控制台汉字横着显示问题的修改 如图: 同一种字体有两种显示方式,比如微软雅黑和@微软雅黑,前一种汉字是竖着显示,后一种汉字是横着显示. 修改方法: prefer ...

  2. C++11-->单生产者,单消费者问题

    参考上一篇C++11并发编程 #include <iostream> #include <queue> #include <assert.h> #include & ...

  3. Linux 文件系统(一)---虚拟文件系统VFS----超级块、inode、dentry、file

    转自:http://blog.csdn.net/shanshanpt/article/details/38943731 http://elixir.free-electrons.com/linux/v ...

  4. 深度解析synchronized的实现原理(并发一)

    一.synchronized实现原理 1.synchronized实现同步的基础: 1).普通同步方法:锁是当前实例对象 2).静态同步方法:锁是当前类的class对象 3).同步方法块:锁是括号里面 ...

  5. vim学习纪要

    普通模式 根据屏幕行上下移动. gj gk g0 g^ g$ 移动到行首第一个非空字符 ^ 反向移动到上一单词的词尾 ge 插入模式 粘贴寄存器中内容 <C-r> 可视模式 移动光标的起始 ...

  6. 分布式大数据系统离线分析技术解决方案(spark2.x)

    一.sark2.x新型的架构系统

  7. if else 和if elif else的区别

    def fuck(a): if a ==1: print(a) if a ==2: print("not good") else: print("tamade" ...

  8. ubuntu的安装与vim(部分)文件的配置

    Ubuntu的安装与vim相关的配置 虚拟机环境的安装(先说说虚拟机的安装步骤把) 对于安装ubuntu来讲不是很难掌握,但是在Linux系统虚拟机下提供了很多便利的手段,而要配置一个顺手的虚拟机环境 ...

  9. Vue对Html标签应用条件渲染

    我的需求 在flag属性为true时 渲染标签的html为 <input :data-val-required="不能为空"/> 在flag属性为false时 渲染标签 ...

  10. Django中Model-Form验证

    Django中Model-Form验证 class UserType(models.Model): caption=models.CharField(max_length=32) class User ...