예제 #1
0
    public static void FunctionSimplifier()
    {
        Console.Write("\n\n\nEnter Function:\t\t");
        string s = Console.ReadLine();

        FunctionTree myFunc = new FunctionTree(s);

        Console.WriteLine("\nSimplification\t=\t" + myFunc.GetSimplifiedFunction().ToString());
        Console.WriteLine("\n\n\n");
    }
예제 #2
0
    public static void SimpleDerivativeComputer()
    {
        Console.Write("\n\n\nEnter Function:\t\t");
        string s = Console.ReadLine();

        Console.Write("\nDerivate w.r.t. ?\t");
        string v = Console.ReadLine();

        Console.Write("\nDerivate at '" + v + "'= ?\t");
        double d = Double.Parse(Console.ReadLine());

        FunctionTree myFunc = new FunctionTree(s);

        Console.WriteLine("\nDerivative\t=\t" + myFunc.GetDerivative(v).GetSimplifiedFunction().ToString());
        Console.WriteLine("Value\t\t=\t" + myFunc.GetDerivative(v).GetValue(v + "=" + d));
        Console.WriteLine("\n\n\n");
    }
예제 #3
0
        public static void CreateFunctionTree(string[] postfixArray, FunctionTree FTree)
        {
            FunctionStack fStk = new FunctionStack();
            string        f;
            int           i = 0;

            FTree.varList = new List <FVar>();

            while (i < postfixArray.Length)
            {
                f = postfixArray[i];
                //System.out.println("fff "+ f);

                if (f.Equals(FNeg.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FNeg.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FNeg(fStk.TopAndPop()));
                }
                else if (f.Equals(FAdd.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FAdd.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FAdd(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FSub.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FSub.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FSub(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FMul.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FMul.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FMul(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FDiv.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FDiv.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FDiv(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FPow.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FPow.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FPow(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FExp.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FExp.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FExp(fStk.TopAndPop()));
                }
                else if (f.Equals(FLn.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FLn.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FLn(fStk.TopAndPop()));
                }
                // sin()
                else if (f.Equals(FSin.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FSin.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FSin(fStk.TopAndPop()));
                }
                else if (f.Equals(FCos.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FCos.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FCos(fStk.TopAndPop()));
                }

                // add rest of the functions

                else if (IsConstant(f))                 // double number
                {
                    fStk.Push(new FCons(Convert.ToDouble(f)));
                }
                else
                {
                    FVar @var = FTree.FindVariable(f);
                    if (@var != null)
                    {
                        fStk.Push(@var);
                    }
                    else
                    {
                        fStk.Push(FTree.AddVariable(new FVar(f)));                         // variable
                    }
                }

                i++;
            }



            FTree.rootNode = fStk.TopAndPop();
        }
예제 #4
0
        public static void CreatePostfixArray(string infix, FunctionTree FTree)
        {
            StringStack opStk = new StringStack();

            // create a new char array to store the Postfix expression
            // and initialize it to NULL
            string[] postfix = new string[infix.Length + 1];

            int i = 0;               // the index used to read the Infix expression
            int p = 0;               // the index used to store to the Postfix expression

            string op;               // holds value of current character from the Infix expression in each loop
            int    opPr;             // holds the Priority of current character from the Infix expression in each loop

            while (i < infix.Length) // while char array element != NULL
            {
                //System.out.println("i = " + i);
                //System.out.println(infix.length());
                op   = infix[i] + "";               // assign current character from Infix expression
                opPr = Priority(op);                // get and store Priority of current character 'op'
                //System.out.println("op char: "+ op);
                postfix[p] = "";
                //System.out.println("got " + opPr);
                if (op.Equals(" ") || op.Equals(","))
                {
                }
                else if (opPr == -1)                 // if current character is not an operator
                {
                    while (opPr == -1)               // put directly into the Postfix expression array until an operator is reached
                    {
                        postfix[p] = postfix[p] + op;
                        i++;
                        if (i == infix.Length)
                        {
                            break;
                        }
                        op   = infix[i] + "";
                        opPr = Priority(op);
                    }
                    if (FunctionList.IsFunction(postfix[p]))
                    {
                        //System.out.println("found function: " + postfix[p]);
                        opStk.Push(postfix[p]);
                    }
                    else
                    {
                        p++;
                    }
                    continue;
                }

                else if (op.Equals(")")) // if current oprtr is 'closing bracket' then Pop Stack
                {                        // values into Postfix expression until 'opening bracket'
                    while (!(opStk.Top().Equals("(")))
                    {
                        postfix[p++] = opStk.TopAndPop();
                    }
                    opStk.Pop();                     // Pop (remove) the 'opening bracket'
                    if (!opStk.IsEmpty() && FunctionList.IsFunction(opStk.Top()))
                    {
                        postfix[p++] = opStk.TopAndPop();
                    }
                }

                else if (opStk.IsEmpty() || op.Equals("(") || opStk.Top().Equals("("))
                {
                    //System.out.println("wrong");
                    opStk.Push(op);                     // Push current car (oprtr) into Stack for any of the above conditions
                    ;
                }

                else if (Priority(opStk.Top()) >= opPr)
                {
                    // if Priority of current oprtr is less than / equal to priority
                    // of oprtr in Top of Stack, then Pop all Stack values of Priority
                    // greater than or equal to current oprtr until lower Priority oprtr
                    // OR an 'opening bracket' is reached OR Stack is Empty, and put the
                    // popped values into the Postfix Expression array.
                    // Then Push current oprtr onto the Stack.
                    while (!opStk.IsEmpty())
                    {
                        postfix[p++] = opStk.TopAndPop();
                        if (!opStk.IsEmpty())
                        {
                            if (opStk.Top().Equals("(") || Priority(opStk.Top()) < opPr)
                            {
                                break;
                            }
                        }
                    }
                    opStk.Push(op);
                }

                else if (Priority(opStk.Top()) < opPr)
                {
                    // if Priority of current oprtr is greater than priority of oprtr in
                    // Top of Stack, then simply Push current oprtr onto the Stack
                    opStk.Push("" + op);
                }

                i++;                 // increment index for the next Infix array Character
            }

            // finally, Pop out the all remaining Stack values into the Postfix expression array
            while (!opStk.IsEmpty())
            {
                postfix[p++] = opStk.TopAndPop();
            }

            // Dispose Stack that is no longer needed
            opStk.MakeEmpty();

            //System.out.println("pppppp" + p);
            postfix = FixSize(postfix, p);

            // return the resultant Postix expression array
            FTree.postfixArray = postfix;
        }
예제 #5
0
        public static void CreateFunctionTree(string[] postfixArray, FunctionTree FTree)
        {
            FunctionStack fStk = new FunctionStack();

            FTree.varList = new List <FVar>();
            string f; int i = 0;

            Function temp1, temp2;
            bool     chk1, chk2;

            while (i < postfixArray.Length)
            {
                f = postfixArray[i];
                //Console.Write(" "+ f);

                if (f.Equals(FNeg.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FNeg.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FNeg(fStk.TopAndPop()));
                }
                else if (f.Equals(FAdd.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FAdd.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FAdd(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FSub.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FSub.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FSub(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FMul.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FMul.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    temp1 = fStk.TopAndPop();
                    chk1  = temp1.GetID().Equals(FCons.ID) && !fStk.Top().GetID().Equals(FCons.ID);

                    if (chk1 && fStk.Top().isTwoSidedFunction&& !((TwoSidedFunction)fStk.Top()).RHS().GetID().Equals(FCons.ID))
                    {
                        temp2 = ((TwoSidedFunction)fStk.Top()).RHS();
                        ((TwoSidedFunction)fStk.Top()).SetRHS(temp1);
                        fStk.Push(new FMul(temp2, fStk.TopAndPop(), 1));
                    }
                    else if (chk1 && fStk.Top().isVariable)
                    {
                        fStk.Push(new FMul(fStk.TopAndPop(), temp1, 1));
                    }
                    else
                    {
                        fStk.Push(new FMul(temp1, fStk.TopAndPop(), 1));
                    }
                }
                else if (f.Equals(FDiv.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FDiv.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FDiv(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FPow.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FPow.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FPow(fStk.TopAndPop(), fStk.TopAndPop(), 1));
                }
                else if (f.Equals(FExp.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FExp.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FExp(fStk.TopAndPop()));
                }
                else if (f.Equals(FLn.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FLn.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FLn(fStk.TopAndPop()));
                }
                // sin()
                else if (f.Equals(FSin.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FSin.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FSin(fStk.TopAndPop()));
                }
                else if (f.Equals(FCos.ID, StringComparison.CurrentCultureIgnoreCase) || f.Equals(FCos.Symbol, StringComparison.CurrentCultureIgnoreCase))
                {
                    fStk.Push(new FCos(fStk.TopAndPop()));
                }

                // add rest of the functions

                else if (IsConstant(f))                 // double number
                {
                    fStk.Push(new FCons(Convert.ToDouble(f)));
                }
                else
                {
                    FVar @var = FTree.FindVariable(f);
                    if (@var != null)
                    {
                        fStk.Push(@var);
                    }
                    else
                    {
                        FunctionTree temp;
                        FVar         tempVar;
                        if ((tempVar = ListHandler.ML_VariableList(MLGUI.GetVariable, f)) != null && tempVar.IsSet())
                        {
                            fStk.Push(new FCons(tempVar.GetValue()));
                        }
                        else if ((temp = ListHandler.ML_FunctionList(MLGUI.GetFunction, f)) != null)
                        {
                            fStk.Push(temp.rootNode);
                        }
                        else
                        {
                            fStk.Push(FTree.AddVariable(ListHandler.ML_VariableList(MLGUI.AddItem, f)));                          // variable
                        }
                    }
                }

                i++;
            }



            FTree.rootNode = fStk.TopAndPop();
        }