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

C#实现简单计算器功能

程序员文章站 2023-12-16 10:19:10
实现效果: form1.cs代码: using system; using system.collections.generic; using syst...

实现效果:

C#实现简单计算器功能

form1.cs代码:

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 cal
{
  public partial class form1 : form
  {
    public double num1;
    public int temp = 0;//记录存储计算方式
    public form1()
    {
      initializecomponent();
    }
    
    private void eq_click(object sender, eventargs e)
    {
      switch (temp)
      {
        //加法运算
        case 1:
          try
          {
            result.text = (num1 + double.parse(result.text)).tostring();
          }
          catch (exception)
          {
            messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //减法运算
        case 2:
          try
          {
            result.text = (num1 - double.parse(result.text)).tostring();
          }
          catch (exception)
          {
            messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //乘法运算
        case 3:
          try
          {
            result.text = (num1 * double.parse(result.text)).tostring();
          }
          catch (exception)
          {
            messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //除法运算,讨论分母为零的情况
        case 4:
          if (double.parse(result.text)==0)
          {
            messagebox.show("除数不能为零", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
            result.resettext();
          }
          else
          {
            result.text = (num1 / double.parse(result.text)).tostring(); temp = 0;
          }
          break;
        case 5:
          try
          {
            result.text = (num1 % double.parse(result.text)).tostring();
          }
          catch (exception)
          {
            messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
          }
          finally
          {
            temp = 0;
          }
          break;
      }
    }

    private void empty_click(object sender, eventargs e)
    {
      result.text = "0";
      num1 = 0;
      temp = 0;
      //清除
      //if (result.text.length > 0)
      //{
      //  result.text = result.text.substring(0, result.text.length - 1);
      //}
    }

    private void zero_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "0";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "0";
      }
    }

    private void one_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "1";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "1";
      }
    }

    private void two_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "2";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "2";
      }
    }
    private void three_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "3";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "3";
      }
    }

    private void four_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "4";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "4";
      }
    }

    private void five_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "5";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "5";
      }
    }

    private void six_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "6";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "6";
      }
    }

    private void seven_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "7";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "7";
      }
    }

    private void eight_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "8";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "8";
      }
    }

    private void nine_click(object sender, eventargs e)
    {
      if (result.text == "0")
      {
        result.text = "9";
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + "9";
      }
    }

    private void point_click(object sender, eventargs e)
    {
      //小数点按键,初始为空,直接按 . 键则显示为0.多少
      if (result.text == "")
      {
        result.text = "0.";
      }
      //如果再次输入.则返回都输入的字符后边并提示信息
      else if (result.text.indexof(".") >= 0)
      {
        messagebox.show("已经输入小数点,无须再次输入", "提示");
      }
      else
      {
        //前边有数字时,则直接在后边加上.
        result.text = result.text + ".";
      }
    }

    private void add_click(object sender, eventargs e)
    {
      temp = 1;
      try
      {
        num1 = double.parse(result.text);
      }
      catch (exception)
      {
        messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
      }
      finally
      {
        result.text = "";//随后让文本框归空
      }
    }

    private void mul_click(object sender, eventargs e)
    {
      temp = 3;
      try
      {
        num1 = double.parse(result.text);
      }
      catch (exception)
      {
        messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
      }
      finally
      {
        result.text = "";//随后让文本框归空
      }
    }

    private void sub_click(object sender, eventargs e)
    {
      temp = 2;
      try
      {
        num1 = double.parse(result.text);
      }
      catch(exception)
      {
        messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
      }
      finally
      { 
      result.text = "";//随后让文本框归空
      }
    }

    private void button1_click(object sender, eventargs e)
    {
      temp = 4;
      try
      {
        num1 = double.parse(result.text);
      }
      catch (exception)
      {
        messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
      }
      finally
      {
        result.text = "";//随后让文本框归空
      }
    }

    private void per_click(object sender, eventargs e)
    {
      temp = 5;
      try
      {
        num1 = double.parse(result.text);
      }
      catch (exception)
      {
        messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
      }
      finally
      {
        result.text = "";//随后让文本框归空
      }
    }

    private void opp_click(object sender, eventargs e)
    {
      //temp = 6;
      try
        {
         result.text = (-double.parse(result.text)).tostring();
        }
      catch (exception)
        {
          messagebox.show("还没输入数字呢", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
        }
      finally
          {
        temp = 0;
          }
    }
    
    private void form1_load(object sender, eventargs e)
    {
      result.text = "0";
    }

    private void result_textchanged(object sender, eventargs e)
    {
      if (result.text.length > 18&& result.text.length<36)
      {
        result.font = new font(result.font.fontfamily, 15, result.font.style);
      }
      if (result.text.length > 36&&result.text.length<45)
      {
        result.font = new font(result.font.fontfamily, 10, result.font.style);
      }
      if (result.text.length > 45)
      {
        messagebox.show("超出范围,将要清空了!", "错误提示", messageboxbuttons.ok, messageboxicon.warning);
        result.resettext();
        result.font = new font(result.font.fontfamily, 20, result.font.style);
      }
    }
  }
}

小编已经测试了代码,大家可以跟着做一下看看,感谢大家对的支持。

上一篇:

下一篇: