溫馨提示×

溫馨提示×

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

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

C# 計算器

發布時間:2020-06-26 15:03:06 來源:網絡 閱讀:474 作者:p412726700 欄目:編程語言

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 Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string op;
        double a, b;
        Operat o = new Operat();
        private void btn1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
               
            }
            textBox1.Text += "1";
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
              
            }
            textBox1.Text += "2";
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
               
            }textBox1.Text += "3";
        }

        private void btn4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
               
            }
            textBox1.Text += "4";
        }

        private void btn5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
               
            }
            textBox1.Text += "5";
        }

        private void btn6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
               
            }
            textBox1.Text += "6";
        }

        private void btn7_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
              
            }
            textBox1.Text += "7";
        }

        private void btn8_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
               
            }
            textBox1.Text += "8";
        }

        private void btn9_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
               
            }
            textBox1.Text += "9";
        }

        private void btn0_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
              
            }
            textBox1.Text += "0";
        }

        private void btnDot_Click(object sender, EventArgs e)
        {
            string s = textBox1.Text;
            if (s.IndexOf('.') > -1)
            {
                MessageBox.Show("已經包含小數點", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            else
            {
                textBox1.Text += ".";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (op == "+" || op == "-" || op == "*" || op == "/")
            {
                op = "+";
            }
            else
            {
                op = "+";
                a = double.Parse(textBox1.Text);
            }
            textBox1.Text = "";
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            if (op == "+" || op == "-" || op == "*" || op == "/")
            {
                op = "-";
            }
            else
            {
                op = "-";
                a = double.Parse(textBox1.Text);
            }
            textBox1.Text = "";
        }

        private void btnMulti_Click(object sender, EventArgs e)
        {
            if (op == "+" || op == "-" || op == "*" || op == "/")
            {
                op = "*";
            }
            else
            {
                op = "*";
                a = double.Parse(textBox1.Text);
            }
            textBox1.Text = "";
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            if (op == "+" || op == "-" || op == "*" || op == "/")
            {
                op = "/";
            }
            else
            {
                op = "/";
                a = double.Parse(textBox1.Text);
            }
            textBox1.Text = "";
        }

        private void btnResult_Click(object sender, EventArgs e)
        {
            b = double.Parse(textBox1.Text);
            textBox1.Text = o.Operation(a, b, op).ToString();

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void btnBackspace_Click(object sender, EventArgs e)
        {
            string sub = textBox1.Text;
            textBox1.Text = sub.Substring(0, sub.Length - 1);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = "";
            }

            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }
           

        }


    }

    public class Operat
    {
        public Double Operation(double a, double b, string o)
        {
            double r = 0; ;
            switch (o)
            {
                case "+":
                    r = a + b;
                    break;
                case "-":
                    r = a - b;
                    break;
                case "*":
                    r = a * b;
                    break;
                case "/":
                    r = a / b;
                    break;
            }
            return r;

        }

    }
}
 

向AI問一下細節

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

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女