示例#1
0
        private void btnEnter_Click(object sender, EventArgs e)
        {
            if (check == tbxCalculator.Text)
            {
                tbxCalculator.Text = "";
                return;
            }
            String text = tbxCalculator.Text;

            char[]   delim  = { '+', '-', '*', '/' };
            String[] texts  = text.Split(delim);
            int      number = 0;
            int      round  = 1;
            char     oper   = ' ';
            int      result = 0;;

            for (int i = 0; i < text.Length; i++)
            {
                if (text[i] == '+')
                {
                    oper = '+';
                }
                else if (text[i] == '-')
                {
                    oper = '-';
                }
                else if (text[i] == '*')
                {
                    oper = '*';
                }
                else if (text[i] == '+')
                {
                    oper = '/';
                }
            }


            if (text.Length >= 2)
            {
                Boolean isBin = false;
                Boolean isHex = false;
                for (int i = 0; i < texts.Length; i++)
                {
                    for (int j = 0; j < texts[i].Length; j++)
                    {
                        if (((texts[i].Length == 8) && (texts[i][j] == '1' || texts[i][j] == '0')))
                        {
                            isBin = true;
                            isHex = false;
                        }
                        else
                        {
                            isHex = true;
                            isBin = false;
                        }
                    }
                    if (isBin)
                    {
                        number = int.Parse(FindBintoDec(2, (int.Parse(texts[i]))));
                        if (round == 1)
                        {
                            result = number;
                        }
                        else
                        {
                            if (oper == '+')
                            {
                                result += number;
                            }
                            else if (oper == '-')
                            {
                                result -= number;
                            }
                            else if (oper == '*')
                            {
                                result *= number;
                            }
                            else if (oper == '/')
                            {
                                result /= number;
                            }
                        }
                        round++;
                    }
                    else if (isHex)
                    {
                        number = int.Parse(FindHextoDec(16, texts[i]));
                        if (round == 1)
                        {
                            result = number;
                        }
                        else
                        {
                            if (oper == '+')
                            {
                                result += number;
                            }
                            else if (oper == '-')
                            {
                                result -= number;
                            }
                            else if (oper == '*')
                            {
                                result *= number;
                            }
                            else if (oper == '/')
                            {
                                result /= number;
                            }
                        }
                        round++;
                    }
                }
                tbxCalculator.Text = result.ToString();
                check = tbxCalculator.Text;
                exchange          ex         = new exchange();
                String            textResult = ex.NumberToWords(int.Parse(tbxCalculator.Text));
                SpeechSynthesizer reader     = new SpeechSynthesizer();
                reader.Dispose();
                reader = new SpeechSynthesizer();
                reader.SpeakAsync("The Result is    " + textResult);
            }
            else
            {
                MessageBox.Show("กรุณากรอกรูปแบบของ Expression ให้ถูกต้อง");
            }
            alphabet = 0;
            op       = 0;
        }
示例#2
0
 private void btnConvert_Click(object sender, EventArgs e)
 {
     if (tbxInput.Text.Length < 10)
     {
         if (cmbMode.Text == "Base 2 --> Base 8")
         {
             tbxResult.Text = FindBintoOct(2, int.Parse(tbxInput.Text)).ToString();
         }
         else if (cmbMode.Text == "Base 2 --> Base 10")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindBintoDec(2, int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 2 --> Base 16")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindBintoHex(2, int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 8 --> Base 2")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindOcttoBin(8, int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 8 --> Base 10")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindOcttoDec(8, int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 8 --> Base 16")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindOcttoHex(8, int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 10 --> Base 2")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindDectoBin(int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 10 --> Base 8")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindDectoOct(int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 10 --> Base 16")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindDectoHex(int.Parse(tbxInput.Text)).ToString();
             }
         }
         else if (cmbMode.Text == "Base 16 --> Base 2")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindHextoBin(16, tbxInput.Text).ToString();
             }
         }
         else if (cmbMode.Text == "Base 16 --> Base 8")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindHextoOct(16, tbxInput.Text.ToUpper()).ToString();
             }
         }
         else if (cmbMode.Text == "Base 16 --> Base 10")
         {
             if (tbxInput.Text.Length < 10)
             {
                 tbxResult.Text = FindHextoDec(16, tbxInput.Text.ToUpper()).ToString();
             }
         }
         else
         {
         }
     }
     else
     {
         MessageBox.Show("Input Number Can't Entered More Than 9 Charactors");
     }
     if (cmbMode.Text != "Base 2 --> Base 16")
     {
         if (cmbMode.Text != "Base 8 --> Base 16")
         {
             if (cmbMode.Text != "Base 10 --> Base 16")
             {
                 exchange          ex     = new exchange();
                 String            textRe = ex.NumberToWords(int.Parse(tbxResult.Text));
                 SpeechSynthesizer read   = new SpeechSynthesizer();
                 read.Dispose();
                 read = new SpeechSynthesizer();
                 read.SpeakAsync("The Result is    " + textRe);
             }
         }
     }
 }