C#实现简易计算器功能(附源码)

作者:Just Do Its 时间:2021-07-18 00:16:00 

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

剖析:

1、先设计界面(按钮、文本框(一个显示算式,一个显示结果))布局
2、单击按钮将其对应内容显示在文本框中
3、单击符号(+、-、×、÷、%)时将第一次输入的数储存起来
4、单击等号时将第二次输入的数存储起来并将第一次输入的数与第二次输入的数按照所单击的符号进行运算将结果显示在第一个文本框中
5、单击C时将两个文本框中的内容清空

重点:

1、声明一个bool类型的变量用于实现单击符号再次输入数字时第一次输入的数字清空显示第二次输入的数字
2、声明两个double类型的变量用于装第一次输入的数和装第二次输入的数
3、声明一个string类型的变量用于判断运算符号

界面布局:

C#实现简易计算器功能(附源码)

具体代码如下:


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 Test_Calculator
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }
       //声明三个变量
       string type; //符号类型
       double x;//装第一个数(按符号(+-×÷%)时textbox1中的数字)
       double y;//装第二个数(按等号时textbox1中的数字)
       bool c=false;
       private void Form1_Load(object sender, EventArgs e)
       {
           this.CenterToScreen();//窗体居中显示
           this.Text = "计算器";
           this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
           textBox1.ReadOnly = true;//文本框只读
           textBox2.TabIndex = 0;//光标焦点在textbox2中
       }

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

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

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

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

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

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

private void button7_Click(object sender, EventArgs e)
       {
           if (c == true)
           {
               c = false;
               textBox1.Text = "";
           }
           textBox1.Text += "7";
           textBox2.Text += "7";
       }

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

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

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

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

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

private void button13_Click(object sender, EventArgs e)
       {
           c = true;
           type = "+";
           textBox2.Text += "+";
           x = double.Parse(textBox1.Text);
       }

private void button14_Click(object sender, EventArgs e)
       {
           c = true;
           type = "-";
           textBox2.Text += "-";
           x = double.Parse(textBox1.Text);
       }

private void button15_Click(object sender, EventArgs e)
       {
           c = true;
           type = "×";
           textBox2.Text += "×";
           x = double.Parse(textBox1.Text);
       }

private void button16_Click(object sender, EventArgs e)
       {
           c = true;
           type = "÷";
           textBox2.Text += "÷";
           x = double.Parse(textBox1.Text);
       }

private void button18_Click(object sender, EventArgs e)
       {
           c = true;
           type = "%";
           textBox2.Text += "%";
           x = double.Parse(textBox1.Text);
       }

private void button17_Click(object sender, EventArgs e)
       {
           y = double.Parse(textBox1.Text);
           //法一
           while (type=="+")
           {
               textBox1.Text = (x + y).ToString();
               textBox2.Text += "=" + textBox1.Text;
               return;
           }
           while (type == "-")
           {
               textBox1.Text = (x - y).ToString();
               textBox2.Text += "=" + textBox1.Text;
               return;
           }
           while (type == "×")
           {
               textBox1.Text = (x * y).ToString();
               textBox2.Text += "=" + textBox1.Text;
               return;
           }
           while (type == "÷")
           {
               if (y!=0)
               {
                   textBox1.Text = (x / y).ToString();
                   textBox2.Text += "=" + textBox1.Text;
               }
               else
               {
                   MessageBox.Show("请重新输入","错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
                   textBox1.Text = "";
                   textBox2.Text = "";
               }
               return;
           }
           while (type == "%")
           {
               textBox1.Text = (x % y).ToString();
               textBox2.Text += "=" + textBox1.Text;
               return;
           }

//法二:
           //if (type=="+")
           //{
           //    textBox1.Text=(x + y).ToString();
           //    textBox2.Text += "=" + textBox1.Text;
           //}
           //if (type=="-")
           //{
           //    textBox1.Text = (x - y).ToString();
           //    textBox2.Text += "=" + textBox1.Text;
           //}
           //if (type=="×")
           //{
           //    textBox1.Text = (x * y).ToString();
           //    textBox2.Text += "=" + textBox1.Text;
           //}
           //if (type=="÷")
           //{
           //    textBox1.Text = (x / y).ToString();
           //    textBox2.Text += "=" + textBox1.Text;
           //}
           //if (type=="%")
           //{
           //    textBox1.Text = (x % y).ToString();
           //    textBox2.Text += "=" + textBox1.Text;
           //}
       }

}
}

效果图:

C#实现简易计算器功能(附源码)

C#实现简易计算器功能(附源码)

C#实现简易计算器功能(附源码)

来源:https://blog.csdn.net/liu991029/article/details/105848935

标签:C#,计算器
0
投稿

猜你喜欢

  • 如何通过Java实现时间轴过程解析

    2022-01-02 00:31:32
  • java异常(Exception)处理机制详解

    2023-06-06 08:21:48
  • Java基础之八大排序算法

    2022-02-05 12:35:47
  • 使用java实现BBS论坛发送邮件过程详解

    2022-12-19 16:58:30
  • Java 网络爬虫基础知识入门解析

    2022-05-16 02:39:06
  • springboot多环境配置文件及自定义配置文件路径详解

    2021-09-30 03:55:54
  • 使用IDEA异常断点来定位java.lang.ArrayStoreException的问题

    2022-06-14 00:43:18
  • .NET企业级项目中遇到的国际化问题和解决方法

    2022-12-03 08:18:06
  • Android通过交互实现贝塞尔曲线的绘制

    2022-10-06 05:04:08
  • SpringMVC Interceptor拦截器使用教程

    2022-05-11 02:17:22
  • Spring Security之默认的过滤器链及自定义Filter操作

    2023-11-24 02:48:35
  • Java提取两个字符串中的相同元素方法

    2023-05-16 14:18:00
  • Java实现的求逆矩阵算法示例

    2023-05-02 03:02:56
  • MyBatis Log 插件无法显示SQL语句的原因解析

    2023-11-24 23:42:55
  • 通过实例学习Either 树和模式匹配

    2023-05-21 02:02:41
  • C# zxing二维码写入的实例代码

    2021-09-01 12:23:26
  • java自带的MessageDigest实现文本的md5加密算法

    2023-10-08 03:35:29
  • Java基础知识精通二维数组的应用

    2022-02-03 03:01:28
  • Unity计时器功能实现示例

    2022-03-08 20:02:39
  • Kafka Producer中的消息缓存模型图解详解

    2022-05-03 06:00:13
  • asp之家 软件编程 m.aspxhome.com