C#实现简单的计算器功能完整实例

 更新时间:2017年08月28日 12:26:31   作者:5t4rk  
这篇文章主要介绍了C#实现简单的计算器功能,结合完整实例形式分析了C#实现常见的数学运算、进制转换等相关操作技巧与界面布局方法,需要的朋友可以参考下

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using System.Diagnostics;
namespace CALCULATOR
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private string _sign;
    private double _first = 10241024;
    private double _second = 10241024;
    private string _number;
    private void button1_Click(object sender, EventArgs e)
    {
      _number += "1";
      this.textBox1.Text += "1";
    }
    private void button2_Click(object sender, EventArgs e)
    {
      _number += "2";
      this.textBox1.Text += "2";
    }
    private void button3_Click(object sender, EventArgs e)
    {
      _number += "3";
      this.textBox1.Text += "3";
    }
    private void button4_Click(object sender, EventArgs e)
    {
      _number += "4";
      this.textBox1.Text += "4";
    }
    private void button8_Click(object sender, EventArgs e)
    {
      _number += "5";
      this.textBox1.Text += "5";
    }
    private void button7_Click(object sender, EventArgs e)
    {
      _number += "6";
      this.textBox1.Text += "6";
    }
    private void button6_Click(object sender, EventArgs e)
    {
      _number += "7";
      this.textBox1.Text += "7";
    }
    private void button5_Click(object sender, EventArgs e)
    {
      _number += "8";
      this.textBox1.Text += "8";
    }
    private void button9_Click(object sender, EventArgs e)
    {
      _number += "9";
      this.textBox1.Text += "9";
    }
    private void button10_Click(object sender, EventArgs e)
    {
      _number += "0";
      this.textBox1.Text += "0";
    }
    private void button16_Click(object sender, EventArgs e)
    {
      //设置加号计算数
      SetNumber(Convert.ToDouble(_number));
      if (_sign == null || _second == 10241024)
      {
        _sign = "+";
        this.textBox1.Text += "+";
        return;
      }
      if (_second != 10241024)
      {
        //开始进行计算
        Computer ComPro = new Computer(_sign, _first, _second);
        this.textBox1.Text = ComPro.Comt(out _first).ToString();
        //验证除数为零的情况
        if (_first == 10241024.10241023)
        {
          this.textBox1.Text = "0";
          MessageBox.Show("除数不能为零!");
          _number = null;
          _first = 10241024;
          _second = 10241024;
          this.textBox1.Text = null;
          _sign = null;
          this.textBox1.Text = null;
        }
        //开始初始化数据
        _sign = "+";
        this.textBox1.Text += "+";
        _second = 10241024;
        _number = null;
        return;
      }
    }
    private void button15_Click(object sender, EventArgs e)
    {
      //设置减号计算数
      SetNumber(Convert.ToDouble(_number));
      if (_sign == null || _second == 10241024)
      {
        _sign = "-";
        this.textBox1.Text += "-";
        return;
      }
      if (_second != 10241024)
      {
        //开始进行计算
        Computer ComPro = new Computer(_sign, _first, _second);
        this.textBox1.Text = ComPro.Comt(out _first).ToString();
        //验证除数为零的情况
        if (_first == 10241024.10241023)
        {
          this.textBox1.Text = "0";
          MessageBox.Show("除数不能为零!");
          _number = null;
          _first = 10241024;
          _second = 10241024;
          this.textBox1.Text = null;
          _sign = null;
          this.textBox1.Text = null;
        }
        //开始初始化数据
        _sign = "-";
        this.textBox1.Text += "-";
        _second = 10241024;
        _number = null;
        return;
      }
    }
    private void button14_Click(object sender, EventArgs e)
    {
      //设置乘号计算数
      SetNumber(Convert.ToDouble(_number));
      if (_sign == null || _second == 10241024)
      {
        _sign = "*";
        this.textBox1.Text += "*";
        return;
      }
      if (_second != 10241024)
      {
        //开始进行计算
        Computer ComPro = new Computer(_sign, _first, _second);
        this.textBox1.Text = ComPro.Comt(out _first).ToString();
        //验证除数为零的情况
        if (_first == 10241024.10241023)
        {
          this.textBox1.Text = "0";
          MessageBox.Show("除数不能为零!");
          _number = null;
          _first = 10241024;
          _second = 10241024;
          this.textBox1.Text = null;
          _sign = null;
          this.textBox1.Text = null;
        }
        //开始初始化数据
        _sign = "*";
        this.textBox1.Text += "*";
        _second = 10241024;
        _number = null;
        return;
      }
    }
      /// <summary>
      /// 设置除号计算数
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
    private void button11_Click(object sender, EventArgs e)
    {
      SetNumber(Convert.ToDouble(_number));
      if (_sign == null || _second == 10241024)
      {
        _sign = "/";
        this.textBox1.Text += "/";
        return;
      }
      if (_second != 10241024)
      {
        //开始进行计算
        Computer ComPro = new Computer(_sign, _first, _second);
        this.textBox1.Text = ComPro.Comt(out _first).ToString();
        //验证除数为零的情况
        if (_first == 10241024.10241023)
        {
          this.textBox1.Text = "0";
          this.textBox1.Text = "除数不能为零!";
          _number = null;
          _first = 10241024;
          _second = 10241024;
          this.textBox1.Text = null;
          _sign = null;
          this.textBox1.Text = null;
        }
        //开始初始化数据
        _sign = "/";
        this.textBox1.Text += "/";
        _second = 10241024;
        _number = null;
        return;
      }
    }
    /// <summary>
    /// 清空输入框
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button12_Click(object sender, EventArgs e)
    {
      _number = null;
      _first = 10241024;
      _second = 10241024;
      this.textBox1.Text = null;
      _sign = null;
      this.textBox1.Text = null;
    }
    private void button13_Click(object sender, EventArgs e)
    {
      SetNumber(Convert.ToDouble(_number));
      //判断是否设置了_second的值
      if (_second == 10241024)
      {
        _second = Convert.ToDouble(_number);
        _number = null;
      }
      //判断是否设置了_first的值
      if (_first == 10241024)
      {
        _first = Convert.ToDouble(_number);
        _number = null;
      }
      //判断是否有_sign的值
      if (_sign == null)
      {
        _sign = "+";
      }
      //开始进行计算
      Computer ComPro = new Computer(_sign, _first, _second);
      this.textBox1.Text = ComPro.Comt(out _first).ToString();
      //验证除数为零的情况
      if (_first == 10241024.10241023)
      {
        this.textBox1.Text = "0";
        MessageBox.Show("除数不能为零!");
        _number = null;
        _first = 10241024;
        _second = 10241024;
        this.textBox1.Text = null;
        _sign = null;
        this.textBox1.Text = null;
      }
      //开始初始化数据
      _sign = null;
      _second = 10241024;
      _number = null;
    }
    public class Computer
    {
      private string _sign;
      private double _first;
      private double _second;
      public Computer(string str, double first, double second)
      {
        _sign = str;
        _first = first;
        _second = second;
      }
      //选择相应的运算符进行运算
      public double Comt(out double first)
      {
        if (_sign == "+")
        {
          first = _first + _second;
          return _first + _second;
        }
        else if (_sign == "-")
        {
          first = _first - _second;
          return _first - _second;
        }
        else if (_sign == "*")
        {
          first = _first * _second;
          return _first * _second;
        }
        else if (_sign == "/")
        {
          if (_second == 0)
          {
            first = 10241024.10241023;
            return 10241024.10241023;
          }
          else
          {
            first = _first / _second;
            return _first / _second;
          }
        }
        else
        {
          first = 10241024.10241024;
          return 10241024.10241024;
        }
      }
    }
    //设置数字函数
    public void SetNumber(double number)
    {
      if (_first == 10241024 && _number != null)
      {
        _first = Convert.ToDouble(_number);
        _number = null;
      }
      else if (_second == 10241024 && _number != null)
      {
        _second = Convert.ToDouble(_number);
        _number = null;
      }
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
      this.label2.Text = DateTime.Now.ToString();
    }
    private void button24_Click(object sender, EventArgs e)
    {
      if (this.textBox1.Text == " ")
      {
        MessageBox.Show("请输入数据");
        this.textBox1.Text = Convert.ToInt64(textBox1.Text.ToString(), 2).ToString();
      }
      else
      {
        this.textBox1.Text = Convert.ToInt64(textBox1.Text.ToString(), 2).ToString();
      }
    }
    private void button22_Click(object sender, EventArgs e)
    {
      if (this.textBox1.Text == " ")
      {
        MessageBox.Show("请输入数据");
        Int64 int64 = Convert.ToInt64(textBox1.Text.ToString(), 2);
        this.textBox1.Text = Convert.ToString(int64,16);
      }
      else
      {
        Int64 int64 = Convert.ToInt64(textBox1.Text.ToString(), 2);
        this.textBox1.Text = Convert.ToString(int64, 16);
      }
    }
    private void button23_Click(object sender, EventArgs e)
    {
      if (this.textBox1.Text == " ")
      {
        MessageBox.Show("请输入数据");
        Int64 int64 = Convert.ToInt64(textBox1.Text.ToString(), 2);
        this.textBox1.Text = Convert.ToString(int64, 8);
      }
      else
      {
        Int64 int64 = Convert.ToInt64(textBox1.Text.ToString(), 2);
        this.textBox1.Text = Convert.ToString(int64, 8);
      }
    }
    private void button21_Click(object sender, EventArgs e)
    {
      if (this.textBox1.Text == " ")
      {
        MessageBox.Show("请输入数据");
        Int64 int64 = Convert.ToInt64(textBox1.Text);
        this.textBox1.Text = Convert.ToString(int64, 2);
      }
      else
      {
        Int64 int64 = Convert.ToInt64(textBox1.Text);
        this.textBox1.Text = Convert.ToString(int64, 2);
      }
    }
    private void button27_Click(object sender, EventArgs e)
    {
      double buffer1= Convert.ToDouble(textBox1.Text);
        buffer1 = 1 / buffer1;
      this.textBox1.Text = buffer1.ToString();
    }
    private void button17_Click(object sender, EventArgs e)
    {
      _number += ".";
      this.textBox1.Text += ".";
    }
    private void button25_Click(object sender, EventArgs e)
    {
      double buffer1 = Convert.ToDouble(textBox1.Text);
      buffer1 = buffer1 * buffer1;
      this.textBox1.Text = buffer1.ToString();
    }
    private void button26_Click(object sender, EventArgs e)
    {
      if (this.textBox1.Text == " ")
      {
        MessageBox.Show("请输入数据");
        Int64 int64 = Convert.ToInt64(this.textBox1.Text);
        Int64 temp1 = 1;
        while (int64 > 1)
        {
          temp1 = temp1*int64 ;
          int64--;
        }
        this.textBox1.Text = temp1.ToString();
      }
      else
      {
        Int64 int64 = Convert.ToInt64(this.textBox1.Text);
        Int64 temp1 = 1;
        while (int64 > 1)
        {
          temp1 = temp1 * int64;
          int64--;
        }
        this.textBox1.Text = temp1.ToString();
      }
    }
    private void button28_Click(object sender, EventArgs e)
    {
      //Int64 int64 = Convert.ToInt64(this.textBox1.Text);
      //double temp=Math.Log10(int64);
      //this.textBox1.Text = temp.ToString();
      Double temp1, temp2;
      string[] STRarry = new string[2];
      STRarry = this.textBox1.Text.Split('.');
      temp1 = Convert.ToDouble(STRarry[0]);
      temp2 = Convert.ToDouble(STRarry[1]);
      // double temp1 = Convert.ToDouble(this.textBox1.Text);
      temp2 = Math.Log(temp2,temp1 );
      this.textBox1.Text = temp2.ToString();
    }
    private void button18_Click(object sender, EventArgs e)
    {
      double temp1 = Convert.ToDouble(this.textBox1.Text);
      double temp2 = Math.Sin(temp1);
      this.textBox1.Text = temp2.ToString();
    }
    private void button19_Click(object sender, EventArgs e)
    {
      double temp1 = Convert.ToDouble(this.textBox1.Text);
      double temp2 = Math.Cos(temp1);
      this.textBox1.Text = temp2.ToString();
    }
    private void button20_Click(object sender, EventArgs e)
    {
      double temp1 = Convert.ToDouble(this.textBox1.Text);
      double temp2 = Math.Tan(temp1);
      this.textBox1.Text = temp2.ToString();
    }
    private void button33_Click(object sender, EventArgs e)
    {
      Double temp1, temp2;
      string[] STRarry=new string[2];
      STRarry=this.textBox1.Text.Split('.');
      temp1 = Convert.ToDouble(STRarry[0]);
      temp2 = Convert.ToDouble(STRarry[1]);
      // double temp1 = Convert.ToDouble(this.textBox1.Text);
      temp2 = Math.Pow(temp1,temp2);
      this.textBox1.Text = temp2.ToString();
    }
    /// <summary>
    /// 计算e的temp1次幂
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button31_Click(object sender, EventArgs e)
    {
      double temp1 = Convert.ToDouble(this.textBox1.Text);
      double temp2 = Math.Exp(temp1);
      this.textBox1.Text = temp2.ToString();
    }
    private void button34_Click(object sender, EventArgs e)
    {
       double temp1 = Convert.ToDouble(this.textBox1.Text);
      double temp2 = Math.Sqrt(temp1);
      this.textBox1.Text = temp2.ToString();
    }
    private void button32_Click(object sender, EventArgs e)
    {
      Double temp1, temp2;
      string[] STRarry = new string[2];
      STRarry = this.textBox1.Text.Split('.');
      temp1 = Convert.ToDouble(STRarry[0]);
      temp2 = Convert.ToDouble(STRarry[1]);
      // double temp1 = Convert.ToDouble(this.textBox1.Text);
      temp2 = Math.IEEERemainder(temp1, temp2);
      this.textBox1.Text = temp2.ToString();
    }
    /// <summary>
    /// 圆的面积
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button30_Click(object sender, EventArgs e)
    {
      double buffer1 = Convert.ToDouble(textBox1.Text);
      buffer1 =3.1415926*buffer1*buffer1;
      this.textBox1.Text = buffer1.ToString();
    }
    /// <summary>
    /// 三角形面积
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button29_Click(object sender, EventArgs e)
    {
      Double temp1, temp2,temp3,p,area;
      string[] STRarry = new string[3];
      STRarry = this.textBox1.Text.Split('.');
      temp1 = Convert.ToDouble(STRarry[0]);
      temp2 = Convert.ToDouble(STRarry[1]);
      temp3 = Convert.ToDouble(STRarry[2]);
      // double temp1 = Convert.ToDouble(this.textBox1.Text);
      p = (temp1 + temp2 + temp3) / 2;
      area= p * (p - temp1) * (p - temp2) * (p - temp3);
      area = Math.Sqrt(area);
      this.textBox1.Text = area.ToString();
    }
    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      string str = e.Link.ToString();
      Process.Start(str);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    }
  }
}

运行效果:

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

在线一元函数(方程)求解计算工具:
http://tools.jb51.net/jisuanqi/equ_jisuanqi

科学计算器在线使用_高级计算器在线计算:
http://tools.jb51.net/jisuanqi/jsqkexue

在线计算器_标准计算器:
http://tools.jb51.net/jisuanqi/jsq

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

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

相关文章

  • 基于NET Core 的Nuget包制作、发布和运用流程解析(完整过程)

    基于NET Core 的Nuget包制作、发布和运用流程解析(完整过程)

    这篇文章主要介绍了基于NET Core 的Nuget包制作、发布和运用流程,本文通过图文并茂的形式给大家介绍了Nuget包制作过程,感兴趣的朋友跟随小编一起看看吧
    2022-02-02
  • 使用C#快速搭建一个在windows运行的exe应用

    使用C#快速搭建一个在windows运行的exe应用

    这篇文章主要介绍了使用C#快速搭建一个在windows运行的exe应用,这是一个比较旧的内容,但是一直都没有空写,今天花点时间,把我掌握的C# 分享给初学的人或者感兴趣的人,希望能对你有一定帮助,感兴趣的小伙伴跟着小编一起来看看吧
    2024-07-07
  • spreadsheetgear插件屏蔽鼠标右键的方法

    spreadsheetgear插件屏蔽鼠标右键的方法

    今天用到spreadsheetGear插件,然后右键有插件自己的菜单。都是英文的,而且还能打开新的窗体。嵌到程序里面,不太合适,所以着手屏蔽
    2014-02-02
  • C#获取两个数的最大公约数和最小公倍数示例

    C#获取两个数的最大公约数和最小公倍数示例

    本文介绍了使用C#获取两个数的最大公约数和最小公倍数的示例,大家参考使用吧
    2014-01-01
  • Visual Studio 2019配置vue项目的图文教程详解

    Visual Studio 2019配置vue项目的图文教程详解

    这篇文章主要介绍了Visual Studio 2019配置vue项目的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • C#读取文件所有行到数组的方法

    C#读取文件所有行到数组的方法

    这篇文章主要介绍了C#读取文件所有行到数组的方法,涉及C#针对文件及数组的相关操作技巧,需要的朋友可以参考下
    2015-07-07
  • C#结束进程及子进程

    C#结束进程及子进程

    这篇文章介绍了C#操作结束进程及子进程的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C#调用新浪微博API实例代码

    C#调用新浪微博API实例代码

    在本篇文章里小编给大家整理的是一篇关于C#调用微博API的相关知识点内容,有需要的朋友们可以学习下。
    2019-11-11
  • WPF利用TabControl控件实现拖拽排序功能

    WPF利用TabControl控件实现拖拽排序功能

    在UI交互中,拖拽操作是一种非常简单友好的交互,这篇文章主要为大家介绍了WPF如何利用TabControl控件实现拖拽排序功能,需要的小伙伴可以参考一下
    2023-10-10
  • Unity实现红酒识别的示例代码

    Unity实现红酒识别的示例代码

    本文主要介绍了如何通过Unity实现红酒识别,可以实现识别图像中的红酒标签,返回红酒名称、国家、产区、酒庄、类型、糖分、葡萄品种、酒品描述等信息,感兴趣的可以学习一下
    2022-02-02

最新评论