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#程序设计有所帮助。

(0)

相关推荐

  • C#实现Winform版计算器

    本文实例为大家分享Winform版计算器的具体实现方法,供大家参考,具体内容如下 前台页面设计 后台代码实现 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; namespace 计

  • c#入门之实现简易存款利息计算器示例

    本想把练习题做了的结果放上来,不过发现附录是有答案的,就算了吧,自己做了没问题就行了哈.之前提到过,要是有朋友有想法,需要做小工具我可以帮忙实现,不过貌似大家都很忙.SO,自己学完第4章后,决定做一个工具:简易存款利息计算器,可以更好地复习前面学过的知识. 原理介绍为啥叫简易呢,因为现在只能计算整存整取(只有1.2.3.5四种年限哈)的利息,并且没有启用自动转存(俗称利滚利,就是把本年和上年的利息之和,作为下年利息计算时的本金)功能,方便和网上已有的工具对比计算结果,判断自己的程序算出来正不正确

  • C#编写的windows计算器的实例代码

    复制代码 代码如下: using System; using System.Drawing; using System.Windows; using System.Windows.Forms; using System.Collections; using System.ComponentModel; using System.Data; namespace comput {     /// <summary>     /// 这是一个计算器的简单实现.     /// </summar

  • 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 pa

  • C#计算器编写代码

    利用C#编写一个计算器.如下图,能够完成基本的四则运算. 当然这个程序甚至还不上Windows附件那个自带的多功能计算器.  不过这个程序的逻辑还是非常值得思考的,首先你要考虑好用户按+ - * / =等运算符号.数字键之后计算器的状态记录问题.  然后要防止多次按某一个键的问题.比如小数点.就不应该让用户在输入一个数的时候键入两次.  最后,还要弄两个数组,一个存放用户在输入的数字,另一个存放用户输入的符号.  制作过程如下,  1.布局如下,同时可以参考<简单实现C#窗体程序判断是否闰年 >

  • C#日历样式的下拉式计算器实例讲解

    本文介绍了如何在Visual Studio中创建用户控件来显示下拉式计算器,弹出效果类似于日历控件. 介绍 如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可能必须手动计算数值.因此,用户就不得不使用计算器得到结果,再填入到输入字段中,或者在工作窗口上单独打开一个计算器窗口.总之,各种不便和麻烦. 这篇文章主要描述的是如何添加下拉式计算器到DataGridView单元格中,如下图: 使用代码 第一步,我们必须先创建一个函数计算器,并且能够使用控件.因此,不妨先创建一个Visual St

  • 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; usi

  • Python设计实现的计算器功能完整实例

    本文实例讲述了Python设计实现的计算器功能.分享给大家供大家参考,具体如下: 通过利用PYTHON 设计处理计算器的功能如: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 ))- (-4*3)/(16-3*2)) 我的处理计算基本思路是: 解题思路是,需要优先处理内层括号运算--外层括号运算--先乘除后加减的原则: 1.正则处理用户输入的字符串,然后对其进行判断,判断计算公式是否有括号,有就先将计算公式进

  • jQuery实现的简单悬浮层功能完整实例

    本文实例讲述了jQuery实现的简单悬浮层功能.分享给大家供大家参考,具体如下: 运行效果图如下: 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml

  • Java swing实现的计算器功能完整实例

    本文实例讲述了Java swing实现的计算器功能.分享给大家供大家参考,具体如下: package awtDemo; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Vector; public class Calculator { String str1 = "0"; // 运算数1 初值一定为0 为了程序的安全 String str2 = "0"

  • jQuery实现的简单在线计算器功能

    本文实例讲述了jQuery实现的简单在线计算器功能.分享给大家供大家参考,具体如下: 先来看看运行效果图: 完整代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/x

  • JS实现的简单四则运算计算器功能示例

    本文实例讲述了JS实现的简单四则运算计算器功能.分享给大家供大家参考,具体如下: 先来看看运行效果: 具体代码如下: <!DOCTYPE html> <html> <meta name="content-type" content="text/html; charset=UTF-8"> <head> <title>www.jb51.net 计算器 Calculator</title> <!

  • PHP实现的简单在线计算器功能示例

    本文实例讲述了PHP实现的简单在线计算器功能.分享给大家供大家参考,具体如下: <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf8"> <title>简单的计算器(www.jb51.net)</title> </head> <body> <?php //如果用户单击了计算按

  • PHP实现的简单四则运算计算器功能示例

    本文实例讲述了PHP实现的简单四则运算计算器功能.分享给大家供大家参考,具体如下: php实现一个简单的四则运算计算器(还不支持括号的优先级).利用栈这种数据结构来计算表达式很赞. 这里可以使用栈的结构,由于php的数组"天然"就有栈的特性,这里直接就利用了数组.当然可以使用栈结构写,道理一样的. 前辈(波兰一位科学家)在计算带有括号的四则表达式,利用逆波兰算法(后缀表达法).简直神了!!其实代码code并不难,难的是算法的指导,要先理解算法,才能编码. <?php $num_a

  • 10个python爬虫入门基础代码实例 + 1个简单的python爬虫完整实例

    本文主要涉及python爬虫知识点: web是如何交互的 requests库的get.post函数的应用 response对象的相关函数,属性 python文件的打开,保存 代码中给出了注释,并且可以直接运行哦 如何安装requests库(安装好python的朋友可以直接参考,没有的,建议先装一哈python环境) windows用户,Linux用户几乎一样: 打开cmd输入以下命令即可,如果python的环境在C盘的目录,会提示权限不够,只需以管理员方式运行cmd窗口 pip install

  • 用C语言实现简单的计算器功能

    本文实例为大家分享了C语言实现简单的计算器功能的具体代码,供大家参考,具体内容如下 简单计算器,实现简单的加减乘除功能,选择对应的运算符输出运算符对应的编号,然后输入我们要计算的两个数,程序会给出计算结果. 效果演示 简单计算器代码 /* 简单计算器 name:zsh  */  #include <stdio.h>    //简单计算器  void menu()    //运算符显示界面  {     printf("---------------------------------

随机推荐