學習C#自作計算器,菜鳥初學,有大神的指點,希望做的不夠好的地方請大家多多指導。同時希望非常無聊的大神能加些其它計算進去


可以做冪運算,根號運算,十進制與二進制互轉,16進制與十進制互轉

namespace WindowsFormsApplication15
{
    public partial class 祥哥計算器 : Form
    {
        public 祥哥計算器()
        {
            InitializeComponent();
        }

        #region 數字點擊事件
        /// <summary>
        /// 按鈕點擊事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "1";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "1";
                SetNum(textBox2.Text);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "2";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "2";
                SetNum(textBox2.Text);
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "3";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "3";
                SetNum(textBox2.Text);
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "4";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "4";
                SetNum(textBox2.Text);
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "5";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "5";
                SetNum(textBox2.Text);
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "7";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "7";
                SetNum(textBox2.Text);
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "6";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "6";
                SetNum(textBox2.Text);
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "8";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "8";
                SetNum(textBox2.Text);
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "9";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "9";
                SetNum(textBox2.Text);
            }
        }
        private void button14_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "0";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "0";
                SetNum(textBox2.Text);
            }
        }
     /// <summary>
     /// 小數點問題
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void buttonpoint_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += ".";
                //if(textBox1.Text.IndexOf(".") >= 1)
                //    MessageBox.Show("已經有小數點了");
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += ".";
                //if(textBox2.Text.IndexOf(".") >=1)
                //    MessageBox.Show("已經有小數點了");
                    SetNum(textBox2.Text);
            }
        }
        private void button00_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "00";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "00";
                SetNum(textBox2.Text);
            }
        }
        private void buttonA_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "A";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "A";
                SetNum(textBox2.Text);
            }
        }

        private void buttonB_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "B";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "B";
                SetNum(textBox2.Text);
            }
        }

        private void buttonC_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "C";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "C";
                SetNum(textBox2.Text);
            }
        }

        private void buttonD_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "D";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "D";
                SetNum(textBox2.Text);
            }
        }

        private void buttonE_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "E";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "E";
                SetNum(textBox2.Text);
            }
        }

        private void buttonF_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                if (textBox1.Text.Length < 10)
                    textBox1.Text += "F";
                SetNum(textBox1.Text);
            }
            else
            {
                if (textBox2.Text.Length < 10)
                    textBox2.Text += "F";
                SetNum(textBox2.Text);
            }
        }
        /// <summary>
        /// 清空鍵與退格鍵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button16_Click_1(object sender, EventArgs e)     ///清空CE鍵
        {
            label1.Text = "";
            textBox3.Clear();
            textBox1.Clear();
            textBox2.Clear();
        }
        private void buttondelete_Click(object sender, EventArgs e)    ///退格鍵
        {
            if (string.IsNullOrWhiteSpace(label1.Text))
            {
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            }
            else
            {
                textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
            }
        }
        #endregion
        /// <summary>
        /// 讀取數字功能
        /// </summary>
        /// <param name="text"></param>
        private void SetNum(string text)    ///調用功能鍵
        {
            {
                if (string.IsNullOrWhiteSpace(label1.Text))
                {
                    textBox1.Text = text;
                }
                else
                {
                    textBox2.Text = text;
                }
            }
        }
    /// <summary>
    /// 四則運算
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button10_Click(object sender, EventArgs e)         ///運算符號
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "+";
        }
     private void button11_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))

            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
                label1.Text = "-";
            }
            //else
            //    label1.Text = "-";
            else if (string.IsNullOrWhiteSpace(label1.Text))

                if (string.IsNullOrWhiteSpace(textBox1.Text))
                {
                    textBox1.Text += "-";
                }
                 else
                    label1.Text = "-"; 
            //else if (string.IsNullOrWhiteSpace(textBox2.Text))
                 else
                textBox2.Text += "-"; 
        }
    private void button15_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "×";
        }
    private void button13_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "÷";
        }
     /// <summary>
     /// 冪次與根號運算(目前只運算正根號)
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void buttonmi_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "aⁿ";
        }
     private void buttongenhao_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "√ ̄";
        }
    /// <summary>
    /// 4種數據轉換
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void buttonBool_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "Dto2";
        }
    private void buttonHex_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "DtoFF";
        }
    private void button2toD_Click(object sender, EventArgs e)
        {
            label1.Text = "2toD";
        }
    private void buttonFFtoD_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {
                textBox1.Text = textBox3.Text;
                textBox2.Text = "";
            }
            label1.Text = "FFtoD";
        }
  /// <summary>
  /// 計算結果
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button12_Click(object sender, EventArgs e)     ///=號控制
        {
             Int32 sum1; string Str1;
            switch (label1.Text)
            {
                case "Dto2":
                    sum1 = Convert.ToInt32(textBox1.Text);
                    textBox3.Text = Convert.ToString(sum1, 2);
                    break;
                case "DtoFF":
                    sum1 = Convert.ToInt32(textBox1.Text);
                    textBox3.Text = Convert.ToString(sum1, 16);
                    textBox3.Text = textBox3.Text.ToUpper();
                    break;
                case "2toD":
                    Str1 = Convert.ToString(textBox1.Text);
                    textBox3.Text = Convert.ToInt32(Str1, 2).ToString();
                    break;
                case "FFtoD":
                    Str1 = Convert.ToString(textBox1.Text);
                    textBox3.Text = Convert.ToInt32(Str1, 16).ToString();
                    break;
                default:
                    textBox3.Text = calNum(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
                    break;
            }
        }
  /// <summary>
    /// 計算算法
    /// </summary>
    /// <param name="num1"></param>
    /// <param name="num2"></param>
    /// <returns></returns>
    private string calNum(double num1, double num2) /// 計算
        {
            double sum = 0;
            switch (label1.Text)
            {
                case "+":
                    sum = num1 + num2;
                    break;
                case "-":
                    sum = num1 - num2;
                    break;
                case "×":
                    sum = num1 * num2;
                    break;
                case "÷":
                    if (num2 != 0)
                        sum = num1 / num2;
                    else
                        MessageBox.Show("語法錯誤,除數不能為0,請重新輸入...");
                    break;
                case "aⁿ":
                    sum = Math.Pow(num1, num2);
                    break;
                case "√ ̄":
                    if (num1 >= 0 && num2 != 0)
                        sum = Math.Pow(num1, 1 / num2);
                    else
                        MessageBox.Show("語法錯誤,根號內不能小於0且不能開0次方,請重新輸入...");
                    break;
            }
            switch (PointBit.SelectedIndex) ///小數點控制    
            {
                case 0:
                    return Convert.ToDouble(sum).ToString("f0");
                case 1:
                    return Convert.ToDouble(sum).ToString("f1");
                case 2:
                    return Convert.ToDouble(sum).ToString("f2");
                case 3:
                    return Convert.ToDouble(sum).ToString("f3");
                case 4:
                    return sum.ToString();
            }
            return sum.ToString();
    }
    }
}

  

namespace WindowsFormsApplication15{    public partial class 祥哥計算器 : Form    {        public 祥哥計算器()        {            InitializeComponent();        }
        #region 數字點擊事件        /// <summary>        /// 按鈕點擊事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "1";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "1";                SetNum(textBox2.Text);            }        }        private void button2_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "2";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "2";                SetNum(textBox2.Text);            }        }        private void button3_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "3";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "3";                SetNum(textBox2.Text);            }        }        private void button4_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "4";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "4";                SetNum(textBox2.Text);            }        }        private void button5_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "5";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "5";                SetNum(textBox2.Text);            }        }        private void button7_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "7";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "7";                SetNum(textBox2.Text);            }        }        private void button6_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "6";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "6";                SetNum(textBox2.Text);            }        }        private void button8_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "8";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "8";                SetNum(textBox2.Text);            }        }        private void button9_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "9";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "9";                SetNum(textBox2.Text);            }        }        private void button14_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "0";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "0";                SetNum(textBox2.Text);            }        }     /// <summary>     /// 小數點問題     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     private void buttonpoint_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += ".";                //if(textBox1.Text.IndexOf(".") >= 1)                //    MessageBox.Show("已經有小數點了");                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += ".";                //if(textBox2.Text.IndexOf(".") >=1)                //    MessageBox.Show("已經有小數點了");                    SetNum(textBox2.Text);            }        }        private void button00_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "00";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "00";                SetNum(textBox2.Text);            }        }        private void buttonA_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "A";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "A";                SetNum(textBox2.Text);            }        }
        private void buttonB_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "B";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "B";                SetNum(textBox2.Text);            }        }
        private void buttonC_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "C";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "C";                SetNum(textBox2.Text);            }        }
        private void buttonD_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "D";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "D";                SetNum(textBox2.Text);            }        }
        private void buttonE_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "E";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "E";                SetNum(textBox2.Text);            }        }
        private void buttonF_Click(object sender, EventArgs e)        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                if (textBox1.Text.Length < 10)                    textBox1.Text += "F";                SetNum(textBox1.Text);            }            else            {                if (textBox2.Text.Length < 10)                    textBox2.Text += "F";                SetNum(textBox2.Text);            }        }        /// <summary>        /// 清空鍵與退格鍵        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button16_Click_1(object sender, EventArgs e)     ///清空CE鍵        {            label1.Text = "";            textBox3.Clear();            textBox1.Clear();            textBox2.Clear();        }        private void buttondelete_Click(object sender, EventArgs e)    ///退格鍵        {            if (string.IsNullOrWhiteSpace(label1.Text))            {                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);            }            else            {                textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);            }        }        #endregion        /// <summary>        /// 讀取數字功能        /// </summary>        /// <param name="text"></param>        private void SetNum(string text)    ///調用功能鍵        {            {                if (string.IsNullOrWhiteSpace(label1.Text))                {                    textBox1.Text = text;                }                else                {                    textBox2.Text = text;                }            }        }    /// <summary>    /// 四則運算    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void button10_Click(object sender, EventArgs e)         ///運算符號        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "+";        }     private void button11_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))
            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";                label1.Text = "-";            }            //else            //    label1.Text = "-";            else if (string.IsNullOrWhiteSpace(label1.Text))
                if (string.IsNullOrWhiteSpace(textBox1.Text))                {                    textBox1.Text += "-";                }                 else                    label1.Text = "-";             //else if (string.IsNullOrWhiteSpace(textBox2.Text))                 else                textBox2.Text += "-";         }    private void button15_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "×";        }    private void button13_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "÷";        }     /// <summary>     /// 冪次與根號運算(目前只運算正根號)     /// </summary>     /// <param name="sender"></param>     /// <param name="e"></param>     private void buttonmi_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "aⁿ";        }     private void buttongenhao_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "√ ̄";        }    /// <summary>    /// 4種數據轉換    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void buttonBool_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "Dto2";        }    private void buttonHex_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "DtoFF";        }    private void button2toD_Click(object sender, EventArgs e)        {            label1.Text = "2toD";        }    private void buttonFFtoD_Click(object sender, EventArgs e)        {            if (!string.IsNullOrWhiteSpace(textBox1.Text) && !string.IsNullOrWhiteSpace(textBox2.Text))            {                textBox1.Text = textBox3.Text;                textBox2.Text = "";            }            label1.Text = "FFtoD";        }  /// <summary>  /// 計算結果  /// </summary>  /// <param name="sender"></param>  /// <param name="e"></param>  private void button12_Click(object sender, EventArgs e)     ///=號控制        {             Int32 sum1; string Str1;            switch (label1.Text)            {                case "Dto2":                    sum1 = Convert.ToInt32(textBox1.Text);                    textBox3.Text = Convert.ToString(sum1, 2);                    break;                case "DtoFF":                    sum1 = Convert.ToInt32(textBox1.Text);                    textBox3.Text = Convert.ToString(sum1, 16);                    textBox3.Text = textBox3.Text.ToUpper();                    break;                case "2toD":                    Str1 = Convert.ToString(textBox1.Text);                    textBox3.Text = Convert.ToInt32(Str1, 2).ToString();                    break;                case "FFtoD":                    Str1 = Convert.ToString(textBox1.Text);                    textBox3.Text = Convert.ToInt32(Str1, 16).ToString();                    break;                default:                    textBox3.Text = calNum(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));                    break;            }        }  /// <summary>    /// 計算算法    /// </summary>    /// <param name="num1"></param>    /// <param name="num2"></param>    /// <returns></returns>    private string calNum(double num1, double num2) /// 計算        {            double sum = 0;            switch (label1.Text)            {                case "+":                    sum = num1 + num2;                    break;                case "-":                    sum = num1 - num2;                    break;                case "×":                    sum = num1 * num2;                    break;                case "÷":                    if (num2 != 0)                        sum = num1 / num2;                    else                        MessageBox.Show("語法錯誤,除數不能為0,請重新輸入...");                    break;                case "aⁿ":                    sum = Math.Pow(num1, num2);                    break;                case "√ ̄":                    if (num1 >= 0 && num2 != 0)                        sum = Math.Pow(num1, 1 / num2);                    else                        MessageBox.Show("語法錯誤,根號內不能小於0且不能開0次方,請重新輸入...");                    break;            }            switch (PointBit.SelectedIndex) ///小數點控制                {                case 0:                    return Convert.ToDouble(sum).ToString("f0");                case 1:                    return Convert.ToDouble(sum).ToString("f1");                case 2:                    return Convert.ToDouble(sum).ToString("f2");                case 3:                    return Convert.ToDouble(sum).ToString("f3");                case 4:                    return sum.ToString();            }            return sum.ToString();    }    }}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM