-2

我在visual studio c#中编写了一个代码,它将整数从二进制转换为十进制模式,从十进制模式转换为二进制,但我希望它能够将带小数点的数字从十进制模式转换为二进制我该怎么做,请帮助我并告诉我我必须在我的代码中进行哪些修改这是我的计算器代码:

                    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 assignment2
        {
            public partial class Form1 : Form
            {
                const int asciiDiff = 48;
                double num1 = 0, num2 = 0, result = 0;
                double fact = 1;
                int[] iHexaNumeric = new int[] { 10, 11, 12, 13, 14, 15 };
                char[] cHexa = new char[] { 'A', 'B', 'C', 'D', 'E', 'F' };
                String a = "";
                char op;
                bool b = false;
                const int base10 = 10;
                public Form1()
                {
                  // 

                    InitializeComponent();
                }

                private void textBox1_TextChanged(object sender, EventArgs e)
                {

                }

                private void button1_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "1";
                }

                private void buttonTow_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "2";
                }

                private void buttonThree_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "3";
                }

                private void buttonFour_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "4";
                }

                private void buttonFive_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "5";
                }

                private void buttonSix_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "6";
                }

                private void buttonSeven_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "7";
                }

                private void buttonEight_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "8";
                }

                private void buttonNine_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "9";
                }

                private void buttonZero_Click(object sender, EventArgs e)
                {
                    a = DisplayResult.Text.ToString();

                    DisplayResult.Text = a + "0";
                }

                private void buttonPlus_Click(object sender, EventArgs e)
                {
                    num1 = Convert.ToDouble(DisplayResult.Text.ToString());
                    op = '+';
                    DisplayResult.Text = string.Empty;
                }

                private void buttonMinus_Click(object sender, EventArgs e)
                {
                    num1 = Convert.ToDouble(DisplayResult.Text.ToString());
                    op = '-';
                    DisplayResult.Text = string.Empty;
                }

                private void buttonMultipler_Click(object sender, EventArgs e)
                {
                    num1 = Convert.ToDouble(DisplayResult.Text.ToString());
                    op = '*';
                    DisplayResult.Text = string.Empty;
                }

                private void buttonDivider_Click(object sender, EventArgs e)
                {
                    num1 = Convert.ToDouble(DisplayResult.Text.ToString());
                    op = '/';
                    DisplayResult.Text = string.Empty;
                }




                private void buttonEqual_Click(object sender, EventArgs e)
                {
                    if (DisplayResult.Text == "")
                        return;
                    else
                    {

                        try
                        {

                            num2 = Convert.ToDouble(DisplayResult.Text.ToString());
                            switch (op)
                            {
                                case '+': //suma
                                    result = (num1 + num2);
                                    DisplayResult.Text = result.ToString();
                                    break;


                                case '-': //resta
                                    result = (num1 - num2);
                                    DisplayResult.Text = result.ToString();
                                    break;

                                case '*': //multiply
                                    result = (num1 * num2);
                                    DisplayResult.Text = result.ToString();
                                    break;

                                case '/': //division
                                    if (num2 != 0)
                                    {
                                        result = (num1 / num2);
                                        DisplayResult.Text = result.ToString();

                                    }
                                    else
                                    {
                                        DisplayResult.Text = "Can't divide by 0";
                                    }
                                    break;
                            }

                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show("Unexpected error occured. Details: " +
                                ex.Message);
                        }
                    }
                }
                private void buttonBackSpace_Click(object sender, EventArgs e)
                {
                    try
                    {

                        String Value = DisplayResult.Text.ToString();
                        int temp = Value.Length;
                        if (temp == 1)
                            DisplayResult.Text = String.Empty;
                        else
                        {
                            DisplayResult.Text = DisplayResult.Text.Substring(0, temp - 1);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Unexpected error in buttonBackSpace occured. Details: " +
                            ex.Message);
                    } 
                }

                private void buttonClear_Click(object sender, EventArgs e)
                {
                    DisplayResult.Text = String.Empty;
                }
                private void buttonDecimal_Click(object sender, EventArgs e)
                {

                    if (DisplayResult.Text.Contains("."))
                    {
                        return;
                    }

                    DisplayResult.Text += ".";


                }

                private void DecimalRadioButton_CheckedChanged(object sender, EventArgs e)
                {
                    if (DecimalRadioButton.Checked == true)
                    {
                        DisplayResult.Text= BaseToDecimal(DisplayResult.Text.ToString(), 2).ToString();

                    }
                    buttonTow.Enabled = true;
                    buttonThree.Enabled = true;
                    buttonFour.Enabled = true;
                    buttonFive.Enabled = true;
                    buttonSix.Enabled = true;
                    buttonSeven.Enabled = true;
                    buttonEight.Enabled = true;
                    buttonNine.Enabled = true;

                }

                private void BinaryRadioButton_CheckedChanged(object sender, EventArgs e)
                {
                    if (BinaryRadioButton.Checked == true)
                    {

                        DisplayResult.Text = DecimalToBase(Convert.ToInt16(DisplayResult.Text.ToString()), 2);

                        buttonTow.Enabled = false;
                        buttonThree.Enabled = false;
                        buttonFour.Enabled = false;
                        buttonFive.Enabled = false;
                        buttonSix.Enabled = false;
                        buttonSeven.Enabled = false;
                        buttonEight.Enabled = false;
                        buttonNine.Enabled = false;
                    }




                }




                string DecimalToBase(int iDec, int numbase)
                {
                    string strBin = "";
                    int[] result = new int[32];
                    int MaxBit = 32;
                    for (; iDec > 0; iDec /= numbase)
                    {
                        int rem = iDec % numbase;
                        result[--MaxBit] = rem;
                    }
                    for (int i = 0; i < result.Length; i++)
                        if ((int)result.GetValue(i) >= base10)
                            strBin += cHexa[(int)result.GetValue(i) % base10];
                        else
                            strBin += result.GetValue(i);
                    strBin = strBin.TrimStart(new char[] { '0' });
                    return strBin;
                }

                int BaseToDecimal(string sBase, int numbase)
                {

                    int dec = 0;
                    int b;
                    int iProduct = 1;
                    string sHexa = "";
                    if (numbase > base10)
                        for (int i = 0; i < cHexa.Length; i++)
                            sHexa += cHexa.GetValue(i).ToString();
                    for (int i = sBase.Length - 1; i >= 0; i--, iProduct *= numbase)
                    {
                        string sValue = sBase[i].ToString();
                        if (sValue.IndexOfAny(cHexa) >= 0)
                            b = iHexaNumeric[sHexa.IndexOf(sBase[i])];
                        else
                            b = (int)sBase[i] - asciiDiff;
                        dec += (b * iProduct);
                    }
                    return dec;
                }
            }
        }
4

1 回答 1

1

将带小数点的数字从十进制转换为二进制时,您要做的就是:首先取小数点前的部分并将其(以通常方式)转换为二进制;小数点后的部分乘以 2 看是否 >= 1;如果不是,则写 0 并继续相乘。当它 = 1.00 时,您就完成了。例如:

2.25 - 你取 0.25;0.25 * 2 = 0.50 --> 0, 0.50 * 2 = 1.00 --> 1,您只需阅读数字即可。因此,0.25 将是 0.01(2.25 将是 10.01 二进制。)

于 2013-10-29T20:00:27.930 回答