C#多功能计算器(winform版)

在这里插入图片描述

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 计算器4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Location=new Point(Screen.PrimaryScreen.WorkingArea.Width/2-this.Width/2,Screen.PrimaryScreen.WorkingArea.Height/2-this.Height/2);
            textBox1.ReadOnly = true; // 禁止输入
            textBox2.ReadOnly = true;
        }

        private const int V = 2;
        string fuhao; // 符号存储
        double num1, num2; // 第一个数,第二个数
        bool isCale = false;  //记录上一个字符是否是运算符
        // 1
        private void button1_Click(object sender, EventArgs e)
        {
            if (isCale==true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 1;
            textBox2.Text += 1;
        }

        // 2
        private void button2_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 2;
            textBox2.Text += 2;
        }
        // 3
        private void button3_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 3;
            textBox2.Text += 3;
        }

        // 4
        private void button4_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 4;
            textBox2.Text += 4;
        }
        // 5
        private void button5_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 5;
            textBox2.Text += 5;
        }
        // 6
        private void button6_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 6;
            textBox2.Text += 6;
        }
        // 7
        private void button7_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 7;
            textBox2.Text += 7;
        }
        // 8
        private void button8_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 8;
            textBox2.Text += 8;
        }
        // 9
        private void button9_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 9;
            textBox2.Text += 9;
        }
        // 0
        private void button10_Click(object sender, EventArgs e)
        {
            if (isCale == true)
            {
                isCale = false;
                textBox1.Text = "";
            }
            textBox1.Text += 0;
            textBox2.Text += 0;
        }
        // 点
        private void button11_Click(object sender, EventArgs e)
        {
            textBox2.Text += ".";
        }
        // π
        private void button12_Click(object sender, EventArgs e)
        {
            textBox2.Text += Math.PI;
        }
        // 加
        private void button16_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "+";
            isCale = true;
            fuhao = "+";
        }
        // 减
        private void button15_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "-";
            fuhao = "-";
            isCale = true;
        }
        // 乘
        private void button14_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "×";
            fuhao = "×";
            isCale = true;
        }
        // 除
        private void button13_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "÷";
            fuhao = "÷";
            isCale = true;
        }
        // 余
        private void button20_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "%";
            fuhao = "%";
            isCale = true;
        }
        // 平方
        private void button19_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "²";
            fuhao = "²";
            isCale = true;
        }
        // 立方
        private void button18_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "³";
            fuhao = "³";
            isCale = true;
        }
        // 开平方
        private void button17_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "√";
            fuhao = "√";
            isCale = true;
        }
        // sin
        private void button24_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "sin";
            fuhao = "s";
            isCale = true;
        }
        // cos
        private void button23_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "cos";
            fuhao = "c";
            isCale = true;
        }
        // tan 
        private void button22_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            textBox2.Text += "tan";
            fuhao = "t";
            isCale = true;
        }
        // 转二进制
        private void button32_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "BIN";
            fuhao = "bin";

        }
        //转八进制
        private void button31_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "OCT";
            fuhao = "oct";
        }
        // 转十六进制
        private void button33_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "HEA";
            fuhao = "hea";
        }
        // &
        private void button21_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "&";
            fuhao = "&";
            isCale = true;
        }
        // |
        private void button28_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "|";
            fuhao = "|";
            isCale = true;
        }
        // ^
        private void button29_Click(object sender, EventArgs e)
        {
            num1 = int.Parse(textBox1.Text);
            textBox2.Text += "^";
            fuhao = "^";
            isCale = true;
        }
        // 清除
        private void button26_Click(object sender, EventArgs e)
        {
            textBox2.Text = null;
            textBox1.Text = null;
        }
        // 删除一格
        private void button27_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length>0)
            {
                textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
            }
        }
        // 等于
        private void button25_Click(object sender, EventArgs e)
        {
            isCale = true;
            num2 = double.Parse(textBox1.Text);
            if (fuhao == "+")
            {
                textBox1.Text = (num1 + num2).ToString();
            }
            if (fuhao == "-")
            {
                textBox1.Text = (num1 - num2).ToString();
            }
            if (fuhao == "×")
            {
                textBox1.Text = (num1 * num2).ToString();
            }
            if (fuhao == "÷")
            {
                textBox1.Text = (num1 % num2).ToString();
            }
            if (fuhao == "%")
            {
                textBox1.Text = (num1 % num2).ToString();
            }
            if (fuhao == "²")
            {
                textBox1.Text =(Math.Pow(num2, 2)).ToString();
            }
            if (fuhao== "³")
            {
                textBox1.Text = (Math.Pow(num2, 3)).ToString();
            }
            if (fuhao== "s")
            {
                textBox1.Text = (Math.Sin(num2 * Math.PI) / 180).ToString();
            }
            if (fuhao== "c")
            {
                textBox1.Text = (Math.Cos(num2 * Math.PI) / 180).ToString();
            }
            if (fuhao== "t")
            {
                textBox1.Text = (Math.Tan(num2 * Math.PI / 180)).ToString();
            }
            if (fuhao == "&")
            {
                textBox1.Text =((int)num1&(int)num2).ToString();
            }
            if (fuhao == "|")
            {
                textBox1.Text = ((int)num1 | (int)num2).ToString();
            }
            if (fuhao == "^")
            {
                textBox1.Text = ((int)num1 ^ (int)num2).ToString();
            }
            if (fuhao == "bin")
            {
               textBox1.Text = Convert.ToString((int)num2,2);
            }
            if (fuhao == "oct")
            {
                textBox1.Text = Convert.ToString((int)num2, 8);
            }
            if (fuhao == "hea")
            {
                textBox1.Text = Convert.ToString((int)num2, 16);
            }
            textBox2.Text += "=" + textBox1.Text;
        }
    }
}

实现功能:模拟微软计算器界面,实现四则混合运算1.键盘输入(KeyUp事件)2.无焦点(按钮失去焦点)3.实现优先级运算。比如直接输入1-2*3=-5,而不是微软计算器的-34.使用操作工厂,使用接口5.实现菜单里的复制粘贴功能6.可视化文本框7.实现中间操作结果显示8.正则表达式验证输入是否为数字9.小数点个数校验10.使用发消息_Flag实现操作符状态的判定及转换部分注释预览:失去焦点: private void text_display_GotFocus(object sender, EventArgs e) { /* * 文本框的“获取焦点”事件发生时执行的方法。每次获得焦点时,就会执行此方法,使之马上失去焦点。 * * 当某控件的Enable属性变为False的时候,它的焦点将转移到TabIndex属性值比它大1的控件上。 * 这时,如果有多个控件的TabIndex属性值同时比它大1,鼠标点击按钮或敲击键盘时候会发出“咚”的一声。 * 故在本程序中,将label_m控件的TabIndex设为1,其它的全部设为0,因此所有的控件在不可用时焦点都会转移到label_m上, * 因为label_m没有Click和KeyUp事件,所以不会出错。 * 这样就实现了全局无焦点的功能。 * */ text_display.Enabled = false; //先使文本框不可用,这时焦点转移到TabIndex比文本框大的下一个控件上 text_display.Enabled = true; //再使文本框可用,这时焦点不会返回。 }KeyUp事件: else if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.D1) { /* 当窗体的某个控件触发了其本身的KeyUp事件之后, * 将会调用keyUp()方法,并判断是哪个按键 * 如果是大键盘或者是小键盘的1时,便调用num_Click()方法。 * 参数是no_1和e。 * 在这里的no_1指的是按钮no_1,e是KeyUp事件 * no_1是按钮,参数格式正确;而e是KeyUp事件,也是事件的一种。KeyEventHandler当然也是EventHandler的一部分。 * 所以调用了之num_Click()后一切按照no_1按钮事件的操作执行 * 所以no_1按钮的这一句 * this.no_1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.num_Click); * 可以不写 */ num_Click(no_1, e); }小数点点击的校验: private void dot_Click(object sender, EventArgs e) { if (!dotFlag) //没点击的情况下 { if (operFlag) //如果点击了运算符,就将文本换为"0.",并将小数点设为已点击 { text_display.Text = "0."; dotFlag = true; } else if (text_display.Text.Equals("0.")) //如果没有点击运算符,切当前文本是"0.",便保持现状但将小数点设为已点击 { dotFlag = true; } else if (text_display.Text.Equals("0") || text_display.Text.Equals("")) { text_display.Text = "0."; dotFlag = true; } else //其他情况直接添加并将小数点设为已点击 { text_display.Text = text_display.Text + "."; dotFlag = true; } } else //如果已点击则什么也不做 { } enterFlag = false; label_m.Focus(); //键盘按键之后焦点由下面的各个_GotFocus()方法控制;鼠标点击之后的焦点有这条语句控制,同样使焦点转移到label_m上。 }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值