资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#实现简单计算器功能-创新互联

实现效果:

为托克托等地区用户提供了全套网页设计制作服务,及托克托网站建设行业解决方案。主营业务为做网站、网站设计、托克托网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

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);
      }
    }
  }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:C#实现简单计算器功能-创新互联
网页链接:http://cdkjz.cn/article/pjpdc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220