Esempio n. 1
0
            public static Double CalculateRPN(string rpnString, double[] values)
            {
                Stack <Double> numbersStack = new Stack <Double>();
                Double         value1, value2;
                int            pos      = 0;
                string         variable = "";

                for (int i = 0; i < rpnString.Length; i++)
                {
                    if (SignChecker.isVariable(rpnString[i]))
                    {
                        variable += rpnString[i];
                    }
                    else if (rpnString[i] == ' ')
                    {
                        if (variable != "")
                        {
                            numbersStack.Push(int.Parse(variable));
                        }
                        variable = "";
                    }
                    else
                    {
                        if (variable != "")
                        {
                            numbersStack.Push(int.Parse(variable));
                            variable = "";
                        }
                        value2 = numbersStack.Pop();
                        value1 = numbersStack.Pop();
                        numbersStack.Push(SignChecker.useOperator(rpnString[i], value1, value2));
                    }
                }
                return(numbersStack.Pop());
            }
Esempio n. 2
0
        //Calculating function
        public static Double CalculateRPN(string rpnString, List <string> variables, double[] values)
        {
            Stack <Double> numbersStack = new Stack <Double>(); //stack for numbers
            Double         value1, value2;                      //first and second values
            string         variable = "";                       //Current variable

            //Replaces all variables from rpn string to variables values
            for (int i = 0; i < variables.Count; i++)
            {
                rpnString = rpnString.Replace(variables[i], values[i].ToString());
            }

            for (int i = 0; i < rpnString.Length; i++)
            {
                //If current symbol is variable
                if (SignChecker.isVariable(rpnString[i]))
                {
                    //Add to variable
                    variable += rpnString[i];
                }
                else if (rpnString[i] == ' ')
                {
                    //If current symbol is space and we have something in variable
                    if (variable != "")
                    {
                        //Add variable to stack
                        numbersStack.Push(double.Parse(variable));
                    }
                    variable = "";
                }
                else if (rpnString[i] == '-' && i != rpnString.Length - 1 && SignChecker.isVariable(rpnString[i + 1]))
                {
                    variable += rpnString[i];
                }
                else
                {
                    //If variable has something
                    if (variable != "")
                    {
                        //Add to stack
                        numbersStack.Push(double.Parse(variable));
                        variable = "";
                    }
                    //Take two values from stack
                    value2 = numbersStack.Pop();
                    value1 = numbersStack.Pop();
                    //Calculate them
                    numbersStack.Push(SignChecker.useOperator(rpnString[i], value1, value2));
                }
            }
            if (variable != "")
            {
                numbersStack.Push(double.Parse(variable));
            }
            return(numbersStack.Pop());
        }
Esempio n. 3
0
            public static IEnumerable <string> findAllX(string rpnString)
            {
                Stack <Double> numbersStack = new Stack <Double>();
                List <string>  stringList   = new List <string>();
                string         variable     = "";

                for (int i = 0; i < rpnString.Length; i++)
                {
                    if (SignChecker.isVariable(rpnString[i]))
                    {
                        variable += rpnString[i];
                    }
                    else
                    {
                        if (variable.Any(c => char.IsLetter(c)))
                        {
                            stringList.Add(variable);
                        }
                        variable = "";
                    }
                }
                return(stringList.Distinct());
            }
Esempio n. 4
0
        //Finding all variables in rpnString
        public static List <string> findAllX(string rpnString)
        {
            Stack <Double> numbersStack = new Stack <Double>(); //Stack for numbers
            List <string>  stringList   = new List <string>();  //List of all variables
            string         variable     = "";

            for (int i = 0; i < rpnString.Length; i++)
            {
                //If current symbol is variable
                if (SignChecker.isVariable(rpnString[i]))
                {
                    //Add to current variable
                    variable += rpnString[i];
                }
                else
                {
                    //If variable has symbols in itself
                    if (variable.Any(c => char.IsLetter(c)))
                    {
                        //Add variable to variable list
                        stringList.Add(variable);
                    }
                    variable = "";
                }
            }
            if (variable != "")
            {
                //If variable has symbols in itself
                if (variable.Any(c => char.IsLetter(c)))
                {
                    //Add variable to variable list
                    stringList.Add(variable);
                }
            }
            //Return list of variables
            return(stringList.Distinct().ToList());
        }
Esempio n. 5
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. 6
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);
        }