計算器的製作

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 MyCalc
{
    public partial class Form1 : Form 
    {
        private double _num1 = 0, _num2 = 0, _result = 0;
        private string _operator = ""; 
        private int _operatorCount = 0;

        private int yuan = 10;  //原來默認爲10進制

        private bool _firstNumberFlag = false;//使得邊計算便輸出
        public Form1()  //窗口設計代碼
        {
            InitializeComponent();
        }

        private void buttonNum1_Click(object sender, EventArgs e)  //鼠標按鈕
        {
            
            if (_firstNumberFlag == true)
            {
                textBoxResult.Text = "1";
                _firstNumberFlag = false;
            }
            else
                textBoxResult.Text = textBoxResult.Text + "1";
            textBoxResult.Focus();//焦點落到編輯框中
            textBoxResult.Select(textBoxResult.Text.Length, 0);//讓光標落在編輯框的尾部
        }

        private void buttonNum2_Click(object sender, EventArgs e)
        {
            if (yuan == 8 || yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "2";
                    _firstNumberFlag = false;
                }
                else
                    textBoxResult.Text = textBoxResult.Text + "2";
            }
               textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum3_Click(object sender, EventArgs e)
        {
            if (yuan == 8 || yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "3";
                    _firstNumberFlag = false;
                }
                else
                    textBoxResult.Text = textBoxResult.Text + "3";

            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum4_Click(object sender, EventArgs e)
        {
            if (yuan == 8 || yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "4";
                    _firstNumberFlag = false;
                }
                else
                    textBoxResult.Text = textBoxResult.Text + "4";
            }
                textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum5_Click(object sender, EventArgs e)
        {
            if (yuan == 8 || yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "5";
                    _firstNumberFlag = false;
                }
                else textBoxResult.Text = textBoxResult.Text + "5";

            } textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum6_Click(object sender, EventArgs e)
        {
            if (yuan == 8 || yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "6";
                    _firstNumberFlag = false;
                }
                else textBoxResult.Text = textBoxResult.Text + "6";

            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum7_Click(object sender, EventArgs e)
        {
            if (yuan == 8 || yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "7";
                    _firstNumberFlag = false;
                }
                else textBoxResult.Text = textBoxResult.Text + "7";

            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum8_Click(object sender, EventArgs e)
        {
            if ( yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "8";
                    _firstNumberFlag = false;
                }
                else
                    textBoxResult.Text = textBoxResult.Text + "8";
            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum9_Click(object sender, EventArgs e)
        {
            if (yuan == 10)
            {
                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = "9";
                    _firstNumberFlag = false;
                }
                else
                    textBoxResult.Text = textBoxResult.Text + "9";

            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonNum0_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBoxResult.Text = "0";
                _firstNumberFlag = false;
            }
            else
                textBoxResult.Text = textBoxResult.Text + "0";
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }



        private void buttonDot_Click(object sender, EventArgs e)//"."的按鈕
        {
            if (yuan == 10)
            {

                if (_firstNumberFlag == true)
                {
                    textBoxResult.Text = ".";
                    _firstNumberFlag = false;
                }
                else
                {
                    if (textBoxResult.Text.IndexOf('.') == -1)  //判斷點的個數
                        textBoxResult.Text = textBoxResult.Text + ".";
                }
            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }







        private void buttonAdd_Click(object sender, EventArgs e)//運算符按鈕 加法
        {
            _firstNumberFlag = true;
            _operatorCount = _operatorCount + 1;
            if (_operatorCount == 1)
            {
                _num1 = Convert.ToDouble(textBoxResult.Text);
                _operator = "+";
               
            }
            else
            {
                _num2 = Convert.ToDouble(textBoxResult.Text);
                if(yuan==10)
                {
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                    }
                    else if(_operator == "-")
                    {
                         _num1 = _num1 - _num2;
                    }
                    else if (_operator== "*")
                    _num1 = _num1 * _num2;
                    else if(_operator == "/")
                    _num1 = _num1 / _num2;
                }
                else if(yuan==2)
                {
                    _num1 =toshijinzhi (_num1 ,yuan);
                    _num2 =toshijinzhi (_num2 ,yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1=toerjinzhi (_num1);
                    }
                    else if(_operator == "-")
                    {
                         _num1 = _num1 - _num2;
                         _num1=toerjinzhi (_num1);
                    }
                    else if (_operator =="*")
                    {
                        _num1 = _num1 * _num2;
                        _num1=toerjinzhi (_num1);
                    }
                    
                    else if(_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1=toerjinzhi (_num1);
                    }
                }

                else if(yuan==8)
                {
                    _num1 =toshijinzhi (_num1 ,yuan);
                    _num2 =toshijinzhi (_num2 ,yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1=tobajinzhi (_num1);
                    }
                    else if(_operator == "-")
                    {
                         _num1 = _num1 - _num2;
                         _num1=tobajinzhi (_num1);
                    }
                    else if (_operator =="*")
                    {
                        _num1 = _num1 * _num2;
                        _num1=tobajinzhi (_num1);
                    }
                    
                    else if(_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1=tobajinzhi (_num1);
                    }
                }    
                _operator = "+";//保存運算符號到類字段_operator中
                textBoxResult.Text = _num1.ToString();
               
            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }





        private void buttonSubtract_Click(object sender, EventArgs e)  //運算符按鈕 減法
        {
            _firstNumberFlag = true;
            _operatorCount = _operatorCount + 1;
            if (_operatorCount == 1)
            {
                _num1 = Convert.ToDouble(textBoxResult.Text);
                _operator = "-";
            }
            else
            {
                _num2 = Convert.ToDouble(textBoxResult.Text);
                if (yuan == 10)
                {
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                    }
                    else if (_operator == "*")
                        _num1 = _num1 * _num2;
                    else if (_operator == "/")
                        _num1 = _num1 / _num2;
                }
                else if (yuan == 2)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                    else if (_operator == "*")
                    {
                        _num1 = _num1 * _num2;
                        _num1 = toerjinzhi(_num1);
                    }

                    else if (_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                }

                else if (yuan == 8)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                    else if (_operator == "*")
                    {
                        _num1 = _num1 * _num2;
                        _num1 = tobajinzhi(_num1);
                    }

                    else if (_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                }
                _operator = "-";//保存運算符號到類字段_operator中
                textBoxResult.Text = _num1.ToString();

            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }


        private void buttonMultiply_Click(object sender, EventArgs e) //運算符按鈕 乘法
        {
            _firstNumberFlag = true;
            _operatorCount = _operatorCount + 1;
            if (_operatorCount == 1)
            {
                _num1 = Convert.ToDouble(textBoxResult.Text);
                _operator = "*";
            }
            else
            {
                _num2 = Convert.ToDouble(textBoxResult.Text);
                if (yuan == 10)
                {
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                    }
                    else if (_operator == "*")
                        _num1 = _num1 * _num2;
                    else if (_operator == "/")
                        _num1 = _num1 / _num2;
                }
                else if (yuan == 2)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                    else if (_operator == "*")
                    {
                        _num1 = _num1 * _num2;
                        _num1 = toerjinzhi(_num1);
                    }

                    else if (_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                }

                else if (yuan == 8)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                    else if (_operator == "*")
                    {
                        _num1 = _num1 * _num2;
                        _num1 = tobajinzhi(_num1);
                    }

                    else if (_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                }
                _operator = "*";//保存運算符號到類字段_operator中
                textBoxResult.Text = _num1.ToString();

            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonDivide_Click(object sender, EventArgs e)  //運算符按鈕 除法
        {
            _firstNumberFlag = true;
            _operatorCount = _operatorCount + 1;
            if (_operatorCount == 1)
            {
                _num1 = Convert.ToDouble(textBoxResult.Text);
                _operator = "/";
            }
            else
            {
                _num2 = Convert.ToDouble(textBoxResult.Text);
                if (yuan == 10)
                {
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                    }
                    else if (_operator == "*")
                        _num1 = _num1 * _num2;
                    else if (_operator == "/")
                        _num1 = _num1 / _num2;
                }
                else if (yuan == 2)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                    else if (_operator == "*")
                    {
                        _num1 = _num1 * _num2;
                        _num1 = toerjinzhi(_num1);
                    }

                    else if (_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1 = toerjinzhi(_num1);
                    }
                }

                else if (yuan == 8)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    if (_operator == "+")
                    {
                        _num1 = _num1 + _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                    else if (_operator == "-")
                    {
                        _num1 = _num1 - _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                    else if (_operator == "*")
                    {
                        _num1 = _num1 * _num2;
                        _num1 = tobajinzhi(_num1);
                    }

                    else if (_operator == "/")
                    {
                        _num1 = _num1 / _num2;
                        _num1 = tobajinzhi(_num1);
                    }
                }
                _operator = "/";//保存運算符號到類字段_operator中
                textBoxResult.Text = _num1.ToString();

            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonEqual_Click(object sender, EventArgs e)  //運算符按鈕 等於號
        {
            _num2 = Convert.ToDouble(textBoxResult.Text);
            if (_operator == "+")
            {
                if(yuan==10)
                {
                    _result = _num1 + _num2;
                }
                else if (yuan == 2)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 + _num2;
                    _result = toerjinzhi(_num1);
                }
                else if (yuan == 8)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 + _num2;
                    _result = tobajinzhi(_num1);
                }
            }


            else if (_operator == "-")
            {
                if (yuan == 10)
                {
                    _result = _num1 - _num2;
                }
                else if (yuan == 2)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 - _num2;
                    _result = toerjinzhi(_num1);
                }
                else if (yuan == 8)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 - _num2;
                    _result = tobajinzhi(_num1);
                }
            }

            else if (_operator == "*")
            {
                if (yuan == 10)
                {
                    _result = _num1 * _num2;
                }
                else if (yuan == 2)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 * _num2;
                    _result = toerjinzhi(_num1);
                }
                else if (yuan == 8)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 * _num2;
                    _result = tobajinzhi(_num1);
                }
            }

            else if (_operator == "/")
            {
                if (yuan == 10)
                {
                    _result = _num1 / _num2;
                }
                else if (yuan == 2)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 / _num2;
                    _result = toerjinzhi(_num1);
                }
                else if (yuan == 8)
                {
                    _num1 = toshijinzhi(_num1, yuan);
                    _num2 = toshijinzhi(_num2, yuan);
                    _num1 = _num1 / _num2;
                    _result = tobajinzhi(_num1);
                }
            }

            textBoxResult.Text = _result.ToString();
            _operatorCount = 0;
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonBackSpace_Click(object sender, EventArgs e)  //回退
        {
            if (textBoxResult.Text.Length > 0)
                
                textBoxResult.Text = textBoxResult.Text.Substring(0, textBoxResult.Text.Length - 1);//刪除最後一個字符
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonClear_Click(object sender, EventArgs e) //清空
        {
            _num1 = 0; _num2 = 0; _result = 0;
            _operator = "";
            _operatorCount = 0;
            _firstNumberFlag = false;
            textBoxResult.Text = "";
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void buttonSign_Click(object sender, EventArgs e)//退格的按鈕
        {
            if (textBoxResult.Text.Length >= 1)
            {
                string firstChar = textBoxResult.Text.Substring(0, 1);
                if (firstChar == "-")
                    textBoxResult.Text = "+" + textBoxResult.Text.Substring(1, textBoxResult.Text.Length - 1);

                else if (firstChar == "+")
                    textBoxResult.Text = "-" + textBoxResult.Text.Substring(1, textBoxResult.Text.Length - 1);

                else
                    textBoxResult.Text = "-" + textBoxResult.Text;
            }
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        private void textBoxResult_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == '0')
                buttonNum0_Click(sender, e);

            else if (e.KeyChar == '1')
                buttonNum1_Click(sender, e);

            else if (e.KeyChar == '2'&&(yuan==8||yuan==10))
                buttonNum2_Click(sender, e);

            else if (e.KeyChar == '3' && (yuan == 8 || yuan == 10))
                buttonNum3_Click(sender, e);

            else if (e.KeyChar == '4' && (yuan == 8 || yuan == 10))
                buttonNum4_Click(sender, e);

            else if (e.KeyChar == '5' && (yuan == 8 || yuan == 10))
                buttonNum5_Click(sender, e);

            else if (e.KeyChar == '6' && (yuan == 8 || yuan == 10))
                buttonNum6_Click(sender, e);

            else if (e.KeyChar == '7' && (yuan == 8 || yuan == 10))
                buttonNum7_Click(sender, e);

            else if (e.KeyChar == '8' && (yuan == 8 || yuan == 10))
                buttonNum8_Click(sender, e);

            else if (e.KeyChar == '9' && (yuan == 8 || yuan == 10))
                buttonNum9_Click(sender, e);

            else if (e.KeyChar == '+')
                buttonAdd_Click(sender, e);

            else if (e.KeyChar == '-')
                buttonSubtract_Click(sender, e);

            else if (e.KeyChar == '*')
                buttonMultiply_Click(sender, e);

            else if (e.KeyChar == '/')
                buttonDivide_Click(sender, e);

            else if (e.KeyChar == '=')
                buttonEqual_Click(sender, e);

            else if (e.KeyChar == 'c')
                buttonClear_Click(sender, e);

            else if (e.KeyChar == '.' && yuan == 10)
                buttonDot_Click(sender, e);

            else if (e.KeyChar == '\b')
                buttonBackSpace_Click(sender, e);

            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }

        

        private void radioButton2_CheckedChanged(object sender, EventArgs e) //二進制按鈕
        {
            if (_operatorCount == 0 && textBoxResult.Text != "")
            {
                _num1 = Convert.ToDouble(textBoxResult.Text);
            }
            
            if (_operatorCount > 0)  //大於等於一個數字
                    _num2 = Convert.ToDouble(textBoxResult.Text);
            if (yuan == 10 && (_num1 != 0 || _num2 != 0))
            {
                //將原有的十進制轉爲二進制
                _num1 = toerjinzhi (_num1);
                _num2 = toerjinzhi(_num2);
                if (_num2 == 0) 
                {
                    textBoxResult.Text = _num1.ToString(); 
                }
                else textBoxResult.Text = _num1.ToString();
            }

            if (yuan == 8 && (_num1 != 0 || _num2 != 0))
            {
                _num1 = toshijinzhi(_num1,yuan);
                _num1 = toerjinzhi(_num1);
                _num2 = toshijinzhi(_num2,yuan);
                _num2 = toerjinzhi(_num2);
                if (_num2 == 0)
                {
                    textBoxResult.Text = _num1.ToString();
                }
                else textBoxResult.Text = _num1.ToString();
            }
            yuan = 2;
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }


        private void radioButton10_CheckedChanged(object sender, EventArgs e)  //十進制按鈕
        {
            if (_operatorCount == 0 && textBoxResult.Text != "")
            {
                _num1 = Convert.ToDouble(textBoxResult.Text);
            }
            if (_operatorCount > 0)  //大於等於一個數字
                _num2 = Convert.ToDouble(textBoxResult.Text);
            if (yuan == 10 && (_num1 != 0 || _num2 != 0))
            {
                if (_num2 == 0)
                {
                    textBoxResult.Text = _num1.ToString();
                }
                else textBoxResult.Text = _num1.ToString();
            }
            else if (yuan == 8 && (_num1 != 0 || _num2 != 0))
            {
                _num1 = toshijinzhi(_num1, yuan);
                _num2 = toshijinzhi(_num2, yuan);
                if (_num2 == 0)
                {
                    textBoxResult.Text = _num1.ToString();
                }
                else textBoxResult.Text = _num1.ToString();
            }
            else if (yuan == 2 && (_num1 != 0 || _num2 != 0))
            {
                _num1 = toshijinzhi(_num1, yuan);
                _num2 = toshijinzhi(_num2, yuan);
                if (_num2 == 0)
                {
                    textBoxResult.Text = _num1.ToString();
                }
                else textBoxResult.Text = _num1.ToString();
            }
            yuan = 10;
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);

        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e) //八進制按鈕
        {
            if (_operatorCount == 0 && textBoxResult.Text != "")
            {
                _num1 = Convert.ToDouble(textBoxResult.Text);
            }
            if (_operatorCount > 0)  //大於等於一個數字
                _num2 = Convert.ToDouble(textBoxResult.Text);
            if (yuan == 8 && (_num1 != 0 || _num2 != 0))
            {
                if (_num2 == 0)
                {
                    textBoxResult.Text = _num1.ToString();
                }
                else textBoxResult.Text = _num1.ToString();
            }
            else if (yuan == 10 && (_num1 != 0 || _num2 != 0))
            {
                _num1 = tobajinzhi(_num1);
                _num2 = tobajinzhi(_num2);
                if (_num2 == 0)
                {
                    textBoxResult.Text = _num1.ToString();
                }
                else textBoxResult.Text = _num1.ToString();
            }
            else if (yuan == 2 && (_num1 != 0 || _num2 != 0))
            {
                _num1 = toshijinzhi(_num1, yuan);
                _num1 = tobajinzhi(_num1);
                _num2 = toshijinzhi(_num2, yuan);
                _num2 = tobajinzhi(_num2);
                if (_num2 == 0)
                {
                    textBoxResult.Text = _num1.ToString();
                }
                else textBoxResult.Text = _num1.ToString();
            }
            yuan = 8;
            textBoxResult.Focus();
            textBoxResult.Select(textBoxResult.Text.Length, 0);
        }


        public double toerjinzhi(double d) //十進制到二進制
        {
            double sum=0;
            int[] s = new int[32];
            int i = 0;
            int q = (int)d;
            while(q!=0)
            {
                s[i++] = q % 2;
                q /= 2;
            }
            for (int j = i - 1; j >= 0; j--)
            {
                sum = sum * 10 + s[j];
            }
             return sum;
        }

        public double tobajinzhi(double d) //十進制到八進制
        {
            double sum = 0;
            int[] s = new int[32];
            int i = 0;
            int q = (int)d;
            while (q != 0)
            {
                s[i++] = q % 8;
                q /= 8;
            }
            for (int j = i - 1; j >= 0; j--)
            {
                sum = sum * 10 + s[j];
            }
            return sum;
        }

        public double toshijinzhi(double d, int shi)  //原來的進制 到十進制
        {
            int i = 0;
            double sum = 0;
            while (d >0)
            {
                sum += (int)d % 10 * Math.Pow(shi, i);
                d = (int)d / 10;
                i++;
            }
            return sum;
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章