コード例 #1
0
ファイル: Form1.cs プロジェクト: lulujing/DesignPattern
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
                oper = OperationFactory.createOperate(((Button)sender).Text);

                oper.NumberA = Convert.ToDouble(txtShow.Text);

                bOperate = true;
            }
        }
コード例 #2
0
ファイル: Calculator.cs プロジェクト: bihuutue/Internship2016
        public double getResult()
        {
            Operation.Operation op = new Operation.Operation();
            double        a;
            double        b;
            double        ans    = 0;
            List <string> opList = new List <string>(new string[] { "+", "-", "*", "/" });

            if (!opList.Contains(tokens[1]))
            {
                valid = false;
            }
            if (!Double.TryParse(tokens[0], out a))
            {
                valid = false;
            }
            if (!Double.TryParse(tokens[2], out b))
            {
                valid = false;
            }
            switch (tokens[1])
            {
            case "+":
                ans = op.plus(a, b);
                break;

            case "-":
                ans = op.minus(a, b);
                break;

            case "*":
                ans = op.time(a, b);
                break;

            case "/":
                if (b != 0)
                {
                    ans = op.divide(a, b);
                }
                else
                {
                    valid = false;
                }
                break;

            default:
                valid = false;
                break;
            }
            return(ans);
        }
コード例 #3
0
ファイル: Form1.cs プロジェクト: lulujing/DesignPattern
        private void buttonEqual_Click(object sender, EventArgs e)
        {
            if (txtShow.Text != "")
            {
                if (((Button)sender).Text != "=")
                {
                    oper = OperationFactory.createOperate(((Button)sender).Text);
                }

                oper.NumberB = Convert.ToDouble(txtShow.Text);


                txtShow.Text = oper.GetResult().ToString();
                bOperate     = true;
            }
        }
コード例 #4
0
        public double getResult()
        {
            Operation.Operation op = new Operation.Operation();
            double a;
            double b;

            a = 0;
            b = 0;
            while (tokens.Count() > 1)
            {
                double current;
                while (tokens.Contains("*") || tokens.Contains("/"))
                {
                    for (int i = 0; i < tokens.Count(); i++)
                    {
                        if (tokens[i] == "*" && i >= 1)
                        {
                            if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b))
                            {
                                current       = op.time(a, b);
                                tokens[i - 1] = Convert.ToString(current);
                                //Delete both at i and i+1
                                tokens.RemoveAt(i);
                                tokens.RemoveAt(i);
                                break;
                            }
                        }
                        if (tokens[i] == "/" && i >= 1)
                        {
                            if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b))
                            {
                                current       = op.divide(a, b);
                                tokens[i - 1] = Convert.ToString(current);
                                //Delete both at i and i+1
                                tokens.RemoveAt(i);
                                tokens.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
                while (tokens.Contains("+") || tokens.Contains("-"))
                {
                    for (int i = 0; i < tokens.Count(); i++)
                    {
                        if (tokens[i] == "+" && i >= 1)
                        {
                            if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b))
                            {
                                current       = op.plus(a, b);
                                tokens[i - 1] = Convert.ToString(current);
                                //Delete both at i and i+1
                                tokens.RemoveAt(i);
                                tokens.RemoveAt(i);
                                break;
                            }
                        }
                        if (tokens[i] == "-" && i >= 1)
                        {
                            if (Double.TryParse(tokens[i - 1], out a) && Double.TryParse(tokens[i + 1], out b))
                            {
                                current       = op.minus(a, b);
                                tokens[i - 1] = Convert.ToString(current);
                                //Delete both at i and i+1
                                tokens.RemoveAt(i);
                                tokens.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            return(Convert.ToDouble(tokens[0]));
        }