欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的简单整数四则运算计算器功能示例

程序员文章站 2023-12-12 15:07:22
本文实例讲述了c#实现的简单整数四则运算计算器功能。分享给大家供大家参考,具体如下: 运行效果图如下: 具体代码如下: using system; us...

本文实例讲述了c#实现的简单整数四则运算计算器功能。分享给大家供大家参考,具体如下:

运行效果图如下:

C#实现的简单整数四则运算计算器功能示例

具体代码如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
namespace 计算器
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
    public string num;
    public int flag;//用于判断输入的操作符
    public double num1, num2;
    private void num0_button_click(object sender, eventargs e)
    {
      num = num + "0";
      num2 = convert.todouble(num);
      textbox.text = num;
    }
    private void num1_button_click(object sender, eventargs e)//重点算法1
    {
      if (textbox.text == "0")
      {
        num = "1";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "1";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num2_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "2";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "2";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num3_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "3";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "3";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num4_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "4";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "4";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num5_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "5";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "5";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num6_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "6";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "6";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num7_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "7";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "7";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num8_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "8";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "8";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num9_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "9";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "9";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void add_button_click(object sender, eventargs e)//重点算法2
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox .text);
        num = "";
        flag = 1;
        textbox.text = "";
        textbox.focus();
      }
    }
    private void dev_button_click(object sender, eventargs e)
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox.text);
        num = "";
        flag = 2;
        textbox.text = "";
        textbox.focus();
      }
    }
    private void mul_button_click(object sender, eventargs e)
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox.text);
        num = "";
        flag = 3;
        textbox.text = "";
        textbox.focus();
      }
    }
    private void chu_button_click(object sender, eventargs e)
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox.text);
        num = "";
        flag = 4;
        // textbox.text = "";
        textbox.focus();
      }
    }
    private void equ_button_click(object sender, eventargs e)
    {
      switch (flag)
      {
        case 1:
          textbox.text = convert.tostring(num1+convert .todouble(num));//重点算法3
          num2 = convert.todouble(textbox .text);
          break;
        case 2:
          textbox.text = convert.tostring(num1 - convert.todouble(num));
          num2 = convert.todouble(textbox.text);
          break;
        case 3:
          textbox.text = convert.tostring(num1 * convert.todouble(num));
          num2 = convert.todouble(textbox.text);
          break;
        case 4:
          textbox.text = convert.tostring(num1 / convert.todouble(num));
          num2 = convert.todouble(textbox.text);
          break;
      }
    }
    private void re_button_click(object sender, eventargs e)
    {
      num = "";
      textbox.text = "0";
    }
  }
}

ps:这里再为大家推荐几款计算工具供大家进一步参考借鉴:

在线一元函数(方程)求解计算工具:

科学计算器在线使用_高级计算器在线计算:

在线计算器_标准计算器:

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#数据结构与算法教程》、《c#程序设计之线程使用技巧总结》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。

上一篇:

下一篇: