C#实现计算器窗体程序

 更新时间:2022年01月31日 12:53:36   作者:羽路星尘  
这篇文章主要为大家详细介绍了C#实现计算器窗体程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现计算器窗体程序的具体代码,供大家参考,具体内容如下

功能设计

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

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

3、计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方。

实现代码

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 test3_1
{
    public partial class Form1 : Form
    {
        double result = 0;              //存储计算结果
        double number = 0;              //存储输入的数字
        bool exist_value = false;       //判断文本框中是否有值
        string operation;               //存储输入的运算符

        /*
         * 初始化
         */

        public Form1()
        {
            InitializeComponent();
        }

        /*
         * 数字键触发事件实现
         */
        private void Seven_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "7";
        }

        private void Eight_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "8";
        }

        private void Nine_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "9";
        }

        private void Four_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "4";
        }

        private void Five_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "5";
        }

        private void Six_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "6";
        }

        private void One_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "1";
        }

        private void Two_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "2";
        }

        private void Three_Click(object sender, EventArgs e)
        {
            if (exist_value == true)
            {
                textBox1.Text = "";
                exist_value = false;
            }
            textBox1.Text += "3";
        }

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

        /*
         * 功能键触发事件
         */
        private void Add_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "+";
            }
        }

        private void Sub_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "-";
            }
        }

        private void Mul_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "*";
            }
        }

        private void Div_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "/";
            }
        }

        private void Squ_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "x^2";
            }
               
        }

        private void Sqrt_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "sqrt";
            }
        }

        private void Log_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "log";
            }
        }

        private void Ln_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                exist_value = true;
                number = double.Parse(textBox1.Text);
                operation = "ln";
            }
        }

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

        private void Equ_Click(object sender, EventArgs e)
        {
            switch (operation)
            {
                case "+": result = number + double.Parse(textBox1.Text); break;
                case "-": result = number - double.Parse(textBox1.Text); break;
                case "*": result = number * double.Parse(textBox1.Text); break;
                case "/":
                    {
                        double temp=double.Parse(textBox1.Text);
                        if (temp != 0)
                            result = number / temp;
                        else
                            MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;
                    }
                case "x^2": result = number * number; break;
                case "sqrt": result = Math.Sqrt(number); break;
                case "log": result = Math.Log10(number); break;
                case "ln": result = Math.Log(number); break;
            }
            textBox1.Text = result + "";
            exist_value = true;
        }
    }
}

界面设计

运行结果

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

相关文章

  • C# 如何使用OpcUaHelper读写OPC服务器

    C# 如何使用OpcUaHelper读写OPC服务器

    这篇文章给大家介绍C# 如何使用OpcUaHelper读写OPC服务器,本文通过图文实例代码相结合给大家介绍的非常详细,需要的朋友参考下吧
    2023-12-12
  • c#实现隐藏与显示任务栏的方法详解

    c#实现隐藏与显示任务栏的方法详解

    本篇文章是对c#中任务栏隐藏与显示的实现方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#结合AForge实现摄像头录像

    C#结合AForge实现摄像头录像

    最近由于兴趣学习了下在C#上使用AForge录制摄像头视频并压缩编码。总体上来说这个第三方.net视觉开发库还是比较稳定的
    2017-09-09
  • C#使用Streamwriter打开文件的方法

    C#使用Streamwriter打开文件的方法

    这篇文章主要介绍了C#使用Streamwriter打开文件的方法,涉及C#操作文件的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • C#中的两种debug方法介绍

    C#中的两种debug方法介绍

    这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹、利用宏定义两种方法,需要的朋友可以参考下
    2015-02-02
  • C#中DataTable 转实体实例详解

    C#中DataTable 转实体实例详解

    这篇文章主要介绍了C#中DataTable 转实体实例详解,需要的朋友可以参考下
    2017-04-04
  • C# SelectedIndexChanged事件详解

    C# SelectedIndexChanged事件详解

    这篇文章主要介绍了C# SelectedIndexChanged事件详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C# Winform中如何绘制动画示例详解

    C# Winform中如何绘制动画示例详解

    这篇文章主要给大家介绍了关于C# Winform中如何绘制动画的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C# Winform具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • WPF中使用WebView2控件的方法及常见问题

    WPF中使用WebView2控件的方法及常见问题

    WebView2为WPF网页浏览工具,具有简单易用,页面显示清晰的优点,下面这篇文章主要给大家介绍了关于WPF中使用WebView2控件的方法及常见问题,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • C#与js实现去除textbox文本框里面重复记录的方法

    C#与js实现去除textbox文本框里面重复记录的方法

    这篇文章主要介绍了C#与js实现去除textbox文本框里面重复记录的方法,很实用的功能,需要的朋友可以参考下
    2014-08-08

最新评论