Winform——计算器进制转换

namespace 进制转换2._0
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string Shizhuaner(int a, int jinzhi) //十转二
        {
            string jieguo = "";
            while (true)
            {
                string s = (a % jinzhi).ToString(); //s的值为a%2后的数,因为进制是2。
                jieguo = s + jieguo; //s在前面是为了让结果倒序输出
                a = a / jinzhi;
                if (a == 0)
                {
                    break;
                }

            }
            return jieguo;
        }

        private void button1_Click(object sender, EventArgs e) //十转二
        {
            string x = Shizhuanba(int.Parse(textBox1.Text), 2);
            textBox2.Text = x;
        }

        public string Shizhuanba(int a, int jinzhi) //十转八
        {
            string jieguo = "";
            while (true)
            {
                string s = (a % jinzhi).ToString();
                jieguo = s + jieguo;
                a = a / jinzhi;
                if (a == 0)
                {
                    break;
                }

            }
            return jieguo;
        }

        private void button2_Click(object sender, EventArgs e) //十转八
        {
            string x = Shizhuanba(int.Parse(textBox1.Text), 8);
            textBox3.Text = x;
        }

        public string Shizhuanshiliu(int a,int jinzhi) //十转十六
        {
            string jieguo = "";
                while (true)
                {
                    int yushu = a % jinzhi;
                    string s = "";
                    if(yushu==10)
                    {
                        s = "A";
                    }
                    else if (yushu == 11)
                    {
                        s = "B";
                    }
                    else if (yushu == 12)
                    {
                        s = "C";
                    }
                    else if (yushu == 13)
                    {
                        s = "D";
                    }
                    else if (yushu == 14)
                    {
                        s = "E";
                    }
                    else if (yushu == 15)
                    {
                        s = "F";
                    }
                    else
                    {
                        s = yushu.ToString();
                    }
                    jieguo = s + jieguo;
                    a = a / jinzhi;
                    if (a == 0)
                    {
                        break;
                    }
                }
            return jieguo;
        }

        private void button3_Click(object sender, EventArgs e) //十转十六
        {
            string x = Shizhuanshiliu(int.Parse(textBox1.Text), 16);
            textBox4.Text = x;
        }

        private void button7_Click(object sender, EventArgs e) //清空键
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
        }



        public string Erzhuanshi(string a)  //二转十
        {
            string jieguo = "";
            int length=a.Length;
            int Sum = 0;
            int n = 0;
            while (true)
            {  
                double y = int.Parse(a.Substring(length - 1, 1)) *Math.Pow(2,n);  
                length--;
                n++;
                int y1 = (int)y;
                Sum =Sum+y1 ;
                jieguo = Sum.ToString(); 
                if(length==0)
                {
                    break;
                }
            }  
            return jieguo;
        }
        private void button6_Click(object sender, EventArgs e) //二转十
        {

            string s = Erzhuanshi(textBox2.Text);
            textBox5.Text = s;
        }


        public string Bazhuanshi(string a)  //八转十
        {
            string jieguo = "";
            int length = a.Length;
            int Sum = 0;
            int n = 0;
            while (true)
            {
                double y = int.Parse(a.Substring(length - 1, 1)) * Math.Pow(8, n);
                length--;
                n++;
                int y1 = (int)y;
                Sum = Sum + y1;
                jieguo = Sum.ToString();
                if (length == 0)
                {
                    break;
                }
            }
            return jieguo;
        }

        private void button5_Click(object sender, EventArgs e) //八转十
        {
            string s = Bazhuanshi(textBox3.Text);
            textBox6.Text = s;
        }


        public string shiliuzhuanshi(string a)  //十六转十
        {
            string jieguo = "";
            int length = a.Length;
            int Sum = 0;
            int n = 0;
           
            
            while (true)
            {   
                 /*
                 A代表10
                 B代表11
                 C代表12
                 D代表13
                 E代表14
                 F代表15
                 */ 
                string z = a.Substring(length - 1, 1);
                if(z=="A")
                {
                    z = "10";
                }
                else if (z == "B")
                {
                    z = "11";
                }
                else if (z == "C")
                {
                    z = "12";
                }
                else if (z == "D")
                {
                    z = "13";
                }
                else if (z == "E")
                {
                    z = "14";
                }
                else if (z == "F")
                {
                    z = "15";
                }
                else
                { 
                
                }
                double y = double.Parse(z) * Math.Pow(16, n);
                length--;
                n++;
                int y1 = (int)y;
                Sum = Sum + y1;
                jieguo = Sum.ToString();
                if (length == 0)
                {
                    break;
                }
            }
            return jieguo;
        }
        private void button4_Click(object sender, EventArgs e) //十六转十
        {
            string s = shiliuzhuanshi(textBox4.Text);
            textBox7.Text = s;
        }
    }
}

 

posted @ 2015-05-29 13:12  XCml  阅读(500)  评论(0编辑  收藏  举报