コード例 #1
0
        public static string GetResult(string input)
        {
            var    numStack        = new Stack <string>();
            var    postfixNotation = GetPostfix(input);
            char   ch;
            string num1, num2, interAns;
            var    num = "";

            for (var i = 0; i < postfixNotation.Length; i++)
            {
                ch = postfixNotation[i];

                if (char.IsDigit(ch) || ch == 'i')
                {
                    num += ch;
                }
                else if (ch == '|')
                {
                    if (num != "")
                    {
                        numStack.Push(num);
                    }
                    num = "";
                }
                else
                {
                    var foundComplex = false;
                    var result       = "";

                    if (num != "")
                    {
                        numStack.Push(num);
                    }
                    num  = "";
                    num2 = numStack.Pop();
                    if (numStack.Count == 0)
                    {
                        return(num2);
                    }
                    num1 = numStack.Pop();

                    if (!num1.Contains("i") && !num2.Contains("i"))
                    {
                        result = SolveTwo(int.Parse(num1), int.Parse(num2), ch) + "";
                        numStack.Push(result);
                        continue;
                    }

                    /*** Complex part ***/
                    // If there is already record of complex num and
                    // usual num e.g. 2+3i
                    if (num1.Contains("+") || num1.Contains("-") || num2.Contains("+") || num2.Contains("-"))
                    {
                        var tmpOpers   = new char[] { '+', '-' };
                        var num1Length = GetAmountNumsInExpression(num1);
                        var num2Length = GetAmountNumsInExpression(num2);

                        if (num2Length > num1Length)
                        {
                            result = GetOperOnComplexNum(num1, ch, num2);
                        }
                        if (num2Length < num1Length)
                        {
                            result = GetOperOnComplexNum(num2, ch, num1);
                        }

                        if (num2Length == num1Length)
                        {
                            var nums = num2.Split(new char[] { '+', '-' });
                            result = GetOperOnComplexNum(nums[0], ch, num1);
                            result = GetOperOnComplexNum(nums[1], ch, result);
                        }

                        if (result == "")
                        {
                            continue;
                        }
                        numStack.Push(result);
                        continue;
                    }
                    //Both numbers are complex
                    if (num2.Contains("i") && num1.Contains("i"))
                    {
                        num2 = num2.Substring(0, num2.Length - 1);
                        num1 = num1.Substring(0, num1.Length - 1);

                        result = SolveTwo(int.Parse(num1), int.Parse(num2), ch) + "i";
                        if (result == "0i")
                        {
                            continue;
                        }
                    }

                    // One of the numbers is complex and operations are
                    // multiply, mod or dividing
                    if (num2.Contains("i") &&
                        (ch == '*' || ch == '/' || ch == '%'))
                    {
                        num2   = num2.Substring(0, num2.Length - 1);
                        result = SolveTwo(int.Parse(num1), int.Parse(num2), ch) + "i";
                    }

                    if (num1.Contains("i") &&
                        (ch == '*' || ch == '/' || ch == '%'))
                    {
                        num1   = num1.Substring(0, num1.Length - 1);
                        result = SolveTwo(int.Parse(num1), int.Parse(num2), ch) + "i";
                    }

                    // One of the numbers is complex and operations are
                    // plus and minus
                    if ((num2.Contains("i") || num1.Contains("i")) &&
                        (ch == '+' || ch == '-'))
                    {
                        result = num1 + "" + ch + num2;
                    }

                    //Result
                    numStack.Push(result);
                }
            }

            return(numStack.Pop());
        }
コード例 #2
0
        public static string GetPostfix(string input)
        {
            var output    = new StringBuilder();
            var operStack = new Stack <char>();
            var num       = "";

            for (var i = 0; i < input.Length; i++)
            {
                var currentChar = input[i];

                if (char.IsDigit(input[i]) || input[i] == 'i')
                {
                    num += currentChar;
                    if (i == input.Length - 1)
                    {
                        output.Append(num);
                    }
                    continue;
                }

                switch (currentChar)
                {
                case '+':
                case '-':
                    if (num != "")
                    {
                        output.Append(num);
                        output.Append("|");
                    }
                    num = "";
                    GotOper(currentChar, 1, operStack, output);
                    break;

                case '*':
                case '/':
                case '%':
                    if (num != "")
                    {
                        output.Append(num);
                        output.Append("|");
                    }
                    num = "";
                    GotOper(currentChar, 2, operStack, output);
                    break;

                case '(':
                    if (num != "")
                    {
                        output.Append(num);
                        output.Append("|");
                    }
                    num = "";
                    operStack.Push(currentChar);
                    break;

                case ')':
                    if (num != "")
                    {
                        output.Append(num);
                        output.Append("|");
                    }
                    num = "";
                    GotParen(currentChar, operStack, output);
                    break;

                default:
                    output.Append(currentChar);
                    break;
                }
            }

            while (operStack.Count != 0)
            {
                output.Append(operStack.Pop());
            }

            return(output.ToString());
        }