コード例 #1
0
        private void Calculate(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;

            if (b.Name == "SinT")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    Result.Text = "Result: " + s.sin(Convert.ToSingle(Num1.Text));
                }
                catch (FormatException)
                {
                    Result.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "CosT")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    Result.Text = "Result: " + s.cos(Convert.ToSingle(Num1.Text));
                }
                catch (FormatException)
                {
                    Result.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "TanT")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    Result.Text = "Result: " + s.tan(Convert.ToSingle(Num1.Text));
                }
                catch (FormatException)
                {
                    Result.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "SimplePlus")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    SimpleResult.Text = "Result: " + s.add(Convert.ToSingle(SimpleNum1.Text), Convert.ToSingle(SimpleNum2.Text));
                }
                catch (FormatException)
                {
                    SimpleResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "SimpleMinus")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    SimpleResult.Text = "Result: " + s.subtract(Convert.ToSingle(SimpleNum1.Text), Convert.ToSingle(SimpleNum2.Text));
                }
                catch (FormatException)
                {
                    SimpleResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "SimpleMultiply")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    SimpleResult.Text = "Result: " + s.multiply(Convert.ToSingle(SimpleNum1.Text), Convert.ToSingle(SimpleNum2.Text));
                }
                catch (FormatException)
                {
                    SimpleResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "SimpleDivide")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    SimpleResult.Text = "Result: " + s.divide(Convert.ToSingle(SimpleNum1.Text), Convert.ToSingle(SimpleNum2.Text));
                }
                catch (FormatException)
                {
                    SimpleResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "SimplePower")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    SimpleResult.Text = "Result: " + s.pow(Convert.ToSingle(SimpleNum1.Text), Convert.ToSingle(SimpleNum2.Text));
                }
                catch (FormatException)
                {
                    SimpleResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "SimpleModulous")
            {
                SimpleCalc s = new SimpleCalc();
                try
                {
                    SimpleResult.Text = "Result: " + s.mod(Convert.ToSingle(SimpleNum1.Text), Convert.ToSingle(SimpleNum2.Text));
                }
                catch (FormatException)
                {
                    SimpleResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "ComplexPlus")
            {
                ComplexCalc c = new ComplexCalc();
                try
                {
                    cFloat num1 = new cFloat(Convert.ToSingle(ComplexReal1.Text), Convert.ToSingle(ComplexImg1.Text));
                    cFloat num2 = new cFloat(Convert.ToSingle(ComplexReal2.Text), Convert.ToSingle(ComplexImg2.Text));
                    cFloat res  = c.add(num1, num2);
                    ComplexResult.Text = "Result: (" + res.getReal() + "," + res.getImg() + ")";
                }
                catch (FormatException)
                {
                    ComplexResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "ComplexMinus")
            {
                ComplexCalc c = new ComplexCalc();
                try
                {
                    cFloat num1 = new cFloat(Convert.ToSingle(ComplexReal1.Text), Convert.ToSingle(ComplexImg1.Text));
                    cFloat num2 = new cFloat(Convert.ToSingle(ComplexReal2.Text), Convert.ToSingle(ComplexImg2.Text));
                    cFloat res  = c.subtract(num1, num2);
                    ComplexResult.Text = "Result: (" + res.getReal() + "," + res.getImg() + ")";
                }
                catch (FormatException)
                {
                    ComplexResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "ComplexMultiply")
            {
                ComplexCalc c = new ComplexCalc();
                try
                {
                    cFloat num1 = new cFloat(Convert.ToSingle(ComplexReal1.Text), Convert.ToSingle(ComplexImg1.Text));
                    cFloat num2 = new cFloat(Convert.ToSingle(ComplexReal2.Text), Convert.ToSingle(ComplexImg2.Text));
                    cFloat res  = c.multiply(num1, num2);
                    ComplexResult.Text = "Result: (" + res.getReal() + "," + res.getImg() + ")";
                }
                catch (FormatException)
                {
                    ComplexResult.Text = "Please Enter Valid Numbers";
                }
            }
            else if (b.Name == "ComplexDivide")
            {
                ComplexCalc c = new ComplexCalc();
                try
                {
                    cFloat num1 = new cFloat(Convert.ToSingle(ComplexReal1.Text), Convert.ToSingle(ComplexImg1.Text));
                    cFloat num2 = new cFloat(Convert.ToSingle(ComplexReal2.Text), Convert.ToSingle(ComplexImg2.Text));
                    cFloat res  = c.divide(num1, num2);
                    ComplexResult.Text = "Result: (" + res.getReal() + "," + res.getImg() + ")";
                }
                catch (FormatException)
                {
                    ComplexResult.Text = "Please Enter Valid Numbers";
                }
            }
        }