C# 计算器窗体程序(进阶版)

本文介绍了如何设计并实现一个C#计算器窗体程序,包括功能设计如10个数字键和小数点,加减乘除等操作键。设计思路涉及操作符优先级处理,通过栈管理运算,同时实时显示运算过程。提供了实现代码和界面设计的概述。
摘要由CSDN通过智能技术生成

功能设计

1、计算器中,添加 0-9 共十个数字键和小数点键。

2、计算器中,增添 加、减、乘、除、等于 五个功能键。

设计思路

1、数字计算有优先级,因此将操作符放入栈内,先进行乘除操作、后进行加减操作。
2、可以显示当前数字计算内容,因此将每一次属于本次运算的数字放入运算信息中进行显示。

实现代码

using System;
using System.Collections;
using System.Windows.Forms;

namespace Calculator
{
   
    public partial class Form1 : Form
    {
   
        // 存储写入数字
        Stack numbers = new Stack();
        // 存储运算符
        Stack operations = new Stack();
        // 判断计算信息是否需要重新显示
        bool show = false;
        // 判断是否需要重新写入
        bool write = false;
        bool exist = false;
        /*
         * 初始化
         */

        public Form1()
        {
   
            numbers.Push(0);
            InitializeComponent();
            textBox1.Text = "0";
            textBox2.Text = "";
        }

        /*
         * 数字键触发事件实现
         */

        private void One_Click(object sender, EventArgs e)
        {
   
            if(textBox1.Text == "0" || write)
                textBox1.Text = "1";
            else
                textBox1.Text += "1";
            write = false;
        }

        private void Two_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "2";
            else
                textBox1.Text += "2";
            write = false;
        }

        private void Three_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "3";
            else
                textBox1.Text += "3";
            write = false;
        }

        private void Four_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "4";
            else
                textBox1.Text += "4";
            write = false;
        }

        private void Five_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "5";
            else
                textBox1.Text += "5";
            write = false;
        }

        private void Six_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "6";
            else
                textBox1.Text += "6";
            write = false;
        }

        private void Seven_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "7";
            else
                textBox1.Text += "7";
            write = false;
        }

        private void Eight_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "8";
            else
                textBox1.Text += "8";
            write = false;
        }

        private void Nine_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "9";
            else
                textBox1.Text += "9";
            write = false;
        }

        private void Zero_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "0";
            else
                textBox1.Text += "0";
            write = false;
        }

        private void Point_Click(object sender, EventArgs e)
        {
   
            if (textBox1.Text == "0" || write)
                textBox1.Text = "0.";
            else if(!exist)
                textBox1.Text += ".";
            write = false;
            exist = true;
        }

        /*
         * 功能键触发事件
         */

        private void Del_Click(object sender, EventArgs e)
        {
   
            string text = textBox1.Text;
            // 删除最后一个字符,若数字长度为1,则将显示置零。
            if (text.Length > 1)
            {
   
                textBox1.Text = text.Substring(0, text.Length - 1);
                if(text.Substring(text.Length-1, 1) == ".")
                {
   
                    exist = false;
                }
            }
            else
                textBox1.Text = "0";
        }

        private void Add_Click(object sender, EventArgs e)
        {
   
            if(show == false)
                textBox2.Text += textBox1.Text + "+";
            else
                textBox2.Text = textBox1.Text + "+";
            // 将写入的数字入栈
            numbers.Push(double.Parse(textBox1.Text));
            // 输入显示置零
            textBox1.Text = "0";
            // 将运算符入栈
            operations.Push("+");
            show = false;
            // 允许重新写入
            write = true;
            exist = false;
        }

        private void Sub_Click(object sender, EventArgs e)
        {
   
            if (show == false)
                textBox2.Text += textBox1.Text + "-";
            else
                textBox2.Text = textBox1.Text + "-";
            // 将写入的数字入栈
            numbers.Push(double.Parse(textBox1.Text));
            // 输入显示置零
            textBox1.Text = "0";
            // 将运算符入栈
            operations.Push
  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的C#计算器窗体程序示例: 1. 创建一个Windows窗体应用程序项目。 2. 在窗体上放置一些控件,包括Label、TextBox和一些Button。 3. 命名控件的名称,例如,将Label命名为lblResult,TextBox命名为txtInput,Button命名为btnAdd、btnSubtract、btnMultiply、btnDivide和btnEqual。 4. 在窗体的代码文件中添加以下代码: ```csharp using System; using System.Windows.Forms; namespace Calculator { public partial class Form1 : Form { Double value = 0; String operation = ""; bool operation_pressed = false; public Form1() { InitializeComponent(); } private void button_click(object sender, EventArgs e) { if ((txtInput.Text == "0") || (operation_pressed)) txtInput.Clear(); operation_pressed = false; Button button = (Button)sender; txtInput.Text = txtInput.Text + button.Text; } private void operator_click(object sender, EventArgs e) { Button button = (Button)sender; operation = button.Text; value = Double.Parse(txtInput.Text); operation_pressed = true; lblResult.Text = value + " " + operation; } private void btnClear_Click(object sender, EventArgs e) { txtInput.Text = "0"; value = 0; lblResult.Text = ""; } private void btnEqual_Click(object sender, EventArgs e) { switch (operation) { case "+": txtInput.Text = (value + Double.Parse(txtInput.Text)).ToString(); break; case "-": txtInput.Text = (value - Double.Parse(txtInput.Text)).ToString(); break; case "*": txtInput.Text = (value * Double.Parse(txtInput.Text)).ToString(); break; case "/": txtInput.Text = (value / Double.Parse(txtInput.Text)).ToString(); break; default: break; } lblResult.Text = ""; } } } ``` 5. 运行程序,您就可以使用这个简单的计算器窗体应用程序了。 这个计算器程序可以进行加、减、乘、除四种基本运算,可以清除输入文本框的内容,也可以计算输入的表达式的结果。您可以根据需要进行修改和拓展,增加其他的功能和运算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羽路星尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值