Esempio n. 1
0
            public static bool checkInputString(string formula)
            {
                int counter1 = 0,
                    counter2 = 2;

                if (SignChecker.isOperation(formula[0]) && formula[0] != '-')
                {
                    return(false);
                }
                if (SignChecker.isOperation(formula[formula.Length - 1]))
                {
                    return(false);
                }
                counter1 = formula.ToCharArray().Where(x => x == '(').Count();
                counter2 = formula.ToCharArray().Where(x => x == ')').Count();
                if (counter1 != counter2)
                {
                    return(false);
                }

                for (int i = 1; i < formula.Length; i++)
                {
                    if (SignChecker.isOperation(formula[i]) && SignChecker.isOperation(formula[i - 1]))
                    {
                        return(false);
                    }
                }

                return(true);
            }
Esempio n. 2
0
        //Checks function
        public static bool checkInputString(string formula)
        {
            int counter1 = 0,
                counter2 = 2;

            //Returns false if first symbol in function is operation (besides minus)
            if (SignChecker.isOperation(formula[0]) && formula[0] != '-')
            {
                return(false);
            }
            //Returs false if last symbol in string is operation
            if (SignChecker.isOperation(formula[formula.Length - 1]))
            {
                return(false);
            }
            //Counts ammount of opening and closing brackets
            counter1 = formula.ToCharArray().Where(x => x == '(').Count();
            counter2 = formula.ToCharArray().Where(x => x == ')').Count();
            //If ammount of brackets doesn't equal, then returns false
            if (counter1 != counter2)
            {
                return(false);
            }
            //Checks if there is no two operations side by side
            for (int i = 1; i < formula.Length; i++)
            {
                if (SignChecker.isOperation(formula[i]) && SignChecker.isOperation(formula[i - 1]))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 3
0
            public static string toRPN(string infixString)
            {
                if (!CheckInput.checkInputString(infixString))
                {
                    return("");
                }
                Stack <char> operationStack = new Stack <char>(); //Stack for all operations
                char         lastOperation;
                string       resultString = "";                   //String with result

                infixString = infixString.Replace(" ", "");
                for (int i = 0; i < infixString.Length; i++)
                {
                    //If it is number, than add to resultString
                    if (SignChecker.isVariable(infixString[i]))
                    {
                        resultString += infixString[i];
                    }
                    else if (SignChecker.isOperation(infixString[i]))
                    {
                        if (infixString[i] == '-' && (((i == 0) || (infixString[i - 1] == '(')) && (SignChecker.isVariable(infixString[i + 1]))))
                        {
                            resultString += 0;
                        }
                        if (operationStack.Count != 0)
                        {
                            lastOperation = operationStack.Peek();

                            if (SignChecker.getOperationPriority(lastOperation) < SignChecker.getOperationPriority(infixString[i]))
                            {
                                operationStack.Push(infixString[i]);
                                resultString += " ";
                            }
                            else
                            {
                                resultString += operationStack.Pop();
                                operationStack.Push(infixString[i]);
                                resultString += " ";
                            }
                        }
                        else
                        {
                            resultString += " ";
                            operationStack.Push(infixString[i]);
                        }
                    }
                    //If current symbol is (, then add it to stack
                    else if (infixString[i] == '(')
                    {
                        operationStack.Push(infixString[i]);
                    }
                    //If current symbol is ), then return all symbols from stack, until (
                    else if (infixString[i] == ')')
                    {
                        while (operationStack.Peek() != '(')
                        {
                            resultString += operationStack.Pop();
                        }
                        operationStack.Pop();
                    }
                }
                //In the end, return all the values from stack
                while (operationStack.Count != 0)
                {
                    resultString += operationStack.Pop() + " ";
                }
                return(resultString);
            }
Esempio n. 4
0
        public static string toRPN(string infixString)
        {
            infixString = infixString.Replace(" ", "");       //Replacing all spaces
            if (!CheckInput.checkInputString(infixString))    //If we have wrong input string
            {
                throw new ArgumentException();                //Than throw exception
            }
            Stack <char> operationStack = new Stack <char>(); //Stack for all operations
            char         lastOperation;                       //Keeping last operation
            string       resultString = "";                   //String with result


            for (int i = 0; i < infixString.Length; i++)
            {
                //If it is number, than add to resultString
                if (SignChecker.isVariable(infixString[i]))
                {
                    resultString += infixString[i];
                }
                //If it is operation
                else if (SignChecker.isOperation(infixString[i]))
                {
                    //If it is unary minus< then add 0 to result string
                    if (infixString[i] == '-' && (((i == 0) || (infixString[i - 1] == '(')) && (SignChecker.isVariable(infixString[i + 1]))))
                    {
                        resultString += 0;
                    }
                    //If we have operations in stack
                    if (operationStack.Count != 0)
                    {
                        //Taking last operation of stack
                        lastOperation = operationStack.Peek();
                        //If priority of operation from stack is higher, then pirority of current operation
                        if (SignChecker.getOperationPriority(lastOperation) < SignChecker.getOperationPriority(infixString[i]))
                        {
                            //Add current operation to stack
                            operationStack.Push(infixString[i]);
                            resultString += " ";
                        }
                        else
                        {
                            //Add operation from stack to result string
                            resultString += operationStack.Pop();
                            //Add current operation to stack
                            operationStack.Push(infixString[i]);
                            resultString += " ";
                        }
                    }
                    else
                    {
                        //Add current operation to stack
                        resultString += " ";
                        operationStack.Push(infixString[i]);
                    }
                }
                //If current symbol is (, then add it to stack
                else if (infixString[i] == '(')
                {
                    operationStack.Push(infixString[i]);
                }
                //If current symbol is ), then return all symbols from stack, until (
                else if (infixString[i] == ')')
                {
                    while (operationStack.Peek() != '(')
                    {
                        resultString += operationStack.Pop();
                    }
                    operationStack.Pop();
                }
            }
            //In the end, return all the values from stack
            while (operationStack.Count != 0)
            {
                resultString += operationStack.Pop() + " ";
            }
            return(resultString);
        }