中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#實現簡單的計算器功能完整實例

發布時間:2020-09-23 12:53:49 來源:腳本之家 閱讀:149 作者:5t4rk 欄目:編程語言

本文實例講述了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)
    {
    }
  }
}

運行效果:

C#實現簡單的計算器功能完整實例

C#實現簡單的計算器功能完整實例

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#程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

宜丰县| 沾化县| 阿城市| 江都市| 郑州市| 绥江县| 辽宁省| 定襄县| 香河县| 本溪市| 澄迈县| 翁源县| 漯河市| 托里县| 大城县| 柞水县| 巴林左旗| 吉安市| 衡东县| 北碚区| 女性| 凌源市| 宁武县| 琼中| 拉萨市| 称多县| 无为县| 裕民县| 贵南县| 固始县| 铁力市| 精河县| 凤山市| 德兴市| 达尔| 淅川县| 阜平县| 视频| 光泽县| 冕宁县| 交口县|