Пример #1
0
        private void ConvertButton_Click(object sender, RoutedEventArgs e)
        {
            if (ComboBox1.SelectedItem.ToString() == "Decimal")
            {
                /* check if input is decimal-formatted */
                try {
                    BaseConverter.CheckDecimal(TextBox1.Text.ToString());
                }
                catch (FormatException) {
                    WarningText.Text = "Error! Invalid number.";
                    return;
                }
                catch (OverflowException) {
                    WarningText.Text = "Error! Enter a smaller number.";
                    return;
                }
                WarningText.Text = "";

                /* decimal to binary conversion */
                if (ComboBox2.SelectedItem.ToString() == "Binary")
                {
                    string result = "";

                    result = BaseConverter.Dec2Bin(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* decimal to hex conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Hexadecimal")
                {
                    string result;

                    result        = BaseConverter.Dec2Hex(TextBox1.Text.ToString());
                    TextBox2.Text = result;
                }

                /* decimal to octal conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Octal")
                {
                    string result;

                    result        = BaseConverter.Dec2Oct(TextBox1.Text.ToString());
                    TextBox2.Text = result;
                }

                /* no conversion */
                else
                {
                    TextBox2.Text = TextBox1.Text;
                }
            }

            else if (ComboBox1.SelectedItem.ToString() == "Binary")
            {
                /* check if input is binary-formatted */
                try {
                    BaseConverter.CheckBinary(TextBox1.Text.ToString());
                }
                catch (FormatException fe) {
                    WarningText.Text = fe.Message;
                    return;
                }
                catch (OverflowException oe) {
                    WarningText.Text = oe.Message;
                    return;
                }

                WarningText.Text = "";

                /* binary to decimal conversion */
                if (ComboBox2.SelectedItem.ToString() == "Decimal")
                {
                    string result = "";

                    result = BaseConverter.Bin2Dec(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* binary to hexadecimal conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Hexadecimal")
                {
                    string result;

                    result = BaseConverter.Bin2Hex(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* binary to octal conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Octal")
                {
                    string result;

                    result = BaseConverter.Bin2Oct(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* no conversion */
                else
                {
                    TextBox2.Text = TextBox1.Text;
                }
            }

            else if (ComboBox1.SelectedItem.ToString() == "Hexadecimal")
            {
                /* check if input is hexadecimal-format */
                try {
                    BaseConverter.CheckHexadecimal(TextBox1.Text.ToString());
                }
                catch (FormatException fe) {
                    WarningText.Text = fe.Message;
                    return;
                }
                catch (OverflowException oe) {
                    WarningText.Text = oe.Message;
                    return;
                }
                WarningText.Text = "";

                /* hexadecimal to binary conversion */
                if (ComboBox2.SelectedItem.ToString() == "Binary")
                {
                    string result;

                    result = BaseConverter.Hex2Bin(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* hexadecimal to decimal conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Decimal")
                {
                    string result;

                    result = BaseConverter.Hex2Dec(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* hexadecimal to octal conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Octal")
                {
                    string result;

                    result = BaseConverter.Hex2Oct(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }
                /* no conversion */
                else
                {
                    TextBox2.Text = TextBox1.Text.ToUpper();
                }
            }

            else if (ComboBox1.SelectedItem.ToString() == "Octal")
            {
                /* check if input is octal-formatted */
                try {
                    BaseConverter.CheckOctal(TextBox1.Text.ToString());
                }
                catch (FormatException fe) {
                    WarningText.Text = fe.Message;
                    return;
                }
                catch (OverflowException oe) {
                    WarningText.Text = oe.Message;
                    return;
                }
                WarningText.Text = "";

                /* octal to binary conversion */
                if (ComboBox2.SelectedItem.ToString() == "Binary")
                {
                    string result;

                    result = BaseConverter.Oct2Bin(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* octal to decimal conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Decimal")
                {
                    string result;

                    result = BaseConverter.Oct2Dec(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* octal to hexadecimal conversion */
                else if (ComboBox2.SelectedItem.ToString() == "Hexadecimal")
                {
                    string result;

                    result = BaseConverter.Oct2Hex(TextBox1.Text.ToString());

                    TextBox2.Text = result;
                }

                /* no conversion */
                else
                {
                    TextBox2.Text = TextBox1.Text;
                }
            }
        }
Пример #2
0
        public static void Main(string[] args)
        {
            BaseConverter testConverter = new BaseConverter();

            Console.WriteLine("Welcome to testConverter!");
            testConverter.ShowSettings();
            testConverter.Number = 102;
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number = -18;
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 11000110;
            testConverter.FromBase = "Binary";
            testConverter.ToBase   = "Decimal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 19142;
            testConverter.FromBase = "Decimal";
            testConverter.ToBase   = "Octal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 377;
            testConverter.FromBase = "Octal";
            testConverter.ToBase   = "Decimal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 8869;
            testConverter.FromBase = "Decimal";
            testConverter.ToBase   = "Hexadecimal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 368298;
            testConverter.FromBase = "Hexadecimal";
            testConverter.ToBase   = "Decimal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 1101110;
            testConverter.FromBase = "Binary";
            testConverter.ToBase   = "Octal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 725;
            testConverter.FromBase = "Octal";
            testConverter.ToBase   = "Binary";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 1101110;
            testConverter.FromBase = "Binary";
            testConverter.ToBase   = "Hexadecimal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 725;
            testConverter.FromBase = "Hexadecimal";
            testConverter.ToBase   = "Binary";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 645;
            testConverter.FromBase = "Octal";
            testConverter.ToBase   = "Hexadecimal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = 9876;
            testConverter.FromBase = "Hexadecimal";
            testConverter.ToBase   = "Octal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.Number   = -385;
            testConverter.FromBase = "Base5";
            testConverter.ToBase   = "Decimal";
            testConverter.ShowSettings();
            testConverter.PrintResult();

            testConverter.ResetBaseConverter();
            testConverter.ShowSettings();
        }