91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

C#實現簡單計算器功能

發布時間:2020-09-02 14:34:51 來源:腳本之家 閱讀:159 作者:彬菌 欄目:編程語言

實現效果:

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

小編已經測試了代碼,大家可以跟著做一下看看,感謝大家對億速云的支持。

向AI問一下細節

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

AI

余江县| 广南县| 永吉县| 赤水市| 祥云县| 莱芜市| 昌江| 乐昌市| 曲阳县| 通江县| 驻马店市| 如东县| 石棉县| 安西县| 东辽县| 德兴市| 隆安县| 忻城县| 南木林县| 天气| 西青区| 平乡县| 鄂托克旗| 积石山| 武陟县| 贵州省| 永仁县| 钟山县| 永吉县| 扶余县| 光山县| 乐平市| 亚东县| 沂源县| 鄄城县| 紫阳县| 株洲县| 彰化市| 安义县| 荔波县| 乌什县|