Example #1
0
        public static DollarEquation Calc(List <string> list)
        {
            var stack = new Stack <DollarEquation>();

            foreach (string s in list)
            {
                if (int.TryParse(s, out int i))
                {
                    DollarEquation o = new DollarEquation {
                        C = i
                    };
                    stack.Push(o);
                }
                else if (s.Contains("x"))
                {
                    DollarEquation o;
                    if (s.Contains("x*x") || s.Contains("x^2"))
                    {
                        o = new DollarEquation {
                            A = int.Parse(s.Replace("x*x", "").Replace("x^2", ""))
                        };
                    }
                    else
                    {
                        o = new DollarEquation {
                            B = int.Parse(s.Replace("x", ""))
                        };
                    }
                    stack.Push(o);
                }
                else
                {
                    switch (s)
                    {
                    case "+":
                        stack.Push(stack.Pop() + stack.Pop());
                        break;

                    case "-":
                        stack.Push(stack.Pop() - stack.Pop());
                        break;

                    case "*":
                        stack.Push(stack.Pop() * stack.Pop());
                        break;

                    case "/":
                        stack.Push(stack.Pop() / stack.Pop());
                        break;

                    case "%":
                        stack.Push(stack.Pop() % stack.Pop());
                        break;
                    }
                }
            }
            return(stack.Pop());
        }
        public static DollarEquation operator -(DollarEquation b, DollarEquation a)
        {
            DollarEquation o = new DollarEquation
            {
                A = a.A - b.A,
                B = a.B - b.B,
                C = a.C - b.C
            };

            return(o);
        }
        public static DollarEquation operator +(DollarEquation b, DollarEquation a)
        {
            DollarEquation o = new DollarEquation
            {
                A = a.A + b.A,
                B = a.B + b.B,
                C = a.C + b.C
            };

            return(o);
        }
        public static DollarEquation operator *(DollarEquation b, DollarEquation a)
        {
            DollarEquation o = new DollarEquation();

            // one dollar equation
            if (a.A == 0 && b.A == 0)
            {
                o.A = a.B * b.B;
                o.B = a.B * b.C + a.C * b.B;
                o.C = a.C * b.C;
            }

            return(o);
        }
        public static DollarEquation operator %(DollarEquation b, DollarEquation a)
        {
            DollarEquation o = new DollarEquation();

            // 1 % 3
            if (a.A == 0 && a.B == 0 && b.A == 0 && b.B == 0)
            {
                if (b.C == 0)
                {
                    throw new Exception("除数不能为0");
                }
                else
                {
                    o.C = a.C % b.C;
                }
            }
            return(o);
        }
        public static DollarEquation operator /(DollarEquation b, DollarEquation a)
        {
            DollarEquation o = new DollarEquation();

            if (a.A == 0 && b.A == 0)
            {
                // 1 / 0
                if (b.B == 0 && b.C == 0)
                {
                    throw new Exception("除数不能为0");
                }
                // 1x / 2
                // B = 1, C = 0
                // B = 0, C = 2
                else if (a.C == 0 && b.B == 0.0 && b.C != 0)
                {
                    o.B = a.B / b.C;
                }
                // 1 / 2x
                // B = 0, C = 1
                // B = 2, C = 0
                else if (a.B == 0 && b.C == 0 && b.B != 0)
                {
                    o.B = a.C / b.B;
                }
                // 1 / 2
                // B = 0, C = 1
                // B = 0, C = 2
                else if (a.B == 0 && b.B == 0 && b.C != 0)
                {
                    o.C = a.C / b.C;
                }
                // 1x / 2x
                // B = 1, C = 0
                // B = 2, C = 0
                else if (a.C == 0 && b.C == 0 && b.B != 0)
                {
                    o.C = a.B / b.B;
                }
            }

            return(o);
        }