利用C#编程一个计算器

开始前先对计算器的架构进行分析。计算器需要按钮和对对应按钮的逻辑处理这两部分。

则可以用到VS2022的Windows窗体应用(.NET Framework)模板如下图

一.找到并创建对应的模板

标题

二.找到视图选项中的工具箱,对form1窗体添加组件,显示框为textBox按钮组件,数字以及运算符用button按钮组件,将数字按钮的text值改为对应的数字例如1,2,3,4等,运算符同理。

三.添加点击事件

  在编译点击事件之前先定义一些布尔常量,以及常量值等。如下图

   1.数字点击事件的实现

点击对应的按钮,将值赋值给文本显示框进行显示,利用strAII进行字符串存储。其余数字点击事件同理。如下图

    2.运算符点击事件

      先判断前面的字符存储是否为空,判断是否有+字符,判断为真后,最后再改变对应运算符布尔的值,对文本内容进行空显示,方便接下来的点击事件文本显示。

  3.结果点击事件

     结果点事件也就是“=”运算符的点击事件,对应的代码如下图,最后一步完成。完整的代码在下文。

  

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        bool cheng = false;
        bool chu = false;
        bool jia = false;
        bool jian = false;
        double num = 0;
        string strAll = "";
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += button2.Text;
            strAll += button2.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (strAll.Length == 0)
            {
                strAll += "";
            }
            else if (strAll[strAll.Length - 1] == '+') 
            {
                strAll += ""; // ""
            }
            else
            {
                jia = true;
                textBox1.Text = "";
                strAll += button8.Text;   
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (strAll.Length == 0)
            {
                strAll += "";
            }
            else if (strAll[strAll.Length - 1] == '-') 
            {
                strAll += ""; 
            }
            else
            {
                jian = true;
                textBox1.Text = "";
                strAll += button12.Text;   
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text += button5.Text;
            strAll += button5.Text;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            strAll = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += button1.Text;
            strAll += button1.Text;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (strAll == "")
            {
               textBox1.Text = strAll;
            }
            else if (strAll[strAll.Length - 1] < '0' || strAll[strAll.Length - 1] > '9')
            {
                strAll = strAll.Remove(strAll.Length - 1, 1);   
                textBox1.Text = strAll;
            }
            else
            {
                DataTable a = new DataTable();
                textBox1.Text = a.Compute(strAll, "").ToString(); 
                cheng = false;
                chu = false;
                jian = false;
                jia = false;
                strAll = textBox1.Text; 
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text += button3.Text;
            strAll += button3.Text;

        }

        private void button6_Click(object sender, EventArgs e)
        {
            textBox1.Text += button6.Text;
            strAll += button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text += button7.Text;
            strAll += button7.Text;
        }

        private void button9_Click(object sender, EventArgs e)
        {
            textBox1.Text += button9.Text;
            strAll += button9.Text;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            textBox1.Text += button10.Text;
            strAll += button10.Text;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox1.Text += button11.Text;
            strAll += button11.Text;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            textBox1.Text += button14.Text;
            strAll += button14.Text;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if (strAll.Length == 0)
            {
                strAll += "";
            }
            else if (strAll[strAll.Length - 1] == '*') 
            {
                strAll += ""; // ""
            }
            else
            {
                cheng = true;
                textBox1.Text = "";
                strAll += button16.Text;   
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (strAll.Length == 0)
            {
                strAll += "";
            }
            else if (strAll[strAll.Length - 1] == '/') 
            {
                strAll += ""; // ""
            }
            else
            {
                chu= true;
               textBox1.Text = "";
                strAll += button15.Text;   
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值