当前位置:编程学习 > C#/ASP.NET >>

判断我是c#菜鸟,这就不会了,高手速进,看看什么才是真正的菜鸟。

题目要求将  定点整数的单符号位补码加运算代码   修改  定点小数的变形补码加运算
下是定点整数的单符号位补码加运算代码:
   
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Enabled=false;
            textBox2.Enabled=false;
            try
            {
                if(textBox1.Text !="" && textBox2.Text !="")
                {
                    byte [] Binary1=Encoding.UTF8.GetBytes(textBox1.Text);
                    byte[] Binary2 = Encoding.UTF8.GetBytes(textBox2.Text);
                     if (Binary1[0] == 48 || Binary1[0] == 49 || Binary2[0] == 48 || Binary2[0] == 49)
                    {
                        MessageBox.Show("请正确输入各个数的正负!");
                        textBox1.Enabled = true;
                        textBox2.Enabled = true;
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox1.Focus();
                        return;
                    }
                    if (Binary1.Length != Binary2.Length)
                    {
                        MessageBox.Show("两个数的长度不一致!");
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox1.Focus();
                        return;
                    }
                     byte[] Binary = new byte[Binary1.Length];
                    bt(Binary1);
                    bt(Binary2);
                    int k = 0;
                    for (int i = Binary1.Length - 1; i >= 0; i--)
                    {
                        if (k == 0)
                        {
                            if (Binary1[i] == 48 && Binary2[i] == 48)
                            {
                                Binary[i] = 48;
                            }
                            else if (Binary1[i] != Binary2[i])
                            {
                                Binary[i] = 49;
                            }
                            else
                            {
                                Binary[i] = 48;
                                k = 1;
                            }
                        }
                        else
                        {
                            if (Binary1[i] == 48 && Binary2[i] == 48)
                            {
                                Binary[i] = 49;
                                k = 0;
                            }
                            else if (Binary1[i] != Binary2[i])
                            {
                                Binary[i] = 48;
                            }
                            else
                            {
                                Binary[i] = 49;
                            }
                        }
                    }
                      if (Binary[0] == 48)
                    {
                        textBox3.Text = textBox3.Text + "+";
                        for (int i = 1; i < Binary.Length; i++)
                        {
                            if (Binary[i] == 48)
                            {
                                textBox3.Text = textBox3.Text + "0";
                            }
                            else
                            {
                                textBox3.Text = textBox3.Text + "1";
                            }
                        }
                    }
                    else
                    {
                        textBox3.Text = textBox3.Text + "-";
                        if (Binary[Binary.Length - 1] == 49)
                        {
                            Binary[Binary.Length - 1] = 48;
                        }
                        else
                        {
                            Binary[Binary.Length - 1] = 49;
                            for (int i = Binary.Length - 2; i > 0; i--)
                            {
                                if (Binary[i] != 49)
                                {
                                    Binary[i] = 49;
                                }
                                else
                                {
                                    Binary[i] = 48;
                                    break;
                                }
                            }
                        }
                        for (int i = 1; i < Binary.Length; i++)
                        {
                            if (Binary[i] == 48)
                            {
                                textBox3.Text = textBox3.Text + "1";
                            }
                            else
                            {
                                textBox3.Text = textBox3.Text + "0";
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("输入错误请重新输入!");
                textBox1.Text = "";
                textBox2.Text = "";
                textBox1.Focus();
            }
        }
        private void txtFirstNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 48 && e.KeyChar != 49 && e.KeyChar != 8 && e.KeyChar != 43 && e.KeyChar != 45)
            {
                e.Handled = true;
            }
        }
        public void bt(byte[] Binary)
        {
            if (Binary[0] == 43)
            {
                Binary[0] = 48;
            }
            else
            {
                Binary[0] = 49;
                for (int i = 1; i < Binary.Length; i++)
                {
                    if (Binary[i] == 48)
                    {
                        Binary[i] = 49;
                    }
                    else
                    {
                        Binary[i] = 48;
                    }
                }
                for (int i = Binary.Length - 1; i > 0; i--)
                {
                    if (Binary[i] == 49)
                    {
                        Binary[i] = 48;
                        continue;
                    }
                    else
                    {
                        Binary[i] = 49;
                        break;
                    }
                }
            }
        }
本菜鸟认为只要加个if判断语句,不知道对不对?也不知道怎么加?
求大神帮忙啊。菜鸟先谢谢了!
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,