Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Joint            joint            = new Joint();
            List <string>    list             = new List <string>();
            List <string>    resultList       = new List <string>();
            string           temp             = null;
            RandomCollection randomCollection = new RandomCollection();
            Unitl            unitl            = new Unitl();

            Console.WriteLine("请输入生成条数:");
            int n = Convert.ToInt32(Console.ReadLine());

            while (true)
            {
                if (n == 0)
                {
                    break;
                }
                list = joint.craeteEqual(randomCollection.randomInt(2, 4));
                temp = string.Join("", list.ToArray());
                Calculator calculator = new Calculator(list);
                int        result     = calculator.run();
                if (result != -1)
                {
                    resultList.Add(temp + "=" + result.ToString());
                    n--;
                }
            }
            unitl.writeTxt(resultList);
        }
Exemplo n.º 2
0
        public Calculator(List <string> equalString)
        {
            this.equalString = equalString;
            Stack <Expression> stack = new Stack <Expression>();
            Expression         left  = null;
            Expression         right = null;
            Unitl unitl = new Unitl();

            equalString = unitl.delMulAndDiv(equalString);
            Console.WriteLine(string.Join("", equalString.ToArray()));
            for (int i = 0; i < equalString.Count(); i++)
            {
                switch (equalString[i])
                {
                case "+":
                    left  = stack.Pop();
                    right = new VarExpression(++i);
                    stack.Push(new AddExpression(left, right));
                    break;

                case "-":
                    left  = stack.Pop();
                    right = new VarExpression(++i);
                    stack.Push(new SubExpression(left, right));
                    break;

                case "*":
                    left  = stack.Pop();
                    right = new VarExpression(++i);
                    stack.Push(new MulExpression(left, right));
                    break;

                case "/":
                    left  = stack.Pop();
                    right = new VarExpression(++i);
                    stack.Push(new DivExpression(left, right));
                    break;

                default:
                    stack.Push(new VarExpression(i));
                    break;
                }
            }
            this.expression = stack.Pop();
        }