Beispiel #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);
        }
Beispiel #2
0
        /*get equation in tru and fal value and take postfixequation in true and false value*/
        public static string[] PFEinTrueandFalse(string equationinfix, bool[] truthValue)
        {
            string postfixEqu1 = infixTopostfix(equationinfix);
            stack  st          = new stack(postfixEqu1.Length);
            stack  tempst      = new stack(postfixEqu1.Length);

            string[] postfixEqu = new string[postfixEqu1.Length];

            string[] getvari = getvariable(equationinfix);

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


            bool[] value;
            value = truthValue;


            for (int i = postfixEqu.Length - 1; i >= 0; i--)
            {
                for (int j = 0; j < getvari.Length; j++)
                {
                    if (postfixEqu[i] == getvari[j])
                    {
                        st.push(value[j].ToString());
                    }
                }
                if (!check(postfixEqu[i]))
                {
                    st.push(postfixEqu[i]);
                }
            }


            string[] result = new string[postfixEqu.Length];
            for (int i = 0; i < result.Length; i++)
            {
                result[i] = st.pop();
            }

            return(result);
        }
Beispiel #3
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());
        }