Exemple #1
0
        /*Infix to postfix*/
        public static string infixTopostfix(string infixequation)
        {
            string abc       = "";
            string equation1 = infixequation;

            string[] equation   = new string[equation1.Length];
            string   expression = "";
            stack    st         = new stack(equation.Length);

            for (int i = 0; i < equation1.Length; i++)
            {
                equation[i] = equation1[i].ToString();
            }

            for (int i = 0; i < equation.Length; i++)
            {
                if (check(equation[i]))
                {
                    expression = expression + equation[i];
                }
                else
                {
                    if (st.isEmpty() || equation[i] == "(")
                    {
                        st.push(equation[i]);
                    }
                    else
                    {
                        abc = st.pop();
                        if (precedence(abc) > precedence(equation[i]) || equation[i] == ")")
                        {
                            while (precedence(abc) > precedence(equation[i]) && abc != "(")
                            {
                                if (abc != "(")
                                {
                                    expression = expression + abc;
                                }
                                abc = st.pop();
                            }
                            if (equation[i] != ")")
                            {
                                st.push(equation[i]);
                            }
                        }
                        else
                        {
                            st.push(abc);
                            st.push(equation[i]);
                        }
                    }
                }
            }

            while (st.isEmpty() != true)
            {
                expression = expression + st.pop();
            }
            return(expression);
        }
Exemple #2
0
        /*Generate answer*/
        public static void generatedAns(string infixEquation, bool[] values)
        {
            string InfixEqu = infixEquation;

            bool[] val = values;

            string[] ab = PFEinTrueandFalse(InfixEqu, val);

            stack st   = new stack(ab.Length);
            stack temp = new stack(ab.Length);

            for (int i = ab.Length - 1; i >= 0; i--)
            {
                st.push(ab[i]);
            }

            bool tempAns;

            while (!st.isEmpty())
            {
                string abc = st.pop();
                while (abc == "True" || abc == "False")
                {
                    temp.push(abc);
                    abc = st.pop();
                }
                bool a, b;

                if (abc == "~")
                {
                    if (temp.pop() == "True")
                    {
                        a = true;
                    }
                    else
                    {
                        a = false;
                    }
                    tempAns = Not(a);
                }
                else
                {
                    if (temp.pop() == "True")
                    {
                        a = true;
                    }
                    else
                    {
                        a = false;
                    }

                    if (temp.pop() == "True")
                    {
                        b = true;
                    }
                    else
                    {
                        b = false;
                    }
                    tempAns = SolveTwoVariable(abc, a, b);
                }
                temp.push(tempAns.ToString());
            }

            Console.Write(temp.pop());
        }